@apdesign/code-style-react 1.1.5 → 1.1.6

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