neo-cmp-cli 1.3.8 → 1.3.9

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": "neo-cmp-cli",
3
- "version": "1.3.8",
3
+ "version": "1.3.9",
4
4
  "description": "前端脚手架:自定义组件开发工具,支持react 和 vue2.0技术栈。",
5
5
  "keywords": [
6
6
  "neo-cli",
@@ -1,6 +1,7 @@
1
1
  const { gitClone } = require('akfun');
2
2
  const { consoleTag } = require('../utils/neoParams'); // 输出标记
3
3
  const { replaceInPackage } = require('../utils/replaceInPackage');
4
+ const { resetPackageVersion } = require('../utils/resetPackageVersion');
4
5
 
5
6
  const templateList = {
6
7
  react: {
@@ -41,6 +42,7 @@ const neoInit = function (type, projectName) {
41
42
  replaceInPackage(finalProjectName, currentTemplate.projectName, finalProjectName);
42
43
  replaceInPackage(finalProjectPath, 'wibetter', 'xxx');
43
44
  replaceInPackage(finalProjectPath, 'neo自定义组件模板', 'neo自定义组件');
45
+ resetPackageVersion(finalProjectPath);
44
46
  },
45
47
  consoleTag
46
48
  );
@@ -2,6 +2,7 @@ const fs = require('fs-extra');
2
2
  const path = require('path');
3
3
  const { consoleTag } = require('../utils/neoParams'); // 输出标记
4
4
  const { replaceInPackage } = require('../utils/replaceInPackage');
5
+ const { resetPackageVersion } = require('../utils/resetPackageVersion');
5
6
 
6
7
  const templateList = {
7
8
  react: {
@@ -46,6 +47,8 @@ const neoInitByCopy = function (type, projectName) {
46
47
  replaceInPackage(finalProjectPath, currentTemplate.projectName, finalProjectName);
47
48
  replaceInPackage(finalProjectPath, 'wibetter', 'xxx');
48
49
  replaceInPackage(finalProjectPath, 'neo自定义组件模板', 'neo自定义组件');
50
+ resetPackageVersion(finalProjectPath);
51
+
49
52
  console.log(`${consoleTag}已创建自定义组件(${finalProjectName})!`);
50
53
  })
51
54
  .catch((err) => console.error(`${consoleTag}自定义组件模板下载失败:`, err));
@@ -0,0 +1,115 @@
1
+ const fs = require('fs-extra');
2
+ const path = require('path');
3
+
4
+ /**
5
+ * 重置 package.json 文件中的版本号到指定版本
6
+ * @param dir 项目目录
7
+ * @param version 目标版本号,默认为 '1.0.0'
8
+ * @returns 替换结果对象: { success: boolean, message: string, filePath?: string, oldVersion?: string, newVersion?: string }
9
+ */
10
+ function resetPackageVersion(dir, version = '1.0.0') {
11
+ try {
12
+ // 检查目录是否存在
13
+ if (!fs.existsSync(dir)) {
14
+ return {
15
+ success: false,
16
+ message: `目录不存在: ${dir}`
17
+ };
18
+ }
19
+
20
+ // 检查是否为目录
21
+ const stat = fs.statSync(dir);
22
+ if (!stat.isDirectory()) {
23
+ return {
24
+ success: false,
25
+ message: `指定路径不是目录: ${dir}`
26
+ };
27
+ }
28
+
29
+ // 构建package.json的完整路径
30
+ const packageJsonPath = path.join(dir, 'package.json');
31
+
32
+ // 检查package.json文件是否存在
33
+ if (!fs.existsSync(packageJsonPath)) {
34
+ return {
35
+ success: false,
36
+ message: `package.json文件不存在: ${packageJsonPath}`
37
+ };
38
+ }
39
+
40
+ // 读取并解析package.json文件
41
+ const content = fs.readFileSync(packageJsonPath, 'utf8');
42
+ let packageJson = {};
43
+
44
+ try {
45
+ packageJson = JSON.parse(content);
46
+ } catch (error) {
47
+ return {
48
+ success: false,
49
+ message: `package.json 文件解析失败: ${error.message}。`
50
+ };
51
+ }
52
+
53
+
54
+ // 保存旧版本号
55
+ const oldVersion = packageJson.version || '未定义';
56
+
57
+ // 检查版本号是否已经是目标版本
58
+ if (packageJson.version === version) {
59
+ return {
60
+ success: true,
61
+ message: `package.json 版本号已经是 ${version}`,
62
+ filePath: packageJsonPath,
63
+ oldVersion,
64
+ newVersion: version
65
+ };
66
+ }
67
+
68
+ // 更新版本号
69
+ packageJson.version = version;
70
+
71
+ // 写入package.json,保持格式化(2空格缩进)
72
+ fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n', 'utf8');
73
+
74
+ return {
75
+ success: true,
76
+ message: `已将 package.json 版本号从 ${oldVersion} 重置为 ${version}`,
77
+ filePath: packageJsonPath,
78
+ oldVersion,
79
+ newVersion: version
80
+ };
81
+ } catch (error) {
82
+ return {
83
+ success: false,
84
+ message: `resetPackageVersion 运行失败: ${error.message}`
85
+ };
86
+ }
87
+ }
88
+
89
+ /**
90
+ * 批量重置多个目录中 package.json 文件的版本号
91
+ * @param dirs 目录数组
92
+ * @param version 目标版本号,默认为 '1.0.0'
93
+ * @returns 批量重置结果
94
+ */
95
+ function resetVersionInMulPackages(dirs, version = '1.0.0') {
96
+ const results = dirs.map((dir) => ({
97
+ dir,
98
+ result: resetPackageVersion(dir, version)
99
+ }));
100
+
101
+ const successCount = results.filter((r) => r.result.success).length;
102
+ const failedCount = results.length - successCount;
103
+
104
+ return {
105
+ total: dirs.length,
106
+ success: successCount,
107
+ failed: failedCount,
108
+ results
109
+ };
110
+ }
111
+
112
+ module.exports = {
113
+ resetPackageVersion,
114
+ resetVersionInMulPackages
115
+ };