@apdesign/code-style-react 1.2.3 → 2.0.1
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/.prettierrc.js +10 -10
- package/CHANGELOG.md +179 -0
- package/MIGRATION.md +211 -0
- package/README.md +262 -0
- package/biome.jsonc +107 -0
- package/cli.js +49 -15
- package/index.js +13 -13
- package/lefthook.yml +19 -0
- package/package.json +67 -45
- package/scripts/buildEslint.sh +59 -59
- package/scripts/initConfigs.js +121 -98
- package/scripts/initHusky.js +36 -33
- package/scripts/initScripts.js +39 -36
- package/scripts/runEslint.js +189 -189
- package/scripts/runStylelint.js +137 -137
- package/stylelint/{.stylelintrc.js → rules/.stylelintrc.js} +39 -39
- package/stylelint/rules/color-must-use-variable.js +63 -61
- package/eslint/.eslintrc.build.js +0 -107
- package/eslint/.eslintrc.js +0 -107
package/scripts/runEslint.js
CHANGED
@@ -1,189 +1,189 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
|
3
|
-
let spawnSync;
|
4
|
-
let path;
|
5
|
-
let fs;
|
6
|
-
let glob;
|
7
|
-
let ESLint;
|
8
|
-
|
9
|
-
async function runEslint(targetPathArg) {
|
10
|
-
try {
|
11
|
-
if (typeof require !== 'undefined') {
|
12
|
-
// CommonJS
|
13
|
-
spawnSync = require('child_process').spawnSync;
|
14
|
-
path = require('path');
|
15
|
-
fs = require('fs');
|
16
|
-
glob = require('glob');
|
17
|
-
ESLint = require('eslint').ESLint;
|
18
|
-
} else {
|
19
|
-
// ESM
|
20
|
-
spawnSync = await import('node:child_process').spawnSync;
|
21
|
-
path = await import('node:path');
|
22
|
-
fs = await import('node:fs');
|
23
|
-
glob = (await import('glob')).default;
|
24
|
-
ESLint = (await import('eslint')).ESLint;
|
25
|
-
}
|
26
|
-
|
27
|
-
// 参数解析
|
28
|
-
const args = targetPathArg || process.argv.slice(2);
|
29
|
-
|
30
|
-
// 检查是否有分支参数 (--branch <branchName>)
|
31
|
-
let branchArg = null;
|
32
|
-
const branchIndex = args.indexOf('--branch');
|
33
|
-
if (branchIndex !== -1 && args[branchIndex + 1]) {
|
34
|
-
branchArg = args[branchIndex + 1];
|
35
|
-
}
|
36
|
-
|
37
|
-
// targetPath 是第一个不是 --branch 的参数
|
38
|
-
let targetPath = process.cwd();
|
39
|
-
for (let i = 0; i < args.length; i++) {
|
40
|
-
// 仅当确实存在 --branch 参数时才跳过它和它的值
|
41
|
-
if (branchIndex !== -1 && (i === branchIndex || i === branchIndex + 1)) continue;
|
42
|
-
targetPath = args[i] || targetPath;
|
43
|
-
break;
|
44
|
-
}
|
45
|
-
|
46
|
-
console.log(
|
47
|
-
console.log(`路径: ${targetPath}`);
|
48
|
-
console.log('');
|
49
|
-
|
50
|
-
// 路径解析
|
51
|
-
let rootDir = path.resolve(targetPath);
|
52
|
-
|
53
|
-
// 如果传的是单个目录名且路径不存在,则在 packages/ 下查找
|
54
|
-
if (!fs.existsSync(rootDir) && targetPath !== process.cwd()) {
|
55
|
-
const baseDir = process.cwd(); // 当前项目根目录
|
56
|
-
const fullPath = path.join(baseDir, 'packages', targetPath);
|
57
|
-
if (fs.existsSync(fullPath)) {
|
58
|
-
rootDir = fullPath;
|
59
|
-
} else {
|
60
|
-
console.error(`❌ 找不到目录: packages/${targetPath}`);
|
61
|
-
process.exit(1);
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
const gitRootResult = spawnSync('git', ['rev-parse', '--show-toplevel'], {
|
66
|
-
cwd: rootDir,
|
67
|
-
encoding: 'utf-8',
|
68
|
-
shell: true,
|
69
|
-
});
|
70
|
-
|
71
|
-
if (gitRootResult.status !== 0 || !gitRootResult.stdout) {
|
72
|
-
console.error('❌ 无法获取 Git 根目录,请确保路径在 Git 仓库内');
|
73
|
-
process.exit(1);
|
74
|
-
}
|
75
|
-
|
76
|
-
const gitRoot = gitRootResult.stdout.trim();
|
77
|
-
|
78
|
-
let targetBranch = 'master';
|
79
|
-
if (targetPath !== process.cwd()) {
|
80
|
-
const pkgName = path.basename(targetPath); // 直接取传入的目录名
|
81
|
-
if (pkgName
|
82
|
-
const suffix = pkgName.split('-').pop();
|
83
|
-
targetBranch = `master-${suffix}`;
|
84
|
-
}
|
85
|
-
}
|
86
|
-
|
87
|
-
// 获取当前分支
|
88
|
-
const currentBranchResult = spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
|
89
|
-
cwd: gitRoot,
|
90
|
-
encoding: 'utf-8',
|
91
|
-
shell: true,
|
92
|
-
});
|
93
|
-
|
94
|
-
if (currentBranchResult.status !== 0 || !currentBranchResult.stdout) {
|
95
|
-
console.error('❌ 无法获取当前 Git 分支');
|
96
|
-
process.exit(1);
|
97
|
-
}
|
98
|
-
|
99
|
-
const currentBranch = currentBranchResult.stdout.trim();
|
100
|
-
|
101
|
-
const branchToUse = branchArg || targetBranch;
|
102
|
-
|
103
|
-
console.log(
|
104
|
-
console.log(`仓库根目录: ${gitRoot}`);
|
105
|
-
console.log(`当前分支: ${currentBranch}`);
|
106
|
-
console.log(`目标分支: ${branchToUse}`);
|
107
|
-
console.log('');
|
108
|
-
|
109
|
-
if (currentBranch !== branchToUse) {
|
110
|
-
const statusResult = spawnSync('git', ['status', '--porcelain'], {
|
111
|
-
cwd: gitRoot,
|
112
|
-
encoding: 'utf-8',
|
113
|
-
shell: true,
|
114
|
-
});
|
115
|
-
if (statusResult.status === 0 && statusResult.stdout.trim()) {
|
116
|
-
console.error(
|
117
|
-
'⚠️ 当前分支有未提交的更改,切换分支可能导致代码丢失,请先提交或暂存更改!\n❌ 终止执行',
|
118
|
-
);
|
119
|
-
process.exit(1);
|
120
|
-
}
|
121
|
-
|
122
|
-
console.log(`➡️ 切换到分支 ${branchToUse} ...`);
|
123
|
-
const checkout = spawnSync('git', ['checkout', branchToUse], {
|
124
|
-
cwd: gitRoot,
|
125
|
-
stdio: 'inherit',
|
126
|
-
shell: true,
|
127
|
-
});
|
128
|
-
if (checkout.status !== 0) {
|
129
|
-
console.error('❌ 切换分支失败');
|
130
|
-
process.exit(1);
|
131
|
-
}
|
132
|
-
} else {
|
133
|
-
console.log(`➡️ 当前已在分支 ${branchToUse}`);
|
134
|
-
}
|
135
|
-
|
136
|
-
console.log('⬇️ 正在拉取最新代码...');
|
137
|
-
const pull = spawnSync('git', ['pull'], {
|
138
|
-
cwd: gitRoot,
|
139
|
-
stdio: 'inherit',
|
140
|
-
shell: true,
|
141
|
-
});
|
142
|
-
if (pull.status !== 0) {
|
143
|
-
console.error('❌ git pull 失败');
|
144
|
-
process.exit(1);
|
145
|
-
}
|
146
|
-
|
147
|
-
const srcPath = path.join(rootDir, 'src');
|
148
|
-
const eslintTarget = fs.existsSync(srcPath) ? srcPath : '.';
|
149
|
-
if (
|
150
|
-
console.
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
}
|
155
|
-
|
156
|
-
const eslint = new ESLint({
|
157
|
-
cwd: gitRoot,
|
158
|
-
extensions: ['.js', '.jsx', '.ts', '.tsx'], // 等效于 --ext
|
159
|
-
reportUnusedDisableDirectives: 'error', // 等效于 --report-unused-disable-directives
|
160
|
-
});
|
161
|
-
const files = glob.sync('**/*.{js,jsx,ts,tsx}', { cwd: eslintTarget, absolute: true });
|
162
|
-
if (files.length === 0) {
|
163
|
-
console.log('- 没有需要检查的文件');
|
164
|
-
return;
|
165
|
-
}
|
166
|
-
|
167
|
-
console.log(
|
168
|
-
console.log(`文件总数: ${files.length}`);
|
169
|
-
console.log('');
|
170
|
-
console.log('🚀 开始执行 ESLint ...\n');
|
171
|
-
|
172
|
-
const results = await eslint.lintFiles(files);
|
173
|
-
const errorsOnly = results.map((result) => {
|
174
|
-
result.messages = result.messages.filter((message) => message.severity === 2); // 只保留错误
|
175
|
-
return result;
|
176
|
-
});
|
177
|
-
const formatter = await eslint.loadFormatter('stylish');
|
178
|
-
const resultText = formatter.format(errorsOnly);
|
179
|
-
console.log(resultText);
|
180
|
-
|
181
|
-
const hasError = results.some((r) => r.errorCount > 0);
|
182
|
-
process.exit(hasError ? 1 : 0);
|
183
|
-
} catch (err) {
|
184
|
-
console.error('❌ 脚本执行出错:', err);
|
185
|
-
process.exit(1);
|
186
|
-
}
|
187
|
-
}
|
188
|
-
|
189
|
-
module.exports = runEslint;
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
let spawnSync;
|
4
|
+
let path;
|
5
|
+
let fs;
|
6
|
+
let glob;
|
7
|
+
let ESLint;
|
8
|
+
|
9
|
+
async function runEslint(targetPathArg) {
|
10
|
+
try {
|
11
|
+
if (typeof require !== 'undefined') {
|
12
|
+
// CommonJS
|
13
|
+
spawnSync = require('node:child_process').spawnSync;
|
14
|
+
path = require('node:path');
|
15
|
+
fs = require('node:fs');
|
16
|
+
glob = require('glob');
|
17
|
+
ESLint = require('eslint').ESLint;
|
18
|
+
} else {
|
19
|
+
// ESM
|
20
|
+
spawnSync = await import('node:child_process').spawnSync;
|
21
|
+
path = await import('node:path');
|
22
|
+
fs = await import('node:fs');
|
23
|
+
glob = (await import('glob')).default;
|
24
|
+
ESLint = (await import('eslint')).ESLint;
|
25
|
+
}
|
26
|
+
|
27
|
+
// 参数解析
|
28
|
+
const args = targetPathArg || process.argv.slice(2);
|
29
|
+
|
30
|
+
// 检查是否有分支参数 (--branch <branchName>)
|
31
|
+
let branchArg = null;
|
32
|
+
const branchIndex = args.indexOf('--branch');
|
33
|
+
if (branchIndex !== -1 && args[branchIndex + 1]) {
|
34
|
+
branchArg = args[branchIndex + 1];
|
35
|
+
}
|
36
|
+
|
37
|
+
// targetPath 是第一个不是 --branch 的参数
|
38
|
+
let targetPath = process.cwd();
|
39
|
+
for (let i = 0; i < args.length; i++) {
|
40
|
+
// 仅当确实存在 --branch 参数时才跳过它和它的值
|
41
|
+
if (branchIndex !== -1 && (i === branchIndex || i === branchIndex + 1)) continue;
|
42
|
+
targetPath = args[i] || targetPath;
|
43
|
+
break;
|
44
|
+
}
|
45
|
+
|
46
|
+
console.log('\n[ESLint 配置]');
|
47
|
+
console.log(`路径: ${targetPath}`);
|
48
|
+
console.log('');
|
49
|
+
|
50
|
+
// 路径解析
|
51
|
+
let rootDir = path.resolve(targetPath);
|
52
|
+
|
53
|
+
// 如果传的是单个目录名且路径不存在,则在 packages/ 下查找
|
54
|
+
if (!fs.existsSync(rootDir) && targetPath !== process.cwd()) {
|
55
|
+
const baseDir = process.cwd(); // 当前项目根目录
|
56
|
+
const fullPath = path.join(baseDir, 'packages', targetPath);
|
57
|
+
if (fs.existsSync(fullPath)) {
|
58
|
+
rootDir = fullPath;
|
59
|
+
} else {
|
60
|
+
console.error(`❌ 找不到目录: packages/${targetPath}`);
|
61
|
+
process.exit(1);
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
const gitRootResult = spawnSync('git', ['rev-parse', '--show-toplevel'], {
|
66
|
+
cwd: rootDir,
|
67
|
+
encoding: 'utf-8',
|
68
|
+
shell: true,
|
69
|
+
});
|
70
|
+
|
71
|
+
if (gitRootResult.status !== 0 || !gitRootResult.stdout) {
|
72
|
+
console.error('❌ 无法获取 Git 根目录,请确保路径在 Git 仓库内');
|
73
|
+
process.exit(1);
|
74
|
+
}
|
75
|
+
|
76
|
+
const gitRoot = gitRootResult.stdout.trim();
|
77
|
+
|
78
|
+
let targetBranch = 'master';
|
79
|
+
if (targetPath !== process.cwd()) {
|
80
|
+
const pkgName = path.basename(targetPath); // 直接取传入的目录名
|
81
|
+
if (pkgName?.includes('-')) {
|
82
|
+
const suffix = pkgName.split('-').pop();
|
83
|
+
targetBranch = `master-${suffix}`;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
// 获取当前分支
|
88
|
+
const currentBranchResult = spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
|
89
|
+
cwd: gitRoot,
|
90
|
+
encoding: 'utf-8',
|
91
|
+
shell: true,
|
92
|
+
});
|
93
|
+
|
94
|
+
if (currentBranchResult.status !== 0 || !currentBranchResult.stdout) {
|
95
|
+
console.error('❌ 无法获取当前 Git 分支');
|
96
|
+
process.exit(1);
|
97
|
+
}
|
98
|
+
|
99
|
+
const currentBranch = currentBranchResult.stdout.trim();
|
100
|
+
|
101
|
+
const branchToUse = branchArg || targetBranch;
|
102
|
+
|
103
|
+
console.log('[Git 信息]');
|
104
|
+
console.log(`仓库根目录: ${gitRoot}`);
|
105
|
+
console.log(`当前分支: ${currentBranch}`);
|
106
|
+
console.log(`目标分支: ${branchToUse}`);
|
107
|
+
console.log('');
|
108
|
+
|
109
|
+
if (currentBranch !== branchToUse) {
|
110
|
+
const statusResult = spawnSync('git', ['status', '--porcelain'], {
|
111
|
+
cwd: gitRoot,
|
112
|
+
encoding: 'utf-8',
|
113
|
+
shell: true,
|
114
|
+
});
|
115
|
+
if (statusResult.status === 0 && statusResult.stdout.trim()) {
|
116
|
+
console.error(
|
117
|
+
'⚠️ 当前分支有未提交的更改,切换分支可能导致代码丢失,请先提交或暂存更改!\n❌ 终止执行',
|
118
|
+
);
|
119
|
+
process.exit(1);
|
120
|
+
}
|
121
|
+
|
122
|
+
console.log(`➡️ 切换到分支 ${branchToUse} ...`);
|
123
|
+
const checkout = spawnSync('git', ['checkout', branchToUse], {
|
124
|
+
cwd: gitRoot,
|
125
|
+
stdio: 'inherit',
|
126
|
+
shell: true,
|
127
|
+
});
|
128
|
+
if (checkout.status !== 0) {
|
129
|
+
console.error('❌ 切换分支失败');
|
130
|
+
process.exit(1);
|
131
|
+
}
|
132
|
+
} else {
|
133
|
+
console.log(`➡️ 当前已在分支 ${branchToUse}`);
|
134
|
+
}
|
135
|
+
|
136
|
+
console.log('⬇️ 正在拉取最新代码...');
|
137
|
+
const pull = spawnSync('git', ['pull'], {
|
138
|
+
cwd: gitRoot,
|
139
|
+
stdio: 'inherit',
|
140
|
+
shell: true,
|
141
|
+
});
|
142
|
+
if (pull.status !== 0) {
|
143
|
+
console.error('❌ git pull 失败');
|
144
|
+
process.exit(1);
|
145
|
+
}
|
146
|
+
|
147
|
+
const srcPath = path.join(rootDir, 'src');
|
148
|
+
const eslintTarget = fs.existsSync(srcPath) ? srcPath : '.';
|
149
|
+
if (fs.existsSync(srcPath)) {
|
150
|
+
console.log(`🔍 检查目录: ${eslintTarget}`);
|
151
|
+
} else {
|
152
|
+
console.error('❌ 当前目录下不存在 src 文件夹');
|
153
|
+
process.exit(1);
|
154
|
+
}
|
155
|
+
|
156
|
+
const eslint = new ESLint({
|
157
|
+
cwd: gitRoot,
|
158
|
+
extensions: ['.js', '.jsx', '.ts', '.tsx'], // 等效于 --ext
|
159
|
+
reportUnusedDisableDirectives: 'error', // 等效于 --report-unused-disable-directives
|
160
|
+
});
|
161
|
+
const files = glob.sync('**/*.{js,jsx,ts,tsx}', { cwd: eslintTarget, absolute: true });
|
162
|
+
if (files.length === 0) {
|
163
|
+
console.log('- 没有需要检查的文件');
|
164
|
+
return;
|
165
|
+
}
|
166
|
+
|
167
|
+
console.log('[ESLint 执行]');
|
168
|
+
console.log(`文件总数: ${files.length}`);
|
169
|
+
console.log('');
|
170
|
+
console.log('🚀 开始执行 ESLint ...\n');
|
171
|
+
|
172
|
+
const results = await eslint.lintFiles(files);
|
173
|
+
const errorsOnly = results.map((result) => {
|
174
|
+
result.messages = result.messages.filter((message) => message.severity === 2); // 只保留错误
|
175
|
+
return result;
|
176
|
+
});
|
177
|
+
const formatter = await eslint.loadFormatter('stylish');
|
178
|
+
const resultText = formatter.format(errorsOnly);
|
179
|
+
console.log(resultText);
|
180
|
+
|
181
|
+
const hasError = results.some((r) => r.errorCount > 0);
|
182
|
+
process.exit(hasError ? 1 : 0);
|
183
|
+
} catch (err) {
|
184
|
+
console.error('❌ 脚本执行出错:', err);
|
185
|
+
process.exit(1);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
|
189
|
+
module.exports = runEslint;
|
package/scripts/runStylelint.js
CHANGED
@@ -1,137 +1,137 @@
|
|
1
|
-
#!/usr/bin/env node
|
2
|
-
|
3
|
-
let spawnSync;
|
4
|
-
let spawn;
|
5
|
-
let path;
|
6
|
-
let fs;
|
7
|
-
|
8
|
-
async function runStylelint(targetPathArg) {
|
9
|
-
try {
|
10
|
-
if (typeof require !== 'undefined') {
|
11
|
-
// CommonJS
|
12
|
-
({ spawnSync, spawn } = require('child_process'));
|
13
|
-
path = require('path');
|
14
|
-
fs = require('fs');
|
15
|
-
} else {
|
16
|
-
// ESM
|
17
|
-
({ spawnSync, spawn } = await import('node:child_process'));
|
18
|
-
path = await import('node:path');
|
19
|
-
fs = await import('node:fs');
|
20
|
-
}
|
21
|
-
|
22
|
-
const targetPath = targetPathArg || process.cwd();
|
23
|
-
let rootDir = path.resolve(targetPath);
|
24
|
-
|
25
|
-
// 如果传的是单个目录名且路径不存在,则在 packages/ 下查找
|
26
|
-
if (!fs.existsSync(rootDir) && targetPath !== process.cwd()) {
|
27
|
-
const baseDir = process.cwd(); // 当前项目根目录
|
28
|
-
const fullPath = path.join(baseDir, 'packages', targetPath);
|
29
|
-
if (fs.existsSync(fullPath)) {
|
30
|
-
rootDir = fullPath;
|
31
|
-
} else {
|
32
|
-
console.error(`找不到目录: packages/${targetPath}`);
|
33
|
-
process.exit(1);
|
34
|
-
}
|
35
|
-
}
|
36
|
-
|
37
|
-
console.log('目标路径:', rootDir);
|
38
|
-
|
39
|
-
const gitRootResult = spawnSync('git', ['rev-parse', '--show-toplevel'], {
|
40
|
-
cwd: rootDir,
|
41
|
-
encoding: 'utf-8',
|
42
|
-
shell: true,
|
43
|
-
});
|
44
|
-
|
45
|
-
if (gitRootResult.status !== 0 || !gitRootResult.stdout) {
|
46
|
-
console.error('无法获取 Git 根目录,请确保路径在 Git 仓库内');
|
47
|
-
process.exit(1);
|
48
|
-
}
|
49
|
-
|
50
|
-
const gitRoot = gitRootResult.stdout.trim();
|
51
|
-
console.log('Git 根目录:', gitRoot);
|
52
|
-
|
53
|
-
let targetBranch = 'master';
|
54
|
-
if (targetPath !== process.cwd()) {
|
55
|
-
const lastDir = path.basename(rootDir);
|
56
|
-
targetBranch = `master-${lastDir.split('-').pop()}`;
|
57
|
-
}
|
58
|
-
console.log('目标分支:', targetBranch);
|
59
|
-
|
60
|
-
// 获取当前分支
|
61
|
-
const currentBranchResult = spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
|
62
|
-
cwd: gitRoot,
|
63
|
-
encoding: 'utf-8',
|
64
|
-
shell: true,
|
65
|
-
});
|
66
|
-
|
67
|
-
if (currentBranchResult.status !== 0 || !currentBranchResult.stdout) {
|
68
|
-
console.error('无法获取当前 Git 分支');
|
69
|
-
process.exit(1);
|
70
|
-
}
|
71
|
-
|
72
|
-
const currentBranch = currentBranchResult.stdout.trim();
|
73
|
-
console.log('当前分支:', currentBranch);
|
74
|
-
|
75
|
-
// 切换分支并拉取最新代码
|
76
|
-
if (currentBranch !== targetBranch) {
|
77
|
-
console.log(`切换到分支 ${targetBranch} ...`);
|
78
|
-
const checkout = spawnSync('git', ['checkout', targetBranch], {
|
79
|
-
cwd: gitRoot,
|
80
|
-
stdio: 'inherit',
|
81
|
-
shell: true,
|
82
|
-
});
|
83
|
-
if (checkout.status !== 0) {
|
84
|
-
console.error(
|
85
|
-
process.exit(1);
|
86
|
-
}
|
87
|
-
}
|
88
|
-
|
89
|
-
console.log('拉取最新代码...');
|
90
|
-
const pull = spawnSync('git', ['pull'], {
|
91
|
-
cwd: gitRoot,
|
92
|
-
stdio: 'inherit',
|
93
|
-
shell: true,
|
94
|
-
});
|
95
|
-
if (pull.status !== 0) {
|
96
|
-
console.error('git pull 失败');
|
97
|
-
process.exit(1);
|
98
|
-
}
|
99
|
-
|
100
|
-
const srcPath = path.join(rootDir, 'src');
|
101
|
-
const stylelintTarget = fs.existsSync(srcPath)
|
102
|
-
? `${srcPath}/**/*.{css,scss,less}`
|
103
|
-
: './**/*.{css,scss,less}';
|
104
|
-
|
105
|
-
if (
|
106
|
-
console.
|
107
|
-
} else {
|
108
|
-
console.
|
109
|
-
}
|
110
|
-
|
111
|
-
console.log('执行 Stylelint...');
|
112
|
-
const stylelint = spawn(
|
113
|
-
'npx',
|
114
|
-
[
|
115
|
-
'stylelint',
|
116
|
-
stylelintTarget,
|
117
|
-
'--allow-empty-input',
|
118
|
-
'--report-needless-disables',
|
119
|
-
'--report-invalid-scope-disables',
|
120
|
-
],
|
121
|
-
{
|
122
|
-
cwd: rootDir,
|
123
|
-
stdio: 'inherit',
|
124
|
-
shell: true,
|
125
|
-
},
|
126
|
-
);
|
127
|
-
|
128
|
-
stylelint.on('close', (code) => {
|
129
|
-
process.exit(code);
|
130
|
-
});
|
131
|
-
} catch (err) {
|
132
|
-
console.error('脚本执行出错:', err);
|
133
|
-
process.exit(1);
|
134
|
-
}
|
135
|
-
}
|
136
|
-
|
137
|
-
module.exports = runStylelint;
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
let spawnSync;
|
4
|
+
let spawn;
|
5
|
+
let path;
|
6
|
+
let fs;
|
7
|
+
|
8
|
+
async function runStylelint(targetPathArg) {
|
9
|
+
try {
|
10
|
+
if (typeof require !== 'undefined') {
|
11
|
+
// CommonJS
|
12
|
+
({ spawnSync, spawn } = require('node:child_process'));
|
13
|
+
path = require('node:path');
|
14
|
+
fs = require('node:fs');
|
15
|
+
} else {
|
16
|
+
// ESM
|
17
|
+
({ spawnSync, spawn } = await import('node:child_process'));
|
18
|
+
path = await import('node:path');
|
19
|
+
fs = await import('node:fs');
|
20
|
+
}
|
21
|
+
|
22
|
+
const targetPath = targetPathArg || process.cwd();
|
23
|
+
let rootDir = path.resolve(targetPath);
|
24
|
+
|
25
|
+
// 如果传的是单个目录名且路径不存在,则在 packages/ 下查找
|
26
|
+
if (!fs.existsSync(rootDir) && targetPath !== process.cwd()) {
|
27
|
+
const baseDir = process.cwd(); // 当前项目根目录
|
28
|
+
const fullPath = path.join(baseDir, 'packages', targetPath);
|
29
|
+
if (fs.existsSync(fullPath)) {
|
30
|
+
rootDir = fullPath;
|
31
|
+
} else {
|
32
|
+
console.error(`找不到目录: packages/${targetPath}`);
|
33
|
+
process.exit(1);
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
console.log('目标路径:', rootDir);
|
38
|
+
|
39
|
+
const gitRootResult = spawnSync('git', ['rev-parse', '--show-toplevel'], {
|
40
|
+
cwd: rootDir,
|
41
|
+
encoding: 'utf-8',
|
42
|
+
shell: true,
|
43
|
+
});
|
44
|
+
|
45
|
+
if (gitRootResult.status !== 0 || !gitRootResult.stdout) {
|
46
|
+
console.error('无法获取 Git 根目录,请确保路径在 Git 仓库内');
|
47
|
+
process.exit(1);
|
48
|
+
}
|
49
|
+
|
50
|
+
const gitRoot = gitRootResult.stdout.trim();
|
51
|
+
console.log('Git 根目录:', gitRoot);
|
52
|
+
|
53
|
+
let targetBranch = 'master';
|
54
|
+
if (targetPath !== process.cwd()) {
|
55
|
+
const lastDir = path.basename(rootDir);
|
56
|
+
targetBranch = `master-${lastDir.split('-').pop()}`;
|
57
|
+
}
|
58
|
+
console.log('目标分支:', targetBranch);
|
59
|
+
|
60
|
+
// 获取当前分支
|
61
|
+
const currentBranchResult = spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
|
62
|
+
cwd: gitRoot,
|
63
|
+
encoding: 'utf-8',
|
64
|
+
shell: true,
|
65
|
+
});
|
66
|
+
|
67
|
+
if (currentBranchResult.status !== 0 || !currentBranchResult.stdout) {
|
68
|
+
console.error('无法获取当前 Git 分支');
|
69
|
+
process.exit(1);
|
70
|
+
}
|
71
|
+
|
72
|
+
const currentBranch = currentBranchResult.stdout.trim();
|
73
|
+
console.log('当前分支:', currentBranch);
|
74
|
+
|
75
|
+
// 切换分支并拉取最新代码
|
76
|
+
if (currentBranch !== targetBranch) {
|
77
|
+
console.log(`切换到分支 ${targetBranch} ...`);
|
78
|
+
const checkout = spawnSync('git', ['checkout', targetBranch], {
|
79
|
+
cwd: gitRoot,
|
80
|
+
stdio: 'inherit',
|
81
|
+
shell: true,
|
82
|
+
});
|
83
|
+
if (checkout.status !== 0) {
|
84
|
+
console.error('切换分支失败');
|
85
|
+
process.exit(1);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
console.log('拉取最新代码...');
|
90
|
+
const pull = spawnSync('git', ['pull'], {
|
91
|
+
cwd: gitRoot,
|
92
|
+
stdio: 'inherit',
|
93
|
+
shell: true,
|
94
|
+
});
|
95
|
+
if (pull.status !== 0) {
|
96
|
+
console.error('git pull 失败');
|
97
|
+
process.exit(1);
|
98
|
+
}
|
99
|
+
|
100
|
+
const srcPath = path.join(rootDir, 'src');
|
101
|
+
const stylelintTarget = fs.existsSync(srcPath)
|
102
|
+
? `${srcPath}/**/*.{css,scss,less}`
|
103
|
+
: './**/*.{css,scss,less}';
|
104
|
+
|
105
|
+
if (fs.existsSync(srcPath)) {
|
106
|
+
console.log(`检查目录样式文件: ${stylelintTarget}`);
|
107
|
+
} else {
|
108
|
+
console.warn(`src 文件夹不存在,改为检查当前目录下样式文件: ${stylelintTarget}`);
|
109
|
+
}
|
110
|
+
|
111
|
+
console.log('执行 Stylelint...');
|
112
|
+
const stylelint = spawn(
|
113
|
+
'npx',
|
114
|
+
[
|
115
|
+
'stylelint',
|
116
|
+
stylelintTarget,
|
117
|
+
'--allow-empty-input',
|
118
|
+
'--report-needless-disables',
|
119
|
+
'--report-invalid-scope-disables',
|
120
|
+
],
|
121
|
+
{
|
122
|
+
cwd: rootDir,
|
123
|
+
stdio: 'inherit',
|
124
|
+
shell: true,
|
125
|
+
},
|
126
|
+
);
|
127
|
+
|
128
|
+
stylelint.on('close', (code) => {
|
129
|
+
process.exit(code);
|
130
|
+
});
|
131
|
+
} catch (err) {
|
132
|
+
console.error('脚本执行出错:', err);
|
133
|
+
process.exit(1);
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
module.exports = runStylelint;
|