@tmsfe/tmskit 0.0.49 → 0.0.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -120,23 +120,23 @@ const succeed$2 = (message = '') => {
120
120
  const warn$2 = message => {
121
121
  console.log(`${moment$1().format('YYYY-MM-DD HH:mm:ss')}`, chalk$5.yellow(message));
122
122
  };
123
- const info$k = (...args) => console.log(`${moment$1().format('YYYY-MM-DD HH:mm:ss')}`, ...args);
123
+ const info$l = (...args) => console.log(`${moment$1().format('YYYY-MM-DD HH:mm:ss')}`, ...args);
124
124
  const infoNoTime$1 = (...args) => console.log(...args);
125
125
  var log$1 = {
126
126
  fail: fail$a,
127
127
  succeed: succeed$2,
128
128
  warn: warn$2,
129
- info: info$k,
129
+ info: info$l,
130
130
  infoNoTime: infoNoTime$1
131
131
  };
132
132
 
133
133
  const ora$2 = require$$0__default;
134
134
  const path$h = require$$1__default$1;
135
- const fs$j = require$$0__default$1;
135
+ const fs$k = require$$0__default$1;
136
136
  const shelljs$7 = require$$0__default$2;
137
137
  const glob = require$$4__default;
138
138
  const {
139
- info: info$j
139
+ info: info$k
140
140
  } = log$1;
141
141
  const shelljsOptions = {
142
142
  slient: true,
@@ -145,7 +145,7 @@ const shelljsOptions = {
145
145
 
146
146
  // 获取当前目录
147
147
  const cwd = process.cwd();
148
- function resolve$k(...args) {
148
+ function resolve$l(...args) {
149
149
  return path$h.resolve(cwd, ...args);
150
150
  }
151
151
 
@@ -178,7 +178,7 @@ function downloadRepoForGit$1(url, dest, branch) {
178
178
  const cwd = process.cwd();
179
179
  return new Promise((resolve, reject) => {
180
180
  // 如果目标目录不存在
181
- if (fs$j.existsSync(dest)) {
181
+ if (fs$k.existsSync(dest)) {
182
182
  shelljs$7.rm('-rf', path$h.join(dest));
183
183
  }
184
184
  shelljs$7.mkdir('-p', dest);
@@ -278,7 +278,7 @@ function createTask$7(task, startText, endText) {
278
278
  return async (...args) => {
279
279
  const start = Date.now();
280
280
  const spinner = ora$2();
281
- info$j(startText);
281
+ info$k(startText);
282
282
  spinner.start();
283
283
  const result = await task(...args);
284
284
  endText && spinner.succeed(`${endText}, 耗时${cost(start) / 1000}s`);
@@ -336,7 +336,7 @@ function findFiles(globPath, filter = []) {
336
336
  // 获取绝对路径
337
337
  function getAbsolutePath$6(pathDir, cwd = '') {
338
338
  let newPath = pathDir;
339
- newPath = newPath.startsWith('/') ? newPath : resolve$k(cwd, newPath);
339
+ newPath = newPath.startsWith('/') ? newPath : resolve$l(cwd, newPath);
340
340
  newPath = newPath.endsWith('/') ? newPath.slice(0, newPath.length - 1) : newPath;
341
341
  return newPath;
342
342
  }
@@ -367,7 +367,7 @@ function getGitUser$2() {
367
367
  return res.stdout;
368
368
  }
369
369
  var widgets = {
370
- resolve: resolve$k,
370
+ resolve: resolve$l,
371
371
  isObject,
372
372
  isArray,
373
373
  createTask: createTask$7,
@@ -872,10 +872,10 @@ var global_1 = {
872
872
  };
873
873
  })(tmsMpconfig);
874
874
 
875
- const fs$i = require$$0__default$1;
875
+ const fs$j = require$$0__default$1;
876
876
  const path$f = require$$1__default$1;
877
877
  const {
878
- info: info$i
878
+ info: info$j
879
879
  } = log$1;
880
880
  const {
881
881
  relativeCwdPath
@@ -885,12 +885,12 @@ const {
885
885
  * @param {string} dirname 目录名
886
886
  * @returns
887
887
  */
888
- const isDirEmpty = dirname => fs$i.promises.readdir(dirname).then(files => files.length === 0);
888
+ const isDirEmpty = dirname => fs$j.promises.readdir(dirname).then(files => files.length === 0);
889
889
 
890
890
  // 判断是否是文件
891
891
  const isFile = pathName => {
892
892
  try {
893
- const stat = fs$i.lstatSync(pathName);
893
+ const stat = fs$j.lstatSync(pathName);
894
894
  return stat.isFile();
895
895
  } catch {
896
896
  return false;
@@ -902,8 +902,8 @@ const isFile = pathName => {
902
902
  * @param {*} dirname 目录名
903
903
  */
904
904
  const ensureDirExist$6 = dirname => {
905
- if (!fs$i.existsSync(dirname)) {
906
- fs$i.mkdirSync(dirname, {
905
+ if (!fs$j.existsSync(dirname)) {
906
+ fs$j.mkdirSync(dirname, {
907
907
  recursive: true
908
908
  });
909
909
  }
@@ -911,25 +911,25 @@ const ensureDirExist$6 = dirname => {
911
911
 
912
912
  // 复制文件
913
913
  const copyFile = function (src, dest) {
914
- if (fs$i.existsSync(dest)) {
915
- fs$i.unlinkSync(dest);
914
+ if (fs$j.existsSync(dest)) {
915
+ fs$j.unlinkSync(dest);
916
916
  }
917
917
  const dir = path$f.dirname(dest);
918
918
  ensureDirExist$6(dir);
919
- fs$i.copyFileSync(src, dest);
919
+ fs$j.copyFileSync(src, dest);
920
920
  };
921
921
 
922
922
  // 判断文件内容是否一致,不一致再进行拷贝
923
923
  function diffContentCopyFile(originFile, destFile) {
924
- if (fs$i.existsSync(destFile)) {
925
- const depDestContent = fs$i.readFileSync(destFile, 'utf8');
926
- const depOriginContent = fs$i.readFileSync(originFile, 'utf8');
924
+ if (fs$j.existsSync(destFile)) {
925
+ const depDestContent = fs$j.readFileSync(destFile, 'utf8');
926
+ const depOriginContent = fs$j.readFileSync(originFile, 'utf8');
927
927
  if (depDestContent !== depOriginContent) {
928
- info$i(`拷贝${relativeCwdPath(originFile)}内容到${relativeCwdPath(destFile)}`);
928
+ info$j(`拷贝${relativeCwdPath(originFile)}内容到${relativeCwdPath(destFile)}`);
929
929
  copyFile(originFile, destFile);
930
930
  }
931
931
  } else {
932
- info$i(`拷贝${relativeCwdPath(originFile)}内容到${relativeCwdPath(destFile)}`);
932
+ info$j(`拷贝${relativeCwdPath(originFile)}内容到${relativeCwdPath(destFile)}`);
933
933
  copyFile(originFile, destFile);
934
934
  }
935
935
  }
@@ -940,7 +940,7 @@ function ext(filePath, extensions) {
940
940
  let extPath = '';
941
941
  // try catch需要包裹:用来处理'./lib/timer'没有后缀的情况
942
942
  try {
943
- const stat = fs$i.lstatSync(newFilePath);
943
+ const stat = fs$j.lstatSync(newFilePath);
944
944
  if (stat.isDirectory()) {
945
945
  extPath = newFilePath[newFilePath.length - 1] === '/' ? 'index' : '/index';
946
946
  newFilePath += extPath;
@@ -948,7 +948,7 @@ function ext(filePath, extensions) {
948
948
  } catch (e) {}
949
949
  for (const ext of extensions) {
950
950
  const file = newFilePath.endsWith(ext) ? newFilePath : newFilePath + ext;
951
- if (fs$i.existsSync(file)) {
951
+ if (fs$j.existsSync(file)) {
952
952
  return {
953
953
  ext,
954
954
  extPath: extPath + ext,
@@ -965,7 +965,7 @@ function ext(filePath, extensions) {
965
965
 
966
966
  // 判断文件是否在某个目录
967
967
  const fileInDir = (dir, file) => {
968
- if (!fs$i.existsSync(dir) || !fs$i.existsSync(file)) {
968
+ if (!fs$j.existsSync(dir) || !fs$j.existsSync(file)) {
969
969
  return false;
970
970
  }
971
971
  const relativePath = path$f.relative(dir, file);
@@ -977,10 +977,10 @@ const fileInDir = (dir, file) => {
977
977
  function findAllFilesOfDir(dir) {
978
978
  const list = [];
979
979
  function listFile(dir) {
980
- const arr = fs$i.readdirSync(dir);
980
+ const arr = fs$j.readdirSync(dir);
981
981
  arr.forEach(item => {
982
982
  const fullPath = path$f.join(dir, item);
983
- const stats = fs$i.statSync(fullPath);
983
+ const stats = fs$j.statSync(fullPath);
984
984
  if (stats.isDirectory()) {
985
985
  listFile(fullPath);
986
986
  } else {
@@ -1019,10 +1019,10 @@ const render$1 = (files, metalsmith, next) => {
1019
1019
  };
1020
1020
  var render_1 = render$1;
1021
1021
 
1022
- const fs$h = require$$0__default$1;
1022
+ const fs$i = require$$0__default$1;
1023
1023
  const inquirer$1 = require$$1__default$5;
1024
1024
  const {
1025
- resolve: resolve$j
1025
+ resolve: resolve$k
1026
1026
  } = widgets;
1027
1027
  const {
1028
1028
  TEMPLATE_TKIT_DIR: TEMPLATE_TKIT_DIR$1
@@ -1034,11 +1034,11 @@ const {
1034
1034
  */
1035
1035
  const parseTemplateQuestions = dir => {
1036
1036
  let prompts = [];
1037
- if (!fs$h.existsSync(`${dir}/questions.json`)) {
1037
+ if (!fs$i.existsSync(`${dir}/questions.json`)) {
1038
1038
  return prompts;
1039
1039
  }
1040
1040
  try {
1041
- const json = JSON.parse(fs$h.readFileSync(`${dir}/questions.json`));
1041
+ const json = JSON.parse(fs$i.readFileSync(`${dir}/questions.json`));
1042
1042
  if (Array.isArray(json) && json.length > 0) {
1043
1043
  json.forEach((item, index) => {
1044
1044
  if (!isQuestionType(item)) {
@@ -1064,7 +1064,7 @@ const isQuestionType = result => {
1064
1064
  return true;
1065
1065
  };
1066
1066
  const ask$1 = templateDir => (files, metalsmith, next) => {
1067
- const prompts = parseTemplateQuestions(resolve$j(templateDir, TEMPLATE_TKIT_DIR$1));
1067
+ const prompts = parseTemplateQuestions(resolve$k(templateDir, TEMPLATE_TKIT_DIR$1));
1068
1068
  const metadata = metalsmith.metadata();
1069
1069
  const filteredPrompts = prompts.filter(prompt => {
1070
1070
  if (metadata[prompt.name] && `${metadata[prompt.name]}`.trim() !== '') {
@@ -1132,7 +1132,7 @@ const report$b = (name, attrs = {}) => {
1132
1132
  var report_1 = report$b;
1133
1133
 
1134
1134
  const path$e = require$$1__default$1;
1135
- const fs$g = require$$0__default$1;
1135
+ const fs$h = require$$0__default$1;
1136
1136
  const shelljs$6 = require$$0__default$2;
1137
1137
  const inquirer = require$$1__default$5;
1138
1138
  const {
@@ -1143,13 +1143,13 @@ const {
1143
1143
  CREATE_TEMPLATE_QUESTION
1144
1144
  } = constant;
1145
1145
  const {
1146
- resolve: resolve$i
1146
+ resolve: resolve$j
1147
1147
  } = widgets;
1148
1148
  const io$2 = io$3;
1149
1149
  const {
1150
1150
  fail: fail$9,
1151
1151
  succeed: succeed$1,
1152
- info: info$h
1152
+ info: info$i
1153
1153
  } = log$1;
1154
1154
  const generator = generator_1;
1155
1155
  const request = require$$0__default$6;
@@ -1163,7 +1163,7 @@ const report$a = report_1;
1163
1163
  */
1164
1164
  async function createProjectDir(targetDir) {
1165
1165
  // 如果目录非空或者已经存在,提示用户,做选择
1166
- if (fs$g.existsSync(targetDir)) {
1166
+ if (fs$h.existsSync(targetDir)) {
1167
1167
  if (!(await io$2.isDirEmpty(targetDir))) {
1168
1168
  fail$9('该目录名已经存在,换个项目名字吧~');
1169
1169
  process.exit(1);
@@ -1183,13 +1183,13 @@ async function createProjectDir(targetDir) {
1183
1183
  function downloadAndUnZipTemplate(templateDir, templateUrl, templateName) {
1184
1184
  return new Promise((resolve, reject) => {
1185
1185
  const localZipPath = `${templateDir}/${templateName}.zip`;
1186
- const stream = fs$g.createWriteStream(localZipPath);
1186
+ const stream = fs$h.createWriteStream(localZipPath);
1187
1187
  request(`${templateUrl}?v=${new Date().getTime()}`).pipe(stream).on('close', err => {
1188
1188
  if (err) {
1189
1189
  reject(err);
1190
1190
  return;
1191
1191
  }
1192
- fs$g.createReadStream(localZipPath).pipe(unzip.Extract({
1192
+ fs$h.createReadStream(localZipPath).pipe(unzip.Extract({
1193
1193
  path: templateDir
1194
1194
  })).on('close', err => {
1195
1195
  if (err) {
@@ -1221,10 +1221,10 @@ async function create(projectName) {
1221
1221
  await createProjectDir(targetDir);
1222
1222
 
1223
1223
  // 新创建缓存目录
1224
- if (fs$g.existsSync(TEMPLATE_DIR)) {
1224
+ if (fs$h.existsSync(TEMPLATE_DIR)) {
1225
1225
  shelljs$6.rm('-rf', TEMPLATE_DIR);
1226
1226
  }
1227
- fs$g.mkdirSync(TEMPLATE_DIR, {
1227
+ fs$h.mkdirSync(TEMPLATE_DIR, {
1228
1228
  recursive: true
1229
1229
  });
1230
1230
 
@@ -1234,8 +1234,8 @@ async function create(projectName) {
1234
1234
  // 生成模板(1. 询问问题, 2. ejs生成模板 3.生成到目标目录)
1235
1235
  generator(path$e.join(TEMPLATE_DIR, TEMPLATE_NAME, projectType), targetDir).then(() => {
1236
1236
  shelljs$6.cd(projectName);
1237
- const hookFilePath = resolve$i(projectName, TEMPLATE_TKIT_DIR, 'hooks.js');
1238
- if (fs$g.existsSync(hookFilePath)) {
1237
+ const hookFilePath = resolve$j(projectName, TEMPLATE_TKIT_DIR, 'hooks.js');
1238
+ if (fs$h.existsSync(hookFilePath)) {
1239
1239
  const hooks = require(hookFilePath);
1240
1240
  if (hooks.afterCreate) {
1241
1241
  hooks.afterCreate.forEach(item => {
@@ -1248,7 +1248,7 @@ async function create(projectName) {
1248
1248
  }
1249
1249
  });
1250
1250
  }
1251
- shelljs$6.rm('-rf', resolve$i(projectName, TEMPLATE_TKIT_DIR));
1251
+ shelljs$6.rm('-rf', resolve$j(projectName, TEMPLATE_TKIT_DIR));
1252
1252
  }
1253
1253
  report$a('create-success', {
1254
1254
  projectType
@@ -1256,12 +1256,12 @@ async function create(projectName) {
1256
1256
  succeed$1('项目创建完成.');
1257
1257
  }).catch(err => {
1258
1258
  fail$9(err.message);
1259
- info$h('详细的错误信息:', err);
1259
+ info$i('详细的错误信息:', err);
1260
1260
  });
1261
1261
  }
1262
1262
  var create_1 = create;
1263
1263
 
1264
- const fs$f = require$$0__default$1;
1264
+ const fs$g = require$$0__default$1;
1265
1265
  const path$d = require$$1__default$1;
1266
1266
  const shellJs$3 = require$$0__default$2;
1267
1267
  const {
@@ -1274,7 +1274,7 @@ const {
1274
1274
  createTask: createTask$6
1275
1275
  } = widgets;
1276
1276
  const {
1277
- info: info$g,
1277
+ info: info$h,
1278
1278
  fail: fail$8
1279
1279
  } = log$1;
1280
1280
 
@@ -1286,9 +1286,9 @@ const {
1286
1286
  async function installCmd(npmName, cmd) {
1287
1287
  try {
1288
1288
  const cmdPackageJson = `${EXTEND_CMD}/package.json`;
1289
- if (!fs$f.existsSync(cmdPackageJson)) {
1289
+ if (!fs$g.existsSync(cmdPackageJson)) {
1290
1290
  ensureDirExist$5(EXTEND_CMD);
1291
- fs$f.writeFileSync(cmdPackageJson, JSON.stringify({
1291
+ fs$g.writeFileSync(cmdPackageJson, JSON.stringify({
1292
1292
  dependencies: {}
1293
1293
  }, null, 2));
1294
1294
  }
@@ -1307,7 +1307,7 @@ async function installCmd(npmName, cmd) {
1307
1307
  }), `开始下载${npmName}`, `下载${npmName}完成`)(npmName);
1308
1308
  } catch (e) {
1309
1309
  fail$8('构建出现错误:');
1310
- info$g(e);
1310
+ info$h(e);
1311
1311
  process.exit(1);
1312
1312
  }
1313
1313
  }
@@ -1319,8 +1319,8 @@ async function installCmd(npmName, cmd) {
1319
1319
  function loadExtendCmd$1() {
1320
1320
  const cmdPackageJson = `${EXTEND_CMD}/package.json`;
1321
1321
  const cmdNpmDir = `${EXTEND_CMD}/node_modules`;
1322
- if (fs$f.existsSync(cmdPackageJson)) {
1323
- const content = fs$f.readFileSync(cmdPackageJson, 'utf8');
1322
+ if (fs$g.existsSync(cmdPackageJson)) {
1323
+ const content = fs$g.readFileSync(cmdPackageJson, 'utf8');
1324
1324
  const json = JSON.parse(content);
1325
1325
  const deps = json.dependencies || {};
1326
1326
  const cmdConfigs = [];
@@ -1328,7 +1328,7 @@ function loadExtendCmd$1() {
1328
1328
  // 检索cmd的npm包
1329
1329
  if (!/^tmskit-cmd-|^@[^/]+\/tmskit-cmd-/.test(name)) return false;
1330
1330
  const cmdConfig = path$d.join(cmdNpmDir, name, 'tms.config.js');
1331
- if (fs$f.existsSync(cmdConfig)) {
1331
+ if (fs$g.existsSync(cmdConfig)) {
1332
1332
  cmdConfigs.push(cmdConfig);
1333
1333
  }
1334
1334
  });
@@ -1365,6 +1365,77 @@ var handleError_1 = {
1365
1365
  handleError: handleError$9
1366
1366
  };
1367
1367
 
1368
+ const {
1369
+ resolve: resolve$i
1370
+ } = widgets;
1371
+ const {
1372
+ info: info$g
1373
+ } = log$1;
1374
+ const fs$f = require$$0__default$1;
1375
+
1376
+ /* eslint-disable no-param-reassign */
1377
+ /**
1378
+ * 检查是否包含 meta-services 分包
1379
+ * @param {Object} appJson app.json配置对象
1380
+ * @returns {boolean}
1381
+ */
1382
+ function hasMetaServicesSubpackage(appJson) {
1383
+ return appJson.subpackages.some(s => s.root === 'modules/meta-services' || s.name === 'meta-services');
1384
+ }
1385
+
1386
+ /**
1387
+ * 处理 agent 配置:根据是否包含 meta-services 分包来添加或删除 agent 配置
1388
+ * @param {Object} appJson app.json配置对象
1389
+ */
1390
+ function handleAgentConfig$1(appJson) {
1391
+ const hasMetaServices = hasMetaServicesSubpackage(appJson);
1392
+ console.log('🔍', hasMetaServices ? '包含' : '不包含', 'meta-services 分包');
1393
+ if (hasMetaServices) {
1394
+ if (!appJson.agent) {
1395
+ try {
1396
+ appJson.agent = require(resolve$i('./agentConfig.json'));
1397
+ info$g('已从 agentConfig.json 加载 agent 配置');
1398
+ } catch (error) {
1399
+ info$g('未找到 agentConfig.json,删除 agent 配置');
1400
+ delete appJson.agent;
1401
+ }
1402
+ }
1403
+ } else if (appJson.agent) {
1404
+ delete appJson.agent;
1405
+ info$g('已移除 agent 配置');
1406
+ }
1407
+ }
1408
+
1409
+ /**
1410
+ * 处理 project.config.json 中的 packOptions.include 配置
1411
+ * @param {Object} appJson app.json配置对象
1412
+ * @param {string} outputDir 输出目录
1413
+ */
1414
+ function handleProjectConfigInclude$1(appJson, outputDir) {
1415
+ const projectConfigPath = resolve$i(outputDir, 'project.config.json');
1416
+ if (!fs$f.existsSync(projectConfigPath)) return;
1417
+ const projectConfig = require(projectConfigPath);
1418
+ const hasMetaServices = hasMetaServicesSubpackage(appJson);
1419
+ if (!projectConfig.packOptions) projectConfig.packOptions = {};
1420
+ if (!projectConfig.packOptions.include) projectConfig.packOptions.include = [];
1421
+ const existingIndex = projectConfig.packOptions.include.findIndex(item => item.type === 'folder' && item.value === 'modules/meta-services');
1422
+ if (hasMetaServices && existingIndex === -1) {
1423
+ projectConfig.packOptions.include.push({
1424
+ type: 'folder',
1425
+ value: 'modules/meta-services'
1426
+ });
1427
+ info$g('已添加 meta-services 到 project.config.json');
1428
+ } else if (!hasMetaServices && existingIndex !== -1) {
1429
+ projectConfig.packOptions.include.splice(existingIndex, 1);
1430
+ info$g('已从 project.config.json 移除 meta-services');
1431
+ }
1432
+ fs$f.writeFileSync(projectConfigPath, JSON.stringify(projectConfig, null, 2));
1433
+ }
1434
+ var handleAgentConfig_1 = {
1435
+ handleAgentConfig: handleAgentConfig$1,
1436
+ handleProjectConfigInclude: handleProjectConfigInclude$1
1437
+ };
1438
+
1368
1439
  /**
1369
1440
  * 生成编译后的app.json
1370
1441
  */
@@ -1391,6 +1462,10 @@ const {
1391
1462
  global: global$a
1392
1463
  } = global_1;
1393
1464
  const report$9 = report_1;
1465
+ const {
1466
+ handleAgentConfig,
1467
+ handleProjectConfigInclude
1468
+ } = handleAgentConfig_1;
1394
1469
 
1395
1470
  /**
1396
1471
  * 更新appJson里面的主包配置
@@ -1566,6 +1641,10 @@ async function buildOutputAppJson$3(tmsConfig, modules) {
1566
1641
 
1567
1642
  // 处理appJson中重复||冲突的地方
1568
1643
  fixAppJson(appJson);
1644
+ // 处理 agent 配置:根据是否包含 meta-services 分包来添加或删除
1645
+ handleAgentConfig(appJson);
1646
+ // 处理 project.config.json 中的 packOptions.include 配置
1647
+ handleProjectConfigInclude(appJson, tmsConfig.outputDir);
1569
1648
  // 更新主包,需在subpackages处理完成后执行, pages/
1570
1649
  updateMainPackages(appJson, tmsConfig.mainPackages);
1571
1650
 
@@ -4195,7 +4274,7 @@ var entry = [{
4195
4274
 
4196
4275
  var require$$12 = {
4197
4276
  name: "@tmsfe/tmskit",
4198
- version: "0.0.49",
4277
+ version: "0.0.53",
4199
4278
  description: "tmskit",
4200
4279
  main: "dist/index.cjs",
4201
4280
  bin: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tmskit",
3
- "version": "0.0.49",
3
+ "version": "0.0.53",
4
4
  "description": "tmskit",
5
5
  "main": "dist/index.cjs",
6
6
  "bin": {
@@ -10,6 +10,7 @@ const { resolve, filterField } = require('../utils/widgets');
10
10
  const { handleError } = require('./handleError');
11
11
  const { global } = require('../utils/global');
12
12
  const report = require('../core/report');
13
+ const { handleAgentConfig, handleProjectConfigInclude } = require('./handleAgentConfig');
13
14
 
14
15
 
15
16
  /**
@@ -191,6 +192,10 @@ async function buildOutputAppJson(tmsConfig, modules) {
191
192
 
192
193
  // 处理appJson中重复||冲突的地方
193
194
  fixAppJson(appJson);
195
+ // 处理 agent 配置:根据是否包含 meta-services 分包来添加或删除
196
+ handleAgentConfig(appJson);
197
+ // 处理 project.config.json 中的 packOptions.include 配置
198
+ handleProjectConfigInclude(appJson, tmsConfig.outputDir);
194
199
  // 更新主包,需在subpackages处理完成后执行, pages/
195
200
  updateMainPackages(appJson, tmsConfig.mainPackages);
196
201
 
@@ -0,0 +1,70 @@
1
+ const { resolve } = require('../utils/widgets');
2
+ const { info } = require('../utils/log');
3
+ const fs = require('fs');
4
+
5
+ /* eslint-disable no-param-reassign */
6
+ /**
7
+ * 检查是否包含 meta-services 分包
8
+ * @param {Object} appJson app.json配置对象
9
+ * @returns {boolean}
10
+ */
11
+ function hasMetaServicesSubpackage(appJson) {
12
+ return appJson.subpackages.some(s => s.root === 'modules/meta-services' || s.name === 'meta-services');
13
+ }
14
+
15
+ /**
16
+ * 处理 agent 配置:根据是否包含 meta-services 分包来添加或删除 agent 配置
17
+ * @param {Object} appJson app.json配置对象
18
+ */
19
+ function handleAgentConfig(appJson) {
20
+ const hasMetaServices = hasMetaServicesSubpackage(appJson);
21
+ console.log('🔍', hasMetaServices ? '包含' : '不包含', 'meta-services 分包');
22
+ if (hasMetaServices) {
23
+ if (!appJson.agent) {
24
+ try {
25
+ appJson.agent = require(resolve('./agentConfig.json'));
26
+ info('已从 agentConfig.json 加载 agent 配置');
27
+ } catch (error) {
28
+ info('未找到 agentConfig.json,删除 agent 配置');
29
+ delete appJson.agent;
30
+ }
31
+ }
32
+ } else if (appJson.agent) {
33
+ delete appJson.agent;
34
+ info('已移除 agent 配置');
35
+ }
36
+ }
37
+
38
+ /**
39
+ * 处理 project.config.json 中的 packOptions.include 配置
40
+ * @param {Object} appJson app.json配置对象
41
+ * @param {string} outputDir 输出目录
42
+ */
43
+ function handleProjectConfigInclude(appJson, outputDir) {
44
+ const projectConfigPath = resolve(outputDir, 'project.config.json');
45
+ if (!fs.existsSync(projectConfigPath)) return;
46
+
47
+ const projectConfig = require(projectConfigPath);
48
+ const hasMetaServices = hasMetaServicesSubpackage(appJson);
49
+
50
+ if (!projectConfig.packOptions) projectConfig.packOptions = {};
51
+ if (!projectConfig.packOptions.include) projectConfig.packOptions.include = [];
52
+
53
+ const existingIndex = projectConfig.packOptions.include.findIndex(item => item.type === 'folder' && item.value === 'modules/meta-services');
54
+
55
+ if (hasMetaServices && existingIndex === -1) {
56
+ projectConfig.packOptions.include.push({ type: 'folder', value: 'modules/meta-services' });
57
+ info('已添加 meta-services 到 project.config.json');
58
+ } else if (!hasMetaServices && existingIndex !== -1) {
59
+ projectConfig.packOptions.include.splice(existingIndex, 1);
60
+ info('已从 project.config.json 移除 meta-services');
61
+ }
62
+
63
+ fs.writeFileSync(projectConfigPath, JSON.stringify(projectConfig, null, 2));
64
+ }
65
+
66
+
67
+ module.exports = {
68
+ handleAgentConfig,
69
+ handleProjectConfigInclude,
70
+ };