@d-zero/lint-staged-config 5.0.0-alpha.4 → 5.0.0-alpha.40

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 D-ZERO Co., Ltd.
3
+ Copyright (c) 2024 D-ZERO Co., Ltd.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,4 +1,34 @@
1
1
  # `@d-zero/lint-staged-config`
2
2
 
3
- - 使用: 🆗 使用可
4
- - 解説: 🚧 準備中
3
+ ## 個別インストール
4
+
5
+ ```sh
6
+ yarn add -D @d-zero/lint-staged-config
7
+ ```
8
+
9
+ ## 使い方
10
+
11
+ `lint-staged.config.mjs`を作成し、読み込んだジェネレーター関数で設定を生成しエクスポートします。
12
+
13
+ ```js
14
+ import lintStagedConfigGenerator from '@d-zero/lint-staged-config';
15
+ export default lintStagedConfigGenerator();
16
+ ```
17
+
18
+ ### 拡張
19
+
20
+ プロジェクトに合わせて設定を追加します。
21
+
22
+ ```js
23
+ import lintStagedConfigGenerator, { defaultMapping } from '@d-zero/lint-staged-config';
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
+ );
34
+ ```
@@ -0,0 +1,9 @@
1
+ export const commands = {
2
+ cspell: 'cspell --no-must-find-files --show-suggestions',
3
+ eslint: 'eslint --fix',
4
+ markuplint: 'markuplint',
5
+ prettier: 'prettier --write',
6
+ puglint: 'pug-lint',
7
+ stylelint: 'stylelint --fix',
8
+ textlint: 'textlint',
9
+ };
@@ -0,0 +1,22 @@
1
+ export const defaultMapping = {
2
+ astro: ['eslint', 'markuplint', 'prettier', 'cspell'],
3
+ cjs: ['eslint', 'prettier', 'cspell'],
4
+ css: ['stylelint', 'prettier', 'cspell'],
5
+ cts: ['eslint', 'prettier', 'cspell'],
6
+ html: ['markuplint', 'prettier', 'cspell'],
7
+ js: ['eslint', 'prettier', 'cspell'],
8
+ json: ['prettier', 'cspell'],
9
+ jsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
10
+ md: ['prettier', 'textlint', 'cspell'],
11
+ mdx: ['prettier', 'textlint', 'cspell'],
12
+ mjs: ['eslint', 'prettier', 'cspell'],
13
+ mts: ['eslint', 'prettier', 'cspell'],
14
+ pug: ['markuplint', 'prettier', 'cspell'],
15
+ scss: ['stylelint', 'prettier', 'cspell'],
16
+ svelte: ['eslint', 'markuplint', 'prettier', 'cspell'],
17
+ ts: ['eslint', 'prettier', 'cspell'],
18
+ tsx: ['eslint', 'markuplint', 'prettier', 'cspell'],
19
+ vue: ['eslint', 'markuplint', 'prettier', 'cspell'],
20
+ yaml: ['cspell'],
21
+ yml: ['cspell'],
22
+ };
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ import path from 'node:path';
2
+ import micromatch from 'micromatch';
3
+ import { commands } from './commands.js';
4
+ import { defaultMapping } from './default-mapping.js';
5
+ export default function (dirOptions, mapping) {
6
+ return (allStagedFiles) => {
7
+ const commandList = [];
8
+ const cwd = process.cwd();
9
+ const dir = typeof dirOptions === 'string' ? dirOptions : dirOptions?.dir;
10
+ const ignore = typeof dirOptions === 'string' ? null : dirOptions?.ignore;
11
+ const baseDir = dir
12
+ ? // 絶対パスかどうか
13
+ path.isAbsolute(dir)
14
+ ? // 絶対パスならそのまま
15
+ dir
16
+ : // 相対パスなら絶対パスに変換
17
+ path.resolve(cwd, dir)
18
+ : // 引数がないならカレントディレクトリ
19
+ cwd;
20
+ mapping = mapping ?? defaultMapping;
21
+ for (const [ext, commandTypes] of Object.entries(mapping)) {
22
+ for (const commandType of commandTypes) {
23
+ const shell = commands[commandType];
24
+ if (!shell) {
25
+ continue;
26
+ }
27
+ const pattern = path
28
+ .resolve(baseDir, '**', `{*.${ext},.*.${ext}}`)
29
+ .replaceAll(path.sep, '/');
30
+ const files = allStagedFiles.map((f) => f.replaceAll(path.sep, '/'));
31
+ let targetFiles = micromatch(files, pattern);
32
+ if (ignore) {
33
+ targetFiles = micromatch.not(targetFiles, ignore);
34
+ }
35
+ if (targetFiles.length <= 0) {
36
+ continue;
37
+ }
38
+ commandList.push(shell + ' ' + targetFiles.map((f) => `"${f}"`).join(' '));
39
+ }
40
+ }
41
+ return commandList;
42
+ };
43
+ }
@@ -0,0 +1,35 @@
1
+ import path from 'node:path';
2
+ import { describe, test, expect } from 'vitest';
3
+ import lintStagedConfigGenerator from './index.js';
4
+ function resolve(...paths) {
5
+ return path.resolve(...paths).replaceAll(path.sep, '/');
6
+ }
7
+ function toRelativePath(...paths) {
8
+ const cwd = process.cwd().replaceAll(path.sep, '/');
9
+ return paths.map((p) => p.replaceAll(cwd, '.'));
10
+ }
11
+ describe('lintStagedConfigGenerator', () => {
12
+ test('defaultMapping', () => {
13
+ const config = lintStagedConfigGenerator();
14
+ const commands = toRelativePath(...config([resolve('README.md')]));
15
+ expect(commands).toStrictEqual([
16
+ 'prettier --write "./README.md"',
17
+ 'textlint "./README.md"',
18
+ 'cspell --no-must-find-files --show-suggestions "./README.md"',
19
+ ]);
20
+ });
21
+ test('ignore option', () => {
22
+ const config = lintStagedConfigGenerator({
23
+ ignore: [resolve('packages', '@d-zero', 'eslint-config', '*')],
24
+ });
25
+ const commands = toRelativePath(...config([
26
+ resolve('packages', '@d-zero', 'eslint-config', 'CHANGELOG.md'),
27
+ resolve('packages', '@d-zero', 'lint-staged-config', 'CHANGELOG.md'),
28
+ ]));
29
+ expect(commands).toStrictEqual([
30
+ 'prettier --write "./packages/@d-zero/lint-staged-config/CHANGELOG.md"',
31
+ 'textlint "./packages/@d-zero/lint-staged-config/CHANGELOG.md"',
32
+ 'cspell --no-must-find-files --show-suggestions "./packages/@d-zero/lint-staged-config/CHANGELOG.md"',
33
+ ]);
34
+ });
35
+ });
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/lint-staged-config",
3
- "version": "5.0.0-alpha.4",
3
+ "version": "5.0.0-alpha.40",
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.",
@@ -9,17 +9,25 @@
9
9
  "publishConfig": {
10
10
  "access": "public"
11
11
  },
12
+ "engines": {
13
+ "node": ">=22.0.0"
14
+ },
12
15
  "type": "module",
16
+ "exports": {
17
+ "import": "./dist/index.js"
18
+ },
13
19
  "files": [
14
- "index.mjs",
15
- "types.ts"
20
+ "dist"
16
21
  ],
17
- "exports": {
18
- "import": "./index.mjs",
19
- "types": "./types.ts"
22
+ "scripts": {
23
+ "build": "tsc"
20
24
  },
21
25
  "dependencies": {
22
- "lint-staged": "15.2.0"
26
+ "lint-staged": "15.2.7",
27
+ "micromatch": "4.0.7"
28
+ },
29
+ "devDependencies": {
30
+ "@types/micromatch": "4.0.9"
23
31
  },
24
- "gitHead": "de8f9c035cfb70bbf4cc5b203869dc39427c9d3a"
32
+ "gitHead": "7c0ce7713b288098b230e149502cd1fe910b4468"
25
33
  }
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
- 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/types.ts DELETED
@@ -1,38 +0,0 @@
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[];