diff --git a/workspaces/mama/src/ManifestManager.class.ts b/workspaces/mama/src/ManifestManager.class.ts index ac0dc8df..afd5e001 100644 --- a/workspaces/mama/src/ManifestManager.class.ts +++ b/workspaces/mama/src/ManifestManager.class.ts @@ -60,6 +60,10 @@ export type ManifestManagerDocument = WorkspacesPackageJSON | PackumentVersion; +export type LocatedManifestManager< + MetadataDef extends Record = Record +> = ManifestManager & { location: string; }; + export class ManifestManager< MetadataDef extends Record = Record > { @@ -70,6 +74,15 @@ export class ManifestManager< gypfile: false }); + /** + * Type guard to check if a ManifestManager instance has a location + */ + static isLocated>( + mama: ManifestManager + ): mama is LocatedManifestManager { + return typeof mama.location !== "undefined"; + } + public metadata: MetadataDef = Object.create(null); public document: WithRequired< ManifestManagerDocument, diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index a0cb5603..f9f525d1 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -27,6 +27,32 @@ const kMinimalPackageJSONIntegrity = hash({ }); describe("ManifestManager", () => { + describe("static isLocated()", () => { + it("Should return true for ManifestManager with location", () => { + const mama = new ManifestManager(kMinimalPackageJSON, { location: "/tmp/path" }); + assert.strictEqual(ManifestManager.isLocated(mama), true); + }); + + it("Should properly narrow type with custom metadata", () => { + interface CustomMetadata { + customField: string; + } + + const mama = new ManifestManager( + kMinimalPackageJSON, + { location: "/tmp/path" } + ); + mama.metadata.customField = "test"; + + if (ManifestManager.isLocated(mama)) { + const location: string = mama.location; + const metadata: CustomMetadata = mama.metadata; + assert.strictEqual(location, "/tmp/path"); + assert.strictEqual(metadata.customField, "test"); + } + }); + }); + describe("static Default", () => { test("Property must be Frozen", () => { const isUpdated = Reflect.set(ManifestManager.Default, "foo", "bar");