1.0.0 • Published 3 years ago
lila-core v1.0.0
lila-core
Lila core library.
install
npm install --save-dev lila-coreuse
// es6
import lila from 'lila-core';
// commonjs
const lila = require('lila-core');init file
Lila requires an init file called lila.init.js in project root directory.
// lila-core should not be imported directly
import tasksPlugin from 'lila-tasks';
import otherPlugin from 'lila-other-plugin';
// here should export a function
export default lila => {
// do some init actions
tasksPlugin(lila);
otherPlugin(lila);
// here return a config generator
return ({ entry, argv, cmd }) => {
// make a config according to `entry, argv, cmd`
const config = { ... }
// return a config object
return config;
};
}config
{
...,
tasks: [ ... ], // tasks run by `lila.runTasks`
}tasks
[
'task1',
['task2'],
['task3', arg0, arg1, ...],
...
]api
lila.setSetting: set a setting
lila.setSetting(name, value);@param/name:type: stringsetting name@param/value:type: *setting value
lila.setSettings: set multiple settings
lila.setSettings(value);@param/value:type: {}key-valuepairs of settings
lila.setSettings({
name1: value1,
name2: value2,
...
});lila.getSetting: get a setting value
const value = lila.getSetting(name);@param/name:type: stringsetting name@return:type: *setting value
lila.getSettings: get multiple setting values
const values = lila.getSettings(names);@param/names:type: string[]setting names@return:type: *[]setting values
const [value1, value2, ...] = lila.getSettings([name1, name2, ...]);lila.getAllSettings: get all settings
const settings = lila.getAllSettings();@return:type: {}key-valuepairs of all settings
lila.registerTask: register a task generator
lila.registerTask(name, generator);@param/name:type: stringtask name@param/generator:type: functiontask generator
generator: task generator
Generate a gulp task callback. See taskFunction.
({ entry, args, argv, cmd, config, lila, gulp }) => gulp-task-callback;@param/options.entry:type: stringcurrent handling entry@param/options.args:type: *[]arguments from config,['taskName', arg0, arg1, ...]@param/options.argv:type: {}wrappedprocess.argv@param/options.cmd:type: stringcommand name, likerun, dev, build, ...@param/options.config:type: {}config of current entry@param/options.lila:type: {}lila-corereference@param/options.gulp:type: {}gulp referencegulp-task-callback: see taskFunction
lila.registerTask('log', ({ entry }) => cb => {
console.log(`entry: ${entry}`);
cb();
});lila.unregisterTask: unregister a task generator
lila.unregisterTask(name);@param/name:type: stringtask name
lila.getTask: get a task generator
const generator = lila.getTask(name);@param/name:type: stringtask name@return:type: functiontask generator
lila.getTasks: get multiple task generators
const generators = lila.getTasks(names);@param/names:type: string[]task names@return:type: function[]task generators
const [generator1, generator2, ...] = lila.getTasks([name1, name2, ...]);lila.getAllTasks: get all task generators
const tasks = lila.getAllTasks();@return:type: {}name-generatorpairs of all tasks
lila.addCommand: add a command
lila.addCommand(initializer);@param/initializer:type: functioncommand initializer
initializer: command initializer
commander => { ... }@param/commander:type: {}see commander.js
commander => {
commander
.command('log')
.action(function () {
console.log('log succeded');
});
}lila.getCommands: get added command initializers
const initializers = lila.getCommands();@return:type: function[]added command initializers
lila.makeConfig: get config of an entry
const config = lila.makeConfig({ entry, argv, cmd });@param/options.entry:type: stringhandling entry@param/options.argv:type: {}wrappedprocess.argv@param/options.cmd:type: stringcommand name
lila.runTasks: run tasks
lila.runTasks({ entries, argv, cmd }, success, error);@param/options.entries:type: string[]entries to build@param/options.argv:type: {}wrappedprocess.argv@param/options.cmd:type: stringcommand name@param/success:type: functionsuccess callback,() => { ... }@param/error:type: functionerror callback,err => { ... }
lila.addCmdOption: add an option to a command
lila.addCmdOption(name, ...option);name: command nameoption: see commander.js#command-specific-options
lila.addCmdOption('run', '-e, --env', 'specify server environment');lila.getCmdOptions: get options of a command
const options = lila.getCmdOptions(name);
// example
options.forEach(option => {
command.option(...option);
});name: command name
lila.makeArgv: make a wrapped process.argv
import commander from 'commander';
import lila from 'lila-core';
const { makeArgv } = lila;
commander
.command('build [entries...]')
.description('pack source codes to production bundles')
.option('-e, --env [env]', 'Specify server enviroment.')
.action((entries, options) => {
// make a wrapped `process.argv`
const argv = makeArgv(options, keepUnknown);
});@param/keepUnknown:type: booldefault: falsewhether to keep unknown args which stored by the key_, see minimist
lila.log:
lila.log(...args) => console.log(...args)
lila.log(color, ...args) => console.log(...chalk.color(args))
lila.log(false, ...args) => console.log(...args)See chalk.
lila.info:
lila.info(...args) => console.info(...chalk.blueBright(args))
lila.info(color, ...args) => console.info(...chalk.color(args))
lila.info(false, ...args) => console.info(...args)See chalk.
lila.warn:
lila.warn(...args) => console.warn(...chalk.yellowBright(args))
lila.warn(color, ...args) => console.warn(...chalk.color(args))
lila.warn(false, ...args) => console.warn(...args)See chalk.
lila.error:
lila.error(...args) => console.error(...chalk.redBright(args))
lila.error(color, ...args) => console.error(...chalk.color(args))
lila.error(false, ...args) => console.error(...args)See chalk.
lila.success:
lila.success(...args) => console.log(...chalk.greenBright(args))
lila.success(color, ...args) => console.log(...chalk.color(args))
lila.success(false, ...args) => console.log(...args)See chalk.
built-in settings
src:type: stringdefault: srcsource directory name.dev:type: stringdefault: devdevelopment directory name.build:type: stringdefault: buildbuild directory name.tmp:type: stringdefault: .lilatmp directory of project.root:type: stringdefault: process.cwd()read onlyroot directory, and you can customize it by--rootthrough command line.defaultEntry:type: stringdefault: @lila/indexread onlydefault entry, if you do not provide an entry in cli.
extensible settings:
beforeTasks: before run tasks
({entries, argv, cmd, lila, gulp}) => { ... }entries:type: []all entriesargv:type: {}wrappedprocess.argvcmd:type: stringcommand namelila:type: {}lila-corereferencegulp:type: {}gulp reference
afterTasks: after run tasks
({entries, argv, cmd, lila, gulp}) => { ... }errorTasks: error occurred when run tasks
({entries, argv, cmd, lila, gulp, error}) => { ... }error: error object
built-in commands
run: run tasks
lila run entry1 entry2 entry3 ...If entry is not provided, @lila/index will be used as default.
lila run # the real entry is @lila/indexextended command line options
--root: custom root path--init: custom init file, defaultlila.init.js
note
You should not import lila-core directly, like:
import lila from 'lila-core'but use it in lila.init.js or plugins:
export default lila => {
// do with lila
}npm packages
- gulp: 4.x