cli-node: document and refactor PackageGraph

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-04-06 14:56:44 +02:00
parent 011217c8e0
commit 7240ec3232
4 changed files with 62 additions and 12 deletions
+1
View File
@@ -21,4 +21,5 @@
*/
export * from './git';
export * from './monorepo';
export * from './roles';
+57 -8
View File
@@ -22,9 +22,16 @@ import { GitUtils } from '../git';
import { Lockfile } from './Lockfile';
import { JsonValue } from '@backstage/types';
type PackageJSON = Package['packageJson'];
/**
* Known fields in Backstage package.json files.
*
* @public
*/
export interface BackstagePackageJson {
name: string;
version: string;
private?: boolean;
export interface ExtendedPackageJSON extends PackageJSON {
main?: string;
module?: string;
types?: string;
@@ -52,20 +59,43 @@ export interface ExtendedPackageJSON extends PackageJSON {
alphaTypes?: string;
betaTypes?: string;
};
dependencies?: {
[key: string]: string;
};
peerDependencies?: {
[key: string]: string;
};
devDependencies?: {
[key: string]: string;
};
optionalDependencies?: {
[key: string]: string;
};
}
export type ExtendedPackage = {
/**
* A local Backstage monorepo package
*
* @public
*/
export type BackstagePackage = {
dir: string;
packageJson: ExtendedPackageJSON;
packageJson: BackstagePackageJson;
};
/**
* A local package in the monorepo package graph.
*
* @public
*/
export type PackageGraphNode = {
/** The name of the package */
name: string;
/** The directory of the package */
dir: string;
/** The package data of the package itself */
packageJson: ExtendedPackageJSON;
packageJson: BackstagePackageJson;
/** All direct local dependencies of the package */
allLocalDependencies: Map<string, PackageGraphNode>;
@@ -90,12 +120,23 @@ export type PackageGraphNode = {
localOptionalDependents: Map<string, PackageGraphNode>;
};
/**
* Represents a local Backstage monorepo package graph.
*
* @public
*/
export class PackageGraph extends Map<string, PackageGraphNode> {
static async listTargetPackages(): Promise<ExtendedPackage[]> {
/**
* Lists all local packages in a monorepo.
*/
static async listTargetPackages(): Promise<BackstagePackage[]> {
const { packages } = await getPackages(paths.targetDir);
return packages as ExtendedPackage[];
return packages as BackstagePackage[];
}
/**
* Creates a package graph from a list of local packages.
*/
static fromPackages(packages: Package[]): PackageGraph {
const graph = new PackageGraph();
@@ -112,7 +153,7 @@ export class PackageGraph extends Map<string, PackageGraphNode> {
graph.set(name, {
name,
dir: pkg.dir,
packageJson: pkg.packageJson as ExtendedPackageJSON,
packageJson: pkg.packageJson as BackstagePackageJson,
allLocalDependencies: new Map(),
publishedLocalDependencies: new Map(),
@@ -210,6 +251,14 @@ export class PackageGraph extends Map<string, PackageGraphNode> {
return targets;
}
/**
* Lists all packages that have changed since a given git ref.
*
* @remarks
*
* If the `analyzeLockfile` option is set to true, the change detection will
* also consider changes to the dependency management lockfile.
*/
async listChangedPackages(options: {
ref: string;
analyzeLockfile?: boolean;
@@ -15,7 +15,7 @@
*/
import { extname } from 'path';
import { ExtendedPackageJSON } from './PackageGraph';
import { BackstagePackageJson } from './PackageGraph';
export interface EntryPoint {
mount: string;
@@ -47,7 +47,7 @@ function parseEntryPoint(mount: string, path: string): EntryPoint {
return { mount, path, name, ext: extname(path) };
}
export function readEntryPoints(pkg: ExtendedPackageJSON): Array<EntryPoint> {
export function readEntryPoints(pkg: BackstagePackageJson): Array<EntryPoint> {
const exp = pkg.exports;
if (typeof exp === 'string') {
return [defaultIndex];
+2 -2
View File
@@ -17,6 +17,6 @@
export { PackageGraph } from './PackageGraph';
export type {
PackageGraphNode,
ExtendedPackage,
ExtendedPackageJSON,
BackstagePackage,
BackstagePackageJson,
} from './PackageGraph';