@apdesign/code-style-react 1.1.7 → 1.1.8

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.7",
3
+ "version": "1.1.8",
4
4
  "scripts": {},
5
5
  "bin": {
6
6
  "apdesign-code-style": "cli.js"
@@ -3,39 +3,30 @@
3
3
  let spawnSync;
4
4
  let path;
5
5
  let fs;
6
- let os;
7
6
  let glob;
7
+ let ESLint;
8
8
 
9
9
  async function runEslint() {
10
10
  try {
11
11
  if (typeof require !== 'undefined') {
12
12
  // CommonJS
13
- ({ spawnSync, spawn } = require('child_process'));
13
+ spawnSync = require('child_process').spawnSync;
14
14
  path = require('path');
15
15
  fs = require('fs');
16
- os = require('os');
17
16
  glob = require('glob');
17
+ ESLint = require('eslint').ESLint;
18
18
  } else {
19
19
  // ESM
20
- ({ spawnSync, spawn } = await import('node:child_process'));
20
+ spawnSync = await import('node:child_process').spawnSync;
21
21
  path = await import('node:path');
22
22
  fs = await import('node:fs');
23
- os = await import('node:os');
24
23
  glob = (await import('glob')).default;
24
+ ESLint = (await import('eslint')).ESLint;
25
25
  }
26
26
 
27
27
  // 参数解析
28
28
  const args = process.argv.slice(2);
29
29
 
30
- let maxWorkers = null; // 默认由 ESLint API 决定
31
- const maxWorkersArgIndex = args.indexOf('--max-workers');
32
- if (maxWorkersArgIndex !== -1 && args[maxWorkersArgIndex + 1]) {
33
- const val = parseInt(args[maxWorkersArgIndex + 1], 10);
34
- if (!isNaN(val) && val > 0) {
35
- maxWorkers = val;
36
- }
37
- }
38
-
39
30
  // 检查是否有分支参数 (--branch <branchName>)
40
31
  let branchArg = null;
41
32
  const branchIndex = args.indexOf('--branch');
@@ -43,23 +34,16 @@ async function runEslint() {
43
34
  branchArg = args[branchIndex + 1];
44
35
  }
45
36
 
46
- // targetPath 是第一个不是 --max-workers 的参数
37
+ // targetPath 是第一个不是 --branch 的参数
47
38
  let targetPath = process.cwd();
48
39
  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;
40
+ if (i === branchIndex || i === branchIndex + 1) continue;
56
41
  targetPath = args[i] || targetPath;
57
42
  break;
58
43
  }
59
44
 
60
45
  console.log(`\n[ESLint 配置]`);
61
46
  console.log(`路径: ${targetPath}`);
62
- console.log(`并发线程数: ${maxWorkers || 'ESLint 默认'}`);
63
47
  console.log('');
64
48
 
65
49
  // 路径解析
@@ -115,7 +99,7 @@ async function runEslint() {
115
99
  console.log(`[Git 信息]`);
116
100
  console.log(`仓库根目录: ${gitRoot}`);
117
101
  console.log(`当前分支: ${currentBranch}`);
118
- console.log(`目标分支: ${branchArg || targetBranch}`);
102
+ console.log(`目标分支: ${branchToUse}`);
119
103
  console.log('');
120
104
 
121
105
  if (currentBranch !== branchToUse) {
@@ -164,41 +148,25 @@ async function runEslint() {
164
148
  console.log(`🔍 检查目录: ${eslintTarget}`);
165
149
  }
166
150
 
167
- const allFiles = glob.sync('**/*.{js,jsx,ts,tsx}', { cwd: eslintTarget, absolute: true });
168
- if (!allFiles.length) {
151
+ const eslint = new ESLint({ cwd: rootDir });
152
+ const files = glob.sync('**/*.{js,jsx,ts,tsx}', { cwd: eslintTarget, absolute: true });
153
+ if (files.length === 0) {
169
154
  console.log('- 没有需要检查的文件');
170
155
  return;
171
156
  }
172
157
 
173
- const cpuCount = os.cpus().length;
174
- const workers = maxWorkers || cpuCount;
175
- const chunkSize = Math.ceil(allFiles.length / workers);
176
- const chunks = [];
177
- for (let i = 0; i < allFiles.length; i += chunkSize) {
178
- chunks.push(allFiles.slice(i, i + chunkSize));
179
- }
180
-
181
158
  console.log(`[ESLint 执行]`);
182
159
  console.log(`文件总数: ${files.length}`);
183
160
  console.log('');
184
161
  console.log('🚀 开始执行 ESLint ...\n');
185
162
 
186
- let finished = 0;
187
- let hasError = false;
188
-
189
- chunks.forEach((chunk) => {
190
- const eslintProcess = require('child_process').spawn(
191
- 'npx',
192
- ['eslint', ...chunk, '--ext', 'js,jsx,ts,tsx', '--report-unused-disable-directives'],
193
- { cwd: rootDir, stdio: 'inherit', shell: true },
194
- );
163
+ const results = await eslint.lintFiles(files);
164
+ const formatter = await eslint.loadFormatter('stylish');
165
+ const resultText = formatter.format(results);
166
+ console.log(resultText);
195
167
 
196
- eslintProcess.on('close', (code) => {
197
- if (code !== 0) hasError = true;
198
- finished++;
199
- if (finished === chunks.length) process.exit(hasError ? 1 : 0);
200
- });
201
- });
168
+ const hasError = results.some((r) => r.errorCount > 0);
169
+ process.exit(hasError ? 1 : 0);
202
170
  } catch (err) {
203
171
  console.error('❌ 脚本执行出错:', err);
204
172
  process.exit(1);