Skip to content

Commit eec68ac

Browse files
committed
feat: add jest mutator to lint-staged and refactor lint-staged mutators
1 parent 4feaaa0 commit eec68ac

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

src/categories/js/jest/jest.utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { isInstalledAndInRootCheck } from 'src/utils/installed';
2+
import { SCRIPT_NAME } from '../commitlint/commitlint.const';
3+
import { CONFIG_FILE_NAME } from './jest.const';
4+
5+
export const isJestInstalled = isInstalledAndInRootCheck(SCRIPT_NAME, CONFIG_FILE_NAME);
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { prettierMutator } from '../lint-staged.utils';
1+
import { eslintMutator, jestMutator, prettierMutator } from '../lint-staged.utils';
22
import { Config } from './config.interface';
33

44
export const defaultConfig: Config = {
55
config: {},
6-
mutators: [prettierMutator],
6+
mutators: [prettierMutator, eslintMutator('*.js'), jestMutator('*.js')],
77
};
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
import { isEslintInstalled } from '../../eslint/eslint.utils';
2-
import { prettierMutator } from '../lint-staged.utils';
3-
import { Config, LintStagedConfig } from './config.interface';
4-
5-
const eslintNodeTsConfigMutator = async (config: LintStagedConfig) => {
6-
if (await isEslintInstalled()) {
7-
config['*.ts'] = 'eslint --fix';
8-
}
9-
};
1+
import { eslintMutator, jestMutator, prettierMutator } from '../lint-staged.utils';
2+
import { Config } from './config.interface';
103

114
export const nodeTsConfig: Config = {
125
config: {},
13-
mutators: [prettierMutator, eslintNodeTsConfigMutator],
6+
mutators: [prettierMutator, eslintMutator('*.ts'), jestMutator('*.ts')],
147
};

src/categories/js/lint-staged/lint-staged.utils.ts

+56-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,70 @@ import { isPrettierInstalled } from '../prettier/prettier.utils';
33
import { addHook, isHuskyInstalled } from '../husky/husky.utils';
44
import { CLI_NAME } from './lint-staged.const';
55
import { CLI_NAME as PRETTIER_CLI_NAME } from '../prettier/prettier.const';
6+
import { isJestInstalled } from '../jest/jest.utils';
7+
import { isEslintInstalled } from '../eslint/eslint.utils';
8+
9+
type ScriptFileExtension = '*.js' | '*.ts';
10+
11+
interface OptionMutator {
12+
check: (config: LintStagedConfig, key: string, value: string) => boolean;
13+
mutate: (config: LintStagedConfig, key: string, value: string) => void;
14+
}
15+
16+
const arrayOptionMutator: OptionMutator = {
17+
check: (config, key) => Array.isArray(config[key]),
18+
mutate: (config, key, value) => {
19+
config[key] = Array.from(new Set([...(config[key] as unknown[]), value]));
20+
},
21+
};
22+
23+
const stringOptionMutator: OptionMutator = {
24+
check: (config, key) => typeof config[key] === 'string',
25+
mutate: (config, key, value) => {
26+
const array = Array.from(new Set([config[key], value]));
27+
config[key] = array.length === 0 ? array[0] : array;
28+
},
29+
};
30+
31+
const undefinedOptionMutator: OptionMutator = {
32+
check: (config, key) => typeof config[key] === 'undefined',
33+
mutate: (config, key, value) => {
34+
config[key] = value;
35+
},
36+
};
37+
38+
const optionMutators: OptionMutator[] = [arrayOptionMutator, stringOptionMutator, undefinedOptionMutator];
39+
40+
export const addOptionToLintStagedConfig = (config: LintStagedConfig, key: string, value: string) => {
41+
const finded = optionMutators.find(({ check }) => check(config, key, value));
42+
43+
if (!finded) {
44+
throw new Error('option mutator is not found!');
45+
}
46+
47+
finded.mutate(config, key, value);
48+
};
649

750
export const huskyIntegration = async () => {
851
if (await isHuskyInstalled()) {
952
await addHook('pre-commit', `npx --no-install ${CLI_NAME}`);
1053
}
1154
};
1255

56+
export const jestMutator = (fileExtension: ScriptFileExtension) => async (config: LintStagedConfig) => {
57+
if (await isJestInstalled()) {
58+
addOptionToLintStagedConfig(config, fileExtension, 'jest --findRelatedTests');
59+
}
60+
};
61+
62+
export const eslintMutator = (fileExtension: ScriptFileExtension) => async (config: LintStagedConfig) => {
63+
if (await isEslintInstalled()) {
64+
addOptionToLintStagedConfig(config, fileExtension, 'eslint --fix');
65+
}
66+
};
67+
1368
export const prettierMutator = async (config: LintStagedConfig) => {
1469
if (await isPrettierInstalled()) {
15-
config['*.{js,json,yml,md}'] = `${PRETTIER_CLI_NAME} --write`;
70+
addOptionToLintStagedConfig(config, '*.{js,json,yml,md}', `${PRETTIER_CLI_NAME} --write`);
1671
}
1772
};

0 commit comments

Comments
 (0)