@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.
@@ -1,98 +1,121 @@
1
- #!/usr/bin/env node
2
-
3
- const fs = require('fs');
4
- const path = require('path');
5
-
6
- function fileExists(filepath) {
7
- try {
8
- return fs.existsSync(filepath);
9
- } catch (err) {
10
- console.error(`❌ Check whether there are any errors in the file: ${filepath}`, err);
11
- return false;
12
- }
13
- }
14
-
15
- function createIfNotExists(filename, content) {
16
- const filepath = path.resolve(process.cwd(), filename);
17
- try {
18
- if (!fs.existsSync(filepath)) {
19
- fs.writeFileSync(filepath, content, { encoding: 'utf-8' });
20
- console.log(`✅ The ${filename} has been created`);
21
- }
22
- } catch (err) {
23
- console.error(`❌ Failed to create the file: ${filename}`, err);
24
- }
25
- }
26
-
27
- function hasAnyFileExist(files) {
28
- const cwd = process.cwd();
29
- return files.some((file) => fileExists(path.join(cwd, file)));
30
- }
31
-
32
- function initConfigs() {
33
- const eslintConfigFiles = [
34
- '.eslintrc.js',
35
- '.eslintrc.cjs',
36
- '.eslintrc.json',
37
- '.eslintrc.yaml',
38
- '.eslintrc.yml',
39
- '.eslintrc',
40
- 'eslint.config.js',
41
- ];
42
-
43
- const stylelintConfigFiles = [
44
- '.stylelintrc.js',
45
- '.stylelintrc.cjs',
46
- '.stylelintrc.json',
47
- '.stylelintrc.yaml',
48
- '.stylelintrc.yml',
49
- '.stylelintrc',
50
- 'stylelint.config.js',
51
- ];
52
-
53
- const prettierConfigFiles = [
54
- '.prettierrc',
55
- '.prettierrc.js',
56
- '.prettierrc.cjs',
57
- '.prettierrc.json',
58
- '.prettierrc.yaml',
59
- '.prettierrc.yml',
60
- 'prettier.config.js',
61
- ];
62
-
63
- try {
64
- if (!hasAnyFileExist(eslintConfigFiles)) {
65
- const eslintContent = `const baseConfig = require('@apdesign/code-style-react/eslint/.eslintrc.js');
66
- module.exports = {
67
- ...baseConfig,
68
- rules: {
69
- ...baseConfig.rules,
70
- },
71
- };`;
72
- createIfNotExists('.eslintrc.cjs', eslintContent);
73
- }
74
-
75
- if (!hasAnyFileExist(stylelintConfigFiles)) {
76
- const stylelintContent = `const baseConfig = require('@apdesign/code-style-react/stylelint/.stylelintrc.js');
77
- module.exports = {
78
- ...baseConfig,
79
- rules: {
80
- ...baseConfig.rules,
81
- },
82
- };`;
83
- createIfNotExists('.stylelintrc.cjs', stylelintContent);
84
- }
85
-
86
- if (!hasAnyFileExist(prettierConfigFiles)) {
87
- const prettierContent = `const baseConfig = require('@apdesign/code-style-react/.prettierrc.js');
88
- module.exports = {
89
- ...baseConfig
90
- };`;
91
- createIfNotExists('.prettierrc.cjs', prettierContent);
92
- }
93
- } catch (err) {
94
- console.error('❌ Failed to initialize configuration:', err);
95
- }
96
- }
97
-
98
- module.exports = initConfigs;
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('node:fs');
4
+ const path = require('node:path');
5
+
6
+ function fileExists(filepath) {
7
+ try {
8
+ return fs.existsSync(filepath);
9
+ } catch (err) {
10
+ console.error(`❌ Check whether there are any errors in the file: ${filepath}`, err);
11
+ return false;
12
+ }
13
+ }
14
+
15
+ function createIfNotExists(filename, content) {
16
+ const filepath = path.resolve(process.cwd(), filename);
17
+ try {
18
+ if (!fs.existsSync(filepath)) {
19
+ fs.writeFileSync(filepath, content, { encoding: 'utf-8' });
20
+ console.log(`✅ The ${filename} has been created`);
21
+ }
22
+ } catch (err) {
23
+ console.error(`❌ Failed to create the file: ${filename}`, err);
24
+ }
25
+ }
26
+
27
+ function hasAnyFileExist(files) {
28
+ const cwd = process.cwd();
29
+ return files.some((file) => fileExists(path.join(cwd, file)));
30
+ }
31
+
32
+ function initConfigs() {
33
+ const biomeConfigFiles = ['biome.json', 'biome.jsonc'];
34
+
35
+ const stylelintConfigFiles = [
36
+ '.stylelintrc.js',
37
+ '.stylelintrc.cjs',
38
+ '.stylelintrc.json',
39
+ '.stylelintrc.yaml',
40
+ '.stylelintrc.yml',
41
+ '.stylelintrc',
42
+ 'stylelint.config.js',
43
+ ];
44
+
45
+ const prettierConfigFiles = [
46
+ '.prettierrc',
47
+ '.prettierrc.js',
48
+ '.prettierrc.cjs',
49
+ '.prettierrc.json',
50
+ '.prettierrc.yaml',
51
+ '.prettierrc.yml',
52
+ 'prettier.config.js',
53
+ ];
54
+
55
+ try {
56
+ // 创建 biome.jsonc 配置(替代 ESLint 和 Prettier)
57
+ if (!hasAnyFileExist(biomeConfigFiles)) {
58
+ const biomeContent = `{
59
+ "$schema": "https://biomejs.dev/schemas/2.2.6/schema.json",
60
+ "extends": ["@apdesign/code-style-react/biome.jsonc"],
61
+ "linter": {
62
+ "rules": {
63
+ // 在这里添加你的自定义规则
64
+ }
65
+ }
66
+ }`;
67
+ createIfNotExists('biome.jsonc', biomeContent);
68
+ }
69
+
70
+ // 创建 Stylelint 配置
71
+ if (!hasAnyFileExist(stylelintConfigFiles)) {
72
+ const stylelintContent = `const baseConfig = require('@apdesign/code-style-react/stylelint/.stylelintrc.js');
73
+ module.exports = {
74
+ ...baseConfig,
75
+ rules: {
76
+ ...baseConfig.rules,
77
+ },
78
+ };`;
79
+ createIfNotExists('.stylelintrc.cjs', stylelintContent);
80
+ }
81
+
82
+ // 创建 Prettier 配置(向后兼容)
83
+ if (!hasAnyFileExist(prettierConfigFiles)) {
84
+ const prettierContent = `const baseConfig = require('@apdesign/code-style-react/.prettierrc.js');
85
+ module.exports = {
86
+ ...baseConfig
87
+ };`;
88
+ createIfNotExists('.prettierrc.cjs', prettierContent);
89
+ }
90
+
91
+ // 创建 lefthook 配置
92
+ const lefthookConfigPath = path.resolve(process.cwd(), 'lefthook.yml');
93
+ if (!fs.existsSync(lefthookConfigPath)) {
94
+ const lefthookContent = `pre-commit:
95
+ parallel: true
96
+ commands:
97
+ lint:
98
+ glob: "*.{js,jsx,ts,tsx,json,jsonc,css,scss,md,mdx}"
99
+ run: npx lint-staged
100
+ `;
101
+ createIfNotExists('lefthook.yml', lefthookContent);
102
+ }
103
+
104
+ // 创建 lint-staged 配置(如果 package.json 中没有)
105
+ const packageJsonPath = path.resolve(process.cwd(), 'package.json');
106
+ if (fs.existsSync(packageJsonPath)) {
107
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
108
+ if (!packageJson['lint-staged']) {
109
+ packageJson['lint-staged'] = {
110
+ '*.{js,jsx,ts,tsx,json,jsonc,css,scss,md,mdx}': ['npx ultracite fix'],
111
+ };
112
+ fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf-8');
113
+ console.log('✅ lint-staged configuration added to package.json');
114
+ }
115
+ }
116
+ } catch (err) {
117
+ console.error('❌ Failed to initialize configuration:', err);
118
+ }
119
+ }
120
+
121
+ module.exports = initConfigs;
@@ -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;