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

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
@@ -24,6 +24,10 @@ import lintStagedConfigGenerator, { defaultMapping } from '@d-zero/lint-staged-c
24
24
  export default lintStagedConfigGenerator(
25
25
  {
26
26
  ignore: [path.resolve(process.cwd(), 'dist', '**', '*')],
27
+ {
28
+ // 例: `CHANGELOG.md`に対してのみ`textlint`を除外する
29
+ textlint: "CHANGELOG.md",
30
+ }
27
31
  },
28
32
  {
29
33
  ...defaultMapping,
@@ -0,0 +1,2 @@
1
+ import type { CommandType } from './types.js';
2
+ export declare const commands: Record<CommandType, string>;
@@ -0,0 +1,2 @@
1
+ import type { CommandMappings } from './types.js';
2
+ export declare const defaultMapping: CommandMappings;
@@ -0,0 +1,13 @@
1
+ import type { CommandMappings, CommandType, LintStagedCommandMapper } from './types.js';
2
+ export interface DirectoryOptions {
3
+ /**
4
+ * ファイルを検索するディレクトリ
5
+ */
6
+ dir?: string;
7
+ /**
8
+ * 除外するファイルのパターン
9
+ */
10
+ ignore?: (string | IgnoreMap)[];
11
+ }
12
+ export type IgnoreMap = Partial<Record<CommandType, string | string[]>>;
13
+ export default function (dirOptions?: string | DirectoryOptions, mapping?: CommandMappings): LintStagedCommandMapper;
package/dist/index.js CHANGED
@@ -30,7 +30,22 @@ export default function (dirOptions, mapping) {
30
30
  const files = allStagedFiles.map((f) => f.replaceAll(path.sep, '/'));
31
31
  let targetFiles = micromatch(files, pattern);
32
32
  if (ignore) {
33
- targetFiles = micromatch.not(targetFiles, ignore);
33
+ for (const ignoreMap of ignore) {
34
+ const ignorePattern = typeof ignoreMap === 'string' ? ignoreMap : ignoreMap[commandType];
35
+ if (!ignorePattern) {
36
+ continue;
37
+ }
38
+ const ignorePatterns = Array.isArray(ignorePattern)
39
+ ? ignorePattern
40
+ : [ignorePattern];
41
+ const absIgnorePatterns = ignorePatterns.map((p) => {
42
+ if (p === path.basename(p)) {
43
+ return path.resolve('**', p);
44
+ }
45
+ return path.isAbsolute(p) ? p : path.resolve(baseDir, p);
46
+ });
47
+ targetFiles = micromatch.not(targetFiles, absIgnorePatterns);
48
+ }
34
49
  }
35
50
  if (targetFiles.length <= 0) {
36
51
  continue;
@@ -0,0 +1,7 @@
1
+ export type TargetFileExtension = 'astro' | 'cjs' | 'css' | 'cts' | 'html' | 'js' | 'json' | 'jsx' | 'md' | 'mdx' | 'mjs' | 'mts' | 'pug' | 'scss' | 'svelte' | 'ts' | 'tsx' | 'vue' | 'yaml' | 'yml';
2
+ export type CommandType = 'cspell' | 'eslint' | 'markuplint' | 'prettier' | 'puglint' | 'stylelint' | 'textlint';
3
+ export type CommandMappings = Readonly<Partial<Record<TargetFileExtension, CommandType[]>>>;
4
+ /**
5
+ * @see https://github.com/okonet/lint-staged#example-export-a-function-to-build-your-own-matchers
6
+ */
7
+ export type LintStagedCommandMapper = (allStagedFiles: readonly string[]) => string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/lint-staged-config",
3
- "version": "5.0.0-alpha.40",
3
+ "version": "5.0.0-alpha.41",
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.",
@@ -14,7 +14,8 @@
14
14
  },
15
15
  "type": "module",
16
16
  "exports": {
17
- "import": "./dist/index.js"
17
+ "import": "./dist/index.js",
18
+ "types": "./dist/index.d.ts"
18
19
  },
19
20
  "files": [
20
21
  "dist"
@@ -29,5 +30,5 @@
29
30
  "devDependencies": {
30
31
  "@types/micromatch": "4.0.9"
31
32
  },
32
- "gitHead": "7c0ce7713b288098b230e149502cd1fe910b4468"
33
+ "gitHead": "25f71f1dfef6d627b908f832124d4e4a0b1572bb"
33
34
  }
@@ -1,35 +0,0 @@
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
- });