preface

We introduced some of the tools in the previous article, Advanced: Useful Tools part 1.

In this article, we’ll cover the remaining tools.

Note: All the examples below use the Macos environment as an example

execa

With this tool, we can execute command line instructions in our code.

The installation

npm install execa
Copy the code

use

const execa = require('execa')

execa('git'['--version']).stdout.pipe(process.stdout);
execa('node'['-v']).stdout.pipe(process.stdout);
Copy the code

For an introduction to Node, see Basics: The Node Repository. In this article we use version 5.1.1 for easy demonstration purposes

We can even use it to do some Git manipulation

execa('git',['clone','https://github.com/varletjs/varlet.git']).stdout.pipe(process.stdout);
Copy the code

At this point, you’re smart enough to have figured out the easiest way to implement the CLI, which is to pull the project template directly from the remote side through the command line

The corresponding git command is available

git clone -b [branch] [repo url] [folderName/user input]

execa('git',['clone','-b','dev','https://github.com/varletjs/varlet.git','niubi']);
Copy the code

ora

In the process of project creation, it is inevitable to encounter a waiting situation.

Taking git Clone as an example, many partners have tried it in detail. Do you find that there is no hint in the process of clone, and the experience is very unfriendly?

At this point we can use ORA to add a process animation.

The installation

npm install ora
Copy the code

use

We still take git Clone as an example

const ora = require('ora');
const execa = require("execa");

const spinner = ora('Loading, please wait for a moment').start();
await execa('git'['clone'.'-b'.'dev'.'https://github.com/varletjs/varlet.git'.'niubi'])
spinner.succeed('Loaded succeed')
Copy the code

Your ORA is working successfully when a little thing like this appears on your command line

Loading completion prompt

fs-extra

Node itself provides the FS module, but it’s pretty cumbersome to use.

Fs-extra is a much easier library to use, adding file system methods not included in the native FS module and adding promise support to fs methods. The node version must be 10.12.0 or later.

This library is also used in Zuoye’s Vue-Next

The installation

npm install fs-extra
Copy the code

Common use

Copy copy

fs.copy('myfile.js'.'mynewfile.js'.err= > {
    if (err) return console.error(err)
    console.log('success! ')})Copy the code

EnsureDir Creates a directory

Make sure the directory exists. If a directory structure does not exist, create one.

fs.ensureDir('./src')
Copy the code

CreateWriteStream writable stream

const writer =  fs.createWriteStream('./myfile.txt')
writer.write('Hello')
Copy the code

At this time, some careful friends found that we change the content to write, each time will rewrite all the content, then how to connect with the existing content to continue to write?

Don’t worry, this is coming ~

const writer =  fs.createWriteStream('./myfile.txt', {flags:'a' })
writer.write(' Varlet')
Copy the code

OutputFile Writes files

This method looks similar to writeFile at first glance, except that it creates a destination file when the file to be written does not exist.

fs.outputFile('mynewfile.txt'.'hello varlet! ')
fs.outputFile('anotherfile.txt'.'hello varlet! ')
Copy the code

Remove delete

fs.remove('anotherfile.txt'.function(err) {
    if (err) return console.error(err)
    console.log("success!")})Copy the code

PathExists Path check

fs.pathExists('index.js'.(err, exists) = > {
    if (err) return console.log(err);
    console.log(exists);
});

fs.pathExists('anotherfile.txt'.(err, exists) = > {
    if (err) return console.log(err);
    console.log(exists);
});
Copy the code

Readdir Reads the file directory

fs.readdir('src'.(err,files) = >{
    if(err) return console.log(err);
    console.log(files)
})
Copy the code

slash

Used to convert Windows backslash paths to forward slash paths \ => /

The installation

npm install slash
Copy the code

use

const string = path.join('foo'.'bar');
// Unix => foo/bar
// Windows => foo\bar

slash(string);
// Unix => foo/bar
// Windows => foo/bar
Copy the code

hash-sum

Super fast unique hash generator.

We can also see this library in zuoye vue-next

The installation

npm i hash-sum -S
Copy the code

use

const hash = require('hash-sum');

hash(value)
Copy the code

In varlet-CLI we use it to handle style scoped

chokidar

We use Chokidar to monitor file changes

The installation

npm install chokidar
Copy the code

use

chokidar.watch('. ').on('all'.(event, path) = > {
    console.log(event, path);
});
Copy the code

In dev mode, we will complete the restart of the service after the modification of the file. In fact, it is using this tool library

The last

Above, the introduction of the tools used by the CLI is concluded. For more information, see the official documentation of each tool.

See documentation execa, ORA, Chokidar, FS-extra, Hash-sum,slash