0.4.3 • Published 5 years ago

rinclude v0.4.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

rinclude

require module with custom folder and generate virtual index.js automatically

install

npm install rinclude

Usage

Add the following to the startup file

const include = require('rinclude');
global.include = include; // It can be used anywhere.

Notice

It no longer create an index.js file.
You do not need to exclude the index.js file from programs like pm2.
You can adjust it with an option.

create index.js file

const include = require('rinclude');

include.path('./custom');

...

// after that
include.clean(); // Remove all recorded paths.

include.options({ createIndex: true, type: 'es6' }); // type es6 or es5
include.path('./custom');

Detailed usage

basic

Add the following to the startup file

const include = require('rinclude');
global.include = include; // It can be used anywhere.

example

// Read files in custom folders
 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── index.js
 ├── app.js
 └── sample.js
// app.js
const include = require('rinclude');
global.include = include; // It can be used anywhere.

include.path('./custom');

const timer = include('timer');
timer.start();
timer.stop();

require('.sample')
// smaple.js
const timer = include('timer');
timer.start();
timer.stop();

Avoid collisions

 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── index.js
 ├── custom2
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── index.js
 └── app.js
include.path('./custom);
include.path('./custom2); // Crash

Use prefix

include.path('./custom');
include.path('./custom2', 'two');

const timer = include('timer');
const timer2 = include('two.timer');

timer.start();
timer.stop();

timer2.start();
timer2.stop();

Automatically create an virtual index file

 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           └── stop.js
 └── app.js
include.path('./custom');
const timer = include('timer'); // The index.js file is required.

Add the .generateIndex file.

 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── .generateIndex
 └── app.js

Automatically create virtual index.js file.

The contents of virtual index.js are as follows.

module.exports = {
  start : require('./start.js'),
  stop : require('./stop.js')
};

You can now access the timer.

include.path('./custom');
const timer = include('timer');

timer.start();
timer.stop();

When you add a file and restart app.js, virtual index.js is created automatically

 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           ├── pause.js       // add new file
 │           └── .generateIndex
 └── app.js

The contents of virtual index.js are as follows.

module.exports = {
  pause: require('./pause.js'),
  start : require('./start.js'),
  stop : require('./stop.js')
};

You can now access the timer.

include.path('./custom');
const timer = include('timer');

timer.start();
timer.stop();
timer.pause();

Usage : .generateIndex

  1. basic
  • Create only a .generateIndex file.
  • It lists all the files in the folder.
 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── .generateIndex
 └── app.js
  • Generated virtual index.js
module.exports = {
  start : require('./start.js'),
  stop : require('./stop.js')
};
  1. You can have a special folder structure.
 .
 ├── custom
 │     └── timer
 │           ├── api
 │           │    ├── start.js
 │           │    └── stop.js
 │           ├── lcd
 │           │    └── display.js
 │           ├── info.js
 │           └── .generateIndex
 └── app.js

ex1)

  • Contents of .gnerateIndex
api, lcd
  • Generated virtual index.js
module.exports = {
  start : require('./api/start.js'),
  stop : require('./api/stop.js'),
  info : require('./info.js'),
  display: require('./lcd/display.js)
};

ex2)

  • Contents of .gnerateIndex
api, lcd:lcd
  • Generated virtual index.js
module.exports = {
  start : require('./api/start.js'),
  stop : require('./api/stop.js'),
  info : require('./info.js'),
  lcd: {
    display: require('./lcd/display.js)
  }
};

ex3)

  • Contents of .gnerateIndex
api:api, lcd
  • Generated virtual index.js
module.exports = {
  api : {
    start : require('./api/start.js'),
    stop : require('./api/stop.js')
  },
  info : require('./info.js'),
  display : require('./lcd/display.js')
};
  1. Excludes undefined folders.
 .
 ├── custom
 │     └── timer
 │           ├── api
 │           │    ├── start.js
 │           │    └── stop.js
 │           ├── lcd
 │           │    └── display.js
 │           ├── something
 │           │    └── notNeeded.js
 │           ├── info.js
 │           └── .generateIndex
 └── app.js
  • Contents of .gnerateIndex
api, lcd:lcd
  • Generated virtual index.js
module.exports = {
  start : require('./api/start.js'),
  stop : require('./api/stop.js'),
  info : require('./info.js'),
  lcd: {
    display: require('./lcd/display.js)
  }
};
  1. It supports several subfolders.
 .
 ├── custom
 │     └── timer
 │           ├── api
 │           │    ├── start.js
 │           │    └── stop.js
 │           ├── lcd
 │           │    ├── check
 │           │    │    └── checking.js
 │           │    └── display.js
 │           ├── info.js
 │           └── .generateIndex
 └── app.js
  • Contents of .gnerateIndex
api, lcd:lcd
  • Generated virtual index.js
module.exports = {
  start : require('./api/start.js'),
  stop : require('./api/stop.js'),
  info : require('./info.js'),
  lcd: {
    check: {
      checking: require('./lcd/check/checking.js')
    },
    display: require('./lcd/display.js)
  }
};

will create index.js

const include = require('rinclude');
include.path('./custom');
include.options({ createIndex: true, type: 'es6' });
// This file was generated automatically.
// Do not edit!!
import pause from './pause.js';
import start from './start.js';
import stop from './stop.js';

export default { pause, start, stop };
const include = require('rinclude');
include.path('./custom');
include.options({ createIndex: true, type: 'es5' });
// This file was generated automatically.
// Do not edit!!
module.exports = {
  pause: require('./pause.js'),
  start : require('./start.js'),
  stop : require('./stop.js')
};

gulp task

When a new file is created or deleted in the path, a new index.js file is created.

Create a new index.js file even if the file content of the .generateIndex changes.

// /tasks/rinclude.js
'use strict';

const include = require('rinclude');
const watch = require('gulp-watch');
require('colors');

// Add a custom library folder here.
const options = {
  libs: [{
    path: '../test',
    prefix: undefined
  }]
};

function manipulate(opts) {
  opts.js = [];
  opts.gen = [];

  opts.libs.forEach(lib => {
    const base = lib.path.replace(/\.+\//g, '');

    opts.js.push(`${base}/**/*.js`);
    opts.gen.push(`${base}/**/.generateIndex`);
  });
}

manipulate(options);

function createIndexjs() {
  include.clean();
  options.libs.forEach(lib => {
    include.path(lib.path, lib.prefix);
  });
  console.log('[rinclude]'.green + ' index.js was created automatically.');
}

function rinclude(cb) {
  include.options({ createIndex: true, type: 'es6' }); // type es6 or es5

  createIndexjs();
  cb();
}

watch(options.js, { readyDelay: 500 }, vinyl => {
  if (/add/.test(vinyl.event)) {
    console.log('[gulp]'.green + ` File ${vinyl.path} was added`);
    createIndexjs();
  }

  if (/unlink/.test(vinyl.event)) {
    console.log('[gulp]'.green + ` File ${vinyl.path} was removed`);
    createIndexjs();
  }
});

watch(options.gen, { readyDelay: 500 }, vinyl => {
  if (/change/.test(vinyl.event)) {
    console.log('[gulp]'.green + ` File ${vinyl.path} was changed`);
    createIndexjs();
  }
});

module.exports = rinclude;
// /gulpfile.js
'use strict';
const gulp = require('gulp');

// load gulp tasks
require('gulp-load-all-tasks')('tasks');

// Build task definitions
gulp.task('dev', gulp.series('rinclude'));
// /server.js
You do not need to require('rinclude').
Because index.js has already been created.
0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.23

9 years ago

0.0.22

9 years ago

0.0.21

9 years ago

0.0.20

9 years ago

0.0.19

9 years ago

0.0.18

9 years ago

0.0.17

9 years ago

0.0.16

9 years ago

0.0.15

9 years ago

0.0.14

9 years ago

0.0.13

9 years ago

0.0.12

9 years ago

0.0.11

9 years ago

0.0.10

9 years ago

0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

10 years ago

0.0.1

10 years ago