Skip to content

Commit 99a94bb

Browse files
committed
feat: add API method to form good json block, close #4
1 parent 3d243ea commit 99a94bb

File tree

4 files changed

+108
-13
lines changed

4 files changed

+108
-13
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ commit message embeds the following JSON block
7070
and the CI has command `run-if echo Foo is \\$FOO`, then on Windows CI it will print
7171
`Foo is bar` and on other platforms it will skip this step.
7272

73+
## API
74+
75+
You can form good Json object to be included in markdown `json` block in the body of
76+
the commit message using provided function
77+
78+
```js
79+
const {getInstallJson} = require('commit-message-install')
80+
// package(s), env, platform
81+
const json = getInstallJson('foo', {foo: 42}, 'linux')
82+
// returns an object
83+
// {platform: "linux", env: {foo: 42}, packages: "foo"}
84+
```
85+
86+
You can pass individual package name like `debug` or several as a single string `debug chalk`
87+
or a list `['debug', 'chalk']`
88+
89+
You can pass for platform either individual `os.platform()` or a "*"" for all, and even
90+
several platforms like `win32,linux` or `linux|darwin`.
91+
7392
## Debugging
7493

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

__snapshots__/commit-message-install-spec.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1-
exports['commit-message-install getJsonBlock finds single json block at start 1'] = {
2-
"foo": "bar"
1+
exports['commit-message-install getInstallJson sets properties and all platforms 1'] = {
2+
"platform": "*",
3+
"env": {
4+
"foo": 42
5+
},
6+
"packages": "debug"
37
}
48

9+
exports['commit-message-install getInstallJson sets given platform 1'] = {
10+
"platform": "linux",
11+
"env": {
12+
"foo": 42
13+
},
14+
"packages": "debug"
15+
}
16+
17+
exports['commit-message-install getInstallJson sets several modules 1'] = {
18+
"platform": "linux",
19+
"env": {},
20+
"packages": "debug chalk"
21+
}
22+
23+
exports['commit-message-install gets last commit message returns just the body of the commit message 1'] = `
24+
message body
25+
`
26+
527
exports['commit-message-install isPlatformAllowed compares platforms isPlatformAllowed 1'] = {
628
"name": "isPlatformAllowed",
729
"behavior": [
@@ -81,14 +103,14 @@ exports['commit-message-install getCommand removes --file and its argument 1'] =
81103
"command": "echo foo bar"
82104
}
83105

106+
exports['commit-message-install getJsonBlock finds single json block at start 1'] = {
107+
"foo": "bar"
108+
}
109+
84110
exports['commit-message-install getJsonBlock finds single json block 1'] = {
85111
"foo": "bar"
86112
}
87113

88114
exports['commit-message-install getJsonBlock returns first found json block 1'] = {
89115
"foo": "bar"
90116
}
91-
92-
exports['commit-message-install gets last commit message returns just the body of the commit message 1'] = `
93-
message body
94-
`

src/commit-message-install-spec.js

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

25+
context('getInstallJson', () => {
26+
const { getInstallJson } = require('.')
27+
28+
it('sets properties and all platforms', () => {
29+
snapshot(getInstallJson('debug', { foo: 42 }, '*'))
30+
})
31+
32+
it('sets given platform', () => {
33+
snapshot(getInstallJson('debug', { foo: 42 }, 'linux'))
34+
})
35+
36+
it('sets several modules', () => {
37+
snapshot(getInstallJson(['debug', 'chalk'], {}, 'linux'))
38+
})
39+
})
40+
2541
context('isPlatformAllowed', () => {
2642
const { isPlatformAllowed } = require('.')
2743

src/index.js

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,20 @@ function runIf (command, json) {
106106
la(is.unemptyString(command), 'missing command to run', command)
107107
la(isRunIf(json), 'invalid runIf json', json)
108108

109-
if (!isPlatformAllowed(json.platform, os.platform())) {
109+
const osPlatform = os.platform()
110+
if (!isPlatformAllowed(json.platform, osPlatform)) {
110111
console.log('Required platform: %s', chalk.green(json.platform))
111-
console.log('Current platform: %s', chalk.red(os.platform()))
112+
console.log('Current platform: %s', chalk.red(osPlatform))
112113
console.log('skipping command ⏩ %s', command)
113114
return Promise.resolve()
114115
}
116+
console.log('Platform %s is allowed', chalk.green(osPlatform))
115117

116118
const options = {
117119
env: json.env,
118120
stdio: 'inherit'
119121
}
120-
return execa.shell(command, options).then(prop('stdout'))
122+
return execa.shell(command, options)
121123
}
122124

123125
function npmInstall (json) {
@@ -127,23 +129,58 @@ function npmInstall (json) {
127129
}
128130
la(isNpmInstall(json), 'invalid JSON to install format', json)
129131

130-
if (!isPlatformAllowed(json.platform, os.platform())) {
132+
const osPlatform = os.platform()
133+
if (!isPlatformAllowed(json.platform, osPlatform)) {
131134
console.log('Required platform: %s', chalk.green(json.platform))
132-
console.log('Current platform: %s', chalk.red(os.platform()))
135+
console.log('Current platform: %s', chalk.red(osPlatform))
133136
console.log('skipping NPM install')
134137
return Promise.resolve()
135138
}
139+
console.log('Platform %s is allowed', chalk.green(osPlatform))
136140

137141
const env = json.env || {}
138142
console.log('installing', json.packages)
139143
if (is.not.empty(env)) {
140144
console.log('with extra environment variables')
141145
console.log(env)
142146
}
147+
if (json.packages.indexOf(',') !== -1) {
148+
console.log('warning: list of packages includes commas')
149+
console.log('npm install might not work!')
150+
console.log(json.packages)
151+
}
143152
return execa('npm', ['install', json.packages], {
144153
env,
145154
stdio: 'inherit'
146-
}).then(prop('stdout'))
155+
})
156+
}
157+
158+
// forms JSON object that can be parsed later
159+
function getInstallJson (packages, env = {}, platform = os.platform()) {
160+
la(
161+
is.unemptyString(packages) || is.strings(packages),
162+
'invalid package / list of packages',
163+
packages
164+
)
165+
la(is.object(env), 'invalid env object', env)
166+
if (is.strings(packages)) {
167+
packages = packages.join(' ')
168+
}
169+
la(is.unemptyString(platform), 'missing platform', platform)
170+
171+
const json = {
172+
platform,
173+
env,
174+
packages
175+
}
176+
la(
177+
isNpmInstall(json),
178+
'formed invalid json object',
179+
json,
180+
'from arguments',
181+
arguments
182+
)
183+
return json
147184
}
148185

149186
module.exports = {
@@ -152,5 +189,6 @@ module.exports = {
152189
runIf,
153190
isPlatformAllowed,
154191
getJsonBlock,
155-
npmInstall
192+
npmInstall,
193+
getInstallJson
156194
}

0 commit comments

Comments
 (0)