shelljs: update

This commit is contained in:
Rico Sta. Cruz 2017-10-27 13:00:27 +08:00
parent a8b4965fb1
commit a86a517673
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 115 additions and 58 deletions

View File

@ -1,92 +1,149 @@
--- ---
title: Shell.js title: Shell.js
category: JavaScript libraries category: JavaScript libraries
layout: 2017/sheet
updated: 2017-10-27
weight: -1
intro: |
[ShellJS](https://github.com/shelljs/shelljs) is a portable (Windows/Linux/OS X) implementation of Unix shell commands on top of the Node.js API.
--- ---
### Example
```js
var shell = require('shelljs')
```
```js
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git')
shell.exit(1)
}
```
```js
// Copy files to release dir
shell.rm('-rf', 'out/Release')
shell.cp('-R', 'stuff/', 'out/Release')
```
```js
// Replace macros in each .js file
shell.cd('lib')
shell.ls('*.js').forEach(function (file) {
shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file)
shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file)
shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file)
})
shell.cd('..')
```
```js
// Run external tool synchronously
if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
shell.echo('Error: Git commit failed')
shell.exit(1)
}
```
Taken from the [Readme](https://github.com/shelljs/shelljs).
### Require ### Require
require 'shelljs/global' ```js
const sh = require('shelljs')
```
### Paths ### Paths
cd 'dir' ```js
sh.cd('dir')
```
mkdir 'dir' ```js
mkdir '-p', 'dir' sh.mkdir('dir')
sh.mkdir('-p', 'dir')
```
### File manip ### File manipulation
cp 'src', 'dest' ```js
cp '-rf', 'src', 'dest' sh.cp('src', 'dest')
sh.cp('-rf', 'src', 'dest')
```
rm 'file' ```js
rm '-rf', 'file' sh.rm('file')
sh.rm('-rf', 'file')
```
mv 'src', 'dest' ```js
mv ['src1','src2'], 'dest' sh.mv('src', 'dest')
sh.mv(['src1','src2'], 'dest')
```
chmod '644', 'file' ```js
chmod 755, 'file' sh.chmod('644', 'file')
chmod 'u+x', 'file' sh.chmod(755, 'file')
sh.chmod('u+x', 'file')
```
### Tests ### Tests
test '-b', 'path' # block device ```js
test '-d', 'path' # dir sh.test('-b', 'path') // block device
test '-e', 'path' # exists sh.test('-d', 'path') // dir
test '-f', 'path' # file sh.test('-e', 'path') // exists
test '-L', 'path' # symlink sh.test('-f', 'path') // file
sh.test('-L', 'path') // symlink
```
### Cat and output ### Cat and output
src = cat('file*.txt') ```js
src = sh.cat('file*.txt')
```
"hello".to('output.txt'); ```js
"hello".toEnd('append.txt'); 'hello'.to('output.txt')
'hello'.toEnd('append.txt')
```
cat('input.txt').to('output.txt'); ```js
sh.cat('input.txt').to('output.txt')
```
### Utils ### Utils
which('x') ```js
pwd() sh.which('x')
sh.pwd()
```
echo 'hi' ```js
sh.echo('hi')
```
exec('node --version').code ```js
exec('node --version').output sh.exec('node --version').code
exec('node --version', {silent:true}).output sh.exec('node --version').output
sh.exec('node --version', { silent: true }).output
```
exec 'node --version', (code, output) -> ```js
echo "exit code #{code}" sh.exec('node --version', (code, output) => {
sh.echo(`exit code ${code}`)
})
```
tempdir() ```js
sh.tempdir()
```
error() # null if no error ```js
sh.error() // null if no error
```
### Make ## Also see
require 'shelljs/make' * <https://github.com/shelljs/shelljs>
target.all = ->
target.bundle()
target.docs()
target.bundle = ->
cd __dirname
mkdir 'build'
cd 'lib'
(cat '*.js').to '../build/output.js'
target.docs = ->
cd __dirname
mkdir 'docs'
cd 'lib'
for file in ls '*.js'
text = grep '//@', file # extract special comments
text.replace '//@', '' # remove comment tags
text.to 'docs/my_docs.md'
### References
* https://github.com/arturadib/shelljs