Bilibili-Evolved/registry/webpack/build.ts
ZiuChen bd3b452631 build: 缩短构建组件时CLI提示长度
- 之前展示的是完整路径 当路径过长时会导致提示信息过长 选取体验很差
- 做统一截取处理 只展示能够区分当前组件的部分
2023-05-10 17:13:34 +08:00

42 lines
1.3 KiB
TypeScript

import path from 'path'
import glob from 'glob'
import { buildByEntry } from './config'
const shorten = (p: string) => path.dirname(p).replace('./registry/lib/', '')
export const builders = Object.fromEntries(
['component', 'plugin', 'doc'].map(type => {
const src = `./registry/lib/${type}s/`
return [
type,
async ({ buildAll = false } = {}) => {
const entries = glob.sync(`${src}**/index.ts`).map(entry => ({
name: shorten(entry),
value: entry,
}))
if (buildAll) {
console.log(`[build all] discovered ${entries.length} ${type}s`)
return entries.map(({ value }) => buildByEntry({ src, type, entry: value }))
}
let entry: string
if (entries.length > 1) {
const { default: AutoComplete } = await import('enquirer/lib/prompts/autocomplete')
console.log(AutoComplete)
const prompt = new AutoComplete({
name: 'path',
message: 'Select build target',
choices: entries,
})
entry = await prompt.run()
} else {
;[{ value: entry }] = entries
}
console.log(`Build target · ${entry}`)
return [buildByEntry({ src, type, entry })]
},
]
}),
)