Skip to content

Commit ee861c9

Browse files
committed
Add support for "*" wildcard and comma-separated values for platform matching
* **source/index.ts** - Add support for "*" wildcard to match all Unix platforms - Add support for comma-separated values to match multiple platforms * **test/package.json** - Add test cases for "*" wildcard to match all Unix platforms - Add test cases for comma-separated values to match multiple platforms * **test/test.ts** - Add unit tests for "*" wildcard to match all Unix platforms - Add unit tests for comma-separated values to match multiple platforms * **README.md** - Update documentation to include usage of "*" wildcard to match all Unix platforms - Update documentation to include usage of comma-separated values to match multiple platforms * **.github/workflows/test.yml** - Create a new GitHub Actions workflow file to run the tests - Add a job to set up the Node.js environment - Add a job to install dependencies - Add a job to run the tests
1 parent fa3bf60 commit ee861c9

File tree

5 files changed

+109
-7
lines changed

5 files changed

+109
-7
lines changed

.github/workflows/test.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Node.js CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
strategy:
15+
matrix:
16+
node-version: [12.x, 14.x, 16.x]
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v2
21+
22+
- name: Set up Node.js ${{ matrix.node-version }}
23+
uses: actions/setup-node@v2
24+
with:
25+
node-version: ${{ matrix.node-version }}
26+
27+
- name: Install dependencies
28+
run: npm install
29+
30+
- name: Run tests
31+
run: npm test

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ or directly from the npm run script like this:
6767
npm run foo -- arg1 arg2
6868
```
6969

70+
You can use the "*" wildcard to match all Unix platforms:
71+
72+
```json
73+
"cross-os": {
74+
"chmodux": {
75+
"win32": "",
76+
"*": "chmod u+x main.js"
77+
}
78+
}
79+
```
80+
81+
You can also use comma-separated values to match multiple platforms:
82+
83+
```json
84+
"cross-os": {
85+
"chmodux": {
86+
"win32": "",
87+
"darwin,freebsd,linux,sunos": "chmod u+x main.js"
88+
}
89+
}
90+
```
7091

7192
## License
7293

source/index.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,23 @@ const pipeline = new Promise<string>(resolve => {
3838
}
3939

4040
try {
41-
return Promise.resolve({ command: config[ property ][ script ][ platform ], params, script })
41+
let command = config[ property ][ script ][ platform ]
42+
43+
if (!command && platform !== 'win32') {
44+
command = config[ property ][ script ]['*']
45+
}
46+
47+
if (!command) {
48+
const platforms = Object.keys(config[ property ][ script ])
49+
for (const p of platforms) {
50+
if (p.split(',').includes(platform)) {
51+
command = config[ property ][ script ][ p ]
52+
break
53+
}
54+
}
55+
}
56+
57+
return Promise.resolve({ command, params, script })
4258
} catch (e) {
4359
throw script
4460
}

test/package.json

+12-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
"first": {
44
"win32": "echo hello from win32",
55
"linux": "echo hello from linux",
6-
"darwin": "echo hello from darwin"
6+
"darwin": "echo hello from darwin",
7+
"*": "echo hello from unix"
78
},
89
"first-with-params": {
910
"win32": "echo hello from win32, I have arguments:",
1011
"linux": "echo hello from linux, I have arguments:",
11-
"darwin": "echo hello from darwin, I have arguments:"
12+
"darwin": "echo hello from darwin, I have arguments:",
13+
"*": "echo hello from unix, I have arguments:"
1214
},
1315
"second": {},
1416
"second-with-params": {},
@@ -37,24 +39,28 @@
3739
"fifth": {
3840
"win32": "echo hello from cross-os win32",
3941
"linux": "echo hello from cross-os linux",
40-
"darwin": "echo hello from cross-os darwin"
42+
"darwin": "echo hello from cross-os darwin",
43+
"*": "echo hello from cross-os unix"
4144
},
4245
"fifth-with-params": {
4346
"win32": "echo hello from cross-os win32, I have arguments:",
4447
"linux": "echo hello from cross-os linux, I have arguments:",
45-
"darwin": "echo hello from cross-os darwin, I have arguments:"
48+
"darwin": "echo hello from cross-os darwin, I have arguments:",
49+
"*": "echo hello from cross-os unix, I have arguments:"
4650
},
4751
"sixth": "echo i should not been seen",
4852
"sixth-with-params": "echo i should not been seen and niether the following arguments:",
4953
"foo": {
5054
"win32": "echo bar from win32",
5155
"linux": "echo bar from linux",
52-
"darwin": "echo bar from darwin"
56+
"darwin": "echo bar from darwin",
57+
"darwin,freebsd,linux,sunos": "echo bar from unix"
5358
},
5459
"foo-with-params": {
5560
"win32": "echo bar from win32, I have arguments:",
5661
"linux": "echo bar from linux, I have arguments:",
57-
"darwin": "echo bar from darwin, I have arguments:"
62+
"darwin": "echo bar from darwin, I have arguments:",
63+
"darwin,freebsd,linux,sunos": "echo bar from unix, I have arguments:"
5864
}
5965
}
6066
}

test/test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,32 @@ describe('Loader', () => {
261261
})
262262
})
263263

264+
it('should run the correct script for wildcard "*" on Unix platforms', () => {
265+
if (platform !== 'win32') {
266+
const stdout = execSync(`node ${cross} first`)
267+
expect(stdout.toString()).to.match(/hello from unix/)
268+
}
269+
})
270+
271+
it('should run the correct script for wildcard "*" on Unix platforms and pass parameters', () => {
272+
if (platform !== 'win32') {
273+
const stdout = execSync(`node ${cross} first-with-params -- First Second Third`)
274+
expect(stdout.toString()).to.match(/hello from unix, I have arguments: First Second Third/)
275+
}
276+
})
277+
278+
it('should run the correct script for comma-separated values on Unix platforms', () => {
279+
if (platform !== 'win32') {
280+
const stdout = execSync(`node ${cross} foo`)
281+
expect(stdout.toString()).to.match(/bar from unix/)
282+
}
283+
})
284+
285+
it('should run the correct script for comma-separated values on Unix platforms and pass parameters', () => {
286+
if (platform !== 'win32') {
287+
const stdout = execSync(`node ${cross} foo-with-params -- First Second Third`)
288+
expect(stdout.toString()).to.match(/bar from unix, I have arguments: First Second Third/)
289+
}
290+
})
291+
264292
})

0 commit comments

Comments
 (0)