@mindbase/node-tools 1.3.12 → 1.3.13

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/README.md CHANGED
@@ -154,9 +154,20 @@ npmpublish project --init
154
154
  ### 配置文件
155
155
 
156
156
  - **全局配置**: `~/.node-tools-publish.json` - 存储注册源和 token
157
- - **项目配置**: `./.node-tools-publish.json` - 存储项目默认配置
157
+ - **项目配置**: `package.json` `publishConfig` 字段
158
+
159
+ ```json
160
+ {
161
+ "publishConfig": {
162
+ "defaultRegistries": ["npm", "codeup"],
163
+ "defaultTokens": {
164
+ "codeup": "token-1"
165
+ }
166
+ }
167
+ }
168
+ ```
158
169
 
159
- > 注意: 请将 `.node-tools-publish.json` 添加到 `.gitignore`
170
+ > 兼容性提示: 如果存在旧版 `.node-tools-publish.json` 文件,仍可正常使用,建议迁移到 `package.json`
160
171
 
161
172
  ---
162
173
 
package/bin/publish.js CHANGED
@@ -222,8 +222,7 @@ program
222
222
 
223
223
  await projectConfig.initConfig(registryId, tokenId);
224
224
  console.log(chalk.green('\n✓ 项目配置已初始化'));
225
- console.log(chalk.gray(` 配置文件: ${projectConfig.configPath}`));
226
- console.log(chalk.yellow(' 注意: 请将 .node-tools-publish.json 添加到 .gitignore'));
225
+ console.log(chalk.gray(` 配置文件: package.json (publishConfig)`));
227
226
  } else {
228
227
  projectConfig.showConfig();
229
228
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mindbase/node-tools",
3
- "version": "1.3.12",
3
+ "version": "1.3.13",
4
4
  "description": "Node.js 开发工具集合:清理 node_modules、查看 Git 日志、发布包",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -41,6 +41,12 @@
41
41
  "node": "20.20.0"
42
42
  },
43
43
  "publishConfig": {
44
- "access": "public"
44
+ "access": "public",
45
+ "defaultRegistries": [
46
+ "npm-official"
47
+ ],
48
+ "defaultTokens": {
49
+ "npm-official": "npm-official-1770867074417"
50
+ }
45
51
  }
46
52
  }
@@ -1,18 +1,18 @@
1
1
  /**
2
2
  * 项目配置管理模块
3
- * 每个项目记录自己默认的源和 token ID
3
+ * package.json 的 publishConfig 字段读取项目默认配置
4
+ * 兼容旧的 .node-tools-publish.json 文件
4
5
  */
5
6
 
6
7
  const { existsSync, readFileSync, writeFileSync } = require('fs');
7
8
  const { join } = require('path');
8
9
 
9
- const PROJECT_CONFIG_FILENAME = '.node-tools-publish.json';
10
+ const OLD_CONFIG_FILENAME = '.node-tools-publish.json';
11
+ const PACKAGE_JSON_FILENAME = 'package.json';
10
12
 
