node-traverse

Bind a directory to an object and load the modules in the directory dynamically.

Usage no npm install needed!

<script type="module">
  import nodeTraverse from 'https://cdn.skypack.dev/node-traverse';
</script>

README

Build Status Maintainability codecov GitHub issues GitHub

LICENSE

MIT

GOAL

Bind a directory to an object and load the modules in the directory dynamically.

INSTALL

npm i node-traverse

HOW TO USE

#   game
#    ├ user
#    │  └ weapon.js -------- exports.id = 3;
#    ├ Fashion.js   -------- exports.getList = function(){ return 1; };
#    ├ Fashion
#    │  └ hair.js   -------- exports.id = 4;
#    ├ friend.js    -------- class A{constructor(n){this.n = n}};A.n=2;module.exports=A;
#    └ user.js      -------- exports.id = 2;


const Trav = require('node-traverse');
const trav = Trav.import('test/game', { baseDir: process.cwd() });

trav.Fashion.getList();   // 1
trav.Fashion.hair.id;     // 4
trav.user.id;             // 2
trav.user.weapon.id;      // 3



const trav = Trav.import('game', {
    baseDir: __dirname,
    importType: Trav.IMPORT_TYPE.CLASS_INSTANCE,
    instanceParams: ['hee'],
});

trav.friend.n;             // hee;

const trav = Trav.import('game', {
    baseDir: __dirname,
    importType: Trav.IMPORT_TYPE.CLASS_AUTO,
});

trav.friend.n;             // 2;
trav.friend('hee').n;      // hee;