index.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. let short_version = version.split('.').pop()
  29. console.log(chalk.hex('#76a9d8').bold(version))
  30. if (!desc)
  31. desc = await input({ message: '请输入描述信息' })
  32. if (!desc)
  33. desc = `版本 - ${version}`
  34. console.log(chalk.hex('#76a9d8').bold(desc))
  35. fs.writeFileSync(
  36. path.join(__dirname, '../config/version.json'),
  37. JSON.stringify({ version, short_version, desc }),
  38. { encoding: 'utf-8' }
  39. )
  40. build(type)
  41. })
  42. program.parse()
  43. }
  44. async function selectBuildingType() {
  45. return await select({
  46. message: 'Select a building type',
  47. choices: [
  48. {
  49. name: 'publish',
  50. value: 'publish',
  51. description: 'build and upload',
  52. },
  53. {
  54. name: 'preview',
  55. value: 'preview',
  56. description: 'build and preview',
  57. }
  58. ],
  59. })
  60. }
  61. async function confirmVersion() {
  62. let curV = fs.readFileSync(path.join(__dirname, '../config/version.json'), { encoding: 'utf-8'})
  63. curV = JSON.parse(curV)
  64. const message = `当前参数为${curV.version},是采用否自增版本号?`
  65. const res = await confirm({ message })
  66. let [main, mid ,min] = curV.version.split('.')
  67. if (!res){
  68. min = await input({ message: '请输入版本号' })
  69. return `${main}.${mid}.${min}`
  70. }
  71. min++
  72. if (min === 50) {
  73. min = 0
  74. mid++
  75. }
  76. if (mid === 50) {
  77. mid === 0
  78. main++
  79. }
  80. return `${main}.${mid}.${min}`
  81. }
  82. function build(type) {
  83. let command = 'rm -rf dist && yarn upload'
  84. if (type === 'preview')
  85. command = 'rm -rf dist && yarn preview'
  86. const script = exec(command)
  87. // 将打包信息,打印到控制台
  88. script.stdout.on('data', (chunk) => {
  89. console.log(chalk.hex('#f9d472').bold(chunk))
  90. })
  91. }
  92. processArgv()