strapi/lint-staged.config.js
2023-11-17 14:48:05 +00:00

51 lines
1.1 KiB
JavaScript

'use strict';
const path = require('path');
const fs = require('fs');
const findUp = require('find-up');
const includes = ['packages', '.github'];
const root = path.resolve(__dirname);
function extractPackageName(pkgJsonPath) {
return JSON.parse(fs.readFileSync(pkgJsonPath).toString()).name;
}
function getLintCommand(files) {
const affectedFolders = new Set();
for (const file of files) {
const r = findUp.sync('package.json', { cwd: file });
const relPath = path.relative(root, r);
if (includes.some((incl) => relPath.startsWith(incl))) {
affectedFolders.add(r);
}
}
const affectedPackages = [...affectedFolders].map(extractPackageName);
if (affectedPackages.length === 0) {
return null;
}
return `nx run-many -t lint -p ${affectedPackages.join()}`;
}
function getCodeCommands(files) {
const lintCmd = getLintCommand(files);
const prettierCmd = `prettier --write ${files.join(' ')}`;
if (lintCmd) {
return [lintCmd, prettierCmd];
}
return [prettierCmd];
}
module.exports = {
'*.{js,ts,jsx,tsx,json}': getCodeCommands,
'*.{md,mdx,css,scss,yaml,yml}': ['prettier --write'],
};