Skip to content

Commit 2b13773

Browse files
Initial upload
0 parents  commit 2b13773

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules/
2+
/tests/
3+
package-lock.json

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Gabriel Silva
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# js-include
2+
This library allows you to include, in-place, common Javascript files (not modules) into your Node.js application and evaluate their script in the global namespace, kind like PHP does.
3+
4+
### Installation
5+
```
6+
npm install @eugabrielsilva/js-include
7+
```
8+
9+
### Usage (single file)
10+
```js
11+
// Declare the module
12+
const includes = require('@eugabrielsilva/js-include');
13+
14+
// Include the file
15+
includes.__includeFile('./inc/myfile.js');
16+
eval(includes.myfile);
17+
```
18+
19+
### Usage (directory)
20+
```js
21+
// Declare the module
22+
const includes = require('@eugabrielsilva/js-include');
23+
24+
// Include the directory
25+
includes.__includeDir('./inc');
26+
27+
for (let file in includes.inc) {
28+
eval(includes.inc[file]);
29+
}
30+
```
31+
32+
### Credits
33+
Library developed and currently maintained by [Gabriel Silva](https://github.com/eugabrielsilva).

js-include.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
module.exports.__includeDir = function(dir, parent = null) {
5+
if(!dir.endsWith('/')) dir += '/';
6+
let files = fs.readdirSync(dir, 'utf8');
7+
let key = path.basename(dir);
8+
9+
if(!parent) {
10+
module.exports[key] = {};
11+
parent = module.exports[key];
12+
}
13+
14+
files.forEach(file => {
15+
if(fs.statSync(dir + file).isDirectory()) {
16+
key = path.basename(dir + file);
17+
if(!parent[key]) parent[key] = {};
18+
module.exports.__includeDir(dir + file, parent[key]);
19+
} else {
20+
module.exports.__includeFile(dir + file, parent);
21+
}
22+
});
23+
}
24+
25+
module.exports.__includeFile = function(file, parent = null) {
26+
if(!file.endsWith('.js')) return;
27+
if(!parent) parent = module.exports;
28+
let key = path.basename(file, '.js');
29+
parent[key] = fs.readFileSync(file, 'utf8').toString();
30+
}

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@eugabrielsilva/js-include",
3+
"description": "PHP-like include library for Node.js",
4+
"version": "1.0.0",
5+
"main": "js-include.js",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/eugabrielsilva/js-include.git"
9+
},
10+
"author": "Gabriel Silva",
11+
"license": "MIT",
12+
"bugs": {
13+
"url": "https://github.com/eugabrielsilva/js-include/issues"
14+
},
15+
"homepage": "https://github.com/eugabrielsilva/js-include#readme"
16+
}

0 commit comments

Comments
 (0)