@htyf-mp/cli 0.3.3 → 0.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htyf-mp/cli",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "红糖云服app小程序模板-cli",
5
5
  "type": "module",
6
6
  "bin": {
package/src/constants.mjs CHANGED
@@ -39,6 +39,7 @@ export const ACTION_TYPES = {
39
39
  INIT: 'init',
40
40
  MP_BUILD: 'mp-build',
41
41
  MP_DEBUG: 'mp-debug',
42
+ SYNC_DEPS: 'sync-deps',
42
43
  CLEAN: 'clean',
43
44
  QUIT: 'quit'
44
45
  };
package/src/index.mjs CHANGED
@@ -33,6 +33,7 @@ import { ProjectInitializer } from './project-initializer.mjs';
33
33
  import { mpBuildShell } from './build.mjs';
34
34
  import { mpDebugShell } from './debug.mjs';
35
35
  import { cleanShell } from './clean.mjs';
36
+ import { syncDepsShell } from './sync-deps.mjs';
36
37
  import { incrementVersion, updateAppConfig } from './utils-functions.mjs';
37
38
 
38
39
  const projectPath = getProjectRoot();
@@ -101,6 +102,7 @@ async function Start(action) {
101
102
  const actionNames = {
102
103
  [ACTION_TYPES.MP_BUILD]: '小程序构建',
103
104
  [ACTION_TYPES.MP_DEBUG]: '小程序调试',
105
+ [ACTION_TYPES.SYNC_DEPS]: '同步依赖版本',
104
106
  [ACTION_TYPES.CLEAN]: '清理模式',
105
107
  [ACTION_TYPES.QUIT]: '退出'
106
108
  };
@@ -113,6 +115,12 @@ async function Start(action) {
113
115
  return;
114
116
  }
115
117
 
118
+ // ========== 同步依赖版本 ==========
119
+ if (action === ACTION_TYPES.SYNC_DEPS) {
120
+ await syncDepsShell();
121
+ return;
122
+ }
123
+
116
124
  // ========== 清理操作 ==========
117
125
  if (action === ACTION_TYPES.CLEAN) {
118
126
  const { cleanType } = await inquirer.prompt([
@@ -267,6 +275,12 @@ if (process.argv.includes('--debug')) {
267
275
  // 记录启动日志
268
276
  Logger.writeToFile('CLI 工具启动', 'INFO');
269
277
 
278
+ // 处理依赖同步参数(命令行直接同步,不进入交互界面)
279
+ if (process.argv.includes('--sync-deps')) {
280
+ await syncDepsShell({ skipConfirm: true });
281
+ process.exit(0);
282
+ }
283
+
270
284
  // 处理清理参数(命令行直接清理,不进入交互界面)
271
285
  if (process.argv.includes('--clean')) {
272
286
  const cleanIndex = process.argv.indexOf('--clean');
@@ -297,10 +311,12 @@ if (process.argv.includes('--help') || process.argv.includes('-h')) {
297
311
  chalk.gray(' node index.mjs --clean temp # 清理临时目录') + '\n' +
298
312
  chalk.gray(' node index.mjs --clean logs # 清理日志文件') + '\n' +
299
313
  chalk.gray(' node index.mjs --clean cache # 清理缓存文件') + '\n\n' +
314
+ chalk.gray(' node index.mjs --sync-deps # 同步依赖版本') + '\n\n' +
300
315
  chalk.white('功能说明:') + '\n' +
301
316
  chalk.gray(' 🆕 初始化新小程序项目 - 创建新的小程序项目') + '\n' +
302
317
  chalk.gray(' 🔍 小程序打包 - 构建小程序包') + '\n' +
303
318
  chalk.gray(' 📦 小程序调试 - 启动真机调试服务') + '\n' +
319
+ chalk.gray(' 🔄 同步依赖版本 - 将 package.json 依赖对齐 shared-output.json') + '\n' +
304
320
  chalk.gray(' 🧹 清理模式 - 清理临时文件') + '\n',
305
321
  {
306
322
  padding: 1,
@@ -322,6 +338,7 @@ inquirer
322
338
  { name: '🆕 初始化新小程序项目', value: ACTION_TYPES.INIT },
323
339
  { name: '🔍 小程序 - 打包小程序', value: ACTION_TYPES.MP_BUILD },
324
340
  { name: '📦 小程序 - 真机调试', value: ACTION_TYPES.MP_DEBUG },
341
+ { name: '🔄 同步依赖版本 - 对齐 shared-output.json', value: ACTION_TYPES.SYNC_DEPS },
325
342
  { name: '🧹 清理模式 - 清理临时文件', value: ACTION_TYPES.CLEAN },
326
343
  { name: '👋 退出', value: ACTION_TYPES.QUIT },
327
344
  ],
@@ -0,0 +1,117 @@
1
+ import path from 'path';
2
+ import fse from 'fs-extra';
3
+ import chalk from 'chalk';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { confirm } from '@inquirer/prompts';
6
+ import { Logger } from './logger.mjs';
7
+ import { getProjectRoot } from './utils.mjs';
8
+
9
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
10
+ const projectPath = getProjectRoot();
11
+ const sharedOutputPath = path.join(__dirname, './shared-output.json');
12
+
13
+ const DEPENDENCY_SECTIONS = [
14
+ 'dependencies',
15
+ 'devDependencies',
16
+ 'peerDependencies',
17
+ 'optionalDependencies',
18
+ ];
19
+
20
+ /**
21
+ * 规范化版本号,去除前缀符号以便比较
22
+ * @param {string} version
23
+ * @returns {string}
24
+ */
25
+ function normalizeVersion(version) {
26
+ return String(version || '').replace(/^[~^>=<]+/, '');
27
+ }
28
+
29
+ /**
30
+ * 收集 package.json 中与 shared-output.json 匹配的待同步项
31
+ * @param {Record<string, string>} packageJson
32
+ * @param {Record<string, string>} sharedVersions
33
+ * @returns {Array<{ name: string, section: string, from: string, to: string }>}
34
+ */
35
+ function collectSyncChanges(packageJson, sharedVersions) {
36
+ const changes = [];
37
+
38
+ for (const section of DEPENDENCY_SECTIONS) {
39
+ const deps = packageJson[section];
40
+ if (!deps) {
41
+ continue;
42
+ }
43
+
44
+ for (const [name, version] of Object.entries(deps)) {
45
+ if (!Object.hasOwn(sharedVersions, name)) {
46
+ continue;
47
+ }
48
+
49
+ const targetVersion = sharedVersions[name];
50
+ if (normalizeVersion(version) !== targetVersion) {
51
+ changes.push({
52
+ name,
53
+ section,
54
+ from: version,
55
+ to: targetVersion,
56
+ });
57
+ }
58
+ }
59
+ }
60
+
61
+ return changes;
62
+ }
63
+
64
+ /**
65
+ * 将 shared-output.json 中的版本同步到项目根目录 package.json
66
+ * @param {{ skipConfirm?: boolean }} [options]
67
+ */
68
+ export async function syncDepsShell(options = {}) {
69
+ const { skipConfirm = false } = options;
70
+ const packageJsonPath = path.join(projectPath, 'package.json');
71
+
72
+ if (!fse.pathExistsSync(packageJsonPath)) {
73
+ Logger.error(`未找到 package.json: ${packageJsonPath}`);
74
+ return;
75
+ }
76
+
77
+ if (!fse.pathExistsSync(sharedOutputPath)) {
78
+ Logger.error(`未找到 shared-output.json: ${sharedOutputPath}`);
79
+ return;
80
+ }
81
+
82
+ const sharedVersions = fse.readJsonSync(sharedOutputPath);
83
+ const packageJson = fse.readJsonSync(packageJsonPath);
84
+ const changes = collectSyncChanges(packageJson, sharedVersions);
85
+
86
+ if (changes.length === 0) {
87
+ Logger.success('所有相关依赖版本已与 shared-output.json 一致,无需同步');
88
+ return;
89
+ }
90
+
91
+ Logger.info(`发现 ${changes.length} 个依赖需要同步:`);
92
+ for (const change of changes) {
93
+ console.log(
94
+ ` ${chalk.cyan(change.name)} ` +
95
+ `[${chalk.gray(change.section)}] ` +
96
+ `${chalk.yellow(change.from)} -> ${chalk.green(change.to)}`
97
+ );
98
+ }
99
+
100
+ const confirmed = skipConfirm || await confirm({
101
+ message: '确认将以上依赖版本同步为 shared-output.json 中的版本?',
102
+ default: true,
103
+ });
104
+
105
+ if (!confirmed) {
106
+ Logger.info('已取消同步操作');
107
+ return;
108
+ }
109
+
110
+ for (const change of changes) {
111
+ packageJson[change.section][change.name] = change.to;
112
+ }
113
+
114
+ fse.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
115
+ Logger.success(`已同步 ${changes.length} 个依赖版本到 ${packageJsonPath}`);
116
+ Logger.info('请运行 npm install 或 pnpm install 安装更新后的依赖');
117
+ }