const {fs, chalkTag, fsExtra, findPages} = require("./tools"); const init = require("./build") /** * pages.json对象 * @type {{globalStyle: {pageOrientation: string, navigationStyle: string}, pages: [{path: string, style: {navigationBarTitleText: string}},{path: string, style: {navigationBarTitleText: string}}], customLogin: *[], easycom: {"^u-(.*)": string, "^(Ai|V)(.*)": string}}} */ const json = { easycom: { "^u-(.*)": "uview-ui/components/u-$1/u-$1.vue", "^(Ai|V)(.*)": "src/components/$1$2.vue", }, pages: [ {path: 'pages/loading', style: {navigationBarTitleText: "欢迎使用"}}, {path: 'pages/auth', style: {navigationBarTitleText: "正在授权"}}, ], globalStyle: { pageOrientation: "auto", navigationStyle: "custom" }, customLogin: [] } /** * 生成页面信息 * @param app 页面 * @param file 文件路径 */ const getFileInfo = (app, file) => { let vue = fs.readFileSync(file).toString() if (/appName/.test(vue)) { let appName = vue.replace(/[\s\S]*(appName:.+),[\s\S]*/gm, '$1') app.label = appName.replace(/(appName:|["'])/g, '').trim() } if (/customLogin/.test(vue)) { json.customLogin.push(app.name) } if (/customNavigation/.test(vue)) { app.style = {navigationStyle: "custom"} } else app.style = {navigationBarTitleText: app.label} if (/enablePullDownRefresh/.test(vue)) { app.style.enablePullDownRefresh = true } } /** * 生成pages.json */ const generatePages = config => { chalkTag.info('开始生成配置文件...') Promise.all([ findPages('src/components/pages', file => { if (/[^\\\/]+\.vue$/g.test(file)) { let app = { path: file.replace(/^src[\\\/](.*).vue/g, '$1').replace(/\\/g, '/') } getFileInfo(app, file) return json.pages.push(app) } }), findPages('src/apps', file => { if (/.*[\\\/].+[\\\/]App[^\\\/]+[\\\/][^\\\/]+\.vue/g.test(file)) { let app = { name: file.replace(/.*[\\\/]([^\\\/]+).vue/g, '$1'), path: file.replace(/^src[\\\/](.*).vue/g, '$1').replace(/\\/g, '/') } getFileInfo(app, file) return app.name == config?.extra?.homePage ? json.pages.unshift(app) : json.pages.push(app) } }) ]).then(() => { fsExtra.outputJson('src/pages.json', json, () => { chalkTag.done('生成pages.json') }) fsExtra.outputJson('src/common/config.json', {...config.extra, customLoginPages: json.customLogin}, () => { chalkTag.done('生成config.json') }) }) } const id = process.argv?.[2] || "8d175659-80d7-47ff-9291-c48e811b9237" init(id) .then(config => generatePages(config))