switch-pages.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * 根据 H5 或小程序环境切换 pages.json 配置
  3. * 用法: node scripts/switch-pages.js h5 | node scripts/switch-pages.js mp-weixin
  4. */
  5. const fs = require('fs')
  6. const path = require('path')
  7. const pagesJsonPath = path.resolve(__dirname, '../src/pages.json')
  8. // H5 页面配置
  9. const h5Pages = [
  10. {
  11. path: 'pages/h5/index'
  12. }
  13. ]
  14. // 小程序页面配置
  15. const mpPages = [
  16. {
  17. path: 'pages/devices/index',
  18. style: {
  19. navigationBarTitleText: '我的设备'
  20. }
  21. },
  22. {
  23. path: 'pages/index/result',
  24. style: {
  25. navigationBarTitleText: '配网结果'
  26. }
  27. },
  28. {
  29. path: 'pages/index/index',
  30. style: {
  31. navigationBarTitleText: '设备配网'
  32. }
  33. },
  34. {
  35. path: 'pages/login/login',
  36. style: {
  37. navigationBarTitleText: '登录'
  38. }
  39. },
  40. {
  41. path: 'pages/login/register',
  42. style: {
  43. navigationBarTitleText: '注册'
  44. }
  45. }
  46. ]
  47. const target = process.argv[2]
  48. if (!target || !['h5', 'mp-weixin'].includes(target)) {
  49. console.log('请指定目标平台: h5 或 mp-weixin')
  50. process.exit(1)
  51. }
  52. try {
  53. const pagesJson = JSON.parse(fs.readFileSync(pagesJsonPath, 'utf-8'))
  54. pagesJson.pages = target === 'h5' ? h5Pages : mpPages
  55. fs.writeFileSync(pagesJsonPath, JSON.stringify(pagesJson, null, '\t'), 'utf-8')
  56. console.log(`已切换到 ${target} 模式`)
  57. } catch (err) {
  58. console.error('切换失败:', err)
  59. process.exit(1)
  60. }