@d-zero/lint-staged-config 5.0.0-alpha.37 → 5.0.0-alpha.38

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 CHANGED
@@ -21,9 +21,14 @@ export default lintStagedConfigGenerator();
21
21
 
22
22
  ```js
23
23
  import lintStagedConfigGenerator, { defaultMapping } from '@d-zero/lint-staged-config';
24
- export default lintStagedConfigGenerator(process.cwd(), {
25
- ...defaultMapping,
26
- // 例: PHPファイルに対して`markuplint`、`prettier`、`cspell`を実行する
27
- php: ['markuplint', 'prettier', 'cspell'],
28
- });
24
+ export default lintStagedConfigGenerator(
25
+ {
26
+ ignore: [path.resolve(process.cwd(), 'dist', '**', '*')],
27
+ },
28
+ {
29
+ ...defaultMapping,
30
+ // 例: PHPファイルに対して`markuplint`、`prettier`、`cspell`を実行する
31
+ php: ['markuplint', 'prettier', 'cspell'],
32
+ },
33
+ );
29
34
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/lint-staged-config",
3
- "version": "5.0.0-alpha.37",
3
+ "version": "5.0.0-alpha.38",
4
4
  "description": "Configurations of lint-staged",
5
5
  "repository": "https://github.com/d-zero-dev/linters.git",
6
6
  "author": "D-ZERO Co., Ltd.",
@@ -13,16 +13,21 @@
13
13
  "node": ">=22.0.0"
14
14
  },
15
15
  "type": "module",
16
+ "exports": {
17
+ "import": "./dist/index.js"
18
+ },
16
19
  "files": [
17
- "index.mjs",
18
- "types.ts"
20
+ "src"
19
21
  ],
20
- "exports": {
21
- "import": "./index.mjs",
22
- "types": "./types.ts"
22
+ "scripts": {
23
+ "build": "tsc"
23
24
  },
24
25
  "dependencies": {
25
- "lint-staged": "15.2.7"
26
+ "lint-staged": "15.2.7",
27
+ "micromatch": "4.0.7"
28
+ },
29
+ "devDependencies": {
30
+ "@types/micromatch": "4.0.9"
26
31
  },
27
- "gitHead": "9f7e27baf2fce73a12662af92396e4d01cf821f3"
32
+ "gitHead": "ffb91050c95bb824e92b618ee6d40d9bed708574"
28
33
  }
@@ -0,0 +1,11 @@
1
+ import type { CommandType } from './types.js';
2
+
3
+ export const commands: Record<CommandType, string> = {
4
+ cspell: 'cspell --no-must-find-files --show-suggestions',
5
+ eslint: 'eslint --fix',
6
+ markuplint: 'markuplint',
7
+ prettier: 'prettier --write',
8
+ puglint: 'pug-lint',
9
+ stylelint: 'stylelint --fix',
10
+ textlint: 'textlint',
11
+ };
@@ -0,0 +1,24 @@
1
+ import type { CommandMappings } from './types.js';
2
+
3
+ export const defaultMapping: CommandMappings = {
4
+ astro: ['eslint', 'markuplint', 'prettier', 'cspell'],
5
+ cjs: ['eslint', 'prettier', 'cspell'],
6
+ css: ['stylelint', 'prettier', 'cspell'],
7
+ cts: ['eslint', 'prettier', 'cspell'],
8
+ html: ['markuplint', 'prettier', 'cspell'],
9
+ js: ['eslint', 'prettier', 'cspell'],
10
+ json: ['prettier', 'cspell'],
11
+ jsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
12
+ md: ['prettier', 'textlint', 'cspell'],
13
+ mdx: ['prettier', 'textlint', 'cspell'],
14
+ mjs: ['eslint', 'prettier', 'cspell'],
15
+ mts: ['eslint', 'prettier', 'cspell'],
16
+ pug: ['markuplint', 'prettier', 'cspell'],
17
+ scss: ['stylelint', 'prettier', 'cspell'],
18
+ svelte: ['eslint', 'markuplint', 'prettier', 'cspell'],
19
+ ts: ['eslint', 'prettier', 'cspell'],
20
+ tsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
21
+ vue: ['eslint', 'markuplint', 'prettier', 'cspell'],
22
+ yaml: ['cspell'],
23
+ yml: ['cspell'],
24
+ };
@@ -0,0 +1,44 @@
1
+ import path from 'node:path';
2
+
3
+ import { describe, test, expect } from 'vitest';
4
+
5
+ import lintStagedConfigGenerator from './index.js';
6
+
7
+ function resolve(...paths: string[]): string {
8
+ return path.resolve(...paths).replaceAll(path.sep, '/');
9
+ }
10
+
11
+ function toRelativePath(...paths: string[]): string[] {
12
+ const cwd = process.cwd().replaceAll(path.sep, '/');
13
+ return paths.map((p) => p.replaceAll(cwd, '.'));
14
+ }
15
+
16
+ describe('lintStagedConfigGenerator', () => {
17
+ test('defaultMapping', () => {
18
+ const config = lintStagedConfigGenerator();
19
+ const commands = toRelativePath(...config([resolve('README.md')]));
20
+ expect(commands).toStrictEqual([
21
+ 'prettier --write "./README.md"',
22
+ 'textlint "./README.md"',
23
+ 'cspell --no-must-find-files --show-suggestions "./README.md"',
24
+ ]);
25
+ });
26
+
27
+ test('ignore option', () => {
28
+ const config = lintStagedConfigGenerator({
29
+ ignore: [resolve('packages', '@d-zero', 'eslint-config', '*')],
30
+ });
31
+ const commands = toRelativePath(
32
+ ...config([
33
+ resolve('packages', '@d-zero', 'eslint-config', 'CHANGELOG.md'),
34
+ resolve('packages', '@d-zero', 'lint-staged-config', 'CHANGELOG.md'),
35
+ ]),
36
+ );
37
+
38
+ expect(commands).toStrictEqual([
39
+ 'prettier --write "./packages/@d-zero/lint-staged-config/CHANGELOG.md"',
40
+ 'textlint "./packages/@d-zero/lint-staged-config/CHANGELOG.md"',
41
+ 'cspell --no-must-find-files --show-suggestions "./packages/@d-zero/lint-staged-config/CHANGELOG.md"',
42
+ ]);
43
+ });
44
+ });
package/src/index.ts ADDED
@@ -0,0 +1,75 @@
1
+ import type { CommandMappings, LintStagedCommandMapper } from './types.js';
2
+
3
+ import path from 'node:path';
4
+
5
+ import micromatch from 'micromatch';
6
+
7
+ import { commands } from './commands.js';
8
+ import { defaultMapping } from './default-mapping.js';
9
+
10
+ export interface DirectoryOptions {
11
+ /**
12
+ * ファイルを検索するディレクトリ
13
+ */
14
+ dir?: string;
15
+
16
+ /**
17
+ * 除外するファイルのパターン
18
+ */
19
+ ignore?: string[];
20
+ }
21
+
22
+ export default function (
23
+ dirOptions?: string | DirectoryOptions,
24
+ mapping?: CommandMappings,
25
+ ): LintStagedCommandMapper {
26
+ return (allStagedFiles) => {
27
+ const commandList: string[] = [];
28
+ const cwd = process.cwd();
29
+
30
+ const dir = typeof dirOptions === 'string' ? dirOptions : dirOptions?.dir;
31
+ const ignore = typeof dirOptions === 'string' ? null : dirOptions?.ignore;
32
+
33
+ const baseDir = dir
34
+ ? // 絶対パスかどうか
35
+ path.isAbsolute(dir)
36
+ ? // 絶対パスならそのまま
37
+ dir
38
+ : // 相対パスなら絶対パスに変換
39
+ path.resolve(cwd, dir)
40
+ : // 引数がないならカレントディレクトリ
41
+ cwd;
42
+
43
+ mapping = mapping ?? defaultMapping;
44
+
45
+ for (const [ext, commandTypes] of Object.entries(mapping)) {
46
+ for (const commandType of commandTypes) {
47
+ const shell = commands[commandType];
48
+
49
+ if (!shell) {
50
+ continue;
51
+ }
52
+
53
+ const pattern = path
54
+ .resolve(baseDir, '**', `{*.${ext},.*.${ext}}`)
55
+ .replaceAll(path.sep, '/');
56
+
57
+ const files = allStagedFiles.map((f) => f.replaceAll(path.sep, '/'));
58
+
59
+ let targetFiles = micromatch(files, pattern);
60
+
61
+ if (ignore) {
62
+ targetFiles = micromatch.not(targetFiles, ignore);
63
+ }
64
+
65
+ if (targetFiles.length <= 0) {
66
+ continue;
67
+ }
68
+
69
+ commandList.push(shell + ' ' + targetFiles.map((f) => `"${f}"`).join(' '));
70
+ }
71
+ }
72
+
73
+ return commandList;
74
+ };
75
+ }
@@ -1,30 +1,31 @@
1
1
  export type TargetFileExtension =
2
+ | 'astro'
3
+ | 'cjs'
4
+ | 'css'
5
+ | 'cts'
6
+ | 'html'
7
+ | 'js'
8
+ | 'json'
9
+ | 'jsx'
2
10
  | 'md'
3
11
  | 'mdx'
4
- | 'json'
5
- | 'yaml'
6
- | 'yml'
7
- | 'js'
8
- | 'cjs'
9
12
  | 'mjs'
10
- | 'jsx'
11
- | 'ts'
12
- | 'cts'
13
13
  | 'mts'
14
- | 'tsx'
15
- | 'html'
16
14
  | 'pug'
17
- | 'css'
18
15
  | 'scss'
19
- | 'astro'
20
16
  | 'svelte'
21
- | 'vue';
17
+ | 'ts'
18
+ | 'tsx'
19
+ | 'vue'
20
+ | 'yaml'
21
+ | 'yml';
22
22
 
23
23
  export type CommandType =
24
24
  | 'cspell'
25
25
  | 'eslint'
26
26
  | 'markuplint'
27
27
  | 'prettier'
28
+ | 'puglint'
28
29
  | 'stylelint'
29
30
  | 'textlint';
30
31
 
package/index.mjs DELETED
@@ -1,96 +0,0 @@
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
- path.isAbsolute(dir)
23
- ? // 絶対パスならそのまま
24
- dir
25
- : // 相対パスなら絶対パスに変換
26
- path.resolve(cwd, dir)
27
- : // 引数がないならカレントディレクトリ
28
- cwd;
29
-
30
- mapping = mapping ?? defaultMapping;
31
-
32
- for (const [ext, commandTypes] of Object.entries(mapping)) {
33
- for (const commandType of commandTypes) {
34
- const shell = commands[commandType];
35
-
36
- if (!shell) {
37
- continue;
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
- continue;
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
- astro: ['eslint', 'markuplint', 'prettier', 'cspell'],
77
- cjs: ['eslint', 'prettier', 'cspell'],
78
- css: ['stylelint', 'prettier', 'cspell'],
79
- cts: ['eslint', 'prettier', 'cspell'],
80
- html: ['markuplint', 'prettier', 'cspell'],
81
- js: ['eslint', 'prettier', 'cspell'],
82
- json: ['prettier', 'cspell'],
83
- jsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
84
- md: ['prettier', 'textlint', 'cspell'],
85
- mdx: ['prettier', 'textlint', 'cspell'],
86
- mjs: ['eslint', 'prettier', 'cspell'],
87
- mts: ['eslint', 'prettier', 'cspell'],
88
- pug: ['markuplint', 'prettier', 'cspell'],
89
- scss: ['stylelint', 'prettier', 'cspell'],
90
- svelte: ['eslint', 'markuplint', 'prettier', 'cspell'],
91
- ts: ['eslint', 'prettier', 'cspell'],
92
- tsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
93
- vue: ['eslint', 'markuplint', 'prettier', 'cspell'],
94
- yaml: ['cspell'],
95
- yml: ['cspell'],
96
- };