参考文章
用于在node中执行终端命令
导入
import shell from 'shelljs'
cat返回文件内容
var str = shell.cat('file*.txt');
var str = shell.cat('file1', 'file2');
var str = shell.cat(['file1', 'file2']); // same as above
pwd获取当前目录
const cur = shell.pwd()
find查找文件
find('src', 'lib');
find(['src', 'lib']); // same as above
find('.').filter(function(file) { return file.match(/\.js$/); });
mkdir创建目录
mkdir('-p', '/tmp/a/b/c/d', '/tmp/e/f/g');
mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']); // same as above
exec(command [, options] [, callback])
Available options:
- async: 是否异步执行,默认为false,如果给定callback则为异步
- fatal: 发生错误时退出,默认为false
- silent: 不要输出console结果,默认为false
- encoding: Character encoding to use. Affects the values returned to stdout and stderr, and what is written to stdout and stderr when not in silent mode (default: ‘utf8’).
- and any option available to Node.js’s child_process.exec()
例子:
var version = exec('node --version', {silent:true}).stdout;
var child = exec('some_long_running_process', {async:true});
child.stdout.on('data', function(data) {
/* ... do something with data ... */
});
exec('some_long_running_process', function(code, stdout, stderr) {
console.log('Exit code:', code);
console.log('Program output:', stdout);
console.log('Program stderr:', stderr);
});