| .. | .. |
|---|
| 6 | 6 | * npm run env:prod 等价于 node scripts/set-env.js prod |
|---|
| 7 | 7 | * |
|---|
| 8 | 8 | * 说明: |
|---|
| 9 | | - * appid 是编译期值(manifest.json 为 HBuilderX 固定输入、微信开发者工具只认 |
|---|
| 10 | | - * project.config.json 的 appid),无法像接口地址那样运行时切换,所以这里做 |
|---|
| 11 | | - * “一次命令写回”。改完必须重新编译一次,dist 下的 project.config.json 才会更新。 |
|---|
| 9 | + * appid 是编译期值(HBuilderX 的编译输入就是 manifest.json),无法像接口地址那样 |
|---|
| 10 | + * 运行时切换,所以这里做“一次命令写回”。改完必须重新编译一次,产物里的 |
|---|
| 11 | + * project.config.json 才会更新。 |
|---|
| 12 | + * |
|---|
| 13 | + * 日常推荐:mp-weixin.appid 保持 null,只让发行脚本回写产物 appid(git 零噪音)。 |
|---|
| 14 | + * 本脚本用于需要在 HBuilderX 里直接运行调试、或要出 App 包(顶层 appid)的场景。 |
|---|
| 12 | 15 | * |
|---|
| 13 | 16 | * 唯一数据源:config/env/<env>.js 中的 UNI_APPID(DCloud 标识)、APPID(小程序 appid) |
|---|
| 14 | 17 | */ |
|---|
| .. | .. |
|---|
| 18 | 21 | const ROOT = path.resolve(__dirname, '..'); |
|---|
| 19 | 22 | const ENV_DIR = path.join(ROOT, 'config', 'env'); |
|---|
| 20 | 23 | |
|---|
| 24 | +// 根目录 project.config.json 不参与 mp-weixin 编译且已不入库,这里只维护 manifest.json |
|---|
| 21 | 25 | const files = { |
|---|
| 22 | | - manifest: path.join(ROOT, 'manifest.json'), |
|---|
| 23 | | - projectConfig: path.join(ROOT, 'project.config.json') |
|---|
| 26 | + manifest: path.join(ROOT, 'manifest.json') |
|---|
| 24 | 27 | }; |
|---|
| 25 | 28 | |
|---|
| 26 | 29 | function fail(msg) { |
|---|
| .. | .. |
|---|
| 28 | 31 | process.exit(1); |
|---|
| 29 | 32 | } |
|---|
| 30 | 33 | |
|---|
| 31 | | -/** 只替换第一处 "appid" : "xxx",保留原有空格/缩进风格 */ |
|---|
| 34 | +/** 只替换第一处 "appid" : <字符串或 null>,保留原有空格/缩进风格 */ |
|---|
| 32 | 35 | function replaceAppId(text, value) { |
|---|
| 33 | 36 | let done = false; |
|---|
| 34 | | - const result = text.replace(/"appid"(\s*:\s*)"[^"]*"/, function(match, sep) { |
|---|
| 37 | + const result = text.replace(/"appid"(\s*:\s*)(?:"[^"]*"|null)/, function(match, sep) { |
|---|
| 35 | 38 | done = true; |
|---|
| 36 | 39 | return '"appid"' + sep + '"' + value + '"'; |
|---|
| 37 | 40 | }); |
|---|
| .. | .. |
|---|
| 48 | 51 | // 2) mp-weixin 下的 appid(微信小程序 appid),只在该区块内替换第一处 |
|---|
| 49 | 52 | const idx = text.indexOf('"mp-weixin"'); |
|---|
| 50 | 53 | if (idx < 0) fail('manifest.json 中未找到 "mp-weixin" 配置块'); |
|---|
| 51 | | - const scoped = replaceAppId(text.slice(idx), env.APPID); |
|---|
| 52 | | - if (!scoped.done) fail('manifest.json 的 "mp-weixin" 中未找到 "appid" 字段'); |
|---|
| 53 | | - text = text.slice(0, idx) + scoped.text; |
|---|
| 54 | + const head = text.slice(0, idx); |
|---|
| 55 | + const body = text.slice(idx); |
|---|
| 56 | + const scoped = replaceAppId(body, env.APPID); |
|---|
| 57 | + if (scoped.done) return head + scoped.text; |
|---|
| 54 | 58 | |
|---|
| 55 | | - return text; |
|---|
| 56 | | -} |
|---|
| 57 | | - |
|---|
| 58 | | -/** 替换 project.config.json 的 appid(保持原格式,避免整文件重排) */ |
|---|
| 59 | | -function patchProjectConfig(text, env) { |
|---|
| 60 | | - const res = replaceAppId(text, env.APPID); |
|---|
| 61 | | - if (!res.done) fail('project.config.json 中未找到 "appid" 字段'); |
|---|
| 62 | | - return res.text; |
|---|
| 59 | + // 3) 字段被删掉时,按同级子项缩进补一行 |
|---|
| 60 | + const m = body.match(/^("mp-weixin"\s*:\s*\{)(\r?\n)([ \t]*)/); |
|---|
| 61 | + if (!m) fail('manifest.json 的 "mp-weixin" 块格式异常,无法写入 appid'); |
|---|
| 62 | + const nl = m[2]; |
|---|
| 63 | + const indent = m[3]; |
|---|
| 64 | + return head + m[1] + nl + indent + '"appid" : "' + env.APPID + '",' + nl + indent + body.slice(m[0].length); |
|---|
| 63 | 65 | } |
|---|
| 64 | 66 | |
|---|
| 65 | 67 | function writeIfChanged(file, next) { |
|---|
| .. | .. |
|---|
| 96 | 98 | console.log(' 小程序 appid : ' + env.APPID); |
|---|
| 97 | 99 | console.log(' 接口地址 : ' + env.HTTP_REQUEST_URL); |
|---|
| 98 | 100 | |
|---|
| 99 | | - const manifestChanged = writeIfChanged(files.manifest, patchManifest(fs.readFileSync(files.manifest, 'utf8'), env)); |
|---|
| 100 | | - const projectChanged = writeIfChanged(files.projectConfig, patchProjectConfig(fs.readFileSync(files.projectConfig, 'utf8'), env)); |
|---|
| 101 | | - const changed = manifestChanged || projectChanged; |
|---|
| 101 | + const changed = writeIfChanged(files.manifest, patchManifest(fs.readFileSync(files.manifest, 'utf8'), env)); |
|---|
| 102 | 102 | |
|---|
| 103 | 103 | console.log(changed |
|---|
| 104 | 104 | ? '[set-env] 完成,请在 HBuilderX 中重新编译一次,并在微信开发者工具中重新打开项目' |
|---|