import * as fs from 'fs' import path from 'path' import getAppInfo from "./config/id.config.mjs"; // 同步读取文件内容 function readFileSync(filePath) { return fs.readFileSync(filePath, 'utf8'); } // 同步写入文件内容 function writeFileSync(filePath, data) { fs.writeFileSync(filePath, data, 'utf8'); } // 替换文件中的文本 function replaceTextInFileSync(filePath, matchKey, str, templateFilePath) { return replaceCustomTextInFileSync({ filePath, templateFilePath, replaceFn: function (fileContent) { return fileContent.replaceAll(matchKey, str) } }) } // 自定义替换文件中的文本 function replaceCustomTextInFileSync({ filePath, templateFilePath, replaceFn }) { try { // 读取文件内容 let fileContent = readFileSync(templateFilePath || filePath); // 替换文本 const newContent = replaceFn(fileContent); // 写入新内容到文件 writeFileSync(filePath, newContent); console.log('环境文本替换:', filePath); } catch (error) { console.error('文本替换失败:', error); } } function replaceStyle (color) { replaceTextInFileSync(path.resolve('./src/assets/env_style/scss.scss'), '#2ABED1', color, path.resolve('./script/template/env_style/scss.scss')) replaceTextInFileSync(path.resolve('./src/assets/env_style/less.less'), '#2ABED1', color, path.resolve('./script/template/env_style/less.less')) } function replaceAppConfig (data) { replaceTextInFileSync(path.resolve('./project-config/index.js'), 'const appConfig = {}', `const appConfig = ${JSON.stringify(data)}`, path.resolve('./script/template/project-config.js')) replaceTextInFileSync(path.resolve('./project-config/index.common.js'), 'const appConfig = {}', `const appConfig = ${JSON.stringify(data)}`, path.resolve('./script/template/project-config.common.js')) } function replaceAppID (data) { replaceCustomTextInFileSync({ filePath: path.resolve('./project.config.json'), templateFilePath: path.resolve('./script/template/project.config.json'), replaceFn: (content) => { let result = content.replaceAll('APP_ID', data.projectInfo.appId) result = result.replaceAll('APP_CODE', data.projectInfo.appCode) return result } }) } export function replaceEnvCode (appCode) { const appInfo = getAppInfo(appCode) // 替换AppId、Tabbar、图标、全部商机列表配置 replaceAppID(appInfo) replaceAppConfig(appInfo) // 替换主题色 replaceStyle(appInfo.projectInfo.themeColor) // 替换请求域名、资源域名 }