From 03beed3dfb68a8f8f1285188a9dcc47986374bf6 Mon Sep 17 00:00:00 2001 From: Ajay Chauhan Date: Sun, 8 Jun 2025 02:39:58 +0530 Subject: [PATCH 1/2] feat(mama): add LocatedManifestManager type and isLocated() type guard for ManifestManager --- workspaces/mama/src/ManifestManager.class.ts | 13 ++++++++ workspaces/mama/test/ManifestManager.spec.ts | 31 ++++++++++++++++++++ 2 files changed, 44 insertions(+) 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..b5eb691a 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -27,6 +27,37 @@ 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); + + if (ManifestManager.isLocated(mama)) { + const location: string = mama.location; + assert.strictEqual(location, "/tmp/path"); + } + }); + + 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"); From 904e668e0ba360ef00c460b9c85f99704037bc58 Mon Sep 17 00:00:00 2001 From: Ajay Chauhan Date: Sun, 8 Jun 2025 13:06:17 +0530 Subject: [PATCH 2/2] test(mama): remove redundant location assertion in ManifestManager tests --- workspaces/mama/test/ManifestManager.spec.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index b5eb691a..f9f525d1 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -31,11 +31,6 @@ describe("ManifestManager", () => { it("Should return true for ManifestManager with location", () => { const mama = new ManifestManager(kMinimalPackageJSON, { location: "/tmp/path" }); assert.strictEqual(ManifestManager.isLocated(mama), true); - - if (ManifestManager.isLocated(mama)) { - const location: string = mama.location; - assert.strictEqual(location, "/tmp/path"); - } }); it("Should properly narrow type with custom metadata", () => {