zhuxiuying
9 天以前 3e068fc2c13f15a8635ae38fd296d10700a36b04
scripts/set-env.js
....@@ -1,76 +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 是编译期值(manifest.json 为 HBuilderX 固定输入、微信开发者工具只认
10
- * project.config.json 的 appid),无法像接口地址那样运行时切换,所以这里做
11
- * “一次命令写回”。改完必须重新编译一次,dist 下的 project.config.json 才会更新。
9
+ * manifest.json 是 HBuilderX 的编译输入(appid 属于编译期值,无法像接口地址那样运行时切换),
10
+ * 但它含环境相关 appid,不适合直接提交,所以:
11
+ * manifest.json.demo(入库模板) --生成--> manifest.json(已 .gitignore)
12
+ * 生成后必须重新编译一次,并在微信开发者工具中重新打开项目(appid 变更需重开)。
1213 *
13
- * 唯一数据源:config/env/<env>.js 中的 UNI_APPID(DCloud 标识)、APPID(小程序 appid)
14
+ * 如需在 HBuilderX 里直接编译/运行调试,先跑一次本脚本;
15
+ * 走命令行发行则直接用 scripts/publish.js(内部会自动生成)。
16
+ *
17
+ * appid 数据源唯一:config/env/<env>.js 中的 UNI_APPID(DCloud 标识)、APPID(小程序 appid)
1418 */
1519 const fs = require('fs');
1620 const path = require('path');
21
+const manifest = require('./manifest');
1722
1823 const ROOT = path.resolve(__dirname, '..');
1924 const ENV_DIR = path.join(ROOT, 'config', 'env');
2025
21
-const files = {
22
- manifest: path.join(ROOT, 'manifest.json'),
23
- projectConfig: path.join(ROOT, 'project.config.json')
24
-};
25
-
2626 function fail(msg) {
2727 console.error('[set-env] ' + msg);
2828 process.exit(1);
29
-}
30
-
31
-/** 只替换第一处 "appid" : "xxx",保留原有空格/缩进风格 */
32
-function replaceAppId(text, value) {
33
- let done = false;
34
- const result = text.replace(/"appid"(\s*:\s*)"[^"]*"/, function(match, sep) {
35
- done = true;
36
- return '"appid"' + sep + '"' + value + '"';
37
- });
38
- return { text: result, done: done };
39
-}
40
-
41
-/** 替换 manifest.json 顶层 appid(第一处出现即为顶层) */
42
-function patchManifest(text, env) {
43
- // 1) 顶层 appid(DCloud 应用标识)
44
- const top = replaceAppId(text, env.UNI_APPID);
45
- if (!top.done) fail('manifest.json 中未找到顶层 "appid" 字段');
46
- text = top.text;
47
-
48
- // 2) mp-weixin 下的 appid(微信小程序 appid),只在该区块内替换第一处
49
- const idx = text.indexOf('"mp-weixin"');
50
- 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
-
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;
63
-}
64
-
65
-function writeIfChanged(file, next) {
66
- const prev = fs.readFileSync(file, 'utf8');
67
- if (prev === next) {
68
- console.log(' - ' + path.relative(ROOT, file) + ' 已是目标值,无需改动');
69
- return false;
70
- }
71
- fs.writeFileSync(file, next, 'utf8');
72
- console.log(' √ ' + path.relative(ROOT, file) + ' 已更新');
73
- return true;
7429 }
7530
7631 function main() {
....@@ -96,13 +51,11 @@
9651 console.log(' 小程序 appid : ' + env.APPID);
9752 console.log(' 接口地址 : ' + env.HTTP_REQUEST_URL);
9853
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;
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();