import fs from 'fs' import path from 'path' import { fileURLToPath } from 'url' import { exec } from 'child_process' import { Command } from 'commander' import { input, select, confirm } from '@inquirer/prompts' import chalk from 'chalk' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) function processArgv() { const program = new Command() // 解析参数 program .option('-t, --type ', 'Building type') .option('-d, --desc ', 'version description') .option('-v, --version ', 'version code') .action(async (options) => { let { type, desc, version } = options if (!type) type = await selectBuildingType() console.log(chalk.hex('#76a9d8').bold(type)) if (type === 'preview') { build(type) return } if (!version) version = await confirmVersion() let short_version = version.split('.').pop() console.log(chalk.hex('#76a9d8').bold(version)) if (!desc) desc = await input({ message: '请输入描述信息' }) if (!desc) desc = `版本 - ${version}` console.log(chalk.hex('#76a9d8').bold(desc)) fs.writeFileSync( path.join(__dirname, '../config/version.json'), JSON.stringify({ version, short_version, desc }), { encoding: 'utf-8' } ) build(type) }) program.parse() } async function selectBuildingType() { return await select({ message: 'Select a building type', choices: [ { name: 'publish', value: 'publish', description: 'build and upload', }, { name: 'preview', value: 'preview', description: 'build and preview', } ], }) } async function confirmVersion() { let curV = fs.readFileSync(path.join(__dirname, '../config/version.json'), { encoding: 'utf-8'}) curV = JSON.parse(curV) const message = `当前参数为${curV.version},是采用否自增版本号?` const res = await confirm({ message }) let [main, mid ,min] = curV.version.split('.') if (!res){ min = await input({ message: '请输入版本号' }) return `${main}.${mid}.${min}` } min++ if (min === 50) { min = 0 mid++ } if (mid === 50) { mid === 0 main++ } return `${main}.${mid}.${min}` } function build(type) { let command = 'rm -rf dist && yarn upload' if (type === 'preview') command = 'rm -rf dist && yarn preview' const script = exec(command) // 将打包信息,打印到控制台 script.stdout.on('data', (chunk) => { console.log(chalk.hex('#f9d472').bold(chunk)) }) } processArgv()