my-code-style 1.0.0 → 1.4.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.
- package/README.md +82 -42
- package/bin/init +918 -0
- package/package.json +220 -118
- package/src/commitlint/base.cjs +0 -1
- package/src/commitlint/scopes.cjs +182 -12
- package/src/eslint/base.cjs +43 -8
- package/src/eslint/flat/_shared.mjs +32 -9
- package/src/eslint/flat/base.mjs +35 -12
- package/src/eslint/flat/vue3.mjs +17 -7
- package/src/eslint/vue3.cjs +22 -2
- package/src/gitattributes +8 -2
- package/src/gitignore +10 -0
- package/src/husky/commit-msg +1 -4
- package/src/husky/pre-commit +0 -3
- package/src/prettier/index.cjs +12 -3
- package/src/stylelint/index.cjs +22 -1
- package/src/stylelint/less.cjs +17 -2
- package/bin/init.cjs +0 -476
package/package.json
CHANGED
|
@@ -1,121 +1,223 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
2
|
+
"name": "my-code-style",
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "Lint/Format/Git 配置工程化 npm 包(ESLint + Prettier + Stylelint + Commitlint + Husky + standard-version)",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "src/eslint/uniapp.cjs",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/myvn/codeStyle.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/myvn/codeStyle#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/myvn/codeStyle/issues"
|
|
14
|
+
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"my-code-style-init": "./bin/init"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./src/eslint/uniapp.cjs",
|
|
20
|
+
"./eslint": "./src/eslint/base.cjs",
|
|
21
|
+
"./eslint/vue3": "./src/eslint/vue3.cjs",
|
|
22
|
+
"./eslint/uniapp": "./src/eslint/uniapp.cjs",
|
|
23
|
+
"./eslint/flat": "./src/eslint/flat/base.mjs",
|
|
24
|
+
"./eslint/flat/vue3": "./src/eslint/flat/vue3.mjs",
|
|
25
|
+
"./eslint/flat/uniapp": "./src/eslint/flat/uniapp.mjs",
|
|
26
|
+
"./prettier": "./src/prettier/index.cjs",
|
|
27
|
+
"./stylelint": "./src/stylelint/index.cjs",
|
|
28
|
+
"./stylelint/less": "./src/stylelint/less.cjs",
|
|
29
|
+
"./stylelint/less-override": "./src/stylelint/less-override.cjs",
|
|
30
|
+
"./commitlint": "./src/commitlint/base.cjs",
|
|
31
|
+
"./commitlint/scopes": "./src/commitlint/scopes.cjs",
|
|
32
|
+
"./versionrc": "./src/versionrc/index.cjs"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"src",
|
|
36
|
+
"bin",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"registry": "https://registry.npmjs.org/"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"test": "node --test tests/*.test.cjs demo-test/*.test.cjs",
|
|
45
|
+
"test:demo": "node --test demo-test/*.test.cjs",
|
|
46
|
+
"test:integration:setup": "node demo-test/setup-integration.cjs",
|
|
47
|
+
"test:integration": "node --test demo-test/integration/*.test.cjs",
|
|
48
|
+
"test:legacy:setup": "node demo-test/setup-legacy.cjs",
|
|
49
|
+
"test:legacy": "node --test demo-test/legacy/*.test.cjs",
|
|
50
|
+
"test:all": "npm test && npm run test:integration && npm run test:legacy",
|
|
51
|
+
"lint": "eslint .",
|
|
52
|
+
"lint:fix": "eslint . --fix",
|
|
53
|
+
"format": "prettier --write .",
|
|
54
|
+
"prepare": "node -e \"if (!process.env.CI) { try { require('child_process').execSync('husky', { stdio: 'ignore' }) } catch {} }\"",
|
|
55
|
+
"release": "standard-version",
|
|
56
|
+
"release:push": "standard-version && git push --follow-tags origin HEAD",
|
|
57
|
+
"pub": "npm run release:push",
|
|
58
|
+
"cz": "czg",
|
|
59
|
+
"link": "pnpm link --global",
|
|
60
|
+
"unlink": "pnpm unlink --global",
|
|
61
|
+
"pack": "npm pack"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=18"
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"@commitlint/cli": "^19.0.0",
|
|
68
|
+
"@commitlint/config-conventional": "^19.0.0",
|
|
69
|
+
"@eslint/eslintrc": "^3.0.0",
|
|
70
|
+
"@eslint/js": "^9.0.0",
|
|
71
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
72
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
73
|
+
"czg": "^1.0.0",
|
|
74
|
+
"eslint": "^8.57.0 || ^9.0.0",
|
|
75
|
+
"eslint-config-prettier": "^10.1.8",
|
|
76
|
+
"eslint-import-resolver-typescript": "^3.0.0",
|
|
77
|
+
"eslint-plugin-import": "^2.0.0",
|
|
78
|
+
"eslint-plugin-import-x": "^4.0.0",
|
|
79
|
+
"eslint-plugin-prettier": "^5.2.0",
|
|
80
|
+
"eslint-plugin-vue": "^10.0.0",
|
|
81
|
+
"globals": "^16.0.0",
|
|
82
|
+
"husky": "^9.0.0",
|
|
83
|
+
"lint-staged": "^16.0.0",
|
|
84
|
+
"postcss-html": "^2.0.0",
|
|
85
|
+
"postcss-less": "^6.0.0",
|
|
86
|
+
"postcss-scss": "^4.0.0",
|
|
87
|
+
"prettier": "^3.0.0",
|
|
88
|
+
"standard-version": "^9.0.0",
|
|
89
|
+
"stylelint": "^16.0.0",
|
|
90
|
+
"stylelint-config-html": "^2.0.0",
|
|
91
|
+
"stylelint-config-recess-order": "^5.0.0",
|
|
92
|
+
"stylelint-config-recommended": "^17.0.0",
|
|
93
|
+
"stylelint-config-recommended-scss": "^16.0.0",
|
|
94
|
+
"stylelint-config-recommended-vue": "^2.0.0",
|
|
95
|
+
"stylelint-prettier": "^5.0.0",
|
|
96
|
+
"typescript-eslint": "^8.0.0",
|
|
97
|
+
"vue-eslint-parser": "^10.3.0"
|
|
98
|
+
},
|
|
99
|
+
"peerDependenciesMeta": {
|
|
100
|
+
"@commitlint/cli": {
|
|
101
|
+
"optional": true
|
|
102
|
+
},
|
|
103
|
+
"@commitlint/config-conventional": {
|
|
104
|
+
"optional": true
|
|
105
|
+
},
|
|
106
|
+
"@eslint/eslintrc": {
|
|
107
|
+
"optional": true
|
|
108
|
+
},
|
|
109
|
+
"@eslint/js": {
|
|
110
|
+
"optional": true
|
|
111
|
+
},
|
|
112
|
+
"@typescript-eslint/eslint-plugin": {
|
|
113
|
+
"optional": true
|
|
114
|
+
},
|
|
115
|
+
"@typescript-eslint/parser": {
|
|
116
|
+
"optional": true
|
|
117
|
+
},
|
|
118
|
+
"czg": {
|
|
119
|
+
"optional": true
|
|
120
|
+
},
|
|
121
|
+
"eslint": {
|
|
122
|
+
"optional": true
|
|
123
|
+
},
|
|
124
|
+
"eslint-config-prettier": {
|
|
125
|
+
"optional": true
|
|
126
|
+
},
|
|
127
|
+
"eslint-import-resolver-typescript": {
|
|
128
|
+
"optional": true
|
|
129
|
+
},
|
|
130
|
+
"eslint-plugin-import": {
|
|
131
|
+
"optional": true
|
|
132
|
+
},
|
|
133
|
+
"eslint-plugin-import-x": {
|
|
134
|
+
"optional": true
|
|
135
|
+
},
|
|
136
|
+
"eslint-plugin-prettier": {
|
|
137
|
+
"optional": true
|
|
138
|
+
},
|
|
139
|
+
"eslint-plugin-vue": {
|
|
140
|
+
"optional": true
|
|
141
|
+
},
|
|
142
|
+
"globals": {
|
|
143
|
+
"optional": true
|
|
144
|
+
},
|
|
145
|
+
"husky": {
|
|
146
|
+
"optional": true
|
|
147
|
+
},
|
|
148
|
+
"lint-staged": {
|
|
149
|
+
"optional": true
|
|
150
|
+
},
|
|
151
|
+
"postcss-html": {
|
|
152
|
+
"optional": true
|
|
153
|
+
},
|
|
154
|
+
"postcss-less": {
|
|
155
|
+
"optional": true
|
|
156
|
+
},
|
|
157
|
+
"postcss-scss": {
|
|
158
|
+
"optional": true
|
|
159
|
+
},
|
|
160
|
+
"prettier": {
|
|
161
|
+
"optional": true
|
|
162
|
+
},
|
|
163
|
+
"standard-version": {
|
|
164
|
+
"optional": true
|
|
165
|
+
},
|
|
166
|
+
"stylelint": {
|
|
167
|
+
"optional": true
|
|
168
|
+
},
|
|
169
|
+
"stylelint-config-html": {
|
|
170
|
+
"optional": true
|
|
171
|
+
},
|
|
172
|
+
"stylelint-config-recess-order": {
|
|
173
|
+
"optional": true
|
|
174
|
+
},
|
|
175
|
+
"stylelint-config-recommended": {
|
|
176
|
+
"optional": true
|
|
177
|
+
},
|
|
178
|
+
"stylelint-config-recommended-scss": {
|
|
179
|
+
"optional": true
|
|
180
|
+
},
|
|
181
|
+
"stylelint-config-recommended-vue": {
|
|
182
|
+
"optional": true
|
|
183
|
+
},
|
|
184
|
+
"stylelint-prettier": {
|
|
185
|
+
"optional": true
|
|
186
|
+
},
|
|
187
|
+
"typescript-eslint": {
|
|
188
|
+
"optional": true
|
|
189
|
+
},
|
|
190
|
+
"vue-eslint-parser": {
|
|
191
|
+
"optional": true
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
"devDependencies": {
|
|
195
|
+
"standard-version": "^9.5.0"
|
|
196
|
+
},
|
|
197
|
+
"keywords": [
|
|
198
|
+
"eslint-config",
|
|
199
|
+
"prettier-config",
|
|
200
|
+
"stylelint-config",
|
|
201
|
+
"commitlint-config",
|
|
202
|
+
"vue",
|
|
203
|
+
"uni-app",
|
|
204
|
+
"typescript"
|
|
205
|
+
],
|
|
206
|
+
"license": "MIT",
|
|
207
|
+
"lint-staged": {
|
|
208
|
+
"**/*.{vue,nvue}": [
|
|
209
|
+
"prettier --write",
|
|
210
|
+
"eslint --fix"
|
|
211
|
+
],
|
|
212
|
+
"**/*.{js,ts,jsx,tsx,cjs,mjs,mts,cts}": [
|
|
213
|
+
"prettier --write",
|
|
214
|
+
"eslint --fix"
|
|
30
215
|
],
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
},
|
|
34
|
-
"engines": {
|
|
35
|
-
"node": ">=18"
|
|
36
|
-
},
|
|
37
|
-
"peerDependencies": {
|
|
38
|
-
"@commitlint/cli": ">=18.0.0",
|
|
39
|
-
"@commitlint/config-conventional": ">=18.0.0",
|
|
40
|
-
"@eslint/eslintrc": ">=3.0.0",
|
|
41
|
-
"@eslint/js": ">=9.0.0",
|
|
42
|
-
"@typescript-eslint/eslint-plugin": ">=6.0.0",
|
|
43
|
-
"@typescript-eslint/parser": ">=6.0.0",
|
|
44
|
-
"czg": ">=1.0.0",
|
|
45
|
-
"eslint": ">=8.0.0",
|
|
46
|
-
"eslint-config-prettier": ">=9.0.0",
|
|
47
|
-
"eslint-config-standard": ">=17.0.0",
|
|
48
|
-
"eslint-import-resolver-typescript": ">=3.0.0",
|
|
49
|
-
"eslint-plugin-import": ">=2.0.0",
|
|
50
|
-
"eslint-plugin-import-x": ">=3.0.0",
|
|
51
|
-
"eslint-plugin-prettier": ">=5.0.0",
|
|
52
|
-
"eslint-plugin-vue": ">=9.0.0",
|
|
53
|
-
"globals": ">=15.0.0",
|
|
54
|
-
"husky": ">=8.0.0",
|
|
55
|
-
"lint-staged": ">=15.0.0",
|
|
56
|
-
"postcss-html": ">=1.0.0",
|
|
57
|
-
"postcss-less": ">=6.0.0",
|
|
58
|
-
"postcss-scss": ">=4.0.0",
|
|
59
|
-
"prettier": ">=3.0.0",
|
|
60
|
-
"standard-version": ">=9.0.0",
|
|
61
|
-
"stylelint": ">=16.0.0",
|
|
62
|
-
"stylelint-config-html": ">=1.0.0",
|
|
63
|
-
"stylelint-config-recess-order": ">=4.0.0",
|
|
64
|
-
"stylelint-config-recommended": ">=14.0.0",
|
|
65
|
-
"stylelint-config-recommended-scss": ">=14.0.0",
|
|
66
|
-
"stylelint-config-recommended-vue": ">=1.0.0",
|
|
67
|
-
"stylelint-prettier": ">=5.0.0",
|
|
68
|
-
"typescript-eslint": ">=8.0.0"
|
|
69
|
-
},
|
|
70
|
-
"peerDependenciesMeta": {
|
|
71
|
-
"@eslint/eslintrc": {
|
|
72
|
-
"optional": true
|
|
73
|
-
},
|
|
74
|
-
"@eslint/js": {
|
|
75
|
-
"optional": true
|
|
76
|
-
},
|
|
77
|
-
"eslint-plugin-vue": {
|
|
78
|
-
"optional": true
|
|
79
|
-
},
|
|
80
|
-
"eslint-plugin-import-x": {
|
|
81
|
-
"optional": true
|
|
82
|
-
},
|
|
83
|
-
"globals": {
|
|
84
|
-
"optional": true
|
|
85
|
-
},
|
|
86
|
-
"postcss-less": {
|
|
87
|
-
"optional": true
|
|
88
|
-
},
|
|
89
|
-
"postcss-scss": {
|
|
90
|
-
"optional": true
|
|
91
|
-
},
|
|
92
|
-
"postcss-html": {
|
|
93
|
-
"optional": true
|
|
94
|
-
},
|
|
95
|
-
"stylelint-config-recommended-scss": {
|
|
96
|
-
"optional": true
|
|
97
|
-
},
|
|
98
|
-
"stylelint-config-recommended-vue": {
|
|
99
|
-
"optional": true
|
|
100
|
-
},
|
|
101
|
-
"stylelint-config-html": {
|
|
102
|
-
"optional": true
|
|
103
|
-
},
|
|
104
|
-
"typescript-eslint": {
|
|
105
|
-
"optional": true
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
"devDependencies": {
|
|
109
|
-
"standard-version": "^9.5.0"
|
|
110
|
-
},
|
|
111
|
-
"keywords": [
|
|
112
|
-
"eslint-config",
|
|
113
|
-
"prettier-config",
|
|
114
|
-
"stylelint-config",
|
|
115
|
-
"commitlint-config",
|
|
116
|
-
"vue",
|
|
117
|
-
"uni-app",
|
|
118
|
-
"typescript"
|
|
216
|
+
"**/*.{html,css}": [
|
|
217
|
+
"prettier --write"
|
|
119
218
|
],
|
|
120
|
-
"
|
|
121
|
-
|
|
219
|
+
"**/*.{json,json5,md,yml,yaml}": [
|
|
220
|
+
"prettier --write"
|
|
221
|
+
]
|
|
222
|
+
}
|
|
223
|
+
}
|
package/src/commitlint/base.cjs
CHANGED
|
@@ -3,6 +3,63 @@ const fs = require("fs")
|
|
|
3
3
|
const path = require("path")
|
|
4
4
|
const { execSync } = require("child_process")
|
|
5
5
|
|
|
6
|
+
const PLURAL_MAP = {
|
|
7
|
+
components: "component",
|
|
8
|
+
views: "view",
|
|
9
|
+
pages: "page",
|
|
10
|
+
layouts: "layout",
|
|
11
|
+
hooks: "hook",
|
|
12
|
+
utils: "util",
|
|
13
|
+
stores: "store",
|
|
14
|
+
apis: "api",
|
|
15
|
+
services: "service",
|
|
16
|
+
plugins: "plugin",
|
|
17
|
+
directives: "directive",
|
|
18
|
+
filters: "filter",
|
|
19
|
+
constants: "constant",
|
|
20
|
+
helpers: "helper",
|
|
21
|
+
scripts: "script",
|
|
22
|
+
styles: "style",
|
|
23
|
+
types: "types",
|
|
24
|
+
settings: "setting",
|
|
25
|
+
assets: "asset",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function toSingular(name) {
|
|
29
|
+
if (!name) {
|
|
30
|
+
return name
|
|
31
|
+
}
|
|
32
|
+
if (PLURAL_MAP[name]) {
|
|
33
|
+
return PLURAL_MAP[name]
|
|
34
|
+
}
|
|
35
|
+
if (name.endsWith("ies") && name.length > 4) {
|
|
36
|
+
return name.slice(0, -3) + "y"
|
|
37
|
+
}
|
|
38
|
+
if (/(?:[sxz]|[ch|sh])es$/.test(name)) {
|
|
39
|
+
return name.slice(0, -2)
|
|
40
|
+
}
|
|
41
|
+
const nonPluralEndingInS = [
|
|
42
|
+
"status",
|
|
43
|
+
"canvas",
|
|
44
|
+
"bus",
|
|
45
|
+
"pass",
|
|
46
|
+
"process",
|
|
47
|
+
"css",
|
|
48
|
+
"less",
|
|
49
|
+
"scss",
|
|
50
|
+
"js",
|
|
51
|
+
"ts",
|
|
52
|
+
"types",
|
|
53
|
+
]
|
|
54
|
+
if (nonPluralEndingInS.includes(name.toLowerCase())) {
|
|
55
|
+
return name
|
|
56
|
+
}
|
|
57
|
+
if (name.endsWith("s") && !name.endsWith("ss") && name.length > 3) {
|
|
58
|
+
return name.slice(0, -1)
|
|
59
|
+
}
|
|
60
|
+
return name
|
|
61
|
+
}
|
|
62
|
+
|
|
6
63
|
/**
|
|
7
64
|
* Generate scopes by reading directory names
|
|
8
65
|
* @param {string|string[]} srcDirs - Source directory name(s) relative to cwd
|
|
@@ -14,30 +71,143 @@ function generateScopes(srcDirs = "src") {
|
|
|
14
71
|
|
|
15
72
|
for (const srcDir of dirs) {
|
|
16
73
|
const resolved = path.resolve(process.cwd(), srcDir)
|
|
17
|
-
if (!fs.existsSync(resolved))
|
|
74
|
+
if (!fs.existsSync(resolved)) {
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
18
77
|
const scopes = fs
|
|
19
78
|
.readdirSync(resolved, { withFileTypes: true })
|
|
20
79
|
.filter((dirent) => dirent.isDirectory())
|
|
21
|
-
.map((dirent) => dirent.name
|
|
80
|
+
.map((dirent) => toSingular(dirent.name))
|
|
22
81
|
allScopes.push(...scopes)
|
|
23
82
|
}
|
|
24
83
|
|
|
25
84
|
return [...new Set(allScopes)]
|
|
26
85
|
}
|
|
27
86
|
|
|
87
|
+
function getStagedFiles(cwd) {
|
|
88
|
+
try {
|
|
89
|
+
const output = execSync("git diff --cached --name-only -z", {
|
|
90
|
+
cwd,
|
|
91
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
92
|
+
}).toString()
|
|
93
|
+
return output.split("\0").filter(Boolean)
|
|
94
|
+
} catch {
|
|
95
|
+
try {
|
|
96
|
+
const output = execSync("git status --porcelain -z", {
|
|
97
|
+
cwd,
|
|
98
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
99
|
+
}).toString()
|
|
100
|
+
const parts = output.split("\0")
|
|
101
|
+
const staged = []
|
|
102
|
+
for (let i = 0; i < parts.length; i++) {
|
|
103
|
+
const entry = parts[i]
|
|
104
|
+
if (!entry) continue
|
|
105
|
+
const x = entry[0]
|
|
106
|
+
const isRenameOrCopy = x === "R" || x === "C"
|
|
107
|
+
const filePath = entry.slice(3)
|
|
108
|
+
if (x !== " " && x !== "?" && x !== "!") {
|
|
109
|
+
if (filePath) staged.push(filePath)
|
|
110
|
+
}
|
|
111
|
+
if (isRenameOrCopy) {
|
|
112
|
+
i++
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return staged
|
|
116
|
+
} catch {
|
|
117
|
+
return []
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
28
122
|
/**
|
|
29
|
-
* Guess the current scope from
|
|
30
|
-
*
|
|
123
|
+
* Guess the current scope from staged files in the git index.
|
|
124
|
+
* Only staged files are considered (unstaged and untracked changes are ignored).
|
|
125
|
+
* Multi-directory strategy: votes by the number of staged changes under each candidate scope directory;
|
|
126
|
+
* in case of a tie, the first encountered scope among staged files is chosen.
|
|
127
|
+
*
|
|
128
|
+
* @param {string|string[]} [srcDirs="src"] - Source directory name(s) relative to cwd
|
|
129
|
+
* @returns {string|undefined} Guessed scope name, or undefined if none matched
|
|
31
130
|
*/
|
|
32
|
-
function guessCurrentScope() {
|
|
131
|
+
function guessCurrentScope(srcDirs = "src") {
|
|
33
132
|
try {
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
.
|
|
40
|
-
|
|
133
|
+
const cwd = process.cwd()
|
|
134
|
+
const rawPrefix = execSync("git rev-parse --show-prefix", {
|
|
135
|
+
cwd,
|
|
136
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
137
|
+
})
|
|
138
|
+
.toString()
|
|
139
|
+
.trim()
|
|
140
|
+
const prefix = rawPrefix.replace(/\\/g, "/")
|
|
141
|
+
|
|
142
|
+
const stagedFiles = getStagedFiles(cwd)
|
|
143
|
+
if (!stagedFiles || stagedFiles.length === 0) {
|
|
144
|
+
return undefined
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const normalizedDirs = (Array.isArray(srcDirs) ? srcDirs : [srcDirs])
|
|
148
|
+
.map((d) => d.replace(/\\/g, "/").replace(/^\/+|\/+$/g, ""))
|
|
149
|
+
.filter(Boolean)
|
|
150
|
+
|
|
151
|
+
const candidateScopes = []
|
|
152
|
+
|
|
153
|
+
for (const file of stagedFiles) {
|
|
154
|
+
const normalizedFile = file.replace(/\\/g, "/")
|
|
155
|
+
if (prefix && !normalizedFile.startsWith(prefix)) {
|
|
156
|
+
continue
|
|
157
|
+
}
|
|
158
|
+
const relativeToCwd = prefix ? normalizedFile.slice(prefix.length) : normalizedFile
|
|
159
|
+
|
|
160
|
+
for (const srcDir of normalizedDirs) {
|
|
161
|
+
const dirPrefix = srcDir ? `${srcDir}/` : ""
|
|
162
|
+
if (relativeToCwd.startsWith(dirPrefix)) {
|
|
163
|
+
const subPath = relativeToCwd.slice(dirPrefix.length)
|
|
164
|
+
const segments = subPath.split("/").filter(Boolean)
|
|
165
|
+
// Must have at least 2 segments (directory + file) to belong to a scope directory
|
|
166
|
+
if (segments.length >= 2) {
|
|
167
|
+
const rawName = segments[0]
|
|
168
|
+
const resolvedDir = path.resolve(cwd, srcDir, rawName)
|
|
169
|
+
if (fs.existsSync(resolvedDir)) {
|
|
170
|
+
if (fs.statSync(resolvedDir).isDirectory()) {
|
|
171
|
+
candidateScopes.push(toSingular(rawName))
|
|
172
|
+
break
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
// If deleted on disk, having segments >= 2 indicates it was a directory in git
|
|
176
|
+
candidateScopes.push(toSingular(rawName))
|
|
177
|
+
break
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (candidateScopes.length === 0) {
|
|
185
|
+
return undefined
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Multi-directory selection strategy: vote by frequency; first encountered breaks ties
|
|
189
|
+
const counts = new Map()
|
|
190
|
+
const order = []
|
|
191
|
+
|
|
192
|
+
for (const scope of candidateScopes) {
|
|
193
|
+
if (!counts.has(scope)) {
|
|
194
|
+
counts.set(scope, 0)
|
|
195
|
+
order.push(scope)
|
|
196
|
+
}
|
|
197
|
+
counts.set(scope, counts.get(scope) + 1)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
let bestScope = undefined
|
|
201
|
+
let maxCount = 0
|
|
202
|
+
for (const scope of order) {
|
|
203
|
+
const count = counts.get(scope)
|
|
204
|
+
if (count > maxCount) {
|
|
205
|
+
maxCount = count
|
|
206
|
+
bestScope = scope
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return bestScope
|
|
41
211
|
} catch {
|
|
42
212
|
return undefined
|
|
43
213
|
}
|
package/src/eslint/base.cjs
CHANGED
|
@@ -5,23 +5,49 @@ module.exports = {
|
|
|
5
5
|
es2021: true,
|
|
6
6
|
node: true,
|
|
7
7
|
},
|
|
8
|
+
parser: "@typescript-eslint/parser",
|
|
8
9
|
extends: [
|
|
9
10
|
"eslint:recommended",
|
|
10
11
|
"plugin:@typescript-eslint/recommended",
|
|
11
12
|
"plugin:import/recommended",
|
|
12
|
-
"standard",
|
|
13
13
|
"prettier",
|
|
14
14
|
"plugin:prettier/recommended",
|
|
15
15
|
],
|
|
16
16
|
parserOptions: {
|
|
17
17
|
ecmaVersion: "latest",
|
|
18
|
-
parser: "@typescript-eslint/parser",
|
|
19
18
|
sourceType: "module",
|
|
19
|
+
ecmaFeatures: {
|
|
20
|
+
jsx: true,
|
|
21
|
+
},
|
|
20
22
|
},
|
|
21
23
|
plugins: ["@typescript-eslint", "prettier", "import"],
|
|
24
|
+
overrides: [
|
|
25
|
+
{
|
|
26
|
+
files: ["**/*.cjs", "**/.*rc.js", "**/*.config.js"],
|
|
27
|
+
rules: { "@typescript-eslint/no-require-imports": "off" },
|
|
28
|
+
},
|
|
29
|
+
],
|
|
22
30
|
rules: {
|
|
23
31
|
// Prettier 集成
|
|
24
|
-
"prettier/prettier": [
|
|
32
|
+
"prettier/prettier": [
|
|
33
|
+
"error",
|
|
34
|
+
{
|
|
35
|
+
singleQuote: false,
|
|
36
|
+
tabWidth: 4,
|
|
37
|
+
semi: false,
|
|
38
|
+
trailingComma: "all",
|
|
39
|
+
endOfLine: "lf",
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
// 格式化与风格
|
|
43
|
+
indent: "off",
|
|
44
|
+
semi: ["error", "never"],
|
|
45
|
+
quotes: ["error", "double", { avoidEscape: false, allowTemplateLiterals: true }],
|
|
46
|
+
curly: ["error", "all"],
|
|
47
|
+
"comma-dangle": ["error", "always-multiline"],
|
|
48
|
+
"object-curly-spacing": ["error", "always"],
|
|
49
|
+
"array-bracket-spacing": ["error", "never"],
|
|
50
|
+
|
|
25
51
|
// Import 相关
|
|
26
52
|
"import/no-unresolved": "off",
|
|
27
53
|
"import/extensions": [
|
|
@@ -29,24 +55,33 @@ module.exports = {
|
|
|
29
55
|
"ignorePackages",
|
|
30
56
|
{ js: "never", jsx: "never", ts: "never", tsx: "never" },
|
|
31
57
|
],
|
|
32
|
-
"import/prefer-default-export":
|
|
58
|
+
"import/prefer-default-export": "off",
|
|
33
59
|
"import/no-extraneous-dependencies": "off",
|
|
60
|
+
|
|
34
61
|
// TypeScript
|
|
35
62
|
"@typescript-eslint/no-redeclare": "error",
|
|
63
|
+
"@typescript-eslint/no-explicit-any": "off",
|
|
64
|
+
"@typescript-eslint/no-unused-vars": "off",
|
|
65
|
+
"@typescript-eslint/no-unused-expressions": "off",
|
|
66
|
+
"@typescript-eslint/ban-ts-comment": "off",
|
|
67
|
+
"@typescript-eslint/no-empty-object-type": "off",
|
|
68
|
+
|
|
36
69
|
// 常用关闭
|
|
37
|
-
"no-console":
|
|
70
|
+
"no-console": "off",
|
|
38
71
|
"no-plusplus": "off",
|
|
39
72
|
"no-shadow": "off",
|
|
40
73
|
"no-underscore-dangle": "off",
|
|
41
74
|
"no-use-before-define": "off",
|
|
42
75
|
"no-undef": "off",
|
|
43
76
|
"no-unused-vars": "off",
|
|
77
|
+
"no-unused-expressions": "off",
|
|
44
78
|
"no-param-reassign": "off",
|
|
45
|
-
"@typescript-eslint/no-unused-vars": "off",
|
|
46
79
|
"no-redeclare": "off",
|
|
47
80
|
"prefer-promise-reject-errors": "off",
|
|
48
|
-
|
|
49
|
-
|
|
81
|
+
|
|
82
|
+
// Standard JS 风格规则已通过手动配置实现,不再依赖 eslint-config-standard
|
|
83
|
+
"style/quotes": "off",
|
|
84
|
+
"style/semi": "off",
|
|
50
85
|
},
|
|
51
86
|
settings: {
|
|
52
87
|
"import/parsers": { "@typescript-eslint/parser": [".ts", ".tsx"] },
|