Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions workspaces/mama/src/ManifestManager.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export type ManifestManagerDocument =
WorkspacesPackageJSON |
PackumentVersion;

export type LocatedManifestManager<
MetadataDef extends Record<string, any> = Record<string, any>
> = ManifestManager<MetadataDef> & { location: string; };

export class ManifestManager<
MetadataDef extends Record<string, any> = Record<string, any>
> {
Expand All @@ -70,6 +74,15 @@ export class ManifestManager<
gypfile: false
});

/**
* Type guard to check if a ManifestManager instance has a location
*/
static isLocated<T extends Record<string, any>>(
mama: ManifestManager<T>
): mama is LocatedManifestManager<T> {
return typeof mama.location !== "undefined";
}

public metadata: MetadataDef = Object.create(null);
public document: WithRequired<
ManifestManagerDocument,
Expand Down
26 changes: 26 additions & 0 deletions workspaces/mama/test/ManifestManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomMetadata>(
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");
Expand Down