This is the first day of my participation in the August More text Challenge. For details, see: August More Text Challenge.

One other thing today is that Node does an automated build of the front end, which makes our development a lot less tedious.

An important component of automated builds is the Core Node module child_process, which lets you create asynchronous processes. With this, you can do a lot of interesting things with shell scripts.

1. child_process

* exec

Create a shell, and then execute commands in the shell. When the execution is complete, pass stdout and stderr as parameters to the callback method. However, the cache is only 200KB by default, which can be set via options.maxBuffer, so only child processes that return a small number of return values are run

const exec = require('child_process').exec; const child = exec(`cd ${path} && npm run build`) child.stdout.on('data', function(data) { console.log(data) }) child.stderr.on('data', function(data) { console.log('stderr: '+ data)}) child.on('close', function(code) {if(code == 0){console.log(' wrap up ')}else{console.log('Error'); }})Copy the code

* spawn

Spawn is similar to exec, except that spawn does not cache the data in the output stream, so there is no size limit. Spawn is usually used to run child processes that return large amounts of data, such as image processing, file reading, etc. And spawn put the shell arguments in the array

const spawn = require('child_process').spawn; const child = spawn(`ls`, ['-a']) child.stdout.on('data', function(data) { console.log(data) }) child.stderr.on('data', function(data) { console.log('stderr: '+ data)}) child.on('close', function(code) {if(code == 0){console.log(' wrap up ')}else{console.log('Error'); }})Copy the code

* execFile

Similar to.exec(), except that no new shell is created. Note: execFile() is ultimately implemented within spawn(), if not set {shell: ‘/ bin/bash}, then spawm () internal will vary on the interpretation of commands, execFile (‘ ls – al.) will be an error directly

const execFile = require('child_process').execFile; const child = execFile(`/Chris/node`) child.stdout.on('data', function(data) { console.log(data) }) child.stderr.on('data', function(data) { console.log('stderr: '+ data)}) child.on('close', function(code) {if(code == 0){console.log(' wrap up ')}else{console.log('Error'); }})Copy the code

* fork

Node also has a module called Cluster, which can be used to create clusters for load balancing. The cluster is created using the fork of child_process.

Fork takes the Node file to create the child process, and there is an additional send method for sending messages to the parent process.

let child = fork("./child.js");

child.on("message",(msg:string) => {
    console.log(`[parent] get a data from child is ${msg}\n`);
});

child.send("\nhello child\n");
Copy the code

* summary

Generally, exec and spawn are used the most. At present, my actual use scenario is probably when I do the automated build of the project. Running scripts through Node can fully automate many packaging, submission, version update, etc., and I will continue to update related content in the future