Skip to content

Commit

Permalink
Verify
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Jan 27, 2024
1 parent 6489c29 commit 4c00de8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-unicorn": "^50.0.1",
"prettier": "^3.2.4",
"prettier-plugin-organize-imports": "^3.2.4",
"rimraf": "^5.0.5",
"syncpack": "^12.3.0",
"typescript": "^5.3.3"
Expand Down
33 changes: 29 additions & 4 deletions packages/chronus/src/change/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ import type { ChronusHost } from "../utils/host.js";
import type { Package, Workspace } from "../workspace-manager/types.js";
import readChangeset from "@changesets/parse";

export type ChangeArea = "committed" | "untrackedOrModified" | "staged";

export interface PackageStatus {
readonly package: Package;
readonly changed?: ChangeArea;
readonly documented?: ChangeArea;
}
export interface AreaStatus {
readonly filesChanged: string[];
readonly packageChanged: Package[];
readonly packagesDocumented: Package[];
}

export interface ChangeStatus {
readonly packages: ReadonlyMap<string, PackageStatus>;
readonly committed: AreaStatus;
readonly untrackedOrModified: AreaStatus;
readonly staged: AreaStatus;
Expand All @@ -25,15 +33,32 @@ export async function findChangeStatus(
const untrackedOrModifiedFiles = await sourceControl.listUntrackedOrModifiedFiles();
const stagedFiles = await sourceControl.listUntrackedOrModifiedFiles();
const publicPackages = workspace.packages.filter((x) => !x.manifest.private);
const packageMap = new Map<string, Package>();
for (const pkg of publicPackages) {
packageMap.set(pkg.name, pkg);
}

const committed = await findAreaStatus(host, publicPackages, filesChanged);
const untrackedOrModified = await findAreaStatus(host, publicPackages, untrackedOrModifiedFiles);
const staged = await findAreaStatus(host, publicPackages, stagedFiles);
const packages = new Map<string, PackageStatus>();
function track(tracking: Package[], data: { readonly changed?: ChangeArea; readonly documented?: ChangeArea }) {
for (const pkg of tracking) {
const existing = packages.get(pkg.name);
if (existing) {
packages.set(pkg.name, { ...existing, ...data });
} else {
packages.set(pkg.name, { package: pkg, ...data });
}
}
}

track(untrackedOrModified.packageChanged, { changed: "untrackedOrModified" });
track(untrackedOrModified.packagesDocumented, { documented: "untrackedOrModified" });
track(staged.packageChanged, { changed: "staged" });
track(staged.packagesDocumented, { documented: "staged" });
track(committed.packageChanged, { changed: "committed" });
track(committed.packagesDocumented, { documented: "committed" });
track(publicPackages, {});

return {
packages,
committed,
untrackedOrModified,
staged,
Expand Down
2 changes: 2 additions & 0 deletions packages/chronus/src/cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "source-map-support/register.js";
import yargs from "yargs";
import { addChangeset } from "./commands/add-changeset.js";
import { verifyChangeset } from "./commands/verify-changeset.js";

export const DEFAULT_PORT = 3000;

Expand All @@ -19,6 +20,7 @@ async function main() {
default: false,
})
.command("add", "Add a new changeset", () => addChangeset(process.cwd()))
.command("verify", "Verify all packages changes have been documented", () => verifyChangeset(process.cwd()))
.parse();
}

Expand Down
39 changes: 39 additions & 0 deletions packages/chronus/src/cli/commands/verify-changeset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createGitSourceControl } from "../../source-control/git.js";
import { NodechronusHost } from "../../utils/node-host.js";
import { createPnpmWorkspaceManager } from "../../workspace-manager/pnpm.js";
import { findChangeStatus } from "../../change/index.js";
import pc from "picocolors";
function log(...args: any[]) {
// eslint-disable-next-line no-console
console.log(...args);
}
export async function verifyChangeset(cwd: string): Promise<void> {
const host = NodechronusHost;
const pnpm = createPnpmWorkspaceManager(host);
const workspace = await pnpm.load(cwd);
const sourceControl = createGitSourceControl(workspace.path);
const status = await findChangeStatus(host, sourceControl, workspace);

const undocummentedPackages = [...status.packages.values()].filter((x) => x.changed && !x.documented);
const documentedPackages = [...status.packages.values()].filter((x) => x.changed && x.documented);
if (undocummentedPackages.length > 0) {
log(`There is undocummented changes. Run ${pc.cyan("chronus add")} to add a changeset.`);
log("");
log(pc.red(`The following packages have changes but are not documented.`));
for (const pkg of undocummentedPackages) {
log(pc.red(` - ${pkg.package.name}`));
}
log("");
log(pc.green("The following packages have already been documented:"));

for (const pkg of documentedPackages) {
log(pc.green(` - ${pkg.package.name}`));
}
process.exit(1);
}

log(pc.green("All changed packages have been documented:"));
for (const pkg of documentedPackages) {
log(pc.green(` - ${pkg.package.name}`));
}
}
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4c00de8

Please sign in to comment.