vue3-core/scripts/utils.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

102 lines
2.2 KiB
JavaScript
Raw Normal View History

// @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'
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 => {
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
/**
*
* @param {ReadonlyArray<string>} partialTargets
* @param {boolean | undefined} includeAllMatching
*/
2023-01-26 14:24:49 +08:00
export function fuzzyMatchTarget(partialTargets, includeAllMatching) {
/** @type {Array<string>} */
2018-09-19 23:35:38 +08:00
const matched = []
2019-11-05 03:28:11 +08:00
partialTargets.forEach(partialTarget => {
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
}
})
2018-09-19 23:35:38 +08:00
if (matched.length) {
return matched
} else {
console.log()
console.error(
2023-10-20 10:23:18 +08:00
` ${pico.white(pico.bgRed(' ERROR '))} ${pico.red(
`Target ${pico.underline(partialTargets.toString())} not found!`,
)}`,
)
console.log()
process.exit(1)
2018-09-19 23:35:38 +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)
})
})
}