Reference address 1, reference address 2
First, npm Package.json bin
1, Package.json
{ "name": "Test", "version": "1.0.0", "description": "", " Main ":" Index.js ", " Scripts ": { " test ":" Echo \ "Error:no test specified\" && exit 1 " }, " keywords ": [], " author ":" ", " license ":" ISC ", "Bin": { "gen": "Bin/gen.js" }, "dependencies": { "Commander": "^2.15.1" }}
2, Bin/gen.js
#!/usr/bin/env nodevar argv = process.argv; var filePath = __dirname; var currentpath = process.cwd (); Console.log (argv) console.log (FilePath) console.log (Currentpath)
II, Commnader + inquirer + minimist + Download-git-repo + ejs (nunjucks, handlebars) + Execa (child_process)
Commander Example
#!/usr/bin/env nodevarprogram = require (' Commander '); Program. Version (' 0.0.1 '). Option ('-C,--chdir <path> ', ' Change the working directory '). Option ('-C,--config <path> ', ' set config path. defaults to./deploy.conf '). Option ('-T,--no-tests ', ' Ignore Test hook '). Option ('-P,--peppers ', ' Add peppers '). Option ('-P,--pineapple ', ' Add pineapple '). Option ('-B,--bbq-sauce ', ' Add BBQ sauce '). Option ('--p,--fuck-you ', ' fuckyou '). Option (' Build--env <fuckyou> ', ' FUCKYOU2 '). Parse (PROCESS.ARGV);if(program.peppers) Console.log ('-peppers '));if(program.pineapple) Console.log ('-pineapple '));if(program.bbqsauce) Console.log ('-BBQ '));if(program.fuckyou) Console.log (' fuckyou '));if(program.env && Program.args.length! = 0{console.log (Program.args);} program. Command (' Init '). Description (' Run Remote setup commands '). Action (function() {Console.log (' Setup '); }); Program. Command (' EXEC <cmd> '). Description (' Run the given remote command '). Action (function(cmd) {console.log (' Exec '%s ', CMD); }); Program. Command (' Teardown <dir> [otherdirs ...] '). Description (' Run teardown commands '). Action (function(dir, otherdirs) {Console.log (' dir '%s ', dir); if(otherdirs) {Otherdirs.foreach (function(ODir) {Console.log (' dir '%s ', ODir); }); } });
How to develop a command-line interaction tool with Nodejs