Skip to content

Commit 84f879c

Browse files
committed
feat: check arch before running
1 parent 17c53df commit 84f879c

File tree

3 files changed

+219
-67
lines changed

3 files changed

+219
-67
lines changed
Lines changed: 154 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,201 @@
1-
exports[
2-
'commit-message-install getCommand removes --file and its argument 1'
3-
] = {
4-
args: ['--file', 'file.txt', 'echo', 'foo', 'bar'],
5-
command: 'echo foo bar'
1+
exports['commit-message-install getCommand removes --file and its argument 1'] = {
2+
"args": [
3+
"--file",
4+
"file.txt",
5+
"echo",
6+
"foo",
7+
"bar"
8+
],
9+
"command": "echo foo bar"
610
}
711

812
exports['commit-message-install getCommand removes -f and its argument 1'] = {
9-
args: ['-f', 'file.txt', 'echo', 'foo', 'bar'],
10-
command: 'echo foo bar'
13+
"args": [
14+
"-f",
15+
"file.txt",
16+
"echo",
17+
"foo",
18+
"bar"
19+
],
20+
"command": "echo foo bar"
1121
}
1222

13-
exports[
14-
'commit-message-install getCommand returns original command from array 1'
15-
] = {
16-
args: ['echo', 'foo', 'bar'],
17-
command: 'echo foo bar'
23+
exports['commit-message-install getCommand returns original command from array 1'] = {
24+
"args": [
25+
"echo",
26+
"foo",
27+
"bar"
28+
],
29+
"command": "echo foo bar"
1830
}
1931

2032
exports['commit-message-install getJsonBlock finds single json block 1'] = {
21-
foo: 'bar'
33+
"foo": "bar"
2234
}
2335

24-
exports[
25-
'commit-message-install getJsonBlock finds single json block at start 1'
26-
] = {
27-
foo: 'bar'
36+
exports['commit-message-install getJsonBlock finds single json block at start 1'] = {
37+
"foo": "bar"
2838
}
2939

30-
exports[
31-
'commit-message-install getJsonBlock returns first found json block 1'
32-
] = {
33-
foo: 'bar'
40+
exports['commit-message-install getJsonBlock returns first found json block 1'] = {
41+
"foo": "bar"
3442
}
3543

3644
exports['commit-message-install getJsonBlock returns json with branch 1'] = {
37-
foo: 'bar',
38-
branch: 'test-branch'
45+
"foo": "bar",
46+
"branch": "test-branch"
3947
}
4048

41-
exports[
42-
'commit-message-install getJsonFromGit extracts the json from git message 1'
43-
] = {
44-
platform: 'win32',
45-
branch: 'some-branch'
49+
exports['commit-message-install getJsonFromGit extracts the json from git message 1'] = {
50+
"platform": "win32",
51+
"branch": "some-branch"
4652
}
4753

48-
exports[
49-
'commit-message-install gets commit message for specific commit returns just the body of specific commit 1'
50-
] = `
54+
exports['commit-message-install gets commit message for specific commit returns just the body of specific commit 1'] = `
5155
message body
5256
`
5357

54-
exports[
55-
'commit-message-install gets last commit message returns just the body of the commit message 1'
56-
] = `
58+
exports['commit-message-install gets last commit message returns just the body of the commit message 1'] = `
5759
message body
5860
`
5961

60-
exports[
61-
'commit-message-install isPlatformAllowed compares platforms isPlatformAllowed 1'
62-
] = {
63-
name: 'isPlatformAllowed',
64-
behavior: [
62+
exports['commit-message-install isArchAllowed compares arch isArchAllowed 1'] = {
63+
"name": "isArchAllowed",
64+
"behavior": [
6565
{
66-
given: ['win32', 'win32'],
67-
expect: true
66+
"given": [
67+
"x64",
68+
"x64"
69+
],
70+
"expect": true
6871
},
6972
{
70-
given: ['win32', 'linux'],
71-
expect: false
73+
"given": [
74+
null,
75+
"x64"
76+
],
77+
"expect": true
7278
},
7379
{
74-
given: ['*', 'linux'],
75-
expect: true
80+
"given": [
81+
"*",
82+
"x64"
83+
],
84+
"expect": true
7685
},
7786
{
78-
given: ['win32,linux', 'linux'],
79-
expect: true
87+
"given": [
88+
null,
89+
null
90+
],
91+
"expect": true
8092
},
8193
{
82-
given: ['win32|linux', 'linux'],
83-
expect: true
94+
"given": [
95+
"ia32,x64",
96+
"ia32"
97+
],
98+
"expect": true
8499
},
85100
{
86-
given: ['win32, linux', 'linux'],
87-
expect: true
101+
"given": [
102+
"ia32,x64",
103+
"x64"
104+
],
105+
"expect": true
106+
},
107+
{
108+
"given": [
109+
"ia32,x64",
110+
null
111+
],
112+
"expect": true
113+
},
114+
{
115+
"given": [
116+
"ia32",
117+
"x64"
118+
],
119+
"expect": false
120+
},
121+
{
122+
"given": [
123+
"x64",
124+
"ia32"
125+
],
126+
"expect": false
127+
}
128+
]
129+
}
130+
131+
exports['commit-message-install isPlatformAllowed compares platforms isPlatformAllowed 1'] = {
132+
"name": "isPlatformAllowed",
133+
"behavior": [
134+
{
135+
"given": [
136+
"win32",
137+
"win32"
138+
],
139+
"expect": true
140+
},
141+
{
142+
"given": [
143+
"win32",
144+
"linux"
145+
],
146+
"expect": false
147+
},
148+
{
149+
"given": [
150+
"*",
151+
"linux"
152+
],
153+
"expect": true
154+
},
155+
{
156+
"given": [
157+
"win32,linux",
158+
"linux"
159+
],
160+
"expect": true
161+
},
162+
{
163+
"given": [
164+
"win32|linux",
165+
"linux"
166+
],
167+
"expect": true
168+
},
169+
{
170+
"given": [
171+
"win32, linux",
172+
"linux"
173+
],
174+
"expect": true
88175
}
89176
]
90177
}
91178

92179
exports['formed json object'] = {
93-
platform: 'linux',
94-
arch: 'x64',
95-
env: {},
96-
packages: 'debug chalk',
97-
status: {
98-
owner: 'foo',
99-
repo: 'bar',
100-
sha: '2d8687c143165218c6b52a76018b76cf99137e48'
180+
"platform": "linux",
181+
"arch": "x64",
182+
"env": {},
183+
"packages": "debug chalk",
184+
"status": {
185+
"owner": "foo",
186+
"repo": "bar",
187+
"sha": "2d8687c143165218c6b52a76018b76cf99137e48"
101188
}
102189
}
103190

104191
exports['parsed back message'] = {
105-
platform: 'linux',
106-
arch: 'x64',
107-
env: {},
108-
packages: 'debug chalk',
109-
status: {
110-
owner: 'foo',
111-
repo: 'bar',
112-
sha: '2d8687c143165218c6b52a76018b76cf99137e48'
192+
"platform": "linux",
193+
"arch": "x64",
194+
"env": {},
195+
"packages": "debug chalk",
196+
"status": {
197+
"owner": "foo",
198+
"repo": "bar",
199+
"sha": "2d8687c143165218c6b52a76018b76cf99137e48"
113200
}
114201
}

src/commit-message-install-spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,30 @@ describe('commit-message-install', () => {
133133
})
134134
})
135135

136+
context('isArchAllowed', () => {
137+
const { isArchAllowed } = require('.')
138+
139+
it('is a function', () => {
140+
la(is.fn(isArchAllowed))
141+
})
142+
143+
it('compares arch', () => {
144+
snapshot(
145+
isArchAllowed,
146+
// required, detected
147+
['x64', 'x64'], // allowed
148+
[undefined, 'x64'], // allowed
149+
['*', 'x64'], // allowed
150+
[undefined, undefined], // allowed
151+
['ia32,x64', 'ia32'], // allowed
152+
['ia32,x64', 'x64'], // allowed
153+
['ia32,x64', undefined], // allowed
154+
['ia32', 'x64'], // not allowed
155+
['x64', 'ia32'] // not allowed
156+
)
157+
})
158+
})
159+
136160
context('getCommand', () => {
137161
const getCommand = require('.').getCommand
138162

src/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function getJsonBlock (message) {
7676

7777
const isRunIf = is.schema({
7878
platform: is.maybe.unemptyString,
79+
arch: is.maybe.unemptyString,
7980
env: is.maybe.object
8081
})
8182

@@ -91,6 +92,27 @@ function isPlatformAllowed (platform, osPlatform) {
9192
return platform === '*' || platform.indexOf(osPlatform) !== -1
9293
}
9394

95+
/**
96+
* Returns true if the required architecture matches current OS architecture.
97+
* If required architecture is undefined or "*", all are allowed.
98+
*
99+
* @param [string] required architecture, like "x64", "ia32" or "*"
100+
* @param [string] current OS architecture
101+
* @example
102+
* isArchAllowed(requiredArch, osArch)
103+
*/
104+
function isArchAllowed (arch, osArch) {
105+
if (!osArch) {
106+
osArch = os.arch()
107+
}
108+
if (!arch) {
109+
return true
110+
}
111+
debug('checking arch, allowed arch is', arch)
112+
la(is.unemptyString(arch), 'invalid allowed arch', arch)
113+
return arch === '*' || arch.indexOf(osArch) !== -1
114+
}
115+
94116
function clone (x) {
95117
return JSON.parse(JSON.stringify(x))
96118
}
@@ -126,6 +148,15 @@ function runIf (command, json) {
126148
}
127149
console.log('Platform %s is allowed', chalk.green(osPlatform))
128150

151+
const osArch = os.arch()
152+
if (!isArchAllowed(json.arch, osArch)) {
153+
console.log('Required arch: %s', chalk.green(json.arch))
154+
console.log('Current arch: %s', chalk.red(osArch))
155+
console.log('skipping command ⏩ %s', command)
156+
return Promise.resolve()
157+
}
158+
console.log('Arch %s is allowed', chalk.green(osArch))
159+
129160
const options = {
130161
env: json.env,
131162
stdio: 'inherit'
@@ -149,6 +180,15 @@ function npmInstall (json) {
149180
}
150181
console.log('Platform %s is allowed', chalk.green(osPlatform))
151182

183+
const osArch = os.arch()
184+
if (!isArchAllowed(json.arch, osArch)) {
185+
console.log('Required arch: %s', chalk.green(json.arch))
186+
console.log('Current arch: %s', chalk.red(osArch))
187+
console.log('skipping NPM install')
188+
return Promise.resolve()
189+
}
190+
console.log('Arch %s is allowed', chalk.green(osArch))
191+
152192
const env = json.env || {}
153193
console.log('installing', json.packages)
154194
if (is.not.empty(env)) {
@@ -177,6 +217,7 @@ module.exports = {
177217
getCommand,
178218
runIf,
179219
isPlatformAllowed,
220+
isArchAllowed,
180221
getJsonBlock,
181222
npmInstall,
182223
getInstallJson,

0 commit comments

Comments
 (0)