Write a small demo, used to replace the touch create file command touchme, can create its own “Buddha bless” annotations file. The effect is as follows:



The command can take an argument to select the symbol of the comment



Now, start the code ~

First create a folder, I’ll call it create-file-cli and then create a package.json file using the NPM init command.

$ mkdir create-file-cli

$ cd create-file-cli

$ npm init -yCopy the code

Then modify package.json to add a bin field, define a touchme command, and specify the file to execute the command.

{
  "name": "create-file-cli"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "bin": {
    "touchme": "bin/touchme.js"
  },
  "keywords": []."author": ""."license": "ISC"
}Copy the code

The next step to implement bin/touchme.js is to use Commander. Js — node.js command line interface complete solution. Unable to read English documents and thoughtful Chinese README.

Bin/touchme. Js as follows

#! /usr/bin/env node

const program = require('commander');
const gen = require('.. /lib/generate-file');

program
  // Version information
  .version('0.0.4'.'-v, --version')
  // Instructions for usage
  .usage('
       
         [options]'
       )
  // Select name option description Default value
  // The option can take one parameter, which can be obtained from program.copy
  // The value is true if there are no arguments
  .option('-c, --copy <source>'.'copy file and add comment')
  .option('-H, --hashtag'.`comment by '#'`)
  .option('-s, --slash'.`comment by '/'`)
  .parse(process.argv);

function resolve(program) {
  // Parameters that do not match any of the options are placed in the array args
  const { copy, hashtag, slash, args } = program;
  if(! args.length) {console.log('Please input filename.');
    return;
  }
  if (copy === true) {
    console.log('You should copy at least one file.');
    return;
  }
  let type = 'star';
  if (slash) type = 'slash';
  if (hashtag) type = 'hashtag';
  for (let i = 0; i < args.length; i++) {
    gen(args[i], copy, type);
  }
}

resolve(program);
Copy the code

The implementation of lib/generate-file.js at https://github.com/G-lory/create-file-cli/ is simply to create a file and write comments.

Option defines command options and parameters.

Program allows you to obtain information about parameters entered on the command line.

Now that the feature is written, all that remains is to publish. Check https://www.npmjs.com first to see if your package name has been published. If so, you need to change the package name first. Then sign up for an account at https://www.npmjs.com. After memorizing your account password and email address, return to the command line.

$ npm login
Username: ...
Password: 
Email: (this IS public)
Logged in as ... on https://registry.npmjs.org/.Copy the code

Note is displayed after a successful login https://registry.npmjs.org/ many students set up the mirror image of the taobao shows, it is not this address, so remember to come back.

$ npm config set registry=http://registry.npmjs.org
Copy the code

Then you can publish the package.

$ npm publishCopy the code

If there are changes later, just change the version number in package.json and do NPM publish again.

Go to the NPM website and search for your own package. Then there is the installation to test the functionality.

Global installation

npm install create-file-cli -g
Copy the code

You can then create the file using the Touchme command. You can also use TouchMe -h to view help.

A command line tool has been created.

Writing for the first time in nuggets, still a bit (cross out) very wet, well, the New Year will fuel.