-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
250 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import type { GitRepository } from "../source-control/git.js"; | ||
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; | ||
readonly all: AreaStatus; | ||
} | ||
|
||
export async function findChangeStatus( | ||
host: ChronusHost, | ||
sourceControl: GitRepository, | ||
workspace: Workspace, | ||
): Promise<ChangeStatus> { | ||
const filesChanged = await sourceControl.listChangedFilesFromBase(); | ||
const untrackedOrModifiedFiles = await sourceControl.listUntrackedOrModifiedFiles(); | ||
const stagedFiles = await sourceControl.listUntrackedOrModifiedFiles(); | ||
const publicPackages = workspace.packages.filter((x) => !x.manifest.private); | ||
|
||
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, | ||
all: { | ||
filesChanged: [ | ||
...new Set([...committed.filesChanged, ...untrackedOrModified.filesChanged, ...staged.filesChanged]), | ||
], | ||
packageChanged: [ | ||
...new Set([...committed.packageChanged, ...untrackedOrModified.packageChanged, ...staged.packageChanged]), | ||
], | ||
packagesDocumented: [ | ||
...new Set([ | ||
...committed.packagesDocumented, | ||
...untrackedOrModified.packagesDocumented, | ||
...staged.packagesDocumented, | ||
]), | ||
], | ||
}, | ||
}; | ||
} | ||
|
||
async function findAreaStatus(host: ChronusHost, packages: Package[], filesChanged: string[]): Promise<AreaStatus> { | ||
return { | ||
filesChanged, | ||
packageChanged: findPackageChanges(packages, filesChanged), | ||
packagesDocumented: await findAlreadyDocumentedChanges(host, packages, filesChanged), | ||
}; | ||
} | ||
|
||
/** Find which packages changed from the file changed. */ | ||
function findPackageChanges(packages: Package[], fileChanged: string[]): Package[] { | ||
const packageChanged = new Set<Package>(); | ||
|
||
for (const file of fileChanged) { | ||
const pkg = packages.find((x) => file.startsWith(x.relativePath + "/")); | ||
if (pkg) { | ||
packageChanged.add(pkg); | ||
} | ||
} | ||
return [...packageChanged]; | ||
} | ||
|
||
/** Find which packages changed from the file changed. */ | ||
async function findAlreadyDocumentedChanges( | ||
host: ChronusHost, | ||
packages: Package[], | ||
fileChanged: string[], | ||
): Promise<Package[]> { | ||
const packagesWithChangelog = new Set<Package>(); | ||
|
||
for (const filename of fileChanged.filter((x) => x.startsWith(".changeset/") && x.endsWith(".md"))) { | ||
const file = await host.readFile(filename); | ||
const changeset = readChangeset(file.content); | ||
for (const release of changeset.releases) { | ||
const pkg = packages.find((x) => x.name === release.name); | ||
if (pkg) { | ||
packagesWithChangelog.add(pkg); | ||
} | ||
} | ||
} | ||
return [...packagesWithChangelog]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { findChangeStatus } from "./find.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.