Preface
Node has brought great changes to front-end development and promoted the automation of front-end development. We can simplify development work and then use various toolkits to generate production environments. If you run sass src/sass/main.scss dist/css/main.css you can compile the Sass file.
In actual development, we may have our own specific needs.
Then we have to learn how to create a Node command line tool.
hello world
The old rules first procedure is hello world . Create a new bin directory in the project and create a file named helper under this directory. The specific content is as follows:
#!/usr/bin/env nodeconsole.log('hello world');Modify the permissions to the helper file:
$ chmod 755 ./bin/helper
Execute the helper file, and the terminal will display hello world :
$ ./bin/helperhello world
Symbol Links
Next we create a symbolic link, and in the global node_modules directory, we generate a symbolic link to point to the local directory of the module, so that we can directly use the helper command.
Add bin field to the project's package.json file:
{ "name": "helper", "bin": { "helper": "bin/helper" }} Execute the npm link command in the current project directory to create a symbolic link for the current module:
$ npm link/node_path/bin/helper -> /node_path/lib/node_modules/myModule/bin/helper/node_path/lib/node_modules/myModule -> /Users/ipluser/myModule
Now we can use the helper command directly:
$ helperhello world
Commander module
In order to write command line tools more efficiently, we use the commander module of TJ master.
$ npm install --save commander
The helper file content is modified to:
#!/usr/bin/env nodevar program = require('commander');program .version('1.0.0') .parse(process.argv); Execute helper -h and helper -V commands:
$ helper -h Usage: helper [options] Options: -h, --help output usage information -V, --version output the version number$ helper -V1.0.0
The commander module provides two built-in commands: -h , --help and -V , --version .
Create command
Create a command to helper hello <author> . When the user enters helper hello ipluser , the terminal displays hello ipluser . Modify the helper file content:
#!/usr/bin/env nodevar program = require('commander');program .version('1.0.0') .usage('<command> [options]') .command('hello', 'hello the author') // Add hello command.parse(process.argv);Create a new helper-hello file in the bin directory:
#!/usr/bin/env nodeconsole.log('hello author'); Execute helper hello command:
$ helper hello ipuserhello author
Analyze input information
We want the author to be entered by the user and the terminal should be displayed as hello ipluser . Modify the content of the helper-hello file and parse the user input information:
#!/usr/bin/env nodevar program = require('commander');program.parse(process.argv);const author = program.args[0];console.log('hello', author); Then execute helper hello ipluser command:
$ helper hello ipluserhello ipluser
Oh yeah, it's finally done, but as a programmer, that's not enough. When the user does not enter the author , we hope that the terminal can remind the user to enter information.
Prompt information
Add prompt information to the helper-hello file:
#!/usr/bin/env nodevar program = require('commander');program.usage('<author>');// When the user enters `helper hello -h` or `helper hello --helper`, function() { console.log(' Examples:'); console.log(' $ helper hello ipluser'); console.log();});program.parse(process.argv);(program.args.length < 1) && program.help(); // When the user does not enter information, call the `help` method to display the help information const author = program.args[0];console.log('hello', author); Execute helper hello or helper hello -h command, and the terminal will display the help information:
$ helper hello Usage: helper-hello <author> Options: -h, --help output usage information Examples: $ helper hello ipluser$ helper hello -h Usage: helper-hello <author> Options: -h, --help output usage information Examples: $ helper hello ipluser
Summarize
At this point, we wrote a helper command line tool and has the helper hello <author> command. Friends who are just interested should quickly start practicing it yourself. Only by doing it yourself can you be considered a real learning. I hope this article will be helpful to everyone.