Skip to content

Commit df80edc

Browse files
committed
feat: add stylelint lint-staged integration
1 parent 405727a commit df80edc

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { eslintMutation, jestMutation, prettierMutation } from '../lint-staged.utils';
1+
import { eslintMutation, jestMutation, prettierMutation, stylelintMutation } from '../lint-staged.utils';
22
import { Config } from './config.interface';
33

44
export const defaultConfig: Config = {
55
config: {},
6-
mutations: [prettierMutation, eslintMutation('*.js'), jestMutation('*.js')],
6+
mutations: [prettierMutation, stylelintMutation, eslintMutation('*.js'), jestMutation('*.js')],
77
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { eslintMutation, jestMutation, prettierMutation } from '../lint-staged.utils';
1+
import { eslintMutation, jestMutation, prettierMutation, stylelintMutation } from '../lint-staged.utils';
22
import { Config } from './config.interface';
33

44
export const nodeTsConfig: Config = {
55
config: {},
6-
mutations: [prettierMutation, eslintMutation('*.ts'), jestMutation('*.ts')],
6+
mutations: [prettierMutation, stylelintMutation, eslintMutation('*.ts'), jestMutation('*.ts')],
77
};

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

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { CLI_NAME as JEST_CLI_NAME } from '../jest/jest.const';
77
import { CLI_NAME as ESLINT_CLI_NAME } from '../eslint/eslint.const';
88
import { isJestInstalled } from '../jest/jest.utils';
99
import { isEslintInstalled } from '../eslint/eslint.utils';
10+
import { isStylelintInstalled } from '../stylelint/stylelint.utils';
11+
import { getPackageJson } from 'src/utils/npm';
12+
import { STYLELINT_CLI_NAME } from '../stylelint/stylelint.const';
1013

1114
type ScriptFileExtension = '*.js' | '*.ts';
1215

@@ -72,3 +75,13 @@ export const prettierMutation = async (config: LintStagedConfig) => {
7275
addOptionToLintStagedConfig(config, '*.{js,json,yml,md}', `${PRETTIER_CLI_NAME} --write`);
7376
}
7477
};
78+
79+
export const stylelintMutation = async (config: LintStagedConfig) => {
80+
if (await isStylelintInstalled()) {
81+
const packageJson = await getPackageJson();
82+
const react = packageJson?.dependencies?.['react'];
83+
const extensions = react ? '*.{css,js,jsx,ts,tsx}' : '*.css';
84+
85+
addOptionToLintStagedConfig(config, extensions, `${STYLELINT_CLI_NAME} --fix`);
86+
}
87+
};
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export const STYLELINT_CONFIG_NAME = '.stylelintrc';
22
export const STYLELINT_IGNORE_NAME = '.stylelintignore';
3+
export const PACKAGE_NAME = 'stylelint';
4+
export const STYLELINT_CLI_NAME = 'stylelint';

src/categories/js/stylelint/stylelint.utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { isInstalledAndInRootCheck } from 'src/utils/installed';
12
import { isPrettierInstalled } from '../prettier/prettier.utils';
23
import { Config } from './config/config.interface';
4+
import { PACKAGE_NAME, STYLELINT_CONFIG_NAME } from './stylelint.const';
5+
6+
export const isStylelintInstalled = isInstalledAndInRootCheck(PACKAGE_NAME, STYLELINT_CONFIG_NAME);
37

48
export const prettierMutation = async (config: Config) => {
59
if (await isPrettierInstalled()) {

0 commit comments

Comments
 (0)