Fetch the repository succeeded.
#!/usr/bin/env node
import * as path from 'node:path'
import { parseArgs } from 'node:util'
import prompts from 'prompts'
import { red, green, bold, blue } from 'kolorist'
import * as banners from './utils/banners'
import getCommand from './utils/getCommand'
import { downloadTemplate } from "./utils/download"
// 初始化
async function init() {
console.log()
// 判断当前node版本是否支持isTTY属性和getColorDepth方法,支持则使用渐变色,否则使用默认色
console.log(
process.stdout.isTTY && process.stdout.getColorDepth() > 8
? banners.gradientBanner
: banners.defaultBanner
)
console.log()
const cwd = process.cwd() // 当前运行目录
// process.argv 返回数组,第一个参数是Node.js的可执行文件名称,第二个参数是当前执行的js文件路径,之后是命令行参数
const args = process.argv.slice(2)
// alias is not supported by parseArgs
const options = {
typescript: { type: 'boolean' },
ts: { type: 'boolean' },
'with-tests': { type: 'boolean' },
tests: { type: 'boolean' }
} as const
const { positionals } = parseArgs({
args,
options,
strict: false
})
let targetDir = positionals[0]
const defaultProjectName = !targetDir ? 'qt-project' : targetDir
let result: {
projectName?: string
packageName?: string
bumoyuOptions?: string[]
needsCli?: false | 'qtAdmin' | 'chromeExtension' | 'bumoyu'
} = {}
try {
result = await prompts(
[
{
name: 'needsCli',
type: 'select',
hint: '- 使用箭头切换按Enter确认。',
message: '选择需要下载的脚手架',
initial: 0,
choices: () => [
{
title: 'QT-Admin',
description: '基于Vue3、Typescript、ElementPlus的后台管理系统',
value: 'qtAdmin'
},
{
title: 'QT-Chrome-Extension',
description: '基于Vue3的chrome插件开发框架',
value: 'chromeExtension'
},
{
title: 'Bumoyu-Project',
description: '个性化定制工作台项目(3个工程)',
value: 'bumoyu'
}
]
},
{
name: 'bumoyuOptions',
type: prev => prev === 'bumoyu' ? 'multiselect' : null,
message: '选择要下载的Bumoyu工程',
choices: [
{
title: 'bumoyu-workbench',
description: '基于Vue3、Typescript、AntDesignVue的前端工程',
value: 'bumoyuWorkbench',
selected: true
},
{
title: 'bumoyu-nest',
description: '基于NestJs的后端工程',
value: 'bumoyuNest',
selected: true
},
{
title: 'bumoyu-chrome-extension',
description: '基于Chrome Manifest V3的chrome插件工程',
value: 'bumoyuChromeExtension',
selected: true
},
],
hint: '- 使用空格选择,按Enter确认。'
},
{
name: 'projectName',
type: 'text',
message: '请输入项目名称:',
initial: defaultProjectName,
onState: (state) => (targetDir = String(state.value).trim() || defaultProjectName)
}
],
{
onCancel: () => {
throw new Error(red('✖') + ` 操作取消`)
}
}
)
} catch (cancelled) {
console.log(cancelled.message)
process.exit(1)
}
// `initial` won't take effect if the prompt type is null
// so we still have to assign the default values here
const {
needsCli,
projectName,
packageName = projectName ?? defaultProjectName,
bumoyuOptions = []
} = result
const needsQtAdmin = needsCli === 'qtAdmin'
const needsChromeExtension = needsCli === 'chromeExtension'
const needsBumoyu = needsCli === 'bumoyu'
const root = path.join(cwd, targetDir)
// 根据选项生成模板
try {
const qtAdminGitUrl = 'https://gitee.com/qt-admin/basic.git'
const qtChromeExtensionUrl = 'https://gitee.com/partiny/qt-chrome-extension.git'
const bumoyuWorkbenchUrl = 'https://gitee.com/partiny/bumoyu-workbench.git'
const bumoyuNestUrl = 'https://gitee.com/partiny/bumoyu-nest.git'
const bumoyuChromeExtensionUrl = 'https://gitee.com/partiny/bumoyu-chrome-extension.git'
if (needsQtAdmin) {
await downloadTemplate(qtAdminGitUrl,`./${packageName}`)
} else if (needsChromeExtension) {
await downloadTemplate(qtChromeExtensionUrl,`./${packageName}`)
} else if (needsBumoyu) {
if (bumoyuOptions?.includes('bumoyuWorkbench')) {
await downloadTemplate(bumoyuWorkbenchUrl,`./${packageName}/bumoyu-workbench`)
}
if (bumoyuOptions?.includes('bumoyuNest')) {
await downloadTemplate(bumoyuNestUrl,`./${packageName}/bumoyu-nest`)
}
if (bumoyuOptions?.includes('bumoyuChromeExtension')) {
await downloadTemplate(bumoyuChromeExtensionUrl,`./${packageName}/bumoyu-chrome-extension`)
}
}
} catch (error) {
console.error(error)
}
// Instructions:
// Supported package managers: pnpm > yarn > bun > npm
const userAgent = process.env.npm_config_user_agent ?? ''
const packageManager = /pnpm/.test(userAgent)
? 'pnpm'
: /yarn/.test(userAgent)
? 'yarn'
: /bun/.test(userAgent)
? 'bun'
: 'npm'
console.log(`\n项目初始化完成,可执行以下命令:\n`)
if (root !== cwd) {
const cdProjectName = path.relative(cwd, root)
console.log(
` ${bold(green(`cd ${cdProjectName.includes(' ') ? `"${cdProjectName}"` : cdProjectName}`))}`
)
}
// 选中bumoyu-project时特殊处理
if (needsBumoyu) {
console.log()
console.log(`不摸鱼项目包含三个工程,具体配置可查看文档 ${blue('https://bumoyu.cn/docs/')}`)
} else {
console.log(` ${bold(green(getCommand(packageManager, 'install')))}`)
console.log(` ${bold(green(getCommand(packageManager, 'dev')))}`)
}
console.log()
}
init().catch((e) => {
console.error(e)
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。