11
- const DEFAULT_PROJECT_CONFIG = {
12
- defaults: {
13
- registries: [],
14
- tokens: {} // { registryId: tokenId }
15
- }
13
+ const DEFAULT_PUBLISH_CONFIG = {
14
+ defaultRegistries: [],
15
+ defaultTokens: {} // { registryId: tokenId }
16
16
  };
17
17
 
18
18
  /**
@@ -21,103 +21,173 @@ const DEFAULT_PROJECT_CONFIG = {
21
21
  class ProjectConfig {
22
22
  constructor(projectPath) {
23
23
  this.projectPath = projectPath;
24
- this.configPath = join(projectPath, PROJECT_CONFIG_FILENAME);
24
+ this.packageJsonPath = join(projectPath, PACKAGE_JSON_FILENAME);
25
+ this.oldConfigPath = join(projectPath, OLD_CONFIG_FILENAME);
25
26
  }
26
27
 
27
28
  /**
28
- * 读取项目配置
29
+ * 读取 package.json
29
30
  */
30
- getProjectConfig() {
31
- if (!existsSync(this.configPath)) {
32
- return JSON.parse(JSON.stringify(DEFAULT_PROJECT_CONFIG));
31
+ getPackageJson() {
32
+ if (!existsSync(this.packageJsonPath)) {
33
+ return {};
33
34
  }
34
35
 
35
36
  try {
36
- const content = readFileSync(this.configPath, 'utf-8');
37
+ const content = readFileSync(this.packageJsonPath, 'utf-8');
37
38
  return JSON.parse(content);
38
39
  } catch (error) {
39
- console.error('项目配置文件读取失败');
40
- return JSON.parse(JSON.stringify(DEFAULT_PROJECT_CONFIG));
40
+ console.error('package.json 读取失败');
41
+ return {};
42
+ }
43
+ }
44
+
45
+ /**
46
+ * 保存 package.json
47
+ */
48
+ setPackageJson(pkg) {
49
+ writeFileSync(this.packageJsonPath, JSON.stringify(pkg, null, 2) + '\n', 'utf-8');
50
+ }
51
+
52
+ /**
53
+ * 读取发布配置(兼容旧文件)
54
+ */
55
+ getPublishConfig() {
56
+ const pkg = this.getPackageJson();
57
+
58
+ // 优先从 package.json.publishConfig 读取
59
+ if (pkg.publishConfig) {
60
+ return {
61
+ defaultRegistries: pkg.publishConfig.defaultRegistries || [],
62
+ defaultTokens: pkg.publishConfig.defaultTokens || {}
63
+ };
64
+ }
65
+
66
+ // 兼容旧的 .node-tools-publish.json
67
+ if (existsSync(this.oldConfigPath)) {
68
+ try {
69
+ const content = readFileSync(this.oldConfigPath, 'utf-8');
70
+ const oldConfig = JSON.parse(content);
71
+ // 迁移提示
72
+ console.log(chalk.yellow('检测到旧配置文件,建议迁移到 package.json'));
73
+ return {
74
+ defaultRegistries: oldConfig.defaults?.registries || [],
75
+ defaultTokens: oldConfig.defaults?.tokens || {}
76
+ };
77
+ } catch (error) {
78
+ // 忽略旧文件错误
79
+ }
41
80
  }
81
+
82
+ return DEFAULT_PUBLISH_CONFIG;
42
83
  }
43
84
 
44
85
  /**
45
- * 保存项目配置
86
+ * 保存发布配置到 package.json
46
87
  */
47
- setProjectConfig(config) {
48
- writeFileSync(this.configPath, JSON.stringify(config, null, 2), 'utf-8');
88
+ setPublishConfig(config) {
89
+ const pkg = this.getPackageJson();
90
+
91
+ // 确保 publishConfig 存在
92
+ if (!pkg.publishConfig) {
93
+ pkg.publishConfig = {};
94
+ }
95
+
96
+ // 保留 npm 标准 registry 字段
97
+ const npmRegistry = pkg.publishConfig.registry;
98
+
99
+ // 设置我们的配置
100
+ pkg.publishConfig.defaultRegistries = config.defaultRegistries;
101
+ pkg.publishConfig.defaultTokens = config.defaultTokens;
102
+
103
+ // 恢复 npm registry
104
+ if (npmRegistry) {
105
+ pkg.publishConfig.registry = npmRegistry;
106
+ }
107
+
108
+ this.setPackageJson(pkg);
49
109
  }
50
110
 
51
111
  /**
52
112
  * 获取默认源列表
53
113
  */
54
114
  getDefaultRegistries() {
55
- const config = this.getProjectConfig();
56
- return config.defaults.registries || [];
115
+ const config = this.getPublishConfig();
116
+ return config.defaultRegistries || [];
57
117
  }
58
118
 
59
119
  /**
60
120
  * 设置默认源列表
61
121
  */
62
122
  setDefaultRegistries(registryIds) {
63
- const config = this.getProjectConfig();
64
- config.defaults.registries = registryIds;
123
+ const config = this.getPublishConfig();
124
+ config.defaultRegistries = registryIds;
65
125
 
66
126
  // 清理无效的 token 映射
67
127
  const newTokens = {};
68
128
  for (const registryId of registryIds) {
69
- if (config.defaults.tokens[registryId]) {
70
- newTokens[registryId] = config.defaults.tokens[registryId];
129
+ if (config.defaultTokens[registryId]) {
130
+ newTokens[registryId] = config.defaultTokens[registryId];
71
131
  }
72
132
  }
73
- config.defaults.tokens = newTokens;
133
+ config.defaultTokens = newTokens;
74
134
 
75
- this.setProjectConfig(config);
135
+ this.setPublishConfig(config);
76
136
  }
77
137
 
78
138
  /**
79
139
  * 获取指定源的默认 token ID
80
140
  */
81
141
  getDefaultTokenId(registryId) {
82
- const config = this.getProjectConfig();
83
- return config.defaults.tokens[registryId];
142
+ const config = this.getPublishConfig();
143
+ return config.defaultTokens[registryId];
84
144
  }
85
145
 
86
146
  /**
87
147
  * 设置指定源的默认 token ID
88
148
  */
89
149
  setDefaultToken(registryId, tokenId) {
90
- const config = this.getProjectConfig();
91
- config.defaults.tokens[registryId] = tokenId;
92
- this.setProjectConfig(config);
150
+ const config = this.getPublishConfig();
151
+ config.defaultTokens[registryId] = tokenId;
152
+ this.setPublishConfig(config);
93
153
  }
94
154
 
95
155
  /**
96
156
  * 初始化项目配置
97
157
  */
98
158
  async initConfig(defaultRegistryId, defaultTokenId) {
99
- const config = this.getProjectConfig();
100
- config.defaults.registries = [defaultRegistryId];
101
- config.defaults.tokens = {
102
- [defaultRegistryId]: defaultTokenId
159
+ const config = {
160
+ defaultRegistries: [defaultRegistryId],
161
+ defaultTokens: {
162
+ [defaultRegistryId]: defaultTokenId
163
+ }
103
164
  };
104
- this.setProjectConfig(config);
165
+ this.setPublishConfig(config);
105
166
  }
106
167
 
107
168
  /**
108
169
  * 显示配置
109
170
  */
110
171
  showConfig() {
111
- const config = this.getProjectConfig();
172
+ const config = this.getPublishConfig();
173
+ const pkg = this.getPackageJson();
112
174
 
113
- console.log('项目配置:');
114
- console.log(` 配置文件: ${this.configPath}`);
115
- console.log(`\n默认源: ${config.defaults.registries.join(', ') || '未设置'}`);
175
+ console.log('项目发布配置:');
176
+ console.log(` 配置文件: ${this.packageJsonPath}`);
177
+ console.log(`\n默认源: ${config.defaultRegistries.join(', ') || '未设置'}`);
116
178
 
117
179
  console.log('\n默认 Tokens:');
118
- for (const [registryId, tokenId] of Object.entries(config.defaults.tokens)) {
180
+ if (Object.keys(config.defaultTokens).length === 0) {
181
+ console.log(' 未设置');
182
+ }
183
+ for (const [registryId, tokenId] of Object.entries(config.defaultTokens)) {
119
184
  console.log(` ${registryId}: ${tokenId}`);
120
185
  }
186
+
187
+ // 兼容性提示
188
+ if (existsSync(this.oldConfigPath)) {
189
+ console.log(chalk.yellow(`\n提示: 检测到旧配置文件 ${OLD_CONFIG_FILENAME},可删除`));
190
+ }
121
191
  }
122
192
 
123
193
  /**
@@ -128,12 +198,17 @@ class ProjectConfig {
128
198
  const { spawn } = require('child_process');
129
199
  const { platform } = process;
130
200
 
131
- // 确保配置文件存在
132
- if (!existsSync(this.configPath)) {
133
- this.setProjectConfig(DEFAULT_PROJECT_CONFIG);
201
+ // 确保 publishConfig 字段存在
202
+ const pkg = this.getPackageJson();
203
+ if (!pkg.publishConfig) {
204
+ pkg.publishConfig = {
205
+ defaultRegistries: [],
206
+ defaultTokens: {}
207
+ };
208
+ this.setPackageJson(pkg);
134
209
  }
135
210
 
136
- console.log(`打开项目配置文件: ${this.configPath}`);
211
+ console.log(`打开项目配置文件: ${this.packageJsonPath}`);
137
212
 
138
213
  // 优先使用 code(VS Code)
139
214
  const editorCmd = process.env.EDITOR || (platform === 'win32' ? 'code' : 'vim');
@@ -143,7 +218,7 @@ class ProjectConfig {
143
218
  ? { shell: true, stdio: 'inherit' }
144
219
  : { stdio: 'inherit' };
145
220
 
146
- spawn('code', [this.configPath, '--wait'], spawnOptions)
221
+ spawn('code', [this.packageJsonPath, '--wait'], spawnOptions)
147
222
  .on('exit', (code) => {
148
223
  if (code === 0) {
149
224
  console.log('\n配置已保存');
@@ -153,20 +228,21 @@ class ProjectConfig {
153
228
  })
154
229
  .on('error', () => {
155
230
  console.log('VS Code 不可用,使用默认编辑器...');
156
- edit(this.configPath, (code) => {
231
+ edit(this.packageJsonPath, (code) => {
157
232
  console.log(code === 0 ? '\n配置已保存' : `\n编辑器退出,代码: ${code}`);
158
233
  });
159
234
  });
160
235
  } else {
161
- edit(this.configPath, (code) => {
236
+ edit(this.packageJsonPath, (code) => {
162
237
  console.log(code === 0 ? '\n配置已保存' : `\n编辑器退出,代码: ${code}`);
163
238
  });
164
239
  }
165
240
  }
166
241
  }
167
242
 
243
+ const chalk = require('chalk');
244
+
168
245
  module.exports = {
169
246
  ProjectConfig,
170
- PROJECT_CONFIG_FILENAME,
171
- DEFAULT_PROJECT_CONFIG
247
+ DEFAULT_PUBLISH_CONFIG
172
248
  };