Command line tool. Jpeg


Node.js is a JavaScript runtime environment based on Chrome V8 engine. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient. Node.js package manager NPM is the world’s largest open source library ecosystem. Node.js as one of the hottest development tools, how to use it to develop command-line programs, is a Web developer should learn to master. This article mainly uses the COMMANDER plug-in to develop the command line tool. Commander. Js is a package that helps you quickly develop the Nodejs command-line tool.

Script interpreter:

#! /usr/bin/env node

At the beginning of the code, the first line must specify the interpreter our script needs to execute. Here, we use Node as the interpreter for the script. And our #! /usr/bin/env node is written to use env to find node and to use node as an interpreter for the program.

env

Env specifies many of the system’s environment variables, including the paths to some of our installed environments. The path we take to install Node may vary depending on the operating system, but its environment variables will be inside env, so here we use env to find node and use Node as an interpreter. So the main purpose of env is to enable our scripts to be interpreted and launched properly on different operating systems. Command-line tool development: here we will compile sASS files into CSS files, and put in the corresponding directory. Monitor sASS file changes automatically perform compilation to CSS this function as an example, step by step to implement sASS file compilation and listening functions.

The plug-in Installation method describe
commander npm install commander A package to help quickly develop Nodejs command-line tools
chalk npm install chalk Terminal output color style output tool
gulp npm install gulp An automated build tool that developers can use to automate common tasks during project development
gulp-sass npm install gulp-sass Tools to compile SASS files into CSS files

Here, in package.json, we define the name of the command and the path of the script to execute when it is executed:

"bin": {
    "cain": "./bin/cain.js"
 },Copy the code

cain.js

#! /usr/bin/env node var program = require('commander'), chalk = require('chalk'), SassMain = require('./sass.js'); / / program.command ('sass').option('--w', 'sass').option('--w', 'sass') ').alias('sa').description('sass ').action(function (option) {var isWatch = option.w? True: false; if (isWatch) { SassMain.sassWatch(); }else { SassMain.sassMake(); }});Copy the code

This is the content of canain.js script, which defines a sass command. Cain sass command: Cain sass option: optional –w (Cain sass watch) alias: Cain sass Description: Description of the command Action: Method to be executed after the command is executed.

sass.js

@author wangzhengkai */ 'use strict'; var sass = require('gulp-sass'), chalk = require('chalk'), Util = require('.. /lib/util'), gulp = require('gulp'); var sassMain = { sassMake: function () { sassChange(); }, sassWatch: function() {console.log(" listening... )); var conf; Util.getConfig(function(config){ conf = config; }); var watcher = gulp.watch(conf.sass.sassUrl+'*.scss'); watcher.on('change', function (event) { sassChange(); }); }}; /** * [sassChange] * @return {[type]} [description] */ function sassChange() {var conf; Util.getConfig(function(config){ conf = config; }); return gulp.src(conf.sass.sassUrl+'*.scss') .pipe(sass().on('error', sass.logError)) .pipe(gulp.dest(conf.sass.output));  } module.exports = sassMain;Copy the code

Sass. Js file defines the sass file compilation method and its listener method, here mainly through the integration of gulp and gulp-sass sass file compilation and listener method. If you are interested, you can try to see the internal implementation of these two plug-ins and do it yourself. In the sass.js file, I refer to an Util file in which I implement a getConfig() method. This method retrieves the configuration file of the user’s current execution directory and reads the configuration content of the user’s Settings.

GetConfig () method:

Function (callback) {var configPath = path.join(process.cwd(), 'caain.config.js '); var config = {}; if (fs.existsSync(configPath)) { try { config = eval(fs.readFileSync(configPath,"utf-8")); callback&&callback(config); } catch (e) {console.log(" failed to read caain.config.js file "); }}else {console.log(" canain.config.js file does not exist, please check and try again "); }},Copy the code

Cain. Config.js file contents:

module.exports = {
    name:"test",
    sass:{
        sassUrl:"sass/",
        output:"css/"
    }
};Copy the code

At this point, the development of sASS file compilation and listening is complete. Finally, to see the effect, simply execute the NPM link command in the package.json file’s directory.

Summary:

In the process of command line tool development, commander, Inquirer, Chalk and other NPM plug-ins can be fully used to complete the development of command line tool, and can achieve great results. If you are interested in it, please try it and make a command line tool of your own.