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