Skip to content

Commit 29d156b

Browse files
committed
New: command to check Node version
1 parent 4660a4a commit 29d156b

File tree

6 files changed

+99
-21
lines changed

6 files changed

+99
-21
lines changed

README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,32 @@ $ npm install --save-dev if-node-version
3535
```
3636
Usage:
3737
$ if-node-version <VersionRange> <Command> [...args]
38+
39+
Run a shell command if it's on the node of specified versions.
40+
Otherwise, do nothing.
41+
42+
Exit code is the exit code of the <Command>.
43+
44+
$ if-node-version <VersionRange>
45+
46+
Check if it's on the node of specified versions.
47+
48+
Exit code is 0 if it's on the node of specified versions.
49+
Otherwise, exit code is 1.
50+
3851
$ if-node-version --help
52+
53+
Show this help text.
54+
3955
$ if-node-version --version
4056
41-
Run a shell command if it's on the node of specified versions.
57+
Show the version number of `if-node-version` command.
4258
4359
Parameters:
44-
<VersionRange> .... A text which specifies the version range of Node.js.
60+
<VersionRange> .... A text which specifies the version range of Node.js
4561
This text format is defined by node-semver module:
4662
https://www.npmjs.com/package/semver#ranges
47-
<Command> ......... A shell command.
63+
<Command> ......... The shell command to execute.
4864
[...args] ......... Parameters of the shell command.
4965
5066
Examples:
@@ -86,6 +102,15 @@ This function returns the object as same as [child_process.spawnSync].
86102
Welcome your contributions!<br>
87103
Please use GitHub's issues/PRs.
88104

105+
### Tools to develop
106+
107+
- `npm install` installs dependencies.
108+
- `npm test` runs tests and measures coverage.
109+
- `npm run coverage` opens the coverage result of `npm test`.
110+
- `npm run clean` removes the coverage result of `npm test`.
111+
- `npm run lint` analyzes codes by ESLint.
112+
- `npm run watch` runs tests (without coverage measurement) when source code is modified.
113+
89114

90115
[npm]: https://www.npmjs.com/
91116
[npm-scripts]: https://docs.npmjs.com/misc/scripts

bin/help.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11

22
Usage:
33
$ if-node-version <VersionRange> <Command> [...args]
4+
5+
Run a shell command if it's on the node of specified versions.
6+
Otherwise, do nothing.
7+
8+
Exit code is the exit code of the <Command>.
9+
10+
$ if-node-version <VersionRange>
11+
12+
Check if it's on the node of specified versions.
13+
14+
Exit code is 0 if it's on the node of specified versions.
15+
Otherwise, exit code is 1.
16+
417
$ if-node-version --help
18+
19+
Show this help text.
20+
521
$ if-node-version --version
622

7-
Run a shell command if it's on the node of specified versions.
23+
Show the version number of `if-node-version` command.
824

925
Parameters:
1026
<VersionRange> .... A text which specifies the version range of Node.js

bin/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Requirements
1111
//------------------------------------------------------------------------------
1212

13-
var spawnIfNodeVersionSatisfies = require("../lib")
13+
var spawn = require("../lib")
1414

1515
//------------------------------------------------------------------------------
1616
// Main
@@ -24,20 +24,24 @@ var args = argv.slice(4)
2424

2525
if (versionRange === "--help" || versionRange === "-h") {
2626
require("./help").printHelp(process.stdout)
27-
return
27+
process.exit(0)
2828
}
2929

3030
if (versionRange === "--version" || versionRange === "-v") {
3131
require("./version").printVersion(process.stdout)
32-
return
32+
process.exit(0)
3333
}
3434

35-
if (argv.length < 4) {
35+
if (!versionRange) {
3636
require("./help").printHelp(process.stderr)
3737
process.exit(1)
3838
}
3939

40-
var cp = spawnIfNodeVersionSatisfies(
40+
if (!command) {
41+
process.exit(spawn.isNodeVersionSatisfies(versionRange) ? 0 : 1)
42+
}
43+
44+
var cp = spawn(
4145
versionRange,
4246
command,
4347
args,

lib/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ function isNodeVersionSatisfies(range) {
2020
return semver.satisfies(process.versions.node, range)
2121
}
2222

23-
//------------------------------------------------------------------------------
24-
// Public Interface
25-
//------------------------------------------------------------------------------
26-
27-
module.exports = function spawnIfNodeVersionSatisfies(
23+
function spawnIfNodeVersionSatisfies(
2824
versionRange,
2925
command,
3026
args,
@@ -36,7 +32,7 @@ module.exports = function spawnIfNodeVersionSatisfies(
3632
return null
3733
}
3834

39-
module.exports.sync = function spawnIfNodeVersionSatisfiesSync(
35+
function spawnIfNodeVersionSatisfiesSync(
4036
versionRange,
4137
command,
4238
args,
@@ -47,3 +43,11 @@ module.exports.sync = function spawnIfNodeVersionSatisfiesSync(
4743
}
4844
return null
4945
}
46+
47+
//------------------------------------------------------------------------------
48+
// Public Interface
49+
//------------------------------------------------------------------------------
50+
51+
module.exports = spawnIfNodeVersionSatisfies
52+
module.exports.sync = spawnIfNodeVersionSatisfiesSync
53+
module.exports.isNodeVersionSatisfies = isNodeVersionSatisfies

test/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var runSync = api.runSync
2525
// Test
2626
//------------------------------------------------------------------------------
2727

28-
describe("Under Node.js v" + version + ",", function() {
28+
describe("[Node API] Under Node.js v" + version + ",", function() {
2929
var ranges = [
3030
">=0.10",
3131
">=0.12",

test/cli.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var runToCheck = cli.runToCheck
2525
// Test
2626
//------------------------------------------------------------------------------
2727

28-
describe("If no argument,", function() {
28+
describe("[CLI Command] If no argument,", function() {
2929
var result = null
3030

3131
before(function() {
@@ -48,7 +48,7 @@ describe("If no argument,", function() {
4848
})
4949
})
5050

51-
describe("If '--help' is given,", function() {
51+
describe("[CLI Command] If '--help' is given,", function() {
5252
var result = null
5353

5454
before(function() {
@@ -70,7 +70,7 @@ describe("If '--help' is given,", function() {
7070
})
7171
})
7272

73-
describe("If '-h' is given,", function() {
73+
describe("[CLI Command] If '-h' is given,", function() {
7474
var result = null
7575

7676
before(function() {
@@ -92,7 +92,7 @@ describe("If '-h' is given,", function() {
9292
})
9393
})
9494

95-
describe("If '--version' is given,", function() {
95+
describe("[CLI Command] If '--version' is given,", function() {
9696
var result = null
9797

9898
before(function() {
@@ -115,7 +115,7 @@ describe("If '--version' is given,", function() {
115115
})
116116
})
117117

118-
describe("Under Node.js v" + version + ",", function() {
118+
describe("[CLI Command] Under Node.js v" + version + ",", function() {
119119
var ranges = [
120120
">=0.10",
121121
">=0.12",
@@ -186,6 +186,35 @@ describe("Under Node.js v" + version + ",", function() {
186186
})
187187
}
188188
})
189+
190+
describe("with no command,", function() {
191+
var result = null
192+
193+
before(function() {
194+
return run([range]).then(function(ret) {
195+
result = ret
196+
})
197+
})
198+
199+
it("should not output to stdout", function() {
200+
assert(result.stdout === "")
201+
})
202+
203+
it("should not output to stderr", function() {
204+
assert(result.stderr === "")
205+
})
206+
207+
if (semver.satisfies(version, range)) {
208+
it("should exit with zero", function() {
209+
assert(result.exitCode === 0)
210+
})
211+
}
212+
else {
213+
it("should exit with one", function() {
214+
assert(result.exitCode === 1)
215+
})
216+
}
217+
})
189218
})
190219
})
191220
})

0 commit comments

Comments
 (0)