const fs = require('fs')
const { exec } = require('child_process')
const { resolve, join } = require('path')

const args = process.argv.slice(2)

console.log('读取脚本参数中 🤬')

const rootPath = join(__dirname, '..')

const file = fs.readFileSync(resolve(rootPath, 'src/router/index.ts'), 'utf8')

const r = file.replace(
  'const _routes = [iot, schedule, view, lowcode, user, login]',
  `const _routes = [${args.map((arg, index) => index > 0 ? ` ${arg}` : arg)}]`
)

console.log('修改路由值成功 🌹')

fs.writeFileSync(resolve(rootPath, 'src/router/index.ts'), r, 'utf-8')

console.log('开始打包  📦')
console.log('打包中...... 💪')
exec('npm run build', (error, stdout, stderr) => {
  if (error) {
    console.error(`执行脚本时出错：${error}`)
    fs.writeFileSync(resolve(rootPath, 'src/router/index.ts'), file, 'utf-8')
  } else {
    console.log('打包成功 🎉')
    fs.writeFileSync(resolve(rootPath, 'src/router/index.ts'), file, 'utf-8')
  }
})
