scenario
During development, you will often encounter the following commands:
git add .
git commit -m '*****'
git push
Copy the code
Shelljs
This library allows us to execute shell commands in js files. What ShellJS does is automate, freeing up time-consuming repetitive routines and improving development efficiency and mood.
1.Installing
$ npm install shelljs -D
Copy the code
2. Create a new file shell.js in the root directory. The content is as follows:
let shell = require('shelljs') let name = process.argv[2] || 'Auto-commit'; let exec = shell.exec if (exec('git add .').code ! == 0) { echo('Error: Git add failed') exit(1) } if (exec(`git commit -am "${name}"`).code ! == 0) { echo('Error: Git commit failed') exit(1) } if (exec('git push').code ! == 0) { echo('Error: Git commit failed') exit(1) }Copy the code
3. Execute all of the above tasks with a single command and add it to package.json as follows:
"script":{
"push":"node ./shell.js"
}
Copy the code
$NPM run push tests ShellJS to submit codeCopy the code
The above operations can be executed by executing shellJS script to submit the code.
Examples
1. When you run the command environment, the config file is differentiated according to the development and production environments. Therefore, in this section, the file is copied to the target file according to the command environment.
Const shell = require('shelljs') const apiEnv = process.env.api_env shell.cp(`./src/config/${apiEnv}.js`, './src/config/index.js')Copy the code
The basic grammar
- shell.which(command)
Search for the address of the command in the environment variable PATH, check whether the command can be executed, and return the absolute address of the command
- echo
Prints the specified content on the console
- exit(code)
Exit the current process with exit code as code
Var shell = require('shelljs') // Check whether the console starts with 'git' if (! Shell.which ('git')) {// output in the console shell.echo('Sorry, this script requires git'); shell.exit(1); }Copy the code
- Rm ([options,] file [, file…] )
If one or more files or directories are deleted from a directory, the deletion cannot be restored. Common parameters: -f: forcibly deletes a file. -i: asks the user before deleting the file. -r: recursively process directories. -v: displays the processing process.
Shell.rm ('-rf','out/Release') // Force a recursive deletion of the 'out/Release directory 'Copy the code
- cp([options,] source_array, dest)
Used to copy one or more source files or directories to the specified file or directory
Shell. Cp (' -r ', 'stuff/',' the out/Release ') / / will ` stuff all copying to ` out/in / ` Release ` directoryCopy the code
- cd
Change the working directory to the specified relative or absolute path. cd.. To return to the previous level, CD – goes back to the previous directory
Shell.cd ('lib') // Go to the 'lib' directoryCopy the code
- ls
Displays a list of targets
ls(path.join('bundle', 'css/')).forEach(cssName => {
***
})
Copy the code
- sed([options,] search_regex, replacement, file_array)
Replace the contents in file_array that match search_regex with replacement to support regular capture group self-reference. One line at a time is processed, the buffer is sent to the screen, the next line is processed, and the loop ends.
/ * -i said direct action source file * / / * will be replaced by build_version fields' v0.1.2 * / shell. Sed (' -i ', 'build_version', 'v0.1.2' file).Copy the code
- cat([options,] file [, file …] )
To read in the contents of one or more files, when one file is specified, and when multiple files are specified, the contents are read together.
shelljs-npm
Shelljs official document
shelljs-github
Shelljs Chinese document introduction