Skip to content

Commit 6aab0fa

Browse files
committed
added option to run from 'cross-os' section in the package.json
1 parent 10e62c7 commit 6aab0fa

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

README.md

+21-6
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ Add scripts to your package.json like so:
2020

2121
```json
2222
"scripts": {
23-
"foo": "cross-os fight",
24-
"fight": {
25-
"darwin": "echo 'i make the product that the artist chooses' && echo 'and the GUI that Melinda uses'",
26-
"win32": "echo 'fine, you wanna be like that, die then.'",
27-
"linux": "echo 'i stomp on a mac and a PC too, im a Linux bitch, i thought you both GNU",
23+
"foo": "cross-os bar",
24+
"bar": {
25+
"darwin": "echo 'i will only run on Mac'",
26+
"win32": "echo 'i will only run on Windows'",
27+
"linux": "echo 'i will only run on Linux'"
2828
}
2929
}
3030
```
@@ -33,6 +33,21 @@ And call it like:
3333
npm run foo
3434
```
3535

36+
Alternatively you can also specify scripts on its own section in your `package.json`
37+
38+
```json
39+
"scripts": {
40+
"foo": "cross-os bar"
41+
}
42+
"cross-os": {
43+
"bar": {
44+
"darwin": "echo 'i will only run on Mac'",
45+
"win32": "echo 'i will only run on Windows'",
46+
"linux": "echo 'i will only run on Linux'"
47+
}
48+
}
49+
```
50+
3651
## License
3752

38-
[MIT](LICENSE)
53+
[MIT](LICENSE) © 2017 [Rafael Milewski](https://github.com/milewski)

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cross-os",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "Allow to add OS-specific scripts in your package.json!",
55
"main": "source/index.js",
66
"bin": "source/index.js",
@@ -34,14 +34,13 @@
3434
"source/*.js",
3535
"test"
3636
],
37-
"dependencies": {
38-
},
3937
"scripts": {
4038
"prepublish": "npm run build && npm test",
4139
"build": "tsc",
4240
"pretest": "npm run build",
4341
"test": "mocha --no-timeouts"
4442
},
43+
"dependencies": {},
4544
"devDependencies": {
4645
"@types/mocha": "^2.2.40",
4746
"@types/node": "^7.0.12",

source/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ const pipeline = new Promise(resolve => {
1515
/**
1616
* Check if the desired script exists
1717
*/
18-
let script = process.argv.pop();
18+
let script = process.argv.pop(),
19+
property = 'scripts';
20+
21+
if (config[property] && !config[property][script]) {
22+
property = 'cross-os'
23+
}
1924

2025
try {
21-
return Promise.resolve(config['scripts'][script][process.platform]);
26+
return Promise.resolve(config[property][script][process.platform]);
2227
} catch (e) {
2328
throw script
2429
}

test/package.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
},
88
"second": {},
99
"third": "echo 'it is working just fine'",
10-
"fourth": "node ../source/index.js first"
10+
"fourth": "node ../source/index.js first",
11+
"sixth": "node ../source/index.js first",
12+
"seventh": "node ../source/index.js fifth"
13+
},
14+
"cross-os": {
15+
"fifth": {
16+
"win32": "echo hello from cross-os win32",
17+
"linux": "echo hello from cross-os linux",
18+
"darwin": "echo hello from cross-os darwin"
19+
},
20+
"sixth": "echo i should not been seen"
1121
}
1222
}

test/test.ts

+45-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('Loader', () => {
4040
})
4141

4242
child.on('exit', code => {
43-
expect(output).to.match(/it is working just fine/)
43+
expect(output.trim()).to.match(/it is working just fine/)
4444
expect(code).to.be(0)
4545
done()
4646
})
@@ -58,7 +58,50 @@ describe('Loader', () => {
5858
})
5959

6060
child.on('exit', code => {
61-
expect(output).to.match(new RegExp(`hello from ${process.platform}`))
61+
expect(output.trim()).to.match(new RegExp(`hello from ${process.platform}`))
62+
expect(code).to.be(0)
63+
done()
64+
})
65+
66+
})
67+
68+
it('should run scripts defined in cross-os attributes', () => {
69+
70+
const stdout = execSync(`node ${cross} fifth`)
71+
expect(stdout.toString()).to.match(new RegExp(`hello from cross-os ${process.platform}`))
72+
73+
})
74+
75+
it('should run scripts defined in cross-os attributes (called from npm scripts)', done => {
76+
77+
const child = exec('npm run seventh --silent')
78+
79+
let output = '';
80+
81+
child.stdout.on('data', (buffer: Buffer) => {
82+
output += buffer.toString('utf-8')
83+
})
84+
85+
child.on('exit', code => {
86+
expect(output.trim()).to.match(new RegExp(`hello from cross-os ${process.platform}`))
87+
expect(code).to.be(0)
88+
done()
89+
})
90+
91+
})
92+
93+
it('scripts should have precedence over cross-os attribute', done => {
94+
95+
const child = exec('npm run sixth --silent')
96+
97+
let output = '';
98+
99+
child.stdout.on('data', (buffer: Buffer) => {
100+
output += buffer.toString('utf-8')
101+
})
102+
103+
child.on('exit', code => {
104+
expect(output.trim()).to.match(new RegExp(`hello from ${process.platform}`))
62105
expect(code).to.be(0)
63106
done()
64107
})

0 commit comments

Comments
 (0)