参考文章
用于在node中执行终端命令
导入

  1. import shell from 'shelljs'
  1. cat返回文件内容

    1. var str = shell.cat('file*.txt');
    2. var str = shell.cat('file1', 'file2');
    3. var str = shell.cat(['file1', 'file2']); // same as above
  2. pwd获取当前目录

    1. const cur = shell.pwd()
  3. find查找文件

    1. find('src', 'lib');
    2. find(['src', 'lib']); // same as above
    3. find('.').filter(function(file) { return file.match(/\.js$/); });
  4. mkdir创建目录

    1. mkdir('-p', '/tmp/a/b/c/d', '/tmp/e/f/g');
    2. 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()

例子:

  1. var version = exec('node --version', {silent:true}).stdout;
  2. var child = exec('some_long_running_process', {async:true});
  3. child.stdout.on('data', function(data) {
  4. /* ... do something with data ... */
  5. });
  6. exec('some_long_running_process', function(code, stdout, stderr) {
  7. console.log('Exit code:', code);
  8. console.log('Program output:', stdout);
  9. console.log('Program stderr:', stderr);
  10. });