|
1 | 1 | # if-node-version |
2 | | -Run commands if on specified node version. |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/if-node-version) |
| 4 | +[](http://www.npmtrends.com/if-node-version) |
| 5 | +[](https://travis-ci.org/mysticatea/if-node-version) |
| 6 | +[](https://coveralls.io/github/mysticatea/if-node-version?branch=master) |
| 7 | +[](https://david-dm.org/mysticatea/if-node-version) |
| 8 | + |
| 9 | +Run a shell command if it's on the node of specified versions. |
| 10 | + |
| 11 | +```bash |
| 12 | +$ if-node-version ">=4" eslint lib test |
| 13 | +``` |
| 14 | + |
| 15 | +```bash |
| 16 | +$ if-node-version "<6" node ./scripts/fallback.js |
| 17 | +``` |
| 18 | + |
| 19 | +Maybe this helps you together with [npm-scripts]. |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +`if-node-version` can be installed with [npm]. |
| 24 | + |
| 25 | +```bash |
| 26 | +$ npm install --save-dev if-node-version |
| 27 | +``` |
| 28 | + |
| 29 | +- `if-node-version` requires Node.js `>=0.10.0` |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +### CLI |
| 34 | + |
| 35 | +``` |
| 36 | +Usage: |
| 37 | + $ if-node-version <VersionRange> <Command> [...args] |
| 38 | + $ if-node-version --help |
| 39 | + $ if-node-version --version |
| 40 | +
|
| 41 | + Run a shell command if it's on the node of specified versions. |
| 42 | +
|
| 43 | +Parameters: |
| 44 | + <VersionRange> .... A text which specifies the version range of Node.js. |
| 45 | + This text format is defined by node-semver module: |
| 46 | + https://www.npmjs.com/package/semver#ranges |
| 47 | + <Command> ......... A shell command. |
| 48 | + [...args] ......... Parameters of the shell command. |
| 49 | +
|
| 50 | +Examples: |
| 51 | + $ if-node-version ">=4" eslint lib test |
| 52 | + $ if-node-version "<6" node ./scripts/fallback.js |
| 53 | +``` |
| 54 | + |
| 55 | +### Node API |
| 56 | + |
| 57 | +```js |
| 58 | +var spawnIfNodeVersion = require("if-node-version") |
| 59 | +``` |
| 60 | + |
| 61 | +#### spawnIfNodeVersion(versionRange, command, args, options) |
| 62 | + |
| 63 | +Spawn a child process with specified parameters if the node version satisfies a given version range. |
| 64 | + |
| 65 | +This function returns [child_process.ChildProcess] object. |
| 66 | + |
| 67 | +- `versionRange` `{string}` - A text which specifies the version range of Node.js. This text format is defined by node-semver module: https://www.npmjs.com/package/semver#ranges |
| 68 | +- `command` `{string}` - The command to run. |
| 69 | +- `args` `{Array.<string>}` - List of string arguments. |
| 70 | +- `options` `{object}` - An option object. See [the document of `child_process.spawn`] |
| 71 | + |
| 72 | +#### spawnIfNodeVersion.sync(versionRange, command, args, options) |
| 73 | + |
| 74 | +This is synchronous version of `spawnIfNodeVersion(versionRange, command, args, options)`. |
| 75 | + |
| 76 | +This function returns the object as same as [child_process.spawnSync]. |
| 77 | + |
| 78 | +**Note:** If you use this function on node `0.10`, you will also need to install [spawn-sync]. |
| 79 | + |
| 80 | +[npm]: https://www.npmjs.com/ |
| 81 | +[npm-scripts]: https://docs.npmjs.com/misc/scripts |
| 82 | +[child_process.ChildProcess]: https://nodejs.org/api/child_process.html#child_process_class_childprocess |
| 83 | +[the document of `child_process.spawn`]: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options |
| 84 | +[child_process.spawnSync]: https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options |
| 85 | +[spawn-sync]: https://www.npmjs.com/package/spawn-sync |
0 commit comments