@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 +1 -1
- package/scripts/runEslint.js +74 -77
package/package.json
CHANGED
package/scripts/runEslint.js
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
-
//
|
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 =
|
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 (
|
47
|
-
|
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(
|
52
|
-
console.log(
|
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(
|
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
|
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
|
-
|
120
|
-
|
121
|
-
|
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(
|
165
|
+
console.warn(`🔍 src 文件夹不存在,改为检查当前目录: ${eslintTarget}`);
|
147
166
|
} else {
|
148
|
-
console.log(
|
167
|
+
console.log(`🔍 检查目录: ${eslintTarget}`);
|
149
168
|
}
|
150
169
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
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
|
-
|
161
|
-
|
162
|
-
|
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
|
-
|
167
|
-
|
168
|
-
|
169
|
-
console.log(
|
170
|
-
|
171
|
-
|
172
|
-
|
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
|
}
|