project-core

2.1.0 • Public • Published

NPM version build status Test coverage David deps node version npm download npm license

project-core

Installation

$ npm install project-core --save

Notes: only support Node.js v6.0 or above

Usage

'use strict';
 
const ProjectCore = require('project-core');
 
// create new instance
const $ = global.$ = new ProjectCore();
 
// util functions, extends from lei-utils
console.log($.utils.md5('haha'));
// add your own function
$.utils.say = function () {};
 
// extends
$.extends({
  // before: optional, will be called before initializing plugins
  before(next) {},
  // init: initialize plugin
  init(next) {},
  // after: optional, will be called after plugins initialized
  after(next) {},
});
 
// init queue
// all the functions that added to init queue will be called sequentially
$.init.add(function (next) {});
$.init.add(function (next) {});
// support async function (don't need to use callback)
$.init.add(async function () {});
 
// add file or dir to init queue
$.init.load('./inits');
 
// events
// ready: will be emitted when project inited
$.event.once('ready', function () {});
// you can emit a custom event
$.event.emit('haha', Math.random());
 
// add ready event listener
// if project was inited, callback immediately
$.ready(function () {});
 
// config
$.config.load('./config');
console.log($.config.get('web.port'));
console.log($.config.has('web.port'));
 
// init
$.init();

Configuration file

JavasSript file

exmaple file dev.js:

module.exports = function (set, get, has) {
 
  set('web.session.secret', '11111111');
  // use the get(name) and has(name) to get specific config value
  // you can also use this.set(), this.get() and this.has()
  // for exmaple: this.set('we.session.secret')
 
};

use $.config.load('/path/to/dev.js') or $.config.load('/path/to/dev') to load this config file.

JSON file

example file dev.json:

{
  "web": {
    "session": {
      "secret": "11111111"
    }
  }
}

use $.config.load('/path/to/dev.json') to load this config file.

YAML file

example file dev.yaml (or dev.yml):

web:
  session:
    secret: '11111111'

use $.config.load('/path/to/dev.yaml') to load this config file.

Extends and republish your own project-core

'use strict';
 
const ProjectCore = require('project-core');
 
const $ = new ProjectCore();
 
// extends example
$.extends(require('./extends_config'));
$.extends(require('./extends_orm'));
$.extends(require('./extends_cache'));
$.extends(require('./extends_module'));
$.extends(require('./extends_status'));
 
// do something when ready
$.event.once('ready', function () {
  // after ready event has been emitted, you can not change anything on project-core instance any more
});
 
// exports
module.exports = $;

or:

'use strict';
 
const ProjectCore = require('project-core');
 
class MyProject extends ProjectCore {
 
  constructor() {
    super();
    // do something
  }
 
  init(callback) {
    // do something before init...
    // and then call super.init()
    super.init(callback);
  }
 
}

License

The MIT License (MIT)

Copyright (c) 2016 Zongmin Lei <leizongmin@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Readme

Keywords

none

Package Sidebar

Install

npm i project-core

Weekly Downloads

3

Version

2.1.0

License

MIT

Last publish

Collaborators

  • leizongmin