Skip to content

Instantly share code, notes, and snippets.

@alex-grover
Last active March 13, 2024 17:27
Show Gist options
  • Save alex-grover/113dfc2969e6a6e25b9bd94b3be29e2a to your computer and use it in GitHub Desktop.
Save alex-grover/113dfc2969e6a6e25b9bd94b3be29e2a to your computer and use it in GitHub Desktop.
Run `next lint` via `lint-staged` in a monorepo
import path from 'path'
import util from 'util'
import child_process from 'child_process'
const exec = util.promisify(child_process.exec)
const projectDirname = 'web' // Update with the path to your Next.js project
const buildEslintCommand = async (filenames) => {
// `next lint` just silently ignores file inputs if the file doesn't exist,
// and the default code snippet in the Next docs doesn't correctly construct
// the file path if you're running `lint-staged` from a directory that's
// outside the project root.
const result = await exec('git rev-parse --show-toplevel')
const gitRoot = result.stdout.trim()
const projectDir = path.join(gitRoot, projectDirname)
return `next lint --fix --file ${filenames
.map((f) => path.relative(projectDir, f))
.join(' --file ')}`
}
export default {
'*.{js,ts,tsx}': [buildEslintCommand, 'prettier --write'],
'*.!(js|ts|tsx)': 'prettier --write --ignore-unknown',
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment