Skip to content

Commit 445d0be

Browse files
author
Kent C. Dodds
authored
fix: add nps-utils to docs and package-scripts.js (#122)
1 parent bc19c08 commit 445d0be

File tree

5 files changed

+446
-316
lines changed

5 files changed

+446
-316
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ All the benefits of npm scripts without the cost of a bloated package.json and l
2323
## Quick Video Intro :tv:
2424

2525
<a href="http://kcd.im/nps-video" title="Pull out npm scripts into another file with nps">
26-
<img src="other/video-screenshot.png" alt="Video Screenshot" title="Video Screenshot" width="700" />
26+
<img src="https://github.com/kentcdodds/nps/raw/master/other/video-screenshot.png" alt="Video Screenshot" title="Video Screenshot" width="700" />
2727
</a>
2828

2929
[Pull out npm scripts into another file with nps][video] by [Elijah Manor](https://github.com/elijahmanor) (5:53)
@@ -41,6 +41,8 @@ this file is a JavaScript file, you can do a lot more with your project scripts.
4141
`package-scripts.js` file:
4242

4343
```javascript
44+
const npsUtils = require('nps-utils') // not required, but handy!
45+
4446
module.exports = {
4547
scripts: {
4648
default: 'node index.js',
@@ -58,9 +60,8 @@ module.exports = {
5860
default: 'webpack',
5961
prod: 'webpack -p',
6062
},
61-
// learn more about concurrently here: https://npm.im/concurrently
62-
validate: 'concurrently "nps lint" "nps test" "nps build"',
63-
// concurrently script too verbose for your liking? Check out other/EXAMPLES.md!
63+
// learn more about npsUtils here: https://npm.im/nps-utils
64+
validate: npsUtils.concurrently.nps('lint', 'test', 'build'),
6465
},
6566
}
6667
```
@@ -407,6 +408,11 @@ This was inspired by [a tweet][tweet] by [@sindresorhus][sindre].
407408
Big thank you to [@tmpvar][tmpvar] for giving up the name `nps`! The original `nps` is now
408409
called [`npmsearch-cli`](https://www.npmjs.com/package/npmsearch-cli).
409410

411+
## Related Packages
412+
413+
- [`nps-utils`][nps-utils] - a collection of utilities to make cross-platform scripts and many other patterns
414+
(like running concurrent/parallel scripts)
415+
410416
## Other Solutions
411417

412418
- [scripty][scripty] has a solution for this problem as well. The reason I didn't go with that though is you still need
@@ -479,3 +485,4 @@ MIT
479485
[npm scripts]: https://docs.npmjs.com/misc/scripts
480486
[video]: http://kcd.im/nps-video
481487
[releases]: https://github.com/kentcdodds/nps/releases/tag/v5.0.0
488+
[nps-utils]: https://github.com/kentcdodds/nps-utils

other/EXAMPLES.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# package-scripts examples
22

3+
## nps-utils
4+
5+
Many common patterns in `nps` can be accomplished with the
6+
[`nps-utils`](https://github.com/kentcdodds/nps-utils) package. Definitely
7+
recommended to check it out!
8+
39
## Links to projects
410

511
Examples of how people use `nps`:
@@ -53,26 +59,31 @@ get the idea 😄. This is a pretty nice win over traditional npm scripts 👍
5359
### parallel scripts
5460

5561
Often, scripts can run concurrently because they are not interdependent. We recommend
56-
[`concurrently`](http://npm.im/concurrently) for this:
62+
[`nps-utils`](http://npm.im/nps-utils) which uses `concurrently` for this:
5763

5864
```javascript
65+
const npsUtils = require('nps-utils')
66+
5967
module.exports = {
6068
scripts: {
61-
validate: concurrent([
69+
sayThings: npsUtils.concurrent({
70+
hi: {script: 'echo hi'},
71+
hey: {script: 'echo hey', color: 'blue.bgGreen.dim'},
72+
hello: 'echo hello there',
73+
}),
74+
validate: npsUtils.concurrent.nps(
6275
'build',
6376
'lint',
6477
'test',
6578
'order.sandwich',
66-
]),
79+
),
80+
build: 'webpack',
81+
lint: 'eslint .',
82+
test: 'jest',
83+
order: {sandwich: 'makemeasandwich'}
6784
// etc...
6885
}
6986
}
70-
71-
function concurrent(scripts) {
72-
const names = scripts.join(',')
73-
const quotedScripts = `"nps ${scripts.join('" "nps ')}"`
74-
return `concurrently --prefix "[{name}]" --names "${names}" ${quotedScripts}`
75-
}
7687
```
7788

7889
## Instructions

package-scripts.js

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
const {series, concurrent, rimraf} = require('nps-utils')
2+
13
const transpile = 'babel --copy-files --out-dir dist --ignore *.test.js,fixtures src'
2-
const cleanDist = 'rimraf dist'
4+
const cleanDist = rimraf('dist')
35

46
module.exports = {
57
scripts: {
@@ -18,14 +20,14 @@ module.exports = {
1820
build: {
1921
default: {
2022
description: 'deletes the `dist` directory and transpiles all relevant `src` to the `dist`',
21-
script: [cleanDist, transpile].join('&&'),
23+
script: series(cleanDist, transpile),
2224
},
2325
watch: {
24-
script: [cleanDist, `${transpile} --watch`].join('&&'),
26+
script: series(cleanDist, `${transpile} --watch`),
2527
},
2628
andValidate: {
2729
description: 'Runs the normal build first, then validates the CLI',
28-
script: 'nps build && nps test.cli',
30+
script: series.nps('build', 'test.cli'),
2931
},
3032
},
3133
lint: {
@@ -38,26 +40,16 @@ module.exports = {
3840
},
3941
release: {
4042
description: 'We automate releases with semantic-release. This should only be run on travis',
41-
script: 'semantic-release pre && npm publish && semantic-release post',
43+
script: series(
44+
'semantic-release pre',
45+
'npm publish',
46+
'semantic-release post'
47+
),
4248
},
4349
validate: {
4450
description: 'This runs several scripts to make sure things look good before committing or on clean install',
45-
script: concurrent({
46-
'build-and-validate': {
47-
script: 'nps build.andValidate',
48-
color: 'bgBlue.bold',
49-
},
50-
test: {
51-
script: 'nps test',
52-
color: 'bgMagenta.bold',
53-
},
54-
'lint-staged': {
55-
script: 'nps lintStaged',
56-
color: 'bgGreen.bold',
57-
},
58-
}),
51+
script: concurrent.nps('test', 'build.andValidate'),
5952
},
60-
lintStaged: 'lint-staged',
6153
format: {
6254
description: 'auto-formats the code',
6355
script: 'prettier-eslint --write "src/**/*.js"',
@@ -76,35 +68,6 @@ module.exports = {
7668
},
7769
}
7870

79-
function concurrent(scripts) {
80-
const {
81-
colors,
82-
scripts: quotedScripts,
83-
names,
84-
} = Object.keys(scripts).reduce(reduceScripts, {
85-
colors: [],
86-
scripts: [],
87-
names: [],
88-
})
89-
const flags = [
90-
// https://github.com/kimmobrunfeldt/concurrently/issues/91
91-
// '--kill-others',
92-
`--prefix-colors "${colors.join(',')}"`,
93-
'--prefix "[{name}]"',
94-
`--names "${names.join(',')}"`,
95-
quotedScripts.join(' '),
96-
]
97-
return `concurrently ${flags.join(' ')}`
98-
99-
function reduceScripts(accumulator, scriptName) {
100-
const {script, color} = scripts[scriptName]
101-
accumulator.names.push(scriptName)
102-
accumulator.colors.push(color)
103-
accumulator.scripts.push(`"${script}"`)
104-
return accumulator
105-
}
106-
}
107-
10871
// this is not transpiled
10972
/*
11073
eslint

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"test": "nps test",
99
"localstart": "npm start build && node ./dist/bin/nps.js",
1010
"commitmsg": "opt --in commit-msg --exec \"validate-commit-msg\"",
11-
"precommit": "opt --in pre-commit --exec \"npm start validate\""
11+
"precommit": "opt --in pre-commit --exec \"lint-staged && npm start validate\""
1212
},
1313
"bin": {
1414
"nps": "./dist/bin/nps.js"
@@ -42,10 +42,8 @@
4242
"babel-preset-env": "^1.1.8",
4343
"babel-preset-stage-2": "^6.22.0",
4444
"babel-register": "^6.23.0",
45-
"cli-tester": "^2.0.0",
4645
"codecov": "^1.0.1",
4746
"commitizen": "^2.9.6",
48-
"concurrently": "^3.3.0",
4947
"cross-env": "^3.1.4",
5048
"cz-conventional-changelog": "^1.2.0",
5149
"eslint": "^3.15.0",
@@ -54,9 +52,9 @@
5452
"jest-cli": "^18.1.0",
5553
"lint-staged": "^3.3.0",
5654
"nps": "*",
55+
"nps-utils": "^1.1.0",
5756
"opt-cli": "^1.5.1",
5857
"prettier-eslint-cli": "^3.0.0",
59-
"rimraf": "^2.6.0",
6058
"semantic-release": "^6.3.6",
6159
"sinon": "^1.17.7",
6260
"validate-commit-msg": "^2.11.1"

0 commit comments

Comments
 (0)