scripts/set-env.js
....@@ -1,78 +1,31 @@
11 /**
2
- * 环境切换脚本:把 config/env/<env>.js 里的 appid 同步到编译期配置文件
2
+ * 环境切换:按 config/env/<env>.js 从模板生成 manifest.json
33 *
44 * 用法:
55 * npm run env:sit 等价于 node scripts/set-env.js sit
66 * npm run env:prod 等价于 node scripts/set-env.js prod
77 *
88 * 说明:
9
- * appid 是编译期值(HBuilderX 的编译输入就是 manifest.json),无法像接口地址那样
10
- * 运行时切换,所以这里做“一次命令写回”。改完必须重新编译一次,产物里的
11
- * project.config.json 才会更新。
9
+ * manifest.json 是 HBuilderX 的编译输入(appid 属于编译期值,无法像接口地址那样运行时切换),
10
+ * 但它含环境相关 appid,不适合直接提交,所以:
11
+ * manifest.json.demo(入库模板) --生成--> manifest.json(已 .gitignore)
12
+ * 生成后必须重新编译一次,并在微信开发者工具中重新打开项目(appid 变更需重开)。
1213 *
13
- * 日常推荐:mp-weixin.appid 保持 null,只让发行脚本回写产物 appid(git 零噪音)。
14
- * 本脚本用于需要在 HBuilderX 里直接运行调试、或要出 App 包(顶层 appid)的场景。
14
+ * 如需在 HBuilderX 里直接编译/运行调试,先跑一次本脚本;
15
+ * 走命令行发行则直接用 scripts/publish.js(内部会自动生成)。
1516 *
16
- * 唯一数据源:config/env/<env>.js 中的 UNI_APPID(DCloud 标识)、APPID(小程序 appid)
17
+ * appid 数据源唯一:config/env/<env>.js 中的 UNI_APPID(DCloud 标识)、APPID(小程序 appid)
1718 */
1819 const fs = require('fs');
1920 const path = require('path');
21
+const manifest = require('./manifest');
2022
2123 const ROOT = path.resolve(__dirname, '..');
2224 const ENV_DIR = path.join(ROOT, 'config', 'env');
2325
24
-// 根目录 project.config.json 不参与 mp-weixin 编译且已不入库,这里只维护 manifest.json
25
-const files = {
26
- manifest: path.join(ROOT, 'manifest.json')
27
-};
28
-
2926 function fail(msg) {
3027 console.error('[set-env] ' + msg);
3128 process.exit(1);
32
-}
33
-
34
-/** 只替换第一处 "appid" : <字符串或 null>,保留原有空格/缩进风格 */
35
-function replaceAppId(text, value) {
36
- let done = false;
37
- const result = text.replace(/"appid"(\s*:\s*)(?:"[^"]*"|null)/, function(match, sep) {
38
- done = true;
39
- return '"appid"' + sep + '"' + value + '"';
40
- });
41
- return { text: result, done: done };
42
-}
43
-
44
-/** 替换 manifest.json 顶层 appid(第一处出现即为顶层) */
45
-function patchManifest(text, env) {
46
- // 1) 顶层 appid(DCloud 应用标识)
47
- const top = replaceAppId(text, env.UNI_APPID);
48
- if (!top.done) fail('manifest.json 中未找到顶层 "appid" 字段');
49
- text = top.text;
50
-
51
- // 2) mp-weixin 下的 appid(微信小程序 appid),只在该区块内替换第一处
52
- const idx = text.indexOf('"mp-weixin"');
53
- if (idx < 0) fail('manifest.json 中未找到 "mp-weixin" 配置块');
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;
58
-
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);
65
-}
66
-
67
-function writeIfChanged(file, next) {
68
- const prev = fs.readFileSync(file, 'utf8');
69
- if (prev === next) {
70
- console.log(' - ' + path.relative(ROOT, file) + ' 已是目标值,无需改动');
71
- return false;
72
- }
73
- fs.writeFileSync(file, next, 'utf8');
74
- console.log(' √ ' + path.relative(ROOT, file) + ' 已更新');
75
- return true;
7629 }
7730
7831 function main() {
....@@ -98,11 +51,11 @@
9851 console.log(' 小程序 appid : ' + env.APPID);
9952 console.log(' 接口地址 : ' + env.HTTP_REQUEST_URL);
10053
101
- const changed = writeIfChanged(files.manifest, patchManifest(fs.readFileSync(files.manifest, 'utf8'), env));
54
+ const res = manifest.generate(env);
10255
103
- console.log(changed
56
+ console.log(res.changed
10457 ? '[set-env] 完成,请在 HBuilderX 中重新编译一次,并在微信开发者工具中重新打开项目'
105
- : '[set-env] 无需改动,当前已是 ' + envName + ' 环境配置');
58
+ : '[set-env] 无需改动,当前 manifest.json 已是 ' + envName + ' 环境');
10659 }
10760
10861 main();