Skip to content

Commit 178d7a8

Browse files
committed
Clean up in prep for usint eslint fixer
1 parent 5fe340f commit 178d7a8

File tree

3 files changed

+75
-18
lines changed

3 files changed

+75
-18
lines changed

Diff for: packages/meta-updater/src/formats.ts

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { equals } from "ramda";
44
import yaml from "yaml";
55

66
export const formats = {
7+
/**
8+
* A format that today we just use for .pre-commit-config.yaml files. This is a yaml file, but we
9+
* need to preserve comments, so we use the `yaml` library's document representation instead of
10+
* parsing it into a plain js object.
11+
*/
712
[".yaml"]: createFormat({
813
async read({ resolvedPath }) {
914
return yaml.parseDocument(await readFile(resolvedPath, "utf-8")).clone();

Diff for: packages/meta-updater/src/getPackageDeps.ts

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import normalizePath from "normalize-path";
22
import path from "path";
33
import { Lockfile } from "@pnpm/lockfile-file";
44

5+
/**
6+
* Get the dependencies of the given package from the pnpm lockfile.
7+
* @param workspaceDir The root of the workspace
8+
* @param packageDir The directory of the package whose dependencies we are
9+
* retrieving
10+
* @param pnpmLockfile The parsed pnpm lockfile
11+
* @returns A map of package names to package specs for the dependencies of the
12+
* given package
13+
*/
514
export function getPackageDeps(
615
workspaceDir: string,
716
packageDir: string,

Diff for: packages/meta-updater/src/updatePreCommit.ts

+61-18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { Document, ParsedNode } from "yaml";
33
import { Context } from "./Context";
44
import { getPackageDeps } from "./getPackageDeps";
55

6+
/**
7+
* Subset of the .pre-commit-config.yaml schema that we care about.
8+
*/
69
interface PreCommitConfig {
710
repos: {
811
hooks: {
@@ -14,15 +17,18 @@ interface PreCommitConfig {
1417
}
1518

1619
/**
17-
* Given a tsconfig.json, update it to match our conventions. This function is
18-
* called by the pnpm `meta-updater` plugin either to check if the tsconfig.json
19-
* is up to date or to update it, depending on flags.
20+
* Given a .pre-commit-config.yaml, update it to match our conventions. This
21+
* function is called by the pnpm `meta-updater` plugin either to check if the
22+
* .pre-commit-config.yaml is up to date or to update it, depending on flags.
2023
* @param context Contains context such as workspace dir and parsed pnpm
2124
* lockfile
22-
* @param rawInput The input tsconfig.json that should be checked / updated
25+
* @param rawInput The input .pre-commit-config.yaml that should be checked /
26+
* updated. This is a parsed yaml document in the `yaml` library's document
27+
* representation; not a plain js object like you'd get from a json parser. We
28+
* need it like this so that we can preserve comments.
2329
* @param options Extra information provided by pnpm; mostly just the directory
24-
* of the package whose tsconfig.json we are updating
25-
* @returns The updated tsconfig.json
30+
* of the package whose .pre-commit-config.yaml we are updating
31+
* @returns The updated .pre-commit-config.yaml
2632
*/
2733
export async function updatePreCommit(
2834
{ workspaceDir, pnpmLockfile }: Context,
@@ -32,36 +38,73 @@ export async function updatePreCommit(
3238
if (rawInput == null) {
3339
return null;
3440
}
35-
/** Directory of the package whose tsconfig.json we are updating */
41+
/** Directory of the package whose .pre-commit-config.yaml we are updating */
3642
const packageDir = options.dir;
3743

3844
if (packageDir !== workspaceDir) {
3945
throw new Error("updatePreCommit should only be called on root");
4046
}
4147

4248
const deps = getPackageDeps(workspaceDir, packageDir, pnpmLockfile);
43-
const prettierVersion = deps["prettier"];
4449

45-
const prettierHookIndex = (rawInput.toJS() as PreCommitConfig).repos
50+
updateHook(deps, rawInput, "prettier", (name) => name === "prettier");
51+
52+
return rawInput;
53+
}
54+
55+
/**
56+
* Updates the additional_dependencies of a hook in a .pre-commit-config.yaml to
57+
* match the versions from the lockfile.
58+
* @param deps Dependencies of the package whose .pre-commit-config.yaml we are
59+
* updating
60+
* @param rawInput The input .pre-commit-config.yaml that should be checked /
61+
* updated
62+
* @param hookId The id of the hook to update
63+
* @param packageMatcher A function that returns true if the given package name
64+
* should be added to the hook's additional_dependencies
65+
*/
66+
function updateHook(
67+
deps: { [x: string]: string },
68+
rawInput: Document<ParsedNode>,
69+
hookId: string,
70+
packageMatcher: (name: string) => boolean,
71+
) {
72+
const packages = Object.entries(deps).filter(([name]) =>
73+
packageMatcher(name),
74+
);
75+
76+
// Find the hook in the .pre-commit-config.yaml. Easier to grab the indices
77+
// form the raw js representation so that we can just use `setIn` to update
78+
// the hook
79+
const desiredHook = (rawInput.toJS() as PreCommitConfig).repos
4680
.flatMap(({ hooks }, repoIndex) =>
4781
hooks.map((hook, hookIndex) => ({ hook, repoIndex, hookIndex })),
4882
)
49-
.filter(({ hook }) => hook.id === "prettier");
83+
.filter(({ hook }) => hook.id === hookId);
5084

51-
if (prettierHookIndex.length === 0) {
52-
throw new Error("No prettier hook found");
85+
if (desiredHook.length === 0) {
86+
throw new Error(`No ${hookId} hook found`);
5387
}
5488

55-
if (prettierHookIndex.length > 1) {
56-
throw new Error("Multiple prettier hooks found");
89+
if (desiredHook.length > 1) {
90+
throw new Error(`Multiple ${hookId} hooks found`);
5791
}
5892

59-
const { repoIndex, hookIndex } = prettierHookIndex[0];
93+
const { repoIndex, hookIndex } = desiredHook[0];
6094

6195
rawInput.setIn(
6296
["repos", repoIndex, "hooks", hookIndex, "additional_dependencies"],
63-
rawInput.createNode([`prettier@${prettierVersion}`]),
97+
rawInput.createNode(
98+
packages
99+
.map(([name, version]) => {
100+
if (version.includes("(")) {
101+
// pnpm includes the integrity hash in the version, which we don't
102+
// need here
103+
version = version.slice(0, version.indexOf("("));
104+
}
105+
return `${name}@${version}`;
106+
})
107+
.sort(),
108+
),
64109
);
65-
66-
return rawInput;
67110
}

0 commit comments

Comments
 (0)