@apdesign/code-style-react 1.1.3 → 1.1.4

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.3",
3
+ "version": "1.1.4",
4
4
  "scripts": {},
5
5
  "bin": {
6
6
  "apdesign-code-style": "cli.js"
@@ -18,6 +18,7 @@
18
18
  "license": "ISC",
19
19
  "description": "",
20
20
  "dependencies": {
21
+ "@types/glob": "^8.1.0",
21
22
  "@typescript-eslint/eslint-plugin": "^6.21.0",
22
23
  "@typescript-eslint/parser": "^6.21.0",
23
24
  "css-color-names": "^1.0.1",
@@ -30,6 +31,7 @@
30
31
  "eslint-plugin-prettier": "^5.2.1",
31
32
  "eslint-plugin-react": "^7.37.2",
32
33
  "eslint-plugin-react-hooks": "^4.6.2",
34
+ "glob": "^11.0.3",
33
35
  "husky": "^9.1.7",
34
36
  "lint-staged": "^15.3.0",
35
37
  "postcss-less": "^6.0.0",
@@ -5,19 +5,27 @@ echo "🔍 Starting ESLint check (Git diff files only)..."
5
5
  PROJECT_ROOT=$(git rev-parse --show-toplevel)
6
6
  cd "$PROJECT_ROOT" || exit 1
7
7
 
8
+ # 参数:commitId(必填),路径(可选)
8
9
  TARGET_COMMIT=$1
9
10
  LATEST_COMMIT=$(git rev-parse HEAD)
10
11
 
11
12
  if [ -z "$TARGET_COMMIT" ]; then
12
13
  echo "❗ Error: Missing target commit ID."
13
- echo "👉 Usage: $0 <target-commit-id>"
14
+ echo "👉 Usage: $0 <target-commit-id> [project-path]"
14
15
  exit 1
15
16
  fi
16
17
 
17
18
  echo "获取到上一次运行commit ID: $TARGET_COMMIT"
18
19
  echo "获取到当前最新的commit ID: $LATEST_COMMIT"
19
20
 
20
- DIFF_FILES=$(git diff --name-only "$TARGET_COMMIT" "$LATEST_COMMIT" -- '*.ts' '*.tsx' '*.js' '*.jsx')
21
+ # 处理路径参数
22
+ if [ -n "$FULL_PATH" ]; then
23
+ PROJECT_NAME=$(basename "$FULL_PATH")
24
+ echo "📂 检测子项目: $PROJECT_NAME"
25
+ DIFF_FILES=$(git diff --name-only "$TARGET_COMMIT" "$LATEST_COMMIT" -- "*/$PROJECT_NAME/*.{ts,tsx,js,jsx}")
26
+ else
27
+ DIFF_FILES=$(git diff --name-only "$TARGET_COMMIT" "$LATEST_COMMIT" -- '*.ts' '*.tsx' '*.js' '*.jsx')
28
+ fi
21
29
 
22
30
  if [ -z "$DIFF_FILES" ]; then
23
31
  echo "✅ No matching file changes detected, skipping ESLint check"
@@ -4,22 +4,54 @@ let spawnSync;
4
4
  let spawn;
5
5
  let path;
6
6
  let fs;
7
+ let os;
8
+ let glob;
7
9
 
8
- async function runEslint(targetPathArg) {
10
+ async function runEslint() {
9
11
  try {
10
12
  if (typeof require !== 'undefined') {
11
13
  // CommonJS
12
14
  ({ spawnSync, spawn } = require('child_process'));
13
15
  path = require('path');
14
16
  fs = require('fs');
17
+ os = require('os');
18
+ glob = require('glob');
15
19
  } else {
16
20
  // ESM
17
21
  ({ spawnSync, spawn } = await import('node:child_process'));
18
22
  path = await import('node:path');
19
23
  fs = await import('node:fs');
24
+ os = await import('node:os');
25
+ glob = (await import('glob')).default;
20
26
  }
21
27
 
22
- const targetPath = targetPathArg || process.cwd();
28
+ // 参数解析
29
+ const args = process.argv.slice(2);
30
+
31
+ // 默认并发数为 CPU 核心数,但不超过 8
32
+ let maxWorkers = Math.min(os.cpus().length, 8);
33
+
34
+ // 找到 --max-workers 参数
35
+ const maxWorkersArgIndex = args.indexOf('--max-workers');
36
+ if (maxWorkersArgIndex !== -1 && args[maxWorkersArgIndex + 1]) {
37
+ const val = parseInt(args[maxWorkersArgIndex + 1], 10);
38
+ if (!isNaN(val) && val > 0) {
39
+ maxWorkers = Math.min(val, 8); // 限制最大值为 8
40
+ }
41
+ }
42
+
43
+ // targetPath 是第一个不是 --max-workers 的参数
44
+ let targetPath = process.cwd();
45
+ for (let i = 0; i < args.length; i++) {
46
+ if (i === maxWorkersArgIndex || i === maxWorkersArgIndex + 1) continue;
47
+ targetPath = args[i] || process.cwd();
48
+ break;
49
+ }
50
+
51
+ console.log(`目标路径: ${targetPath}`);
52
+ console.log(`使用并发数: ${maxWorkers}`);
53
+
54
+ // 路径解析
23
55
  let rootDir = path.resolve(targetPath);
24
56
 
25
57
  // 如果传的是单个目录名且路径不存在,则在 packages/ 下查找
@@ -34,7 +66,7 @@ async function runEslint(targetPathArg) {
34
66
  }
35
67
  }
36
68
 
37
- console.log('目标路径:', rootDir);
69
+ console.log('目标路径解析为:', rootDir);
38
70
 
39
71
  const gitRootResult = spawnSync('git', ['rev-parse', '--show-toplevel'], {
40
72
  cwd: rootDir,
@@ -72,6 +104,17 @@ async function runEslint(targetPathArg) {
72
104
  const currentBranch = currentBranchResult.stdout.trim();
73
105
  console.log('当前分支:', currentBranch);
74
106
 
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
+ }
117
+
75
118
  // 切换分支并拉取最新代码
76
119
  if (currentBranch !== targetBranch) {
77
120
  console.log(`切换到分支 ${targetBranch} ...`);
@@ -105,19 +148,46 @@ async function runEslint(targetPathArg) {
105
148
  console.log(`检查目录: ${eslintTarget}`);
106
149
  }
107
150
 
108
- console.log('执行 ESLint...');
109
- const eslint = spawn(
110
- 'npx',
111
- ['eslint', eslintTarget, '--ext', 'ts,tsx,js,jsx', '--report-unused-disable-directives'],
112
- {
113
- cwd: rootDir,
114
- stdio: 'inherit',
115
- shell: true,
116
- },
117
- );
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('没有需要检查的文件');
156
+ return;
157
+ }
118
158
 
119
- eslint.on('close', (code) => {
120
- process.exit(code);
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
+ }
165
+
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
+ });
121
191
  });
122
192
  } catch (err) {
123
193
  console.error('脚本执行出错:', err);
@@ -0,0 +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 (!fs.existsSync(srcPath)) {
106
+ console.warn(`src 文件夹不存在,改为检查当前目录下样式文件: ${stylelintTarget}`);
107
+ } else {
108
+ console.log(`检查目录样式文件: ${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;