mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const fs = require('fs')
const path = require('path')
 
const {
  parseJson
} = require('./json')
 
const merge = require('./pages-json').default
 
function normalizeUniModulesPagesJson (pagesJson, pluginId) {
  if (Array.isArray(pagesJson.pages)) {
    pagesJson.pages.forEach(page => {
      page.path = 'uni_modules/' + pluginId + '/' + page.path
    })
  }
  if (Array.isArray(pagesJson.subPackages)) {
    pagesJson.subPackages.forEach(subPackage => {
      subPackage.root = 'uni_modules/' + pluginId + '/' + subPackage.root
    })
  }
  return pagesJson
}
 
function initUniModules () {
  global.uniModules = []
  try {
    global.uniModules = fs
      .readdirSync(path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules'))
      .filter(module =>
        fs.existsSync(
          path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, 'package.json')
        )
      )
  } catch (e) {}
}
 
module.exports = {
  initUniModules,
  getPagesJson (content) {
    const uniModulesDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules')
    const pluginPagesJsons = []
    global.uniModules.forEach(plugin => {
      const pagesJsonPath = path.resolve(uniModulesDir, plugin, 'pages.json')
      if (fs.existsSync(pagesJsonPath)) {
        pluginPagesJsons.push(
          normalizeUniModulesPagesJson(parseJson(fs.readFileSync(pagesJsonPath).toString(), true), plugin)
        )
      }
    })
    content = content || fs.readFileSync(path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'), 'utf8')
    const mainPagesJson = parseJson(content, true)
    if (pluginPagesJsons.length) {
      const pagesJson = merge(pluginPagesJsons.concat(mainPagesJson))
      if (Array.isArray(mainPagesJson.pages)) { // entry page
        const entryPagePath = mainPagesJson.pages[0].path
        const index = pagesJson.pages.findIndex(page => page.path === entryPagePath)
        const entryPage = pagesJson.pages[index]
        pagesJson.pages.splice(index, 1)
        pagesJson.pages.unshift(entryPage)
      }
      return pagesJson
    }
    return mainPagesJson
  }
}