Skip to content

Commit 34cb764

Browse files
committed
New: basic implementation
1 parent eefad1a commit 34cb764

File tree

9 files changed

+282
-1
lines changed

9 files changed

+282
-1
lines changed

.eslintrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": ["mysticatea/es5", "mysticatea/node"],
3+
"rules": {
4+
"require-jsdoc": "off",
5+
"no-console": "off"
6+
}
7+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
/npm-debug.log

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,85 @@
11
# if-node-version
2-
Run commands if on specified node version.
2+
3+
[![npm version](https://img.shields.io/npm/v/if-node-version.svg)](https://www.npmjs.com/package/if-node-version)
4+
[![Downloads/month](https://img.shields.io/npm/dm/if-node-version.svg)](http://www.npmtrends.com/if-node-version)
5+
[![Build Status](https://travis-ci.org/mysticatea/if-node-version.svg?branch=master)](https://travis-ci.org/mysticatea/if-node-version)
6+
[![Coverage Status](https://coveralls.io/repos/mysticatea/if-node-version/badge.svg?branch=master&service=github)](https://coveralls.io/github/mysticatea/if-node-version?branch=master)
7+
[![Dependency Status](https://david-dm.org/mysticatea/if-node-version.svg)](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

bin/help.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2016 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
var fs = require("fs")
13+
var path = require("path")
14+
15+
//------------------------------------------------------------------------------
16+
// Helpers
17+
//------------------------------------------------------------------------------
18+
19+
var help = fs.readFileSync(path.join(__dirname, "help.txt"), "utf8")
20+
21+
//------------------------------------------------------------------------------
22+
// Public Interface
23+
//------------------------------------------------------------------------------
24+
25+
exports.printHelp = function printHelp() {
26+
console.log(help)
27+
}

bin/help.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
Usage:
3+
$ if-node-version <VersionRange> <Command> [...args]
4+
$ if-node-version --help
5+
$ if-node-version --version
6+
7+
Run a shell command if it's on the node of specified versions.
8+
9+
Parameters:
10+
<VersionRange> .... A text which specifies the version range of Node.js
11+
This text format is defined by node-semver module:
12+
https://www.npmjs.com/package/semver#ranges
13+
<Command> ......... A shell command.
14+
[...args] ......... Parameters of the shell command.
15+
16+
Examples:
17+
$ if-node-version ">=4" eslint lib test
18+
$ if-node-version "<6" node ./scripts/fallback.js
19+

bin/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env node
2+
/**
3+
* @author Toru Nagashima
4+
* @copyright 2016 Toru Nagashima. All rights reserved.
5+
* See LICENSE file in root directory for full license.
6+
*/
7+
"use strict"
8+
9+
//------------------------------------------------------------------------------
10+
// Requirements
11+
//------------------------------------------------------------------------------
12+
13+
var spawnIfNodeVersionSatisfies = require("../lib")
14+
15+
//------------------------------------------------------------------------------
16+
// Main
17+
//------------------------------------------------------------------------------
18+
/*eslint-disable no-process-exit*/
19+
20+
var argv = process.argv
21+
var versionRange = argv[2]
22+
var command = argv[3]
23+
var args = argv.slice(4)
24+
25+
if (argv.length < 4) {
26+
require("./help").printHelp()
27+
process.exit(1)
28+
}
29+
30+
if (versionRange === "--help" || versionRange === "-h") {
31+
require("./help").printHelp()
32+
return
33+
}
34+
35+
if (versionRange === "--version" || versionRange === "-v") {
36+
require("./version").printVersion()
37+
return
38+
}
39+
40+
spawnIfNodeVersionSatisfies(versionRange, command, args, {stdio: "inherit"})

bin/version.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2016 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
var path = require("path")
13+
var version = require(path.resolve(__dirname, "../package.json")).version
14+
15+
//------------------------------------------------------------------------------
16+
// Public Interface
17+
//------------------------------------------------------------------------------
18+
19+
exports.printVersion = function printVersion() {
20+
console.log("v" + version)
21+
}

lib/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @author Toru Nagashima
3+
* @copyright 2016 Toru Nagashima. All rights reserved.
4+
* See LICENSE file in root directory for full license.
5+
*/
6+
"use strict"
7+
8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
var semver = require("semver")
13+
var spawn = require("cross-spawn")
14+
15+
//------------------------------------------------------------------------------
16+
// Helpers
17+
//------------------------------------------------------------------------------
18+
19+
function isNodeVersionSatisfies(range) {
20+
return semver.satisfies(process.versions.node, range)
21+
}
22+
23+
//------------------------------------------------------------------------------
24+
// Public Interface
25+
//------------------------------------------------------------------------------
26+
27+
module.exports = function spawnIfNodeVersionSatisfies(
28+
versionRange,
29+
command,
30+
args,
31+
options
32+
) {
33+
if (isNodeVersionSatisfies(versionRange)) {
34+
return spawn(command, args, options)
35+
}
36+
return null
37+
}
38+
39+
module.exports.sync = function spawnIfNodeVersionSatisfiesSync(
40+
versionRange,
41+
command,
42+
args,
43+
options
44+
) {
45+
if (isNodeVersionSatisfies(versionRange)) {
46+
return spawn.sync(command, args, options)
47+
}
48+
return null
49+
}

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "if-node-version",
3+
"version": "0.0.0",
4+
"description": "Run commands if on specified node version.",
5+
"engines": {
6+
"node": ">=0.10.0"
7+
},
8+
"main": "lib/index.js",
9+
"bin": "bin/index.js",
10+
"files": ["bin", "lib"],
11+
"scripts": {
12+
"test": "echo \"Error: no test specified\" && exit 1"
13+
},
14+
"dependencies": {
15+
"cross-spawn": "^4.0.0",
16+
"semver": "^5.2.0"
17+
},
18+
"devDependencies": {
19+
"eslint": "^2.13.1",
20+
"eslint-config-mysticatea": "^4.0.0"
21+
},
22+
"repository": {
23+
"type": "git",
24+
"url": "git+https://github.com/mysticatea/if-node-version.git"
25+
},
26+
"keywords": [],
27+
"author": "Toru Nagashima",
28+
"license": "MIT",
29+
"bugs": {
30+
"url": "https://github.com/mysticatea/if-node-version/issues"
31+
},
32+
"homepage": "https://github.com/mysticatea/if-node-version#readme"
33+
}

0 commit comments

Comments
 (0)