Skip to content

Commit 41b720a

Browse files
committed
feat: add run-if command, close #2
1 parent c86c25f commit 41b720a

File tree

6 files changed

+113
-14
lines changed

6 files changed

+113
-14
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ script:
1313
- DEBUG=commit-message-install node ./bin/commit-message-install.js
1414
# synthetic test case
1515
- DEBUG=commit-message-install node ./bin/commit-message-install.js -f test/message.txt
16+
- npm run demo
1617
after_success:
1718
- npm run semantic-release
1819
branches:

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ Happy installation
5252
**note** `platform` can be `*` or specific one like `darwin` (from Node `os.platform()`) or a
5353
list of several platforms like `darwin,linux`
5454

55+
### Run or skip command based on platform
56+
57+
If the commit message allows a specific platform, you can run any command, while
58+
setting environment variables from the commit message. For example if th
59+
commit message embeds the following JSON block
60+
61+
```json
62+
{
63+
"platform": "win32",
64+
"env": {
65+
"FOO": "bar"
66+
}
67+
}
68+
```
69+
70+
and the CI has command `run-if echo Foo is \\$FOO`, then on Windows CI it will print
71+
`Foo is bar` and on other platforms it will skip this step.
72+
5573
## Debugging
5674

5775
- Run this tool with `DEBUG=commit-message-install` environment variable set

__snapshots__/commit-message-install-spec.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,51 @@ exports['commit-message-install getCommand removes --file and its argument 1'] =
3636
exports['commit-message-install getJsonBlock returns first found json block 1'] = {
3737
"foo": "bar"
3838
}
39+
40+
exports['commit-message-install isPlatformAllowed compares platforms isPlatformAllowed 1'] = {
41+
"name": "isPlatformAllowed",
42+
"behavior": [
43+
{
44+
"given": [
45+
"win32",
46+
"win32"
47+
],
48+
"expect": true
49+
},
50+
{
51+
"given": [
52+
"win32",
53+
"linux"
54+
],
55+
"expect": false
56+
},
57+
{
58+
"given": [
59+
"*",
60+
"linux"
61+
],
62+
"expect": true
63+
},
64+
{
65+
"given": [
66+
"win32,linux",
67+
"linux"
68+
],
69+
"expect": true
70+
},
71+
{
72+
"given": [
73+
"win32|linux",
74+
"linux"
75+
],
76+
"expect": true
77+
},
78+
{
79+
"given": [
80+
"win32, linux",
81+
"linux"
82+
],
83+
"expect": true
84+
}
85+
]
86+
}

src/commit-message-install-spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ describe('commit-message-install', () => {
2121
})
2222
})
2323

24+
context('isPlatformAllowed', () => {
25+
const { isPlatformAllowed } = require('.')
26+
27+
it('is a function', () => {
28+
la(is.fn(isPlatformAllowed))
29+
})
30+
31+
it('compares platforms', () => {
32+
snapshot(
33+
isPlatformAllowed,
34+
['win32', 'win32'],
35+
['win32', 'linux'],
36+
['*', 'linux'],
37+
['win32,linux', 'linux'],
38+
['win32|linux', 'linux'],
39+
['win32, linux', 'linux']
40+
)
41+
})
42+
})
43+
2444
context('getCommand', () => {
2545
const { getCommand } = require('.')
2646

src/index.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ const isRunIf = is.schema({
7575
env: is.maybe.object
7676
})
7777

78-
function isPlatformAllowed (platform) {
78+
function isPlatformAllowed (platform, osPlatform = os.platform()) {
79+
if (!platform) {
80+
return true
81+
}
82+
debug('checking platform, allowed platform is', platform)
7983
la(is.unemptyString(platform), 'invalid allowed platform', platform)
80-
return platform === '*' || platform.indexOf(os.platform()) !== -1
84+
return platform === '*' || platform.indexOf(osPlatform) !== -1
8185
}
8286

8387
function getCommand (args) {
@@ -97,25 +101,33 @@ function getCommand (args) {
97101
function runIf (command, json) {
98102
la(is.unemptyString(command), 'missing command to run', command)
99103
la(isRunIf(json), 'invalid runIf json', json)
100-
return execa.shell(command, { env: json.env }).then(prop('stdout'))
104+
105+
if (!isPlatformAllowed(json.platform, os.platform())) {
106+
console.log('Required platform: %s', json.platform)
107+
console.log('Current platform: %s', os.platform())
108+
console.log('skipping command')
109+
return Promise.resolve()
110+
}
111+
112+
const options = {
113+
env: json.env,
114+
stdio: 'inherit'
115+
}
116+
return execa.shell(command, options).then(prop('stdout'))
101117
}
102118

103119
function npmInstall (json) {
104120
if (!json) {
105121
debug('missing json for npm install')
106-
return
122+
return Promise.resolve()
107123
}
108124
la(isNpmInstall(json), 'invalid JSON to install format', json)
109125

110-
if (json.platform) {
111-
debug('checking platform, expecting', json.platform)
112-
la(is.unemptyString(json.platform), 'invalid json platform', json.platform)
113-
if (!isPlatformAllowed(json.platform)) {
114-
console.log('Required platform: %s', json.platform)
115-
console.log('Current platform: %s', os.platform())
116-
console.log('skipping install')
117-
return
118-
}
126+
if (!isPlatformAllowed(json.platform, os.platform())) {
127+
console.log('Required platform: %s', json.platform)
128+
console.log('Current platform: %s', os.platform())
129+
console.log('skipping install')
130+
return Promise.resolve()
119131
}
120132

121133
const env = json.env || {}

test/run-if.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ used to simulate commit message
33

44
```json
55
{
6-
"platform": "*",
6+
"platform": "darwin|linux",
77
"env": {
88
"FOO": "bar"
99
}

0 commit comments

Comments
 (0)