Chinaunix首页 | 论坛 | 博客
  • 博客访问: 52340
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
文章分类
文章存档

2016年(48)

我的朋友

分类: JavaScript

2016-10-27 17:51:52

shelljs

Portable Unix shell commands for Node.js

   

ShellJS is a portable (Windows/Linux/OS X) implementation of Unix shell commands on top of the Node.js API. You can use it to eliminate your shell script's dependency on Unix while still keeping its familiar and powerful commands. You can also install it globally so you can run it from outside Node projects - say goodbye to those gnarly Bash scripts!

ShellJS supports node v0.11, v0.12, v4, v5, v6, and all releases of iojs.

The project is  and battled-tested in projects like:

  •  - Firefox's next-gen PDF reader
  •  - Firefox's infamous debugger
  •  - Most popular JavaScript linter
  •  - jQuery-compatible JavaScript library for modern browsers
  •  - Web application stack and development tool
  •  - Open source PaaS for quick API backend generation
  • And .

If you have feedback, suggestions, or need help, feel free to post in our .

Think ShellJS is cool? Check out some related projects (like --a javascript-based POSIX shell) in our !

Upgrading from an older version? Check out our  page to see what changes to watch out for while upgrading.

If you just want cross platform UNIX commands, checkout our new project , a utility to expose shelljs to the command line.

For example:

点击(此处)折叠或打开

  1. $ shx mkdir -p foo
  2. $ shx touch foo/bar.txt
  3. $ shx rm -rf foo

For documentation on all the latest features, check out our . To read docs that are consistent with the latest release, check out  or shelljs.org.

Via npm:

点击(此处)折叠或打开

  1. $ npm install [-g] shelljs


If the global option -g is specified, the binary shjs will be installed. This makes it possible to run ShellJS scripts much like any shell script from the command line, i.e. without requiring a node_modules folder:

点击(此处)折叠或打开

  1. $ shjs my_script


点击(此处)折叠或打开

  1. require('shelljs/global');
  2.  
  3. if (!which('git')) {
  4.   echo('Sorry, this script requires git');
  5.   exit(1);
  6. }
  7.  
  8. // Copy files to release dir
  9. rm('-rf', 'out/Release');
  10. cp('-R', 'stuff/', 'out/Release');
  11.  
  12. // Replace macros in each .js file
  13. cd('lib');
  14. ls('*.js').forEach(function(file) {
  15.   sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
  16.   sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
  17.   sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file);
  18. });
  19. cd('..');
  20.  
  21. // Run external tool synchronously
  22. if (exec('git commit -am "Auto-commit"').code !== 0) {
  23.   echo('Error: Git commit failed');
  24.   exit(1);
  25. }

CoffeeScript is also supported automatically:

点击(此处)折叠或打开

  1. require 'shelljs/global'
  2.  
  3. if not which 'git'
  4.   echo 'Sorry, this script requires git'
  5.   exit 1
  6.  
  7. # Copy files to release dir
  8. rm '-rf', 'out/Release'
  9. cp '-R', 'stuff/', 'out/Release'
  10.  
  11. # Replace macros in each .js file
  12. cd 'lib'
  13. for file in ls '*.js'
  14.   sed '-i', 'BUILD_VERSION', 'v0.1.2', file
  15.   sed '-i', /^.*REMOVE_THIS_LINE.*$/, '', file
  16.   sed '-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file
  17. cd '..'
  18.  
  19. # Run external tool synchronously
  20. if (exec 'git commit -am "Auto-commit"').code != 0
  21.   echo 'Error: Git commit failed'
  22.   exit 1

The example above uses the convenience script shelljs/global to reduce verbosity. If polluting your global namespace is not desirable, simply requireshelljs.

Example:

点击(此处)折叠或打开

  1. var shell = require('shelljs');
  2. shell.echo('hello world');

All commands run synchronously, unless otherwise stated. All commands accept standard bash globbing characters (*, ?, etc.), compatible with the.

For less-commonly used commands and features, please check out our .

Changes to directory dir for the duration of the script. Changes to home directory if no argument is supplied.

Returns the current directory.

Available options:

  • -R: recursive
  • -A: all files (include files beginning with ., except for . and ..)
  • -d: list directories themselves, not their contents
  • -l: list objects representing each file, each with fields containing ls -l output fields. See  for more info

Examples:

点击(此处)折叠或打开

  1. ls('projs/*.js');
  2. ls('-R', '/users/me', '/tmp');
  3. ls('-R', ['/users/me', '/tmp']); // same as above
  4. ls('-l', 'file.txt');

Returns array of files in the given path, or in current directory if no path provided.

Examples:

点击(此处)折叠或打开

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

Returns array of all files (however deep) in the given paths.

The main difference from ls('-R', path) is that the resulting file names include the base directories, e.g. lib/resources/file1 instead of just file1.

Available options:

  • -f: force (default behavior)
  • -n: no-clobber
  • -r, -R: recursive
  • -L: follow symlinks
  • -P: don't follow symlinks

Examples:

点击(此处)折叠或打开

  1. cp('file1', 'dir1');
  2. cp('-R', 'path/to/dir/', '~/newCopy/');
  3. cp('-Rf', '/tmp/*', '/usr/local/*', '/home/tmp');
  4. cp('-Rf', ['/tmp/*', '/usr/local/*'], '/home/tmp');

Copies files.

Available options:

  • -f: force
  • -r, -R: recursive

Examples:

点击(此处)折叠或打开

  1. rm('-rf', '/tmp/*');
  2. rm('some_file.txt', 'another_file.txt');
  3. rm(['some_file.txt', 'another_file.txt']);

Removes files.

Available options:

  • -f: force (default behavior)
  • -n: no-clobber

Examples:

点击(此处)折叠或打开

  1. mv('-n', 'file', 'dir/');
  2. mv('file1', 'file2', 'dir/');
  3. mv(['file1', 'file2'], 'dir/');

Moves files.

Available options:

  • -p: full path (will create intermediate dirs if necessary)

Examples:

点击(此处)折叠或打开

  1. mkdir('-p', '/tmp/a/b/c/d', '/tmp/e/f/g');
  2. mkdir('-p', ['/tmp/a/b/c/d', '/tmp/e/f/g']);

Creates directories.

Available expression primaries:

  • '-b', 'path': true if path is a block device
  • '-c', 'path': true if path is a character device
  • '-d', 'path': true if path is a directory
  • '-e', 'path': true if path exists
  • '-f', 'path': true if path is a regular file
  • '-L', 'path': true if path is a symbolic link
  • '-p', 'path': true if path is a pipe (FIFO)
  • '-S', 'path': true if path is a socket

Examples:

点击(此处)折叠或打开

  1. if (test('-d', path)) { /* do something with dir */ };

 wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

wemall地址:
代码地址:

 


阅读(486) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~