Skip to content

Commit b7ccfd8

Browse files
committed
chore: refactoring into smaller files
1 parent 598c63b commit b7ccfd8

File tree

7 files changed

+120
-97
lines changed

7 files changed

+120
-97
lines changed

__snapshots__/commit-message-install-spec.js

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,6 @@ exports['commit-message-install gets commit message for specific commit returns
66
message body
77
`
88

9-
exports['commit-message-install getInstallJson sets properties and all platforms 1'] = {
10-
"platform": "*",
11-
"env": {
12-
"foo": 42
13-
},
14-
"packages": "debug"
15-
}
16-
17-
exports['commit-message-install getInstallJson sets given platform 1'] = {
18-
"platform": "linux",
19-
"env": {
20-
"foo": 42
21-
},
22-
"packages": "debug"
23-
}
24-
25-
exports['commit-message-install getInstallJson sets several modules 1'] = {
26-
"platform": "linux",
27-
"env": {},
28-
"packages": "debug chalk"
29-
}
30-
31-
exports['commit-message-install getInstallJson sets branch 1'] = {
32-
"platform": "linux",
33-
"env": {},
34-
"packages": "debug chalk",
35-
"branch": "test-branch"
36-
}
37-
389
exports['commit-message-install isPlatformAllowed compares platforms isPlatformAllowed 1'] = {
3910
"name": "isPlatformAllowed",
4011
"behavior": [
@@ -131,7 +102,7 @@ exports['commit-message-install getJsonBlock returns json with branch 1'] = {
131102
"branch": "test-branch"
132103
}
133104

134-
exports['commit-message-install getJsonFromGit returns undefined without valid block 1'] = {
105+
exports['commit-message-install getJsonFromGit extracts the json from git message 1'] = {
135106
"platform": "win32",
136107
"branch": "some-branch"
137108
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
exports['getInstallJson sets properties and all platforms 1'] = {
2+
"platform": "*",
3+
"env": {
4+
"foo": 42
5+
},
6+
"packages": "debug"
7+
}
8+
9+
exports['getInstallJson sets given platform 1'] = {
10+
"platform": "linux",
11+
"env": {
12+
"foo": 42
13+
},
14+
"packages": "debug"
15+
}
16+
17+
exports['getInstallJson sets several modules 1'] = {
18+
"platform": "linux",
19+
"env": {},
20+
"packages": "debug chalk"
21+
}
22+
23+
exports['getInstallJson sets branch 1'] = {
24+
"platform": "linux",
25+
"env": {},
26+
"packages": "debug chalk",
27+
"branch": "test-branch"
28+
}

src/commit-message-install-spec.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,6 @@ describe('commit-message-install', () => {
8686
})
8787
})
8888

89-
context('getInstallJson', () => {
90-
const getInstallJson = require('.').getInstallJson
91-
92-
it('sets properties and all platforms', () => {
93-
snapshot(getInstallJson('debug', { foo: 42 }, '*'))
94-
})
95-
96-
it('sets given platform', () => {
97-
snapshot(getInstallJson('debug', { foo: 42 }, 'linux'))
98-
})
99-
100-
it('sets several modules', () => {
101-
snapshot(getInstallJson(['debug', 'chalk'], {}, 'linux'))
102-
})
103-
104-
it('sets branch', () => {
105-
snapshot(getInstallJson(['debug', 'chalk'], {}, 'linux', 'test-branch'))
106-
})
107-
})
108-
10989
context('isPlatformAllowed', () => {
11090
const isPlatformAllowed = require('.').isPlatformAllowed
11191

src/get-install-json-spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict'
2+
3+
/* eslint-env mocha */
4+
const la = require('lazy-ass')
5+
const is = require('check-more-types')
6+
const snapshot = require('snap-shot-it')
7+
8+
describe('getInstallJson', () => {
9+
const getInstallJson = require('.').getInstallJson
10+
11+
it('is a function', () => {
12+
la(is.fn(getInstallJson))
13+
})
14+
15+
it('sets properties and all platforms', () => {
16+
snapshot(getInstallJson('debug', { foo: 42 }, '*'))
17+
})
18+
19+
it('sets given platform', () => {
20+
snapshot(getInstallJson('debug', { foo: 42 }, 'linux'))
21+
})
22+
23+
it('sets several modules', () => {
24+
snapshot(getInstallJson(['debug', 'chalk'], {}, 'linux'))
25+
})
26+
27+
it('sets branch', () => {
28+
snapshot(getInstallJson(['debug', 'chalk'], {}, 'linux', 'test-branch'))
29+
})
30+
})

src/get-install-json.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const la = require('lazy-ass')
2+
const is = require('check-more-types')
3+
const os = require('os')
4+
const debug = require('debug')('commit-message-install')
5+
const { isNpmInstall } = require('./utils')
6+
7+
// forms JSON object that can be parsed later
8+
function getInstallJson (packages, env, platform, branch) {
9+
if (!env) {
10+
env = {}
11+
}
12+
if (!platform) {
13+
platform = os.platform()
14+
}
15+
la(
16+
is.unemptyString(packages) || is.strings(packages),
17+
'invalid package / list of packages',
18+
packages
19+
)
20+
la(is.object(env), 'invalid env object', env)
21+
if (is.strings(packages)) {
22+
packages = packages.join(' ')
23+
}
24+
la(is.unemptyString(platform), 'missing platform', platform)
25+
26+
const json = {
27+
platform,
28+
env,
29+
packages
30+
}
31+
if (branch) {
32+
la(is.unemptyString(branch), 'invalid branch name', branch)
33+
debug('branch name', branch)
34+
json.branch = branch
35+
}
36+
37+
la(
38+
isNpmInstall(json),
39+
'formed invalid json object',
40+
json,
41+
'from arguments',
42+
arguments
43+
)
44+
return json
45+
}
46+
47+
module.exports = getInstallJson

src/index.js

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const is = require('check-more-types')
66
const os = require('os')
77
const execa = require('execa')
88
const chalk = require('chalk')
9+
const getInstallJson = require('./get-install-json')
10+
const { isNpmInstall } = require('./utils')
911

1012
const prop = name => object => object[name]
1113

@@ -72,13 +74,6 @@ function getJsonBlock (message) {
7274
}
7375
}
7476

75-
// to install multiple packages, use comma-separated list
76-
const isNpmInstall = is.schema({
77-
platform: is.maybe.unemptyString,
78-
env: is.maybe.object,
79-
packages: is.unemptyString
80-
})
81-
8277
const isRunIf = is.schema({
8378
platform: is.maybe.unemptyString,
8479
env: is.maybe.object
@@ -171,46 +166,6 @@ function npmInstall (json) {
171166
})
172167
}
173168

174-
// forms JSON object that can be parsed later
175-
function getInstallJson (packages, env, platform, branch) {
176-
if (!env) {
177-
env = {}
178-
}
179-
if (!platform) {
180-
platform = os.platform()
181-
}
182-
la(
183-
is.unemptyString(packages) || is.strings(packages),
184-
'invalid package / list of packages',
185-
packages
186-
)
187-
la(is.object(env), 'invalid env object', env)
188-
if (is.strings(packages)) {
189-
packages = packages.join(' ')
190-
}
191-
la(is.unemptyString(platform), 'missing platform', platform)
192-
193-
const json = {
194-
platform,
195-
env,
196-
packages
197-
}
198-
if (branch) {
199-
la(is.unemptyString(branch), 'invalid branch name', branch)
200-
debug('branch name', branch)
201-
json.branch = branch
202-
}
203-
204-
la(
205-
isNpmInstall(json),
206-
'formed invalid json object',
207-
json,
208-
'from arguments',
209-
arguments
210-
)
211-
return json
212-
}
213-
214169
// looks at the current Git message,
215170
// extracts JSON block
216171
function getJsonFromGit () {

src/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const is = require('check-more-types')
2+
3+
// to install multiple packages, use comma-separated list
4+
const isNpmInstall = is.schema({
5+
platform: is.maybe.unemptyString,
6+
env: is.maybe.object,
7+
packages: is.unemptyString
8+
})
9+
10+
module.exports = {
11+
isNpmInstall
12+
}

0 commit comments

Comments
 (0)