index.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import fs from 'fs'
  2. import path from 'path'
  3. import { fileURLToPath } from 'url'
  4. import { exec } from 'child_process'
  5. import { Command } from 'commander'
  6. import { input, select, confirm } from '@inquirer/prompts'
  7. import chalk from 'chalk'
  8. const __filename = fileURLToPath(import.meta.url)
  9. const __dirname = path.dirname(__filename)
  10. function processArgv() {
  11. const program = new Command()
  12. // 解析参数
  13. program
  14. .option('-t, --type <type>', 'Building type')
  15. .option('-d, --desc <char>', 'version description')
  16. .option('-v, --version <char>', 'version code')
  17. .action(async (options) => {
  18. let { type, desc, version } = options
  19. if (!type)
  20. type = await selectBuildingType()
  21. console.log(chalk.hex('#76a9d8').bold(type))
  22. if (type === 'preview') {
  23. build(type)
  24. return
  25. }
  26. if (!version)
  27. version = await confirmVersion()
  28. console.log(chalk.hex('#76a9d8').bold(version))
  29. if (!desc)
  30. desc = await input({ message: '请输入描述信息' })
  31. if (!desc)
  32. desc = `版本 - ${version}`
  33. console.log(chalk.hex('#76a9d8').bold(desc))
  34. fs.writeFileSync(
  35. path.join(__dirname, '../config/version.json'),
  36. JSON.stringify({ version, desc }),
  37. { encoding: 'utf-8' }
  38. )
  39. build(type)
  40. })
  41. program.parse()
  42. }
  43. async function selectBuildingType() {
  44. return await select({
  45. message: 'Select a building type',
  46. choices: [
  47. {
  48. name: 'publish',
  49. value: 'publish',
  50. description: 'build and upload',
  51. },
  52. {
  53. name: 'preview',
  54. value: 'preview',
  55. description: 'build and preview',
  56. }
  57. ],
  58. })
  59. }
  60. async function confirmVersion() {
  61. let curV = fs.readFileSync(path.join(__dirname, '../config/version.json'), { encoding: 'utf-8'})
  62. curV = JSON.parse(curV)
  63. const message = `当前参数为${curV.version},是采用否自增版本号?`
  64. const res = await confirm({ message })
  65. if (!res)
  66. return await input({ message: '请输入版本号' })
  67. let [main, mid ,min] = curV.version.split('.')
  68. min++
  69. if (min === 50) {
  70. min = 0
  71. mid++
  72. }
  73. if (mid === 50) {
  74. mid === 0
  75. main++
  76. }
  77. return `${main}.${mid}.${min}`
  78. }
  79. function build(type) {
  80. let command = 'rm -rf dist && yarn upload'
  81. if (type === 'preview')
  82. command = 'rm -rf dist && yarn preview'
  83. const script = exec(command)
  84. // 将打包信息,打印到控制台
  85. script.stdout.on('data', (chunk) => {
  86. console.log(chalk.hex('#f9d472').bold(chunk))
  87. })
  88. }
  89. processArgv()