First, the necessary plug-in
1. BABEL:ES6 syntax support, requires babel-perset-es2015 (convert to ES5 execution), Babel, Babel-core (program execution)
2. Commander: Custom Command Plug-in, provide parameter delivery, etc.
3. Liftoff: Implements input monitoring of the command window, as well as the command format specification.
4. Minimist: Read command line parameter plug-in
The above plugins can be installed directly via NPM.
Second, the sample code analysis:
2.1. Cli.js
var liftOff = require ("LiftOff");
Import app from "./app";
var argv = require ("Minimist") (Process.argv.slice (2));
var cli =new liftOff ({
Name: "Sam",
Processtitle: ' Samlong ',
ModuleName: "Sam",
extensions:{
'. js ': null
}
});
Cli.launch ({
CWD:ARGV.R | | Argv.root,
CONFIGPATH:ARGV.F | | Argv.file
},function (env) {
Console.log ("command name:" + argv["_"]);
Console.log ("Execution path:" + ENV.CWD);
var commander = require (' Commander ');
var _app = new app ();
_app.register (commander);
});
1. Liftoff class: Parsing command-line arguments by importing liftoff third-party plug-ins, and capturing by liftoff instance method launch
2. Require ("Minimist") (Process.argv.slice (2)): Convert command-line arguments to Objects
3../app: Import command Execution class
2.2. App.js
class app{ Register (commander) { Commander . Option ('-D--date ', "Show Time") . Parse (PROCESS.ARGV); if (commander.date) { console.log (' Date command is executed '); } default APP;
A simple command execution class that is implemented through commander.
2.3. Index.js
Require ("Babel-core/register");//import Babel-core, used to convert my Es6require ("./src/app") in code; require ('./ Src/cli ');
2.4. Execution Effect:
Third, git address
[Email Protected]:cqhaibin/commandertest.git
Nodejs command line, custom