2023-02-03 18:24:03 +08:00
|
|
|
// @ts-check
|
2023-01-26 14:24:49 +08:00
|
|
|
import fs from 'node:fs'
|
2023-10-20 10:23:18 +08:00
|
|
|
import pico from 'picocolors'
|
2023-01-26 14:24:49 +08:00
|
|
|
import { createRequire } from 'node:module'
|
2024-07-05 11:51:05 +08:00
|
|
|
import { spawn } from 'node:child_process'
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2023-01-26 14:24:49 +08:00
|
|
|
const require = createRequire(import.meta.url)
|
|
|
|
|
|
|
|
export const targets = fs.readdirSync('packages').filter(f => {
|
2024-01-09 15:30:08 +08:00
|
|
|
if (
|
|
|
|
!fs.statSync(`packages/${f}`).isDirectory() ||
|
|
|
|
!fs.existsSync(`packages/${f}/package.json`)
|
|
|
|
) {
|
2018-10-17 05:41:59 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
const pkg = require(`../packages/${f}/package.json`)
|
2019-10-05 01:08:06 +08:00
|
|
|
if (pkg.private && !pkg.buildOptions) {
|
2018-10-17 05:41:59 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2023-01-26 14:24:49 +08:00
|
|
|
})
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2023-11-30 14:17:51 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {ReadonlyArray<string>} partialTargets
|
|
|
|
* @param {boolean | undefined} includeAllMatching
|
|
|
|
*/
|
2023-01-26 14:24:49 +08:00
|
|
|
export function fuzzyMatchTarget(partialTargets, includeAllMatching) {
|
2023-11-30 14:17:51 +08:00
|
|
|
/** @type {Array<string>} */
|
2018-09-19 23:35:38 +08:00
|
|
|
const matched = []
|
2019-11-05 03:28:11 +08:00
|
|
|
partialTargets.forEach(partialTarget => {
|
2019-10-02 23:19:30 +08:00
|
|
|
for (const target of targets) {
|
|
|
|
if (target.match(partialTarget)) {
|
|
|
|
matched.push(target)
|
|
|
|
if (!includeAllMatching) {
|
|
|
|
break
|
|
|
|
}
|
2018-10-23 23:58:37 +08:00
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
2019-10-02 23:19:30 +08:00
|
|
|
})
|
2018-09-19 23:35:38 +08:00
|
|
|
if (matched.length) {
|
|
|
|
return matched
|
|
|
|
} else {
|
2019-10-16 05:13:35 +08:00
|
|
|
console.log()
|
|
|
|
console.error(
|
2023-10-20 10:23:18 +08:00
|
|
|
` ${pico.white(pico.bgRed(' ERROR '))} ${pico.red(
|
2023-11-30 14:17:51 +08:00
|
|
|
`Target ${pico.underline(partialTargets.toString())} not found!`,
|
2019-10-16 05:13:35 +08:00
|
|
|
)}`,
|
|
|
|
)
|
|
|
|
console.log()
|
|
|
|
|
|
|
|
process.exit(1)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-05 11:51:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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,
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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()
|
|
|
|
const result = { ok, code, stderr, stdout }
|
|
|
|
resolve(result)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|