@apdesign/code-style-react 1.2.2 → 2.0.0

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.
@@ -1,33 +1,36 @@
1
- #!/usr/bin/env node
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
- const { execSync } = require('child_process');
6
-
7
- function initHusky() {
8
- const projectRoot = process.cwd();
9
- const huskyTarget = path.join(projectRoot, '.husky');
10
- const huskySource = path.join(__dirname, '../husky');
11
-
12
- try {
13
- if (fs.existsSync(huskyTarget)) {
14
- fs.rmSync(huskyTarget, { recursive: true, force: true });
15
- }
16
- fs.cpSync(huskySource, huskyTarget, { recursive: true });
17
-
18
- fs.readdirSync(huskyTarget).forEach((file) => {
19
- const filePath = path.join(huskyTarget, file);
20
- if (fs.statSync(filePath).isFile()) {
21
- fs.chmodSync(filePath, 0o755);
22
- }
23
- });
24
-
25
- execSync('git config core.hooksPath .husky');
26
-
27
- console.log('✅ Husky hooks installed from @apdesign/code-style-react');
28
- } catch (err) {
29
- console.error('❌ Failed to initialize husky:', err);
30
- }
31
- }
32
-
33
- module.exports = initHusky;
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+ const { execSync } = require('node:child_process');
6
+
7
+ // 文件权限常量:可执行文件权限 (rwxr-xr-x)
8
+ const EXECUTABLE_PERMISSION = 0o755;
9
+
10
+ function initHusky() {
11
+ const projectRoot = process.cwd();
12
+ const huskyTarget = path.join(projectRoot, '.husky');
13
+ const huskySource = path.join(__dirname, '../husky');
14
+
15
+ try {
16
+ if (fs.existsSync(huskyTarget)) {
17
+ fs.rmSync(huskyTarget, { recursive: true, force: true });
18
+ }
19
+ fs.cpSync(huskySource, huskyTarget, { recursive: true });
20
+
21
+ fs.readdirSync(huskyTarget).forEach((file) => {
22
+ const filePath = path.join(huskyTarget, file);
23
+ if (fs.statSync(filePath).isFile()) {
24
+ fs.chmodSync(filePath, EXECUTABLE_PERMISSION);
25
+ }
26
+ });
27
+
28
+ execSync('git config core.hooksPath .husky');
29
+
30
+ console.log('✅ Husky hooks installed from @apdesign/code-style-react');
31
+ } catch (err) {
32
+ console.error('❌ Failed to initialize husky:', err);
33
+ }
34
+ }
35
+
36
+ module.exports = initHusky;
@@ -1,36 +1,39 @@
1
- #!/usr/bin/env node
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
-
6
- function initScripts() {
7
- const projectRoot = process.cwd();
8
- const projectScriptsDir = path.join(projectRoot, 'scripts');
9
- const packageScriptsDir = path.join(__dirname, '../scripts');
10
-
11
- try {
12
- if (!fs.existsSync(projectScriptsDir)) {
13
- fs.mkdirSync(projectScriptsDir, { recursive: true });
14
- console.log('📁 Created scripts directory in project');
15
- }
16
-
17
- fs.readdirSync(packageScriptsDir)
18
- .filter((file) => file.endsWith('.sh'))
19
- .forEach((file) => {
20
- const srcFile = path.join(packageScriptsDir, file);
21
- const destFile = path.join(projectScriptsDir, file);
22
-
23
- if (fs.existsSync(destFile)) {
24
- return;
25
- }
26
-
27
- fs.copyFileSync(srcFile, destFile);
28
- fs.chmodSync(destFile, 0o755);
29
- console.log(`📄 Copied ${file} to scripts/`);
30
- });
31
- } catch (err) {
32
- console.error('❌ Failed to initialize scripts:', err);
33
- }
34
- }
35
-
36
- module.exports = initScripts;
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+
6
+ // 文件权限常量:可执行文件权限 (rwxr-xr-x)
7
+ const EXECUTABLE_PERMISSION = 0o755;
8
+
9
+ function initScripts() {
10
+ const projectRoot = process.cwd();
11
+ const projectScriptsDir = path.join(projectRoot, 'scripts');
12
+ const packageScriptsDir = path.join(__dirname, '../scripts');
13
+
14
+ try {
15
+ if (!fs.existsSync(projectScriptsDir)) {
16
+ fs.mkdirSync(projectScriptsDir, { recursive: true });
17
+ console.log('📁 Created scripts directory in project');
18
+ }
19
+
20
+ fs.readdirSync(packageScriptsDir)
21
+ .filter((file) => file.endsWith('.sh'))
22
+ .forEach((file) => {
23
+ const srcFile = path.join(packageScriptsDir, file);
24
+ const destFile = path.join(projectScriptsDir, file);
25
+
26
+ if (fs.existsSync(destFile)) {
27
+ return;
28
+ }
29
+
30
+ fs.copyFileSync(srcFile, destFile);
31
+ fs.chmodSync(destFile, EXECUTABLE_PERMISSION);
32
+ console.log(`📄 Copied ${file} to scripts/`);
33
+ });
34
+ } catch (err) {
35
+ console.error('❌ Failed to initialize scripts:', err);
36
+ }
37
+ }
38
+
39
+ module.exports = initScripts;
@@ -1,185 +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(`\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 && 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.error(`❌ 当前目录下不存在 src 文件夹`);
151
- process.exit(1);
152
- } else {
153
- console.log(`🔍 检查目录: ${eslintTarget}`);
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 formatter = await eslint.loadFormatter('stylish');
174
- const resultText = formatter.format(results);
175
- console.log(resultText);
176
-
177
- const hasError = results.some((r) => r.errorCount > 0);
178
- process.exit(hasError ? 1 : 0);
179
- } catch (err) {
180
- console.error('❌ 脚本执行出错:', err);
181
- process.exit(1);
182
- }
183
- }
184
-
185
- 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;