Coder Social home page Coder Social logo

module_loader's Introduction

Module loader

Automatic loads modules in specially appointed configs and locations.

Somebody says that convention is greater than configuration,so I make some strict conventions to make my projects normalized and easy to extend.

Install

npm i kz_loader -S

Test

npm test

Usage

you should prepare your modules in folders like below:

├─config
│      db.json
│      get_even_number.json
│
├─init
│      db.js
│
├─util
│      get_even_number.js
│
└─worker
        db.js

the modules in folder 'config' should be json files,and module in other folders should be js file.

a json file should be like this:

confA.json

{
  "some_propertise": "some_content"
}

a js file will be like this: module_A.js

module.exports = async (module_name, config, g) => {
  //module_name is the module's filename,here the name is 'module_A'

  //config is all configs  of this application,you can get this by using config['some_propertise'],this will return 'some_content'

  //g is a global variable,you can set anything you want here

  //if you return something,you will get it after load
  return;
};

then you should prepare the config of these modules:

const loader = require('module_loader');

//orders is the loading order of each type modules,modules not in this array won't load

//path point out the run folder,default for current foler
let configs = {
  orders: {
    config: ['db', 'redis'],
    util: ['get_even_number'],
    init: ['db', 'redis'],
    worker: ['db']
  },
  path: 'test/fake2'
};
my_loader = new loader(configs);

//you can get you want if you return something in module
let result = await my_loader.load();

//you can also get notified by event
/*
my_loader.on('ok', result => {
    ...
});
my_loader.load();
*/

you can find the detail usage in test folder

module_loader's People

Contributors

kzfile avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.