Skip to content

Commit

Permalink
Refactoring!
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Nov 17, 2015
1 parent 91460d7 commit 8004ce4
Show file tree
Hide file tree
Showing 46 changed files with 1,153 additions and 780 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "mysticatea/nodejs"
"extends": "mysticatea/nodejs",
"rules": {
"require-jsdoc": 2
}
}
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/node_modules
/bin
/coverage
/docs
/lib
/node_modules
/npm-debug.log
/test-workspace/npm-debug.log
/test-workspace/test.txt
58 changes: 31 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
[![Coverage Status](https://coveralls.io/repos/mysticatea/npm-run-all/badge.svg?branch=master)](https://coveralls.io/r/mysticatea/npm-run-all?branch=master)
[![npm version](https://badge.fury.io/js/npm-run-all.svg)](http://badge.fury.io/js/npm-run-all)

A CLI tool to run multiple npm-scripts sequentially or in parallel.

## Platform dependencies

This package works in both Windows and UNIX-like environments.

It requires at least node version 0.10 and **npm version 2.0.0**. To upgrade npm, run:
A CLI tool to run multiple npm-scripts in parallel or sequential.

```
npm install -g npm@latest
> npm-run-all clean lint build:*
```

## Installation
Expand All @@ -22,6 +16,12 @@ npm install -g npm@latest
npm install npm-run-all
```

- This package works in both Windows and UNIX-like environments.
- This package is tested in the follow node versions.
- `0.10` (*requires `npm >= 2.0.0`, so please run `npm install -g npm@latest`*)
- `0.12`
- `4.x`
- `5.x`

## Usage

Expand Down Expand Up @@ -82,17 +82,16 @@ npm-run-all --parallel watch:*
In this case, runs sub tasks of `watch`. e.g. `watch:html`, `watch:js`.
But, doesn't run sub-sub tasks. e.g. `watch:js:index`.

> `npm-run-all` reads a task list from `package.json` at the current working
> directory.
`npm-run-all` reads the actual npm-script list from `package.json` in the current directory.

```
npm-run-all --parallel watch:**:*
npm-run-all --parallel watch:**
```

If you use a globstar `**`, runs both sub tasks and sub-sub tasks.

This matching rule is similar to [glob](https://www.npmjs.com/package/glob#glob-primer).
Its difference is one; the separator is `:`, instead of `/`.
The Difference is one -- the separator is `:`, instead of `/`.

## Node API

Expand All @@ -108,21 +107,26 @@ var promise = runAll(tasks, options);

Run npm-scripts.

* **tasks** `string|string[]` -- Glob-like patterns for task names.
* **options** `object`
* **options.parallel** `boolean` -- A flag to run tasks in parallel. By default,
`false`.
* **options.stdin** `stream.Readable` -- A readable stream that sends to stdin
of tasks. By default, nothing. Set `process.stdin` in order to send from
key inputs.
* **options.stdout** `stream.Writable` -- A writable stream that receives stdout
of tasks. By default, nothing. Set `process.stdout` in order to print to
console.
* **options.stderr** `stream.Writable` -- A writable stream that receives stderr
of tasks. By default, nothing. Set `process.stderr` in order to print to
console.
* **options.taskList** `string[]` -- A string array that is all task names.
By default, reads from `package.json` at the current working directory.
- **patterns** `string|string[]` -- Glob-like patterns for task names.
- **options** `object`
- **options.parallel** `boolean` --
A flag to run tasks in parallel.
Default is `false`.
- **options.stdin** `stream.Readable` --
A readable stream to send to the stdin of npm-scripts.
Default is nothing.
Set `process.stdin` in order to send from stdin.
- **options.stdout** `stream.Writable` --
A writable stream to receive from the stdout of npm-scripts.
Default is nothing.
Set `process.stdout` in order to print to stdout.
- **options.stderr** `stream.Writable` --
A writable stream to receive from the stderr of npm-scripts
Default is nothing.
Set `process.stderr` in order to print to stderr.
- **options.taskList** `string[]` --
A string array that is all task names.
By default, reads from `package.json` in the current directory.

`runAll` returns a promise that becomes *fulfilled* when all tasks are completed.
The promise will become *rejected* when any of the tasks exit with a non-zero code.
5 changes: 5 additions & 0 deletions esdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"source": "src",
"destination": "docs",
"access": ["public", "protected", "private"]
}
4 changes: 0 additions & 4 deletions mocha-babel-hook.js

This file was deleted.

50 changes: 19 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,41 @@
"name": "npm-run-all",
"version": "1.2.13",
"description": "A CLI tool to run multiple npm-scripts on sequential or parallel.",
"main": "lib/index.js",
"main": "lib/npm-run-all.js",
"bin": {
"npm-run-all": "lib/command.js"
"npm-run-all": "bin/npm-run-all.js"
},
"files": [
"bin",
"lib"
],
"config": {
"test": "OK"
},
"scripts": {
"preversion": "npm run build",
"clean": "rimraf lib coverage",
"lint": "eslint src",
"build": "npm-run-all clean lint && babel src --out-dir lib --plugin babel-plugin-unassert",
"test": "npm run lint && istanbul cover node_modules/mocha/bin/_mocha -- test/*.js --require mocha-babel-hook --timeout 30000",
"testing": "mocha test/*.js --require mocha-babel-hook --timeout 30000 --watch --growl",
"coveralls": "cat coverage/lcov.info | coveralls",
"start": "node test/tasks/append.js start",
"stop": "node test/tasks/append.js stop",
"test-task:env-check": "node test/tasks/env-check.js",
"test-task:append": "node test/tasks/append.js",
"test-task:append:a": "node test/tasks/append.js a",
"test-task:append:a:c": "node test/tasks/append.js ac",
"test-task:append:a:d": "node test/tasks/append.js ad",
"test-task:append:b": "node test/tasks/append.js b",
"test-task:append2": "node test/tasks/append2.js",
"test-task:error": "node test/tasks/error.js",
"test-task:stdio": "node test/tasks/stdio.js"
"clean": "rimraf lib bin coverage",
"lint": "eslint src test",
"build": "babel-node src/bin/npm-run-all.js clean lint build:*",
"build:babel": "babel src --out-dir . --optional runtime",
"build:esdoc": "esdoc -c esdoc.json",
"test": "npm run lint && babel-node node_modules/isparta/bin/isparta cover node_modules/mocha/bin/_mocha -- test/*.js --require scripts/mocha-babel-hook --timeout 12000",
"testing": "mocha test/*.js --require scripts/mocha-babel-hook --timeout 12000 --watch --growl",
"coveralls": "cat coverage/lcov.info | coveralls"
},
"dependencies": {
"es6-promise": "^3.0.2",
"babel-runtime": "^5.8.29",
"minimatch": "^3.0.0",
"shell-quote": "^1.4.3",
"which": "^1.2.0"
},
"devDependencies": {
"babel": "^5.8.29",
"babel-core": "^5.8.33",
"babel-plugin-espower": "^1.0.0",
"babel-plugin-unassert": "^1.2.0",
"babel-plugin-espower": "^1.1.0",
"coveralls": "^2.11.4",
"eslint": "^1.8.0",
"eslint-config-mysticatea": "^1.8.0",
"eslint-plugin-mysticatea": "^1.0.2",
"istanbul": "^0.4.0",
"esdoc": "^0.4.3",
"eslint": "^1.9.0",
"eslint-config-mysticatea": "^1.9.0",
"eslint-plugin-mysticatea": "^1.0.3",
"isparta": "^3.5.3",
"mocha": "^2.3.3",
"npm-run-all": "^1.2.11",
"power-assert": "^1.1.0",
"rimraf": "^2.4.3"
},
Expand Down
5 changes: 5 additions & 0 deletions scripts/mocha-babel-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require("babel/register")({
ignore: /node_modules|test\-workspace/,
optional: ["runtime"],
plugins: ["babel-plugin-espower"]
});
31 changes: 31 additions & 0 deletions src/bin/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/

/**
* Print a help text.
*
* @param {stream.Writable} output - A writable stream to print.
* @returns {Promise} Always a fulfilled promise.
* @private
*/
export default function printHelp(output) {
output.write(`
Usage: npm-run-all [OPTIONS] [...tasks]
Run specified tasks.
Options:
-h, --help Print this text.
-p, --parallel [...tasks] Run a group of tasks in parallel.
-s, --sequential [...tasks] Run a group of tasks in sequencial.
-v, --version Print version number.
See Also:
https://github.com/mysticatea/npm-run-all#readme
`);

return Promise.resolve(null);
}
69 changes: 69 additions & 0 deletions src/bin/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
import runAll from "../lib/npm-run-all";

const START_PROMISE = Promise.resolve(null);

/**
* Parses arguments.
*
* @param {string[]} args - Arguments to parse.
* @returns {{parallel: boolean, patterns: string[]}[]} A running plan.
*/
function parse(args) {
return args.reduce((queue, arg) => {
switch (arg) {
case "-s":
case "--sequential":
queue.push({parallel: false, patterns: []});
break;

case "-p":
case "--parallel":
queue.push({parallel: true, patterns: []});
break;

default:
if (arg[0] === "-") {
throw new Error(`Invalid Option: ${arg}`);
}
queue[queue.length - 1].patterns.push(arg);
break;
}
return queue;
}, [{parallel: false, patterns: []}]);
}

/**
* Parses arguments, then run specified npm-scripts.
*
* @param {string[]} args - Arguments to parse.
* @param {stream.Writable} stdout - A writable stream to print logs.
* @param {stream.Writable} stderr - A writable stream to print errors.
* @returns {Promise} A promise which comes to be fulfilled when all npm-scripts are completed.
* @private
*/
export default function npmRunAll(args, stdout, stderr) {
try {
return parse(args).reduce(
(prev, group) => (group.patterns.length === 0) ?
prev :
prev.then(() => runAll(
group.patterns,
{
stdout,
stderr,
stdin: process.stdin,
parallel: group.parallel
}
)),
START_PROMISE
);
}
catch (err) {
return Promise.reject(err);
}
}
49 changes: 49 additions & 0 deletions src/bin/npm-run-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env node

/**
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/

/**
* The main process of `npm-run-all` command.
*
* @param {string[]} args - Arguments to parse.
* @param {stream.Writable} stdout - A writable stream to print logs.
* @param {stream.Writable} stderr - A writable stream to print errors.
* @returns {Promise} A promise which comes to be fulfilled when all npm-scripts are completed.
* @private
*/
export default function main(
args,
stdout = null,
stderr = null
) {
switch (args[0]) {
case undefined:
case "-h":
case "--help":
return require("./help")(stdout);

case "-v":
case "--version":
return require("./version")(stdout);

default:
return require("./main")(args, stdout, stderr);
}
}

/* eslint-disable no-process-exit */
/* istanbul ignore if */
if (require.main === module) {
// Execute.
const promise = main(process.argv.slice(2), process.stdout, process.stderr);

// Error Handling.
promise.catch(err => {
console.error("ERROR:", err.message); // eslint-disable-line no-console
process.exit(1);
});
}
35 changes: 35 additions & 0 deletions src/bin/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/

// In tests, `../../package.json` is correct.
const version = read("../package.json") || read("../../package.json");

/**
* Reads the version of `npm-run-all`.
*
* @param {string} path - A path to `package.json`.
* @returns {string|null} A version text.
*/
function read(path) {
try {
return require(path).version;
}
catch (err) {
return null;
}
}

/**
* Print a version text.
*
* @param {stream.Writable} output - A writable stream to print.
* @returns {Promise} Always a fulfilled promise.
* @private
*/
export default function printVersion(output) {
output.write(`v${version}\n`);
return Promise.resolve(null);
}

0 comments on commit 8004ce4

Please sign in to comment.