@apdesign/code-style-react 1.1.5 → 1.1.7

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": "@apdesign/code-style-react",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "scripts": {},
5
5
  "bin": {
6
6
  "apdesign-code-style": "cli.js"
@@ -1,7 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  let spawnSync;
4
- let spawn;
5
4
  let path;
6
5
  let fs;
7
6
  let os;
@@ -28,39 +27,39 @@ async function runEslint() {
28
27
  // 参数解析
29
28
  const args = process.argv.slice(2);
30
29
 
31
- // 默认并发数为 CPU 核心数,但不超过 8
32
- let maxWorkers = Math.min(os.cpus().length, 8);
33
-
34
- let branchArg = null;
35
-
36
- // 找到 --max-workers 参数
30
+ let maxWorkers = null; // 默认由 ESLint API 决定
37
31
  const maxWorkersArgIndex = args.indexOf('--max-workers');
38
32
  if (maxWorkersArgIndex !== -1 && args[maxWorkersArgIndex + 1]) {
39
33
  const val = parseInt(args[maxWorkersArgIndex + 1], 10);
40
34
  if (!isNaN(val) && val > 0) {
41
- maxWorkers = Math.min(val, 8); // 限制最大值为 8
42
- }
43
- }
44
-
45
- // targetPath 是第一个不是 --max-workers 的参数
46
- let targetPath = process.cwd();
47
- for (let i = 0; i < args.length; i++) {
48
- if (i === maxWorkersArgIndex || i === maxWorkersArgIndex + 1) continue;
49
- if (!targetPath || targetPath === process.cwd()) {
50
- targetPath = args[i];
51
- continue;
35
+ maxWorkers = val;
52
36
  }
53
37
  }
54
38
 
55
39
  // 检查是否有分支参数 (--branch <branchName>)
40
+ let branchArg = null;
56
41
  const branchIndex = args.indexOf('--branch');
57
42
  if (branchIndex !== -1 && args[branchIndex + 1]) {
58
43
  branchArg = args[branchIndex + 1];
59
44
  }
60
45
 
61
- console.log(`\n[ESLint 启动配置]`);
62
- console.log(`- 检查路径: ${targetPath}`);
63
- console.log(`- 并发进程数: ${maxWorkers}`);
46
+ // targetPath 是第一个不是 --max-workers 的参数
47
+ let targetPath = process.cwd();
48
+ for (let i = 0; i < args.length; i++) {
49
+ if (
50
+ i === maxWorkersArgIndex ||
51
+ i === maxWorkersArgIndex + 1 ||
52
+ i === branchIndex ||
53
+ i === branchIndex + 1
54
+ )
55
+ continue;
56
+ targetPath = args[i] || targetPath;
57
+ break;
58
+ }
59
+
60
+ console.log(`\n[ESLint 配置]`);
61
+ console.log(`路径: ${targetPath}`);
62
+ console.log(`并发线程数: ${maxWorkers || 'ESLint 默认'}`);
64
63
  console.log('');
65
64
 
66
65
  // 路径解析
@@ -73,13 +72,11 @@ async function runEslint() {
73
72
  if (fs.existsSync(fullPath)) {
74
73
  rootDir = fullPath;
75
74
  } else {
76
- console.error(`找不到目录: packages/${targetPath}`);
75
+ console.error(`❌ 找不到目录: packages/${targetPath}`);
77
76
  process.exit(1);
78
77
  }
79
78
  }
80
79
 
81
- console.log('目标路径解析为:', rootDir);
82
-
83
80
  const gitRootResult = spawnSync('git', ['rev-parse', '--show-toplevel'], {
84
81
  cwd: rootDir,
85
82
  encoding: 'utf-8',
@@ -87,7 +84,7 @@ async function runEslint() {
87
84
  });
88
85
 
89
86
  if (gitRootResult.status !== 0 || !gitRootResult.stdout) {
90
- console.error('无法获取 Git 根目录,请确保路径在 Git 仓库内');
87
+ console.error('无法获取 Git 根目录,请确保路径在 Git 仓库内');
91
88
  process.exit(1);
92
89
  }
93
90
 
@@ -107,43 +104,45 @@ async function runEslint() {
107
104
  });
108
105
 
109
106
  if (currentBranchResult.status !== 0 || !currentBranchResult.stdout) {
110
- console.error('无法获取当前 Git 分支');
107
+ console.error('无法获取当前 Git 分支');
111
108
  process.exit(1);
112
109
  }
113
110
 
114
111
  const currentBranch = currentBranchResult.stdout.trim();
115
112
 
113
+ const branchToUse = branchArg || targetBranch;
114
+
116
115
  console.log(`[Git 信息]`);
117
- console.log(`- 仓库根目录: ${gitRoot}`);
118
- console.log(`- 当前分支: ${currentBranch}`);
119
- console.log(`- 目标分支: ${branchArg || targetBranch}`);
116
+ console.log(`仓库根目录: ${gitRoot}`);
117
+ console.log(`当前分支: ${currentBranch}`);
118
+ console.log(`目标分支: ${branchArg || targetBranch}`);
120
119
  console.log('');
121
120
 
122
- const statusResult = spawnSync('git', ['status', '--porcelain'], {
123
- cwd: gitRoot,
124
- encoding: 'utf-8',
125
- shell: true,
126
- });
127
- if (statusResult.status === 0 && statusResult.stdout.trim()) {
128
- console.warn(
129
- '⚠️ 检测到当前分支有未提交的更改,切换分支可能失败或导致代码丢失,请先提交或暂存更改!\n',
130
- );
131
- }
121
+ if (currentBranch !== branchToUse) {
122
+ const statusResult = spawnSync('git', ['status', '--porcelain'], {
123
+ cwd: gitRoot,
124
+ encoding: 'utf-8',
125
+ shell: true,
126
+ });
127
+ if (statusResult.status === 0 && statusResult.stdout.trim()) {
128
+ console.error(
129
+ '⚠️ 当前分支有未提交的更改,切换分支可能导致代码丢失,请先提交或暂存更改!\n❌ 终止执行',
130
+ );
131
+ process.exit(1);
132
+ }
132
133
 
133
- // 切换分支并拉取最新代码
134
- if (!branchArg && currentBranch !== targetBranch) {
135
- console.log(`➡️ 切换到分支 ${targetBranch} ...`);
136
- const checkout = spawnSync('git', ['checkout', targetBranch], {
134
+ console.log(`➡️ 切换到分支 ${branchToUse} ...`);
135
+ const checkout = spawnSync('git', ['checkout', branchToUse], {
137
136
  cwd: gitRoot,
138
137
  stdio: 'inherit',
139
138
  shell: true,
140
139
  });
141
140
  if (checkout.status !== 0) {
142
- console.error(`切换分支失败`);
141
+ console.error('❌ 切换分支失败');
143
142
  process.exit(1);
144
143
  }
145
- } else if (branchArg) {
146
- console.log(`➡️ 使用指定分支 ${branchArg} `);
144
+ } else {
145
+ console.log(`➡️ 当前已在分支 ${branchToUse}`);
147
146
  }
148
147
 
149
148
  console.log('⬇️ 正在拉取最新代码...');
@@ -153,64 +152,55 @@ async function runEslint() {
153
152
  shell: true,
154
153
  });
155
154
  if (pull.status !== 0) {
156
- console.error('git pull 失败');
155
+ console.error('git pull 失败');
157
156
  process.exit(1);
158
157
  }
159
158
 
160
159
  const srcPath = path.join(rootDir, 'src');
161
160
  const eslintTarget = fs.existsSync(srcPath) ? srcPath : '.';
162
161
  if (!fs.existsSync(srcPath)) {
163
- console.warn(`src 文件夹不存在,改为检查当前目录: ${eslintTarget}`);
162
+ console.warn(`🔍 src 文件夹不存在,改为检查当前目录: ${eslintTarget}`);
164
163
  } else {
165
- console.log(`检查目录: ${eslintTarget}`);
164
+ console.log(`🔍 检查目录: ${eslintTarget}`);
166
165
  }
167
166
 
168
167
  const allFiles = glob.sync('**/*.{js,jsx,ts,tsx}', { cwd: eslintTarget, absolute: true });
169
- if (allFiles.length === 0) {
170
- console.log('没有需要检查的文件');
168
+ if (!allFiles.length) {
169
+ console.log('- 没有需要检查的文件');
171
170
  return;
172
171
  }
173
172
 
174
- // 多进程拆分
175
- const chunkSize = Math.max(1, Math.ceil(allFiles.length / maxWorkers)); // 最少一个文件
173
+ const cpuCount = os.cpus().length;
174
+ const workers = maxWorkers || cpuCount;
175
+ const chunkSize = Math.ceil(allFiles.length / workers);
176
176
  const chunks = [];
177
177
  for (let i = 0; i < allFiles.length; i += chunkSize) {
178
178
  chunks.push(allFiles.slice(i, i + chunkSize));
179
179
  }
180
180
 
181
- let finished = 0;
182
- let hasError = false;
183
-
184
- // 并行 ESLint
185
181
  console.log(`[ESLint 执行]`);
186
- console.log(`- 共计文件: ${allFiles.length}`);
187
- console.log(`- 拆分任务: ${chunks.length} 个`);
182
+ console.log(`文件总数: ${files.length}`);
188
183
  console.log('');
189
- console.log('🚀 开始并行执行 ESLint ...\n');
184
+ console.log('🚀 开始执行 ESLint ...\n');
185
+
186
+ let finished = 0;
187
+ let hasError = false;
190
188
 
191
189
  chunks.forEach((chunk) => {
192
- const eslint = spawn(
190
+ const eslintProcess = require('child_process').spawn(
193
191
  'npx',
194
- ['eslint', ...chunk, '--ext', 'ts,tsx,js,jsx', '--report-unused-disable-directives'],
195
- {
196
- cwd: rootDir,
197
- stdio: 'inherit',
198
- shell: true,
199
- },
192
+ ['eslint', ...chunk, '--ext', 'js,jsx,ts,tsx', '--report-unused-disable-directives'],
193
+ { cwd: rootDir, stdio: 'inherit', shell: true },
200
194
  );
201
195
 
202
- eslint.on('close', (code) => {
203
- if (code !== 0 && code !== null) {
204
- hasError = true;
205
- }
196
+ eslintProcess.on('close', (code) => {
197
+ if (code !== 0) hasError = true;
206
198
  finished++;
207
- if (finished === chunks.length) {
208
- process.exit(hasError ? 1 : 0);
209
- }
199
+ if (finished === chunks.length) process.exit(hasError ? 1 : 0);
210
200
  });
211
201
  });
212
202
  } catch (err) {
213
- console.error('脚本执行出错:', err);
203
+ console.error('脚本执行出错:', err);
214
204
  process.exit(1);
215
205
  }
216
206
  }