| 1 |
/** |
| 2 |
* A script to sync the exact linter dependencies |
| 3 |
* from `./package-lock.json` to `../.pre-commit-config.yaml` |
| 4 |
*/ |
| 5 |
|
| 6 |
import { readFile, writeFile } from "node:fs/promises"; |
| 7 |
import { join } from "node:path"; |
| 8 |
|
| 9 |
import { load } from "js-yaml"; |
| 10 |
|
| 11 |
import { packages } from "./package-lock.json"; |
| 12 |
import { array, object, optional, string } from "./src/lib/validation"; |
| 13 |
|
| 14 |
const preCommitConfigPath = join(__dirname, "..", ".pre-commit-config.yaml"); |
| 15 |
|
| 16 |
const confValidator = object({ |
| 17 |
repos: array( |
| 18 |
object({ |
| 19 |
hooks: array( |
| 20 |
object({ |
| 21 |
language: optional(string), |
| 22 |
additional_dependencies: optional(array(string)), |
| 23 |
}), |
| 24 |
), |
| 25 |
}), |
| 26 |
), |
| 27 |
}); |
| 28 |
|
| 29 |
async function main() { |
| 30 |
const rawConfig = await readFile(preCommitConfigPath, "utf-8"); |
| 31 |
let newConfig = rawConfig; |
| 32 |
const conf = confValidator(load(rawConfig)); |
| 33 |
for (const { hooks } of conf.unwrap().repos) { |
| 34 |
for (const { language, additional_dependencies } of hooks) { |
| 35 |
if (language === "node" && additional_dependencies) { |
| 36 |
for (const dep of additional_dependencies) { |
| 37 |
const name = dep.split(/@[^@]*$/)[0] ?? "ERROR"; |
| 38 |
const { version } = |
| 39 |
packages[`node_modules/${name}` as keyof typeof packages]; |
| 40 |
const currentDep = `${name}@${version}`; |
| 41 |
if (dep !== currentDep) { |
| 42 |
console.log( |
| 43 |
`Updating dependency '${name}' from '${dep}' to '${currentDep}'.`, |
| 44 |
); |
| 45 |
newConfig = newConfig.replace(`"${dep}"`, `"${currentDep}"`); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
if (newConfig !== rawConfig) { |
| 52 |
console.log("Writing updated pre-commit config."); |
| 53 |
await writeFile(preCommitConfigPath, newConfig, "utf-8"); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
main().catch((e: unknown) => { |
| 58 |
console.error(e); |
| 59 |
}); |