@d-zero/lint-staged-config 5.0.0-dev.34

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 D-ZERO Co., Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # `@d-zero/lint-staged-config`
2
+
3
+ - 使用: 🆗 使用可
4
+ - 解説: 🚧 準備中
package/index.mjs ADDED
@@ -0,0 +1,96 @@
1
+ import path from 'node:path';
2
+
3
+ import micromatch from 'micromatch';
4
+
5
+ /**
6
+ * lint-staged Config generator
7
+ *
8
+ * @param {string} [dir]
9
+ * @param {import("./types").CommandMappings} [mapping]
10
+ * @return {import("./types").LintStagedCommandMapper}
11
+ */
12
+ export default function (dir, mapping) {
13
+ return (allStagedFiles) => {
14
+ /**
15
+ * @type {string[]}
16
+ */
17
+ const commandList = [];
18
+ const cwd = process.cwd();
19
+
20
+ dir = !dir
21
+ ? // 引数がないならカレントディレクトリ
22
+ cwd
23
+ : // 絶対パスかどうか
24
+ path.isAbsolute(dir)
25
+ ? // 絶対パスならそのまま
26
+ dir
27
+ : // 相対パスなら絶対パスに変換
28
+ path.resolve(cwd, dir);
29
+
30
+ mapping = mapping ?? defaultMapping;
31
+
32
+ Object.entries(mapping).forEach(([ext, commandTypes]) => {
33
+ commandTypes.forEach((commandType) => {
34
+ const shell = commands[commandType];
35
+
36
+ if (!shell) {
37
+ return;
38
+ }
39
+
40
+ const pattern = path
41
+ .resolve(dir, '**', `{*.${ext},.*.${ext}}`)
42
+ .replaceAll(path.sep, '/');
43
+
44
+ const files = allStagedFiles.map((f) => f.replaceAll(path.sep, '/'));
45
+
46
+ const targetFiles = micromatch(files, pattern);
47
+
48
+ if (targetFiles.length <= 0) {
49
+ return;
50
+ }
51
+
52
+ commandList.push(shell + ' ' + targetFiles.map((f) => `"${f}"`).join(' '));
53
+ });
54
+ });
55
+
56
+ return commandList;
57
+ };
58
+ }
59
+
60
+ /**
61
+ * @type {Record<import("./types").CommandType, string>}
62
+ */
63
+ export const commands = {
64
+ cspell: 'cspell --no-must-find-files --show-suggestions',
65
+ eslint: 'eslint --fix',
66
+ markuplint: 'markuplint',
67
+ prettier: 'prettier --write',
68
+ puglint: 'pug-lint',
69
+ stylelint: 'stylelint --fix',
70
+ };
71
+
72
+ /**
73
+ * @type {import("./types").CommandMappings}
74
+ */
75
+ export const defaultMapping = {
76
+ md: ['prettier', 'textlint', 'cspell'],
77
+ mdx: ['prettier', 'textlint', 'cspell'],
78
+ json: ['prettier', 'cspell'],
79
+ yaml: ['cspell'],
80
+ yml: ['cspell'],
81
+ js: ['eslint', 'prettier', 'cspell'],
82
+ cjs: ['eslint', 'prettier', 'cspell'],
83
+ mjs: ['eslint', 'prettier', 'cspell'],
84
+ jsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
85
+ ts: ['eslint', 'prettier', 'cspell'],
86
+ cts: ['eslint', 'prettier', 'cspell'],
87
+ mts: ['eslint', 'prettier', 'cspell'],
88
+ tsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
89
+ html: ['markuplint', 'prettier', 'cspell'],
90
+ pug: ['markuplint', 'prettier', 'cspell'],
91
+ css: ['stylelint', 'prettier', 'cspell'],
92
+ scss: ['stylelint', 'prettier', 'cspell'],
93
+ astro: ['eslint', 'markuplint', 'prettier', 'cspell'],
94
+ svelte: ['eslint', 'markuplint', 'prettier', 'cspell'],
95
+ vue: ['eslint', 'markuplint', 'prettier', 'cspell'],
96
+ };
package/index.spec.mjs ADDED
@@ -0,0 +1,19 @@
1
+ import path from 'node:path';
2
+
3
+ import lintStagedConfigGenerator from './index.mjs';
4
+
5
+ describe('lintStagedConfigGenerator', () => {
6
+ const defaultConfig = lintStagedConfigGenerator();
7
+ const cwd = process.cwd().replaceAll(path.sep, '/');
8
+
9
+ test('defaultMapping', () => {
10
+ expect(
11
+ defaultConfig([path.resolve(process.cwd(), 'README.md')]).map((shell) =>
12
+ shell.replaceAll(cwd + '/', './').replaceAll(cwd, '.'),
13
+ ),
14
+ ).toStrictEqual([
15
+ 'prettier --write "./README.md"',
16
+ 'cspell --no-must-find-files --show-suggestions "./README.md"',
17
+ ]);
18
+ });
19
+ });
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@d-zero/lint-staged-config",
3
+ "version": "5.0.0-dev.34+c65aa52",
4
+ "description": "Configurations of lint-staged",
5
+ "repository": "https://github.com/d-zero-dev/frontend-env.git",
6
+ "author": "D-ZERO Co., Ltd.",
7
+ "license": "MIT",
8
+ "private": false,
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "type": "module",
13
+ "exports": {
14
+ "import": "./index.mjs",
15
+ "types": "./types.ts"
16
+ },
17
+ "prettier": "@d-zero/prettier-config",
18
+ "dependencies": {
19
+ "lint-staged": "13.2.1"
20
+ },
21
+ "gitHead": "c65aa521b5014591272ca6c1d659111830e4d8b3"
22
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "@d-zero/tsconfig/tsconfig.json",
3
+ "compilerOptions": {
4
+ "noEmit": true
5
+ }
6
+ }
package/types.ts ADDED
@@ -0,0 +1,38 @@
1
+ export type TargetFileExtension =
2
+ | 'md'
3
+ | 'mdx'
4
+ | 'json'
5
+ | 'yaml'
6
+ | 'yml'
7
+ | 'js'
8
+ | 'cjs'
9
+ | 'mjs'
10
+ | 'jsx'
11
+ | 'ts'
12
+ | 'cts'
13
+ | 'mts'
14
+ | 'tsx'
15
+ | 'html'
16
+ | 'pug'
17
+ | 'css'
18
+ | 'scss'
19
+ | 'astro'
20
+ | 'svelte'
21
+ | 'vue';
22
+
23
+ export type CommandType =
24
+ | 'cspell'
25
+ | 'eslint'
26
+ | 'markuplint'
27
+ | 'prettier'
28
+ | 'stylelint'
29
+ | 'textlint';
30
+
31
+ export type CommandMappings = Readonly<
32
+ Partial<Record<TargetFileExtension, CommandType[]>>
33
+ >;
34
+
35
+ /**
36
+ * @see https://github.com/okonet/lint-staged#example-export-a-function-to-build-your-own-matchers
37
+ */
38
+ export type LintStagedCommandMapper = (allStagedFiles: readonly string[]) => string[];