@htyf-mp/cli 0.3.2 → 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 +1 -1
- package/src/HtyfImportsPlugin.mjs +37 -31
- package/src/constants.mjs +1 -0
- package/src/index.mjs +17 -0
- package/src/shared-output.json +92 -78
- package/src/sync-deps.mjs +117 -0
package/package.json
CHANGED
|
@@ -40,59 +40,64 @@ function getSharedConfig() {
|
|
|
40
40
|
// ==================== 工具函数 ====================
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* 从 module.context 中提取包名与真实包根目录
|
|
44
44
|
* @param {string} moduleContext - 模块上下文路径
|
|
45
|
-
* @returns {string|null}
|
|
45
|
+
* @returns {{ packageName: string, packageRoot: string }|null}
|
|
46
46
|
*/
|
|
47
|
-
function
|
|
47
|
+
function extractPackageInfo(moduleContext) {
|
|
48
48
|
if (!moduleContext?.includes('node_modules')) {
|
|
49
49
|
return null;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
const match = moduleContext.match(PNPM_PACKAGE_REGEX);
|
|
53
|
-
|
|
53
|
+
if (!match?.[1]) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
packageName: match[1],
|
|
59
|
+
packageRoot: moduleContext.slice(0, match.index + match[0].length),
|
|
60
|
+
};
|
|
54
61
|
}
|
|
55
62
|
|
|
56
63
|
/**
|
|
57
64
|
* 检查包是否包含 native 代码(带缓存)
|
|
58
|
-
* @param {string}
|
|
65
|
+
* @param {string} packageRoot - 包在 node_modules 中的真实根目录
|
|
59
66
|
* @returns {boolean} 是否包含 native 代码
|
|
60
67
|
*/
|
|
61
|
-
function hasNativeCode(
|
|
62
|
-
if (!
|
|
68
|
+
function hasNativeCode(packageRoot) {
|
|
69
|
+
if (!packageRoot) {
|
|
63
70
|
return false;
|
|
64
71
|
}
|
|
65
72
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return nativeCodeCache.get(packageName);
|
|
73
|
+
if (nativeCodeCache.has(packageRoot)) {
|
|
74
|
+
return nativeCodeCache.get(packageRoot);
|
|
69
75
|
}
|
|
70
76
|
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
77
|
+
const iosPath = path.join(packageRoot, 'ios');
|
|
78
|
+
const androidPath = path.join(packageRoot, 'android');
|
|
79
|
+
|
|
75
80
|
const hasNative = fse.pathExistsSync(iosPath) || fse.pathExistsSync(androidPath);
|
|
76
|
-
nativeCodeCache.set(
|
|
77
|
-
|
|
81
|
+
nativeCodeCache.set(packageRoot, hasNative);
|
|
82
|
+
|
|
78
83
|
return hasNative;
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
/**
|
|
82
87
|
* 获取包版本(带缓存)
|
|
83
|
-
* @param {string}
|
|
88
|
+
* @param {string} packageRoot - 包在 node_modules 中的真实根目录
|
|
84
89
|
* @returns {string|null} 包版本,如果无法获取则返回 null
|
|
85
90
|
*/
|
|
86
|
-
function getPackageVersion(
|
|
87
|
-
if (!
|
|
91
|
+
function getPackageVersion(packageRoot) {
|
|
92
|
+
if (!packageRoot) {
|
|
88
93
|
return null;
|
|
89
94
|
}
|
|
90
95
|
|
|
91
|
-
if (versionCache.has(
|
|
92
|
-
return versionCache.get(
|
|
96
|
+
if (versionCache.has(packageRoot)) {
|
|
97
|
+
return versionCache.get(packageRoot);
|
|
93
98
|
}
|
|
94
99
|
|
|
95
|
-
const pkgJsonPath = path.join(
|
|
100
|
+
const pkgJsonPath = path.join(packageRoot, 'package.json');
|
|
96
101
|
let version = null;
|
|
97
102
|
|
|
98
103
|
try {
|
|
@@ -101,11 +106,10 @@ function getPackageVersion(packageName) {
|
|
|
101
106
|
version = pkgJson.version || null;
|
|
102
107
|
}
|
|
103
108
|
} catch (error) {
|
|
104
|
-
// 静默失败,返回 null
|
|
105
109
|
version = null;
|
|
106
110
|
}
|
|
107
111
|
|
|
108
|
-
versionCache.set(
|
|
112
|
+
versionCache.set(packageRoot, version);
|
|
109
113
|
return version;
|
|
110
114
|
}
|
|
111
115
|
|
|
@@ -290,29 +294,31 @@ export class HtyfModulesPlugin {
|
|
|
290
294
|
continue;
|
|
291
295
|
}
|
|
292
296
|
|
|
293
|
-
const
|
|
294
|
-
if (!
|
|
297
|
+
const packageInfo = extractPackageInfo(module.context);
|
|
298
|
+
if (!packageInfo) {
|
|
295
299
|
continue;
|
|
296
300
|
}
|
|
297
301
|
|
|
302
|
+
const { packageName, packageRoot } = packageInfo;
|
|
303
|
+
|
|
298
304
|
// 如果已经处理过该包,跳过
|
|
299
305
|
if (dependencyMap.has(packageName)) {
|
|
300
306
|
const existing = dependencyMap.get(packageName);
|
|
301
|
-
// 如果之前不是 native
|
|
302
|
-
if (!existing.nativeModule && hasNativeCode(
|
|
307
|
+
// 如果之前不是 native 模块,基于当前 module 的真实路径再次检查
|
|
308
|
+
if (!existing.nativeModule && hasNativeCode(packageRoot)) {
|
|
303
309
|
existing.nativeModule = true;
|
|
304
310
|
nativeModules.add(packageName);
|
|
305
311
|
}
|
|
306
312
|
continue;
|
|
307
313
|
}
|
|
308
314
|
|
|
309
|
-
//
|
|
315
|
+
// 首次处理该包,version / nativeModule 均基于 module.context 解析出的真实路径
|
|
310
316
|
// react-native 必须视为原生依赖,无论是否检测到 ios/android 目录
|
|
311
317
|
const nativeModule =
|
|
312
|
-
packageName === "react-native" ? true : hasNativeCode(
|
|
318
|
+
packageName === "react-native" ? true : hasNativeCode(packageRoot);
|
|
313
319
|
const dependencyInfo = {
|
|
314
320
|
name: packageName,
|
|
315
|
-
version: getPackageVersion(
|
|
321
|
+
version: getPackageVersion(packageRoot),
|
|
316
322
|
nativeModule,
|
|
317
323
|
};
|
|
318
324
|
|
package/src/constants.mjs
CHANGED
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
|
],
|
package/src/shared-output.json
CHANGED
|
@@ -1,101 +1,115 @@
|
|
|
1
1
|
{
|
|
2
|
-
"@bam.tech/react-native-image-resizer": "3.0.11",
|
|
3
2
|
"@borndotcom/react-native-godot": "1.0.1",
|
|
4
3
|
"@boterop/react-native-background-timer": "2.6.0",
|
|
5
|
-
"@callstack/repack": "5.2.
|
|
4
|
+
"@callstack/repack": "5.2.5",
|
|
6
5
|
"@d11/react-native-fast-image": "8.13.0",
|
|
7
|
-
"@dr.pogodin/react-native-fs": "2.
|
|
8
|
-
"@
|
|
9
|
-
"@
|
|
10
|
-
"@react-native-async-storage/async-storage": "
|
|
6
|
+
"@dr.pogodin/react-native-fs": "2.39.1",
|
|
7
|
+
"@kesha-antonov/react-native-background-downloader": "4.5.6",
|
|
8
|
+
"@lodev09/react-native-true-sheet": "3.11.1",
|
|
9
|
+
"@react-native-async-storage/async-storage": "3.1.1",
|
|
11
10
|
"@react-native-camera-roll/camera-roll": "7.10.2",
|
|
12
11
|
"@react-native-clipboard/clipboard": "1.16.3",
|
|
13
12
|
"@react-native-community/blur": "4.4.1",
|
|
14
13
|
"@react-native-community/checkbox": "0.5.20",
|
|
15
|
-
"@react-native-community/datetimepicker": "
|
|
16
|
-
"@react-native-community/
|
|
17
|
-
"@react-native-community/
|
|
18
|
-
"@react-native-
|
|
19
|
-
"@react-native-
|
|
20
|
-
"@react-native-documents/picker": "11.0.3",
|
|
14
|
+
"@react-native-community/datetimepicker": "9.1.0",
|
|
15
|
+
"@react-native-community/netinfo": "12.0.1",
|
|
16
|
+
"@react-native-community/slider": "5.2.0",
|
|
17
|
+
"@react-native-documents/picker": "12.0.1",
|
|
18
|
+
"@react-native-documents/viewer": "4.0.0",
|
|
21
19
|
"@react-native-masked-view/masked-view": "0.3.2",
|
|
22
20
|
"@react-native-picker/picker": "2.11.4",
|
|
23
21
|
"@react-native-segmented-control/segmented-control": "2.5.7",
|
|
24
|
-
"@sayem314/react-native-keep-awake": "
|
|
25
|
-
"@shopify/react-native-skia": "2.
|
|
26
|
-
"expo": "
|
|
27
|
-
"expo-asset": "
|
|
28
|
-
"expo-audio": "
|
|
29
|
-
"expo-brightness": "
|
|
30
|
-
"expo-camera": "
|
|
31
|
-
"expo-clipboard": "
|
|
32
|
-
"expo-constants": "
|
|
33
|
-
"expo-crypto": "
|
|
34
|
-
"expo-document-picker": "
|
|
35
|
-
"expo-
|
|
36
|
-
"expo-
|
|
37
|
-
"expo-
|
|
38
|
-
"expo-
|
|
39
|
-
"expo-
|
|
40
|
-
"expo-
|
|
41
|
-
"expo-
|
|
42
|
-
"expo-
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"react-native
|
|
49
|
-
"react-
|
|
50
|
-
"react-native-
|
|
51
|
-
"react-native-
|
|
52
|
-
"react-native-
|
|
53
|
-
"react-native-
|
|
22
|
+
"@sayem314/react-native-keep-awake": "2.0.0",
|
|
23
|
+
"@shopify/react-native-skia": "2.6.8",
|
|
24
|
+
"expo": "56.0.12",
|
|
25
|
+
"expo-asset": "56.0.17",
|
|
26
|
+
"expo-audio": "56.0.12",
|
|
27
|
+
"expo-brightness": "56.0.5",
|
|
28
|
+
"expo-camera": "56.0.8",
|
|
29
|
+
"expo-clipboard": "56.0.4",
|
|
30
|
+
"expo-constants": "56.0.18",
|
|
31
|
+
"expo-crypto": "56.0.4",
|
|
32
|
+
"expo-document-picker": "56.0.4",
|
|
33
|
+
"expo-file-system": "56.0.8",
|
|
34
|
+
"expo-font": "56.0.7",
|
|
35
|
+
"expo-keep-awake": "56.0.3",
|
|
36
|
+
"expo-linear-gradient": "56.0.4",
|
|
37
|
+
"expo-linking": "56.0.14",
|
|
38
|
+
"expo-modules-autolinking": "56.0.16",
|
|
39
|
+
"expo-modules-core": "56.0.17",
|
|
40
|
+
"expo-router": "56.2.11",
|
|
41
|
+
"expo-system-ui": "56.0.5",
|
|
42
|
+
"expo-video": "56.1.4",
|
|
43
|
+
"jcore-react-native": "2.3.6",
|
|
44
|
+
"jpush-react-native": "3.2.7",
|
|
45
|
+
"llama.rn": "0.12.5",
|
|
46
|
+
"lottie-react-native": "7.3.8",
|
|
47
|
+
"react-airplay": "2.0.2",
|
|
48
|
+
"react-native-agora": "4.6.2",
|
|
49
|
+
"react-native-avoid-softinput": "9.0.0",
|
|
50
|
+
"react-native-background-actions": "4.1.0",
|
|
51
|
+
"react-native-ble-manager": "12.5.0",
|
|
52
|
+
"react-native-blob-util": "0.24.10",
|
|
53
|
+
"react-native-camera-kit": "18.0.0",
|
|
54
54
|
"react-native-config": "1.6.1",
|
|
55
|
-
"react-native-device-info": "15.0.
|
|
56
|
-
"react-native-edge-to-edge": "1.
|
|
57
|
-
"react-native-enriched": "0.
|
|
58
|
-
"react-native-
|
|
59
|
-
"react-native-
|
|
55
|
+
"react-native-device-info": "15.0.2",
|
|
56
|
+
"react-native-edge-to-edge": "1.8.1",
|
|
57
|
+
"react-native-enriched-html": "1.0.0",
|
|
58
|
+
"react-native-enriched-markdown": "0.6.0",
|
|
59
|
+
"react-native-file-access": "4.0.3",
|
|
60
|
+
"react-native-gesture-handler": "3.0.2",
|
|
60
61
|
"react-native-get-random-values": "2.0.0",
|
|
61
|
-
"react-native-global-exception-handler": "1.0.
|
|
62
|
-
"react-native-google-mobile-ads": "16.0
|
|
63
|
-
"react-native-
|
|
64
|
-
"react-native-
|
|
65
|
-
"react-native-image-colors": "2.5.1",
|
|
62
|
+
"react-native-global-exception-handler": "1.0.2",
|
|
63
|
+
"react-native-google-mobile-ads": "16.4.0",
|
|
64
|
+
"react-native-iap": "15.3.4",
|
|
65
|
+
"react-native-image-colors": "2.6.0",
|
|
66
66
|
"react-native-image-picker": "8.2.1",
|
|
67
|
-
"react-native-keyboard-controller": "1.
|
|
68
|
-
"react-native-keychain": "10.0.0",
|
|
67
|
+
"react-native-keyboard-controller": "1.21.12",
|
|
69
68
|
"react-native-linear-gradient": "2.8.3",
|
|
70
|
-
"react-native-localize": "3.
|
|
71
|
-
"react-native-
|
|
72
|
-
"react-native-
|
|
69
|
+
"react-native-localize": "3.7.0",
|
|
70
|
+
"react-native-media-toolkit": "2.3.6",
|
|
71
|
+
"react-native-mmkv": "4.3.2",
|
|
72
|
+
"react-native-nitro-bg-timer-plus": "0.3.2",
|
|
73
|
+
"react-native-nitro-buffer": "0.2.2",
|
|
74
|
+
"react-native-nitro-cookies": "1.2.0",
|
|
75
|
+
"react-native-nitro-fetch": "1.4.2",
|
|
76
|
+
"react-native-nitro-geolocation": "1.4.1",
|
|
77
|
+
"react-native-nitro-haptics": "0.2.3",
|
|
78
|
+
"react-native-nitro-http-server": "1.9.2",
|
|
79
|
+
"react-native-nitro-markdown": "0.8.1",
|
|
80
|
+
"react-native-nitro-modules": "0.35.10",
|
|
81
|
+
"react-native-nitro-player": "1.4.3",
|
|
82
|
+
"react-native-nitro-sound": "0.2.15",
|
|
83
|
+
"react-native-nitro-sqlite": "9.6.0",
|
|
84
|
+
"react-native-nitro-sse": "2.3.1",
|
|
85
|
+
"react-native-nitro-text-decoder": "0.2.0",
|
|
86
|
+
"react-native-nitro-unzip": "0.5.3",
|
|
87
|
+
"react-native-nitro-websockets": "1.0.3",
|
|
73
88
|
"react-native-orientation-locker": "1.7.0",
|
|
74
|
-
"react-native-pager-view": "
|
|
75
|
-
"react-native-permissions": "5.
|
|
76
|
-
"react-native-
|
|
77
|
-
"react-native-quick-
|
|
78
|
-
"react-native-
|
|
79
|
-
"react-native-
|
|
80
|
-
"react-native-
|
|
81
|
-
"react-native-
|
|
82
|
-
"react-native-
|
|
83
|
-
"react-native-
|
|
84
|
-
"react-native-splash-view": "0.0.
|
|
85
|
-
"react-native-svg": "15.15.
|
|
89
|
+
"react-native-pager-view": "8.0.2",
|
|
90
|
+
"react-native-permissions": "5.6.0",
|
|
91
|
+
"react-native-qr-kit": "1.1.6",
|
|
92
|
+
"react-native-quick-base64": "3.0.0",
|
|
93
|
+
"react-native-quick-crypto": "1.1.5",
|
|
94
|
+
"react-native-reanimated": "4.5.0",
|
|
95
|
+
"react-native-restart": "0.0.28",
|
|
96
|
+
"react-native-safe-area-context": "5.8.0",
|
|
97
|
+
"react-native-screens": "4.25.2",
|
|
98
|
+
"react-native-share": "12.3.1",
|
|
99
|
+
"react-native-splash-view": "0.0.21",
|
|
100
|
+
"react-native-svg": "15.15.5",
|
|
101
|
+
"react-native-teleport": "1.1.9",
|
|
86
102
|
"react-native-track-player": "5.0.0-alpha0-nightly-359af5a12d712d3b685530aed9b9625865a25d74",
|
|
87
103
|
"react-native-vector-icons": "10.3.0",
|
|
88
|
-
"react-native-video": "6.
|
|
89
|
-
"react-native-view-shot": "
|
|
90
|
-
"react-native-vision-camera": "4.7.3",
|
|
104
|
+
"react-native-video": "6.19.2",
|
|
105
|
+
"react-native-view-shot": "5.1.1",
|
|
91
106
|
"react-native-vlc-media-player": "1.0.98",
|
|
92
107
|
"react-native-volume-manager": "2.0.8",
|
|
93
108
|
"react-native-webrtc": "124.0.7",
|
|
94
|
-
"react-native-webview": "
|
|
95
|
-
"react-native-wgpu": "0.4.1",
|
|
109
|
+
"react-native-webview": "14.0.1",
|
|
96
110
|
"react-native-wifi-reborn": "4.13.6",
|
|
97
|
-
"react-native-worklets": "0.
|
|
98
|
-
"react-native-worklets-core": "1.6.
|
|
99
|
-
"
|
|
100
|
-
"react-native": "0.
|
|
111
|
+
"react-native-worklets": "0.10.0",
|
|
112
|
+
"react-native-worklets-core": "1.6.3",
|
|
113
|
+
"whisper.rn": "0.6.0",
|
|
114
|
+
"react-native": "0.86.0"
|
|
101
115
|
}
|
|
@@ -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
|
+
}
|