Skip to content

Commit

Permalink
Require Node.js >=6 and Grunt >=1
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Dec 22, 2018
1 parent 86680b4 commit 62cdda5
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
sudo: false
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
17 changes: 9 additions & 8 deletions gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
'use strict';

module.exports = grunt => {
let checkCounter = 0;

function check(substring) {
checkCounter++;

return (err, stdout, stderr, cb) => {
if (err) {
grunt.fatal(`Command failed: ${err}`);
return (error, stdout, stderr, callback) => {
if (error) {
grunt.fatal(`Command failed: ${error}`);
}

if (!stdout.includes(substring)) {
grunt.fatal(`Expected substring not found: ${substring}`);
}

checkCounter--;
cb();
callback();
};
}

let path = null;

function pathCallback(err, stdout, stderr, cb) {
if (err) {
grunt.fatal(`Command failed: ${err}`);
function pathCallback(error, stdout, stderr, callback) {
if (error) {
grunt.fatal(`Command failed: ${error}`);
}

if (path === null) {
Expand All @@ -32,7 +33,7 @@ module.exports = grunt => {
grunt.fatal('Path shouldn\'t have changed!');
}

cb();
callback();
}

grunt.initConfig({
Expand Down
20 changes: 4 additions & 16 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
87 changes: 42 additions & 45 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@
{
"name": "grunt-shell",
"version": "2.1.0",
"description": "Run shell commands",
"license": "MIT",
"repository": "sindresorhus/grunt-shell",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && grunt"
},
"files": [
"tasks"
],
"keywords": [
"gruntplugin",
"shell",
"command",
"cmd",
"exec",
"spawn",
"child",
"process",
"cli"
],
"dependencies": {
"chalk": "^1.0.0",
"npm-run-path": "^2.0.0"
},
"devDependencies": {
"grunt": "^1.0.1",
"grunt-cli": "^1.2.0",
"xo": "0.20.3"
},
"peerDependencies": {
"grunt": ">=0.4.0"
},
"xo": {
"esnext": true
}
"name": "grunt-shell",
"version": "2.1.0",
"description": "Run shell commands",
"license": "MIT",
"repository": "sindresorhus/grunt-shell",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && grunt"
},
"files": [
"tasks"
],
"keywords": [
"gruntplugin",
"shell",
"command",
"cmd",
"exec",
"spawn",
"child",
"process",
"cli"
],
"dependencies": {
"chalk": "^2.4.1",
"npm-run-path": "^2.0.0"
},
"devDependencies": {
"grunt": "^1.0.1",
"grunt-cli": "^1.2.0",
"xo": "^0.23.0"
},
"peerDependencies": {
"grunt": ">=1"
}
}
32 changes: 15 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@

> Run shell commands
A good way to interact with other CLI tools. E.g. compiling Compass `compass compile` or get the current git branch `git branch`.
A good way to interact with other CLI tools. For example, get the current Git branch with `git branch`.

**Use [Stack Overflow](https://stackoverflow.com/questions/tagged/gruntjs) for support questions.**

---

<p align="center"><b>🔥 Want to strengthen your core JavaScript skills and master ES6?</b><br>I would personally recommend this awesome <a href="https://ES6.io/friend/AWESOME">ES6 course</a> by Wes Bos.</p>

---


## Install

```
$ npm install --save-dev grunt-shell
```

<a href="https://www.patreon.com/sindresorhus">
<img src="https://c5.patreon.com/external/logo/[email protected]" width="160">
</a>


## Usage

```js
require('load-grunt-tasks')(grunt); // npm install --save-dev load-grunt-tasks
require('load-grunt-tasks')(grunt);

grunt.initConfig({
shell: {
Expand All @@ -33,7 +31,7 @@ grunt.initConfig({
target: {
command: 'ls'
},
another: 'ls ./src' // shorthand
another: 'ls ./src' // Shorthand
}
});

Expand Down Expand Up @@ -90,7 +88,7 @@ module.exports = grunt => {
grunt.initConfig({
shell: {
greet: {
command: greeting => 'echo ' + greeting
command: greeting => `echo ${greeting}`
}
}
});
Expand All @@ -117,14 +115,14 @@ grunt.initConfig({
Do whatever you want with the output.

```js
function log(err, stdout, stderr, cb) {
if (err) {
cb(err);
function log(error, stdout, stderr, callback) {
if (error) {
callback(error);
return;
}

console.log(stdout);
cb();
callback();
}

grunt.initConfig({
Expand All @@ -141,7 +139,7 @@ grunt.initConfig({

### Option passed to the .exec() method

Run a command in another directory. In this example we run it in a subfolder using the `cwd` (current working directory) option.
Run a command in another directory. In this example, we run it in a subfolder using the `cwd` (current working directory) option.

```js
grunt.initConfig({
Expand Down Expand Up @@ -233,13 +231,13 @@ Default: `false`

Set `stdin` to [act as a raw device](https://nodejs.org/api/tty.html#tty_readstream_setrawmode_mode).

### callback(err, stdout, stderr, cb)
### callback(error, stdout, stderr, callback)

Type: `Function`

Lets you override the default callback with your own.

**Make sure to call the `cb` method when you're done.** Supply an error as the first argument to `cb` to print a warning and cause the task to fail.
**Make sure to call the `callback` method when you're done.** Supply an error as the first argument to `callback` to print a warning and cause the task to fail.

### preferLocal

Expand Down
42 changes: 21 additions & 21 deletions tasks/shell.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';
const exec = require('child_process').exec;
const {exec} = require('child_process');
const chalk = require('chalk');
const npmRunPath = require('npm-run-path');

const TEN_MEGABYTES = 1000 * 1000 * 10;

module.exports = grunt => {
grunt.registerMultiTask('shell', 'Run shell commands', function () {
const cb = this.async();
const opts = this.options({
grunt.registerMultiTask('shell', 'Run shell commands', function (...args) {
const callback = this.async();
const options = this.options({
stdout: true,
stderr: true,
stdin: true,
Expand All @@ -20,7 +20,7 @@ module.exports = grunt => {
}
});

let cmd = typeof this.data === 'string' || typeof this.data === 'function' ?
let cmd = (typeof this.data === 'string' || typeof this.data === 'function') ?
this.data :
this.data.command;

Expand All @@ -29,27 +29,27 @@ module.exports = grunt => {
}

// Increase max buffer
opts.execOptions = Object.assign({}, opts.execOptions);
opts.execOptions.maxBuffer = opts.execOptions.maxBuffer || TEN_MEGABYTES;
options.execOptions = Object.assign({}, options.execOptions);
options.execOptions.maxBuffer = options.execOptions.maxBuffer || TEN_MEGABYTES;

cmd = grunt.template.process(typeof cmd === 'function' ? cmd.apply(grunt, arguments) : cmd);
cmd = grunt.template.process(typeof cmd === 'function' ? cmd.apply(grunt, args) : cmd);

if (opts.preferLocal === true) {
opts.execOptions.env = npmRunPath.env({env: opts.execOptions.env || process.env});
if (options.preferLocal === true) {
options.execOptions.env = npmRunPath.env({env: options.execOptions.env || process.env});
}

if (this.data.cwd) {
opts.execOptions.cwd = this.data.cwd;
options.execOptions.cwd = this.data.cwd;
}

const cp = exec(cmd, opts.execOptions, (err, stdout, stderr) => {
if (typeof opts.callback === 'function') {
opts.callback.call(this, err, stdout, stderr, cb);
const cp = exec(cmd, options.execOptions, (error, stdout, stderr) => {
if (typeof options.callback === 'function') {
options.callback.call(this, error, stdout, stderr, callback);
} else {
if (err && opts.failOnError) {
grunt.warn(err);
if (error && options.failOnError) {
grunt.warn(error);
}
cb();
callback();
}
});

Expand All @@ -65,19 +65,19 @@ module.exports = grunt => {

grunt.verbose.writeln('Command:', chalk.yellow(cmd));

if (opts.stdout || grunt.option('verbose')) {
if (options.stdout || grunt.option('verbose')) {
captureOutput(cp.stdout, process.stdout);
}

if (opts.stderr || grunt.option('verbose')) {
if (options.stderr || grunt.option('verbose')) {
captureOutput(cp.stderr, process.stderr);
}

if (opts.stdin) {
if (options.stdin) {
process.stdin.resume();
process.stdin.setEncoding('utf8');

if (opts.stdinRawMode && process.stdin.isTTY) {
if (options.stdinRawMode && process.stdin.isTTY) {
process.stdin.setRawMode(true);
}

Expand Down

0 comments on commit 62cdda5

Please sign in to comment.