Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions .lintstagedrc.mjs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import micromatch from 'micromatch';
/** Staged paths use forward slashes; lint-staged may pass absolute or relative paths. */

/**
* Returns whether any staged path is a file named `name` at the root of the path list
* or nested under a directory (e.g. `package.json` or `packages/foo/package.json`).
*
* @param {string[]} changes - an array of strings with file paths that have changed
* @param {string} name - Basename to match (e.g. `package.json`).
* @returns {boolean}
*/
const hasFilename = (changes, name) =>
changes.some((file) => file === name || file.endsWith(`/${name}`));

export default {
// Use custom function to avoid overlaps that could cause race conditions
[`*`]: (allChanges) => {
const commands = [];

if (micromatch.some(allChanges, '**/package.json')) {
if (hasFilename(allChanges, 'package.json')) {
commands.push(`yarn syncpack format`);
}

if (micromatch.some(allChanges, 'yarn.lock')) {
if (hasFilename(allChanges, 'yarn.lock')) {
commands.push(`yarn dedupe`);
}

const eslintExtensions = `{mdx,ts,tsx,js,jsx,json}`;
const eslintFiles = micromatch(
allChanges,
[`**/*.${eslintExtensions}`, `?(.)**.${eslintExtensions}`],
{ dot: true }
);
const ESLINT_EXT = /\.(mdx|tsx?|jsx?|json)$/;
const eslintFiles = allChanges.filter((file) => ESLINT_EXT.test(file));

if (eslintFiles.length) {
commands.push(
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
"jest-environment-jsdom-global": "4.0.0",
"jest-junit": "^16.0.0",
"lint-staged": "^16.4.0",
"micromatch": "^4.0.5",
"mutationobserver-shim": "^0.3.3",
"nx": "22.6.0",
"nx-cloud": "19.1.0",
Expand Down
7 changes: 3 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13399,7 +13399,6 @@ __metadata:
jest-junit: "npm:^16.0.0"
lint-staged: "npm:^16.4.0"
lodash: "npm:^4.17.23"
micromatch: "npm:^4.0.5"
mutationobserver-shim: "npm:^0.3.3"
nx: "npm:22.6.0"
nx-cloud: "npm:19.1.0"
Expand Down Expand Up @@ -18412,9 +18411,9 @@ __metadata:
linkType: hard

"picomatch@npm:^2.0.4, picomatch@npm:^2.2.1, picomatch@npm:^2.2.2, picomatch@npm:^2.2.3, picomatch@npm:^2.3.1":
version: 2.3.1
resolution: "picomatch@npm:2.3.1"
checksum: 10c0/26c02b8d06f03206fc2ab8d16f19960f2ff9e81a658f831ecb656d8f17d9edc799e8364b1f4a7873e89d9702dff96204be0fa26fe4181f6843f040f819dac4be
version: 2.3.2
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was really what we were after

resolution: "picomatch@npm:2.3.2"
checksum: 10c0/a554d1709e59be97d1acb9eaedbbc700a5c03dbd4579807baed95100b00420bc729335440ef15004ae2378984e2487a7c1cebd743cfdb72b6fa9ab69223c0d61
languageName: node
linkType: hard

Expand Down
Loading