create-base-files.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // 基础配置文件生成脚本
  2. // 此脚本用于生成 src/manifest.json 和 src/pages.json 基础文件
  3. // 由于这两个配置文件会被添加到 .gitignore 中,因此需要通过此脚本确保项目能正常运行
  4. import fs from 'node:fs'
  5. import path from 'node:path'
  6. import { fileURLToPath } from 'node:url'
  7. // 获取当前文件的目录路径(替代 CommonJS 中的 __dirname)
  8. const __filename = fileURLToPath(import.meta.url)
  9. const __dirname = path.dirname(__filename)
  10. // 最简可运行配置
  11. const manifest = { }
  12. const pages = {
  13. pages: [
  14. {
  15. path: 'pages/index/index',
  16. type: 'home',
  17. style: {
  18. navigationStyle: 'custom',
  19. navigationBarTitleText: '首页',
  20. },
  21. },
  22. {
  23. path: 'pages/me/me',
  24. type: 'page',
  25. style: {
  26. navigationBarTitleText: '我的',
  27. },
  28. },
  29. ],
  30. subPackages: [],
  31. }
  32. // 使用修复后的 __dirname 来解析文件路径
  33. const manifestPath = path.resolve(__dirname, '../src/manifest.json')
  34. const pagesPath = path.resolve(__dirname, '../src/pages.json')
  35. // 确保 src 目录存在
  36. const srcDir = path.resolve(__dirname, '../src')
  37. if (!fs.existsSync(srcDir)) {
  38. fs.mkdirSync(srcDir, { recursive: true })
  39. }
  40. // 如果 src/manifest.json 不存在,就创建它;存在就不处理,以免覆盖
  41. if (!fs.existsSync(manifestPath) || fs.statSync(manifestPath).size === 0) {
  42. fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2))
  43. }
  44. // 如果 src/pages.json 不存在,就创建它;存在就不处理,以免覆盖
  45. if (!fs.existsSync(pagesPath) || fs.statSync(pagesPath).size === 0) {
  46. fs.writeFileSync(pagesPath, JSON.stringify(pages, null, 2))
  47. }