@fisker/lint-staged-config
Version:
fisker's Shareable lint-staged Configuration
161 lines (133 loc) • 3.42 kB
JavaScript
;
const PRETTIER = 'prettier --write';
const ESLINT = 'eslint --fix';
const STYLELINT = 'stylelint --fix --allow-empty-input';
const MARKDOWNLINT = 'markdownlint-cli2 --fix';
const SORT_PACKAGE_JSON = 'sort-package-json';
const config$c = {
commands: [STYLELINT, PRETTIER],
};
const config$b = {
extensions: 'gql,graphql',
commands: [PRETTIER],
};
// TODO: htmlhint
const config$a = {
extensions: 'html,htm',
commands: [STYLELINT, PRETTIER],
};
function toArray$1(array) {
array = Array.isArray(array) ? array : String(array).split(',');
return array.map((item) => item.trim()).filter(Boolean)
}
const languageExtensions = {
graphql: 'gql,graphql',
html: 'html,htm',
javascript: 'js,jsx,mjs,cjs',
// more markdown extensions: mdown, mdwn, mkd, mkdn, mkdown
markdown: 'md,markdown',
typescript: 'ts,tsx,mts,cts',
yaml: 'yaml,yml',
};
function getExtensions(language) {
return toArray$1(languageExtensions[language] || language)
}
const config$9 = {
extensions: getExtensions('javascript'),
commands: [ESLINT, PRETTIER],
};
const config$8 = {
commands: [PRETTIER],
};
const config$7 = {
commands: [PRETTIER],
};
const config$6 = {
commands: [STYLELINT, PRETTIER],
};
// markdown files
// first prettier then lint
const config$5 = {
// more ext: mdown,mdwn,mkd,mkdn,mkdown
extensions: 'md,markdown',
commands: [MARKDOWNLINT, PRETTIER],
};
const config$4 = {};
const config$3 = {
commands: [STYLELINT, PRETTIER],
};
const config$2 = {
extensions: 'ts,tsx',
commands: [PRETTIER],
};
const config$1 = {
commands: [ESLINT, STYLELINT, PRETTIER],
};
const config = {
extensions: 'yaml,yml',
commands: [PRETTIER],
};
var languages = /*#__PURE__*/Object.freeze({
__proto__: null,
css: config$c,
graphql: config$b,
html: config$a,
javascript: config$9,
json: config$8,
json5: config$7,
less: config$6,
markdown: config$5,
mdx: config$4,
scss: config$3,
typescript: config$2,
vue: config$1,
yaml: config
});
function isSameCommands(commands1, commands2) {
return commands1.join(',') === commands2.join(',')
}
function mergeCommands(commands) {
return commands.reduce((allCommands, current) => {
const {extensions, commands: currentCommands} = current;
const result = allCommands.find(({commands}) =>
isSameCommands(commands, currentCommands),
);
if (result) {
result.extensions = [...result.extensions, ...extensions];
return allCommands
}
return [...allCommands, current]
}, [])
}
function toArray(array) {
array = Array.isArray(array) ? array : array.split(',');
return array.map((item) => item.trim()).filter(Boolean)
}
function parseCommands(languages) {
return Object.keys(languages)
.map((language) => {
const {extensions = language, commands = []} = languages[language];
return {
extensions: toArray(extensions),
commands: toArray(commands),
}
})
.filter(
({commands, extensions}) =>
commands.length !== 0 && extensions.length !== 0,
)
}
function toGlob(extensions) {
return extensions.length > 1
? `*.{${extensions.sort().join(',')}}`
: `*.${extensions}`
}
module.exports = mergeCommands(parseCommands(languages)).reduce(
(config, {extensions, commands}) => ({
...config,
[toGlob(extensions)]: commands,
}),
{
'package.json': [SORT_PACKAGE_JSON],
},
);