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

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.
@@ -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.38",
3
+ "version": "5.0.0-alpha.39",
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.",
@@ -17,7 +17,7 @@
17
17
  "import": "./dist/index.js"
18
18
  },
19
19
  "files": [
20
- "src"
20
+ "dist"
21
21
  ],
22
22
  "scripts": {
23
23
  "build": "tsc"
@@ -29,5 +29,5 @@
29
29
  "devDependencies": {
30
30
  "@types/micromatch": "4.0.9"
31
31
  },
32
- "gitHead": "ffb91050c95bb824e92b618ee6d40d9bed708574"
32
+ "gitHead": "6c28cd0388f8a2b5f1ff68cfa4698473a8ef05a7"
33
33
  }
package/src/commands.ts DELETED
@@ -1,11 +0,0 @@
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
- };
@@ -1,24 +0,0 @@
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
- };
package/src/index.spec.ts DELETED
@@ -1,44 +0,0 @@
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 DELETED
@@ -1,75 +0,0 @@
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
- }
package/src/types.ts DELETED
@@ -1,39 +0,0 @@
1
- export type TargetFileExtension =
2
- | 'astro'
3
- | 'cjs'
4
- | 'css'
5
- | 'cts'
6
- | 'html'
7
- | 'js'
8
- | 'json'
9
- | 'jsx'
10
- | 'md'
11
- | 'mdx'
12
- | 'mjs'
13
- | 'mts'
14
- | 'pug'
15
- | 'scss'
16
- | 'svelte'
17
- | 'ts'
18
- | 'tsx'
19
- | 'vue'
20
- | 'yaml'
21
- | 'yml';
22
-
23
- export type CommandType =
24
- | 'cspell'
25
- | 'eslint'
26
- | 'markuplint'
27
- | 'prettier'
28
- | 'puglint'
29
- | 'stylelint'
30
- | 'textlint';
31
-
32
- export type CommandMappings = Readonly<
33
- Partial<Record<TargetFileExtension, CommandType[]>>
34
- >;
35
-
36
- /**
37
- * @see https://github.com/okonet/lint-staged#example-export-a-function-to-build-your-own-matchers
38
- */
39
- export type LintStagedCommandMapper = (allStagedFiles: readonly string[]) => string[];