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 runningsass src/sass/ dist/css/
The Sass file can be compiled.
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 first procedure ishello world
. New construction in the projectbinDirectory, under which the directory is created ishelperThe specific content of the document is as follows:
#!/usr/bin/env node ('hello world');
Modify the permissions to the helper file:
$ chmod 755 ./bin/helper
implementhelperThe file will be displayed on the terminalhello world
:
$ ./bin/helper hello world
Symbol Links
Next we create a symbolic link, globallynode_modulesIn the directory, a symbolic link is generated to point to the local directory of the module, so that we can use it directlyhelper
Order.
In the projectAdd to the filebinFields:
{ "name": "helper", "bin": { "helper": "bin/helper" } }
Execute in the current project directorynpm link
Command 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 it directlyhelper
Order:
$ helper hello world
Commander module
In order to write command line tools more efficiently, we use TJ's mastercommanderModule.
$ npm install --save commander
helperThe file content is modified to:
#!/usr/bin/env node var program = require('commander'); program .version('1.0.0') .parse();
implementhelper -h
andhelper -V
Order:
$ helper -h Usage: helper [options] Options: -h, --help output usage information -V, --version output the version number $ helper -V 1.0.0
commanderModule provision-h
, --help
and-V
, --version
Two built-in commands.
Create command
Create ahelper hello <author>
command when the user entershelper hello ipluser
When the terminal displayshello ipluser
. RevisehelperFile content:
#!/usr/bin/env node var program = require('commander'); program .version('1.0.0') .usage('<command> [options]') .command('hello', 'hello the author') // Add hello command .parse();
existbinCreated in the directoryhelper-hellodocument:
#!/usr/bin/env node ('hello author');
implementhelper hello
Order:
$ helper hello ipluser hello author
Analyze input information
We hopeauthorIt is input by the user and the terminal should display it ashello ipluser
. Revisehelper-hello
File content, parse user input information:
#!/usr/bin/env node var program = require('commander'); (); const author = [0]; ('hello', author);
Execute againhelper hello ipluser
Order:
$ helper hello ipluser hello ipluser
Oh yeah, it's finally done, but as a programmer, that's not enough. When the user does not enterauthorWhen we hope the terminal can remind the user to enter information.
Prompt information
existhelper-helloAdd prompt information to the file:
#!/usr/bin/env node var program = require('commander'); ('<author>'); // When the user enters `helper hello -h` or `helper hello --helper`, the command usage example is displayed('--help', function() { (' Examples:'); (' $ helper hello ipluser'); (); }); (); ( < 1) && (); // When the user does not enter information, call the `help` method to display help information const author = [0]; ('hello', author);
implementhelper hello
orhelper hello -h
Command, the terminal will display 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 it be considered true learning. I hope this article will be helpful to everyone.