forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
111 lines (100 loc) · 2.48 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// @ts-check
import fs from 'node:fs'
import pico from 'picocolors'
import { createRequire } from 'node:module'
import { spawn } from 'node:child_process'
const require = createRequire(import.meta.url)
export const targets = fs.readdirSync('packages').filter(f => {
if (
!fs.statSync(`packages/${f}`).isDirectory() ||
!fs.existsSync(`packages/${f}/package.json`)
) {
return false
}
const pkg = require(`../packages/${f}/package.json`)
if (pkg.private && !pkg.buildOptions) {
return false
}
return true
})
/**
*
* @param {ReadonlyArray<string>} partialTargets
* @param {boolean | undefined} includeAllMatching
*/
export function fuzzyMatchTarget(partialTargets, includeAllMatching) {
/** @type {Array<string>} */
const matched = []
partialTargets.forEach(partialTarget => {
for (const target of targets) {
if (target.match(partialTarget)) {
matched.push(target)
if (!includeAllMatching) {
break
}
}
}
})
if (matched.length) {
return matched
} else {
console.log()
console.error(
` ${pico.white(pico.bgRed(' ERROR '))} ${pico.red(
`Target ${pico.underline(partialTargets.toString())} not found!`,
)}`,
)
console.log()
process.exit(1)
}
}
/**
* @param {string} command
* @param {ReadonlyArray<string>} args
* @param {object} [options]
*/
export async function exec(command, args, options) {
return new Promise((resolve, reject) => {
const _process = spawn(command, args, {
stdio: [
'ignore', // stdin
'pipe', // stdout
'pipe', // stderr
],
...options,
shell: process.platform === 'win32',
})
/**
* @type {Buffer[]}
*/
const stderrChunks = []
/**
* @type {Buffer[]}
*/
const stdoutChunks = []
_process.stderr?.on('data', chunk => {
stderrChunks.push(chunk)
})
_process.stdout?.on('data', chunk => {
stdoutChunks.push(chunk)
})
_process.on('error', error => {
reject(error)
})
_process.on('exit', code => {
const ok = code === 0
const stderr = Buffer.concat(stderrChunks).toString().trim()
const stdout = Buffer.concat(stdoutChunks).toString().trim()
if (ok) {
const result = { ok, code, stderr, stdout }
resolve(result)
} else {
reject(
new Error(
`Failed to execute command: ${command} ${args.join(' ')}: ${stderr}`,
),
)
}
})
})
}