Skip to content

Commit e093e94

Browse files
committed
feat(scripts): run scripts and bins
1 parent 9cc9d7a commit e093e94

5 files changed

Lines changed: 73 additions & 10 deletions

File tree

packages/scripts/src/run.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ import {
77
} from 'argue-cli'
88
import {
99
detectPackageManager,
10+
readPackageJsonUpward,
1011
readScripts,
1112
runParallel,
1213
runSerial
1314
} from './utils/index.js'
1415

1516
const pm = detectPackageManager()
17+
const pkg = await readPackageJsonUpward()
1618
const { parallel } = readOptions(
1719
option(alias('parallel', 'p'), Boolean),
1820
)
1921
const scripts = readScripts()
2022

2123
if (parallel) {
22-
process.exit(await runParallel(pm, scripts))
24+
process.exit(await runParallel(pm, scripts, pkg))
2325
} else {
24-
process.exit(await runSerial(pm, scripts))
26+
process.exit(await runSerial(pm, scripts, pkg))
2527
}

packages/scripts/src/utils/args.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ export function getRunArgs(pm, args) {
3333
]
3434
}
3535

36+
/**
37+
* Get args to run command.
38+
* @param {string} pm - Package manager name.
39+
* @param {string[]} args - Command args.
40+
* @param {{ scripts?: Record<string, string> }} pkg - package.json
41+
* @returns {[string, string[]]} - Bin and commands.
42+
*/
43+
export function getArgs(pm, args, pkg) {
44+
if (pkg.scripts && (args[0] in pkg.scripts)) {
45+
return [pm, getRunArgs(pm, args)]
46+
}
47+
48+
const [bin, ...restArgs] = args
49+
50+
return [bin, restArgs]
51+
}
52+
3653
/**
3754
* Read scripts from argv to run with package manager.
3855
* @returns Package manager scripts to run.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { resolve, dirname } from 'path'
2+
import { constants, promises } from 'fs'
3+
4+
const { access, readFile } = promises
5+
6+
/**
7+
* Find file starts from cwd ups to root.
8+
* @param {string} filename
9+
* @param {string} [dir]
10+
* @returns {Promise<string>} Path to file.
11+
*/
12+
export async function findFileUpward(filename, dir = process.cwd()) {
13+
const file = resolve(dir, filename)
14+
15+
try {
16+
await access(file, constants.R_OK)
17+
18+
return file
19+
} catch (err) {
20+
if (dir === '.') {
21+
throw err
22+
}
23+
24+
return findFileUpward(filename, dirname(dir))
25+
}
26+
}
27+
28+
/**
29+
* Find and read package.json file.
30+
* @returns {Promise<object>} package.json
31+
*/
32+
export async function readPackageJsonUpward() {
33+
try {
34+
const path = await findFileUpward('package.json')
35+
const pkg = JSON.parse(await readFile(path, 'utf8'))
36+
37+
return pkg
38+
} catch (err) {
39+
throw new Error(`Can't find package.json`)
40+
}
41+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './args.js'
2+
export * from './files.js'
23
export * from './spawn.js'
34
export * from './run.js'

packages/scripts/src/utils/run.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { cpus } from 'os'
22
import pLimit from 'p-limit'
33
import { spawn } from './spawn.js'
4-
import { getRunArgs } from './args.js'
4+
import { getArgs } from './args.js'
55

66
/**
77
* Run package manager scripts serial.
88
* @param {string} pm
99
* @param {string[][]} scripts
10+
* @param {{ scripts?: Record<string, string> }} pkg - package.json
1011
* @returns Exit code
1112
*/
12-
export async function runSerial(pm, scripts) {
13-
const pmScripts = scripts.map(script => getRunArgs(pm, script))
13+
export async function runSerial(pm, scripts, pkg) {
14+
const cmds = scripts.map(script => getArgs(pm, script, pkg))
1415
let exitCode = 0
1516

16-
for (const pmScript of pmScripts) {
17-
exitCode = exitCode || (await spawn(pm, pmScript)).exitCode
17+
for (const [bin, args] of cmds) {
18+
exitCode = exitCode || (await spawn(bin, args)).exitCode
1819
}
1920

2021
return exitCode
@@ -24,12 +25,13 @@ export async function runSerial(pm, scripts) {
2425
* Run package manager scripts parallel.
2526
* @param {string} pm
2627
* @param {string[][]} scripts
28+
* @param {{ scripts?: Record<string, string> }} pkg - package.json
2729
* @returns Exit code.
2830
*/
29-
export async function runParallel(pm, scripts) {
31+
export async function runParallel(pm, scripts, pkg) {
3032
const limit = pLimit(cpus().length)
31-
const pmScripts = scripts.map(script => getRunArgs(pm, script))
32-
const tasks = pmScripts.map(pmScript => limit(() => spawn(pm, pmScript, false)))
33+
const cmds = scripts.map(script => getArgs(pm, script, pkg))
34+
const tasks = cmds.map(([bin, args]) => limit(() => spawn(bin, args, false)))
3335
let exitCode = 0
3436
/** @type {{ exitCode: number, output?: string | Error }} */
3537
let result

0 commit comments

Comments
 (0)