KoaHub.JS用于Node.js的可移植Unix shell命令程序代码
shelljs
Portable Unix shell commands for Node.js
ShellJS - 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 unit-tested and battled-tested in projects like:
- PDF.js - Firefox's next-gen PDF reader
- Firebug - Firefox's infamous debugger
- JSHint - Most popular JavaScript linter
- Zepto - jQuery-compatible JavaScript library for modern browsers
- Yeoman - Web application stack and development tool
- Deployd.com - Open source PaaS for quick API backend generation
- And many more.
If you have feedback, suggestions, or need help, feel free to post in our issue tracker.
Think ShellJS is cool? Check out some related projects (like cash--a javascript-based POSIX shell) in our Wiki page!
Upgrading from an older version? Check out our breaking changes page to see what changes to watch out for while upgrading.
Command line use
If you just want cross platform UNIX commands, checkout our new project shelljs/shx, a utility to expose shelljs to the command line.
For example:
$ shx mkdir -p foo $ shx touch foo/bar.txt $ shx rm -rf foo
A quick note about the docs
For documentation on all the latest features, check out our README. To read docs that are consistent with the latest release, check out the npm page or shelljs.org.
Installing
Via npm:
$ 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:
$ shjs my_script
JavaScript
require('shelljs/global');
if (!which('git')) {
echo('Sorry, this script requires git');
exit(1);
}
// Copy files to release dir
rm('-rf', 'out/Release');
cp('-R', 'stuff/', 'out/Release');
// Replace macros in each .js file
cd('lib');
ls('*.js').forEach(function(file) {
sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file);
});
cd('..');
// Run external tool synchronously
if (exec('git commit -am "Auto-commit"').code !== 0) {
echo('Error: Git commit failed');
exit(1);
}
CoffeeScript
CoffeeScript is also supported automatically:
require 'shelljs/global'
if not which 'git'
echo 'Sorry, this script requires git'
exit 1
# Copy files to release dir
rm '-rf', 'out/Release'
cp '-R', 'stuff/', 'out/Release'
# Replace macros in each .js file
cd 'lib'
for file in ls '*.js'
sed '-i', 'BUILD_VERSION', 'v0.1.2', file
sed '-i', /^.*REMOVE_THIS_LINE.*$/, '', file
sed '-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, cat('macro.js'), file
cd '..'
# Run external tool synchronously
if (exec 'git commit -am "Auto-commit"').code != 0
echo 'Error: Git commit failed'
exit 1
Global vs. Local
The example above uses the convenience script shelljs/global to reduce verbosity. If polluting your global namespace is not desirable, simply requireshelljs.
Example:
var shell = require('shelljs');
shell.echo('hello world');
Command reference
All commands run synchronously, unless otherwise stated. All commands accept standard bash globbing characters (*, ?, etc.), compatible with thenode glob module.
For less-commonly used commands and features, please check out our wiki page.
cd([dir])
Changes to directory dir for the duration of the script. Changes to home directory if no argument is supplied.
pwd()
Returns the current directory.
ls([options,] [path, ...])
ls([options,] path_array)
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 containingls -loutput fields. See fs.Stats for more info
Examples:
ls('projs/*.js');
ls('-R', '/users/me', '/tmp');
ls('-R', ['/users/me', '/tmp']); // same as above
ls('-l', 'file.txt'); // { name: 'file.txt', mode: 33188, nlink: 1, ...}
Returns array of files in the given path, or in current directory if no path provided.
find(path [, path ...])
find(path_array)
Examples:
find('src', 'lib');
find(['src', 'lib']); // same as above
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.
cp([options,] source [, source ...], dest)
cp([options,] source_array, dest)
Available options:
-f: force (default behavior)-n: no-clobber-r,-R: recursive-L: follow symlinks-P: don't follow symlinks
Examples:
cp('file1', 'dir1');
cp('-R', 'path/to/dir/', '~/newCopy/');
cp('-Rf', '/tmp/*', '/usr/local/*', '/home/tmp');
cp('-Rf', ['/tmp/*', '/usr/local/*'], '/home/tmp'); // same as above
Copies files.
rm([options,] file [, file ...])
rm([options,] file_array)
Available options:
-f: force-r, -R: recursive
Examples:
rm('-rf', '/tmp/*');
rm('some_file.txt', 'another_file.txt');
rm(['some_file.txt', 'another_file.txt']); // same as above
Removes files.
mv([options ,] source [, source ...], dest')
mv([options ,] source_array, dest')
Available options:
-f: force (default behavior)-n: no-clobber
Examples:
mv('-n', 'file', 'dir/');
mv('file1', 'file2', 'dir/');
mv(['file1', 'file2'], 'dir/'); // same as above
Moves files.
mkdir([options,] dir [, dir ...])
mkdir([options,] dir_array)
Available options:
-p: full path (will create intermediate dirs if necessary)
Examples:
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
Creates directories.
test(expression)
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:
if (test('-d', path)) { /* do something with dir */ };
wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统
wemall地址:http://www.wemallshop.com
代码地址:http://js.koahub.com/home/feature/shelljs
KoaHub.JS用于Node.js的可移植Unix shell命令程序代码的更多相关文章
- KoaHub.JS用于Node.js的cron作业调度程序代码
node-schedule A cron-like and not-cron-like job scheduler for Node. Node Schedule Announcement: ...
- KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情
KoaHub平台基于Node.js开发的Koa 链接支付宝插件代码信息详情 easy-alipay alipay payment & notification APIs easy-alipay ...
- .NET程序员也学Node.js——初识Node.js
清明在石门休了八天假,一眨眼,4月又到中旬了...看到.NET在天朝彻底沦陷而又无能为力,我开始尝试去学习一些新的东西来充实自己,我自然是打死不会去学java的,没有为什么,于是乎,最近开始学习一些前 ...
- 如何在vscode里面调试js和node.js
一般大家调试都是在浏览器端调试js的,不过有些时候也想和后台一样在代码工具里面调试js或者node.js,下面介绍下怎样在vscode里面走断点. 1,用来调试js 一:在左侧扩展中搜索Debugge ...
- KoaHub.JS基于Node.js开发的mysql的node.js驱动程序代码
mysql A node.js driver for mysql. It is written in JavaScript, does not require compiling, and is 10 ...
- KoaHub平台基于Node.js开发的Koa的简单包装到请求库的类似接口
co-request co-request promisify wrapper for request co-request Simple wrapper to the request library ...
- KoaHub平台基于Node.js开发的Koa的调试实用程序
debug small debugging utility debug tiny node.js debugging utility modelled after node core's debugg ...
- KoaHub.JS基于Node.js开发的Koa 生成验证码插件代
ccap node.js generate captcha using c++ library CImg without install any other lib or software node- ...
- KoaHub平台基于Node.js开发的Koa JWT认证插件代码信息详情
koa-jwt Koa JWT authentication middleware. koa-jwt Koa middleware that validates JSON Web Tokens and ...
随机推荐
- 验证浏览器是否安装已flash插件的js脚本
function flashChecker() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(document ...
- Mac 安装Rudy环境 pod安装前的准备工作
之前已经说过怎么使用pod 今天说一下安装pod之前的准备工作 首先呢就是Rudy 环境(前提是你已经安装了Xcode) 在终端输入一下命令 期间可能也许会要你输入密码 curl -L https:/ ...
- JS前端的分享功能
给网页加上分享代码,借助网友的力量推广网站,目前已经很流行了 以下是网页代码 QQ空间分享代码如下: <a href="javascript:void(0);" onclic ...
- NodeJS Stream 四:Writable
什么是可写流 可写流是对数据流向设备的抽象,用来消费上游流过来的数据,通过可写流程序可以把数据写入设备,常见的是本地磁盘文件或者 TCP.HTTP 等网络响应. 看一个之前用过的例子 process. ...
- [nRF51822] 16、nRF51822的随机数生成器,及随机数生成器的一些知识(可以帮您补补随机数发生器的知识)
1.前言 随机数生成器在通信.加密.图像传输等领域应用广泛,且一般起到关键性作用.我在最近设计的一个近场射频通信协议的碰撞避退算法的过程中,便对此有深深体会. 2.伪随机数发生器 随机数发生器一般包括 ...
- Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例
Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...
- 史上最全的synchronized解释
首先:推荐使用synchronized(obj)这种方法体的使用方式,一个类里面建议尽量使用单一的同步方法,多种方法混用,维护成本太大. 其次:关于java5.0新增的ReenTrantLock方法: ...
- js 计算月/周的第一天和最后一天
因为项目开发中遇到需要向后台传本周的开始和结束时间,以及上一周的起止时间,就琢磨了半天,总算写出来一套,写篇文章是为了方便自己记忆,也是分享给需要的人,水平有限,写的不好请见谅: getDateStr ...
- Tomcat使用常见问题
1,启动服务器,闪退问题 原因:tomcat软件是用java语言开发的,软件启动时,会默认到系统环境变量中查找一个名叫JAVA_HOME的变量.这个变量的作用是找到tomcat启动所需的JVM. ...
- HTML5和CSS3
一.HTML5 HTML5 是 HTML 标准的最新演进版本. 这个术语代表了两个不同的概念:它是一个新的 HTML 语言版本包含了新的元素,属性和行为,同时包含了一系列可以被用来让 Web 站点和应 ...