Skip to content

Commit 06ead41

Browse files
authored
Add a command for running all the packages devs in parallel (#283)
i still prefer the pnpm PR
1 parent 25e4703 commit 06ead41

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ You can then build all the packages:
1919
npm run build --workspaces=true
2020
```
2121

22+
When working on the packages, it can be helpful to have them rebuild on change:
23+
24+
```sh
25+
npm run dev
26+
```
27+
2228
## Packages
2329

2430
| Name | Description | Version |

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
],
2222
"version": "0.0.0",
2323
"scripts": {
24+
"dev": "node scripts/parallel.js npm run dev",
2425
"prepare": "husky",
2526
"lint": "eslint --cache",
2627
"lint:fix": "npm run lint -- --fix",
@@ -37,6 +38,7 @@
3738
"eslint": "^9.0.0",
3839
"eslint-config-prettier": "^10.0.0",
3940
"eslint-plugin-n": "17.19.0",
41+
"execa": "^8.0.1",
4042
"husky": "^9.0.0",
4143
"prettier": "^3.0.0",
4244
"typescript-eslint": "^8.33.0"

scripts/parallel.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { execaCommand as run } from 'execa'
2+
import pkg from '../package.json' with { type: 'json' }
3+
import { createInterface } from 'node:readline'
4+
5+
const args = process.argv.slice(2)
6+
if (!args.length) {
7+
console.error('Usage: npm run parallel <command>')
8+
throw new Error('No command provided')
9+
}
10+
const cmd = args.join(' ')
11+
const FORCE_COLOR = ['0', 'false', ''].includes(/** @type {string} */ (process.env.FORCE_COLOR)) ? undefined : '1'
12+
await Promise.all(
13+
pkg.workspaces.map((cwd, index) => {
14+
const name = cwd.split('/').pop()
15+
const actor = run(cmd, {
16+
cwd,
17+
shell: true,
18+
env: { ...process.env, FORCE_COLOR },
19+
})
20+
if (!actor.stdout || !actor.stderr) {
21+
throw new Error(`No stdout or stderr for ${name} in ${cwd}`)
22+
}
23+
const color = `\x1b[${31 + (index % 6)}m`
24+
const reset = '\x1b[0m'
25+
const prefix = `${color}[${name}]${reset} `
26+
27+
createInterface({ input: actor.stdout }).on('line', (line) => {
28+
process.stdout.write(`${prefix}${line}\n`)
29+
})
30+
createInterface({ input: actor.stderr }).on('line', (line) => {
31+
process.stderr.write(`${prefix}${line}\n`)
32+
})
33+
34+
return actor
35+
}),
36+
)

0 commit comments

Comments
 (0)