Skip to content
Merged
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
3 changes: 3 additions & 0 deletions workspaces/mama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Default values are injected if they are not present in the document. This behavi
> [!NOTE]
> document is deep cloned (there will no longer be any reference to the object supplied as an argument)

### getEntryFiles(): IterableIterator< string >
Deeply extract entry files from package `main` and Node.js `exports` fields.

### spec
Return the NPM specification (which is the combinaison of `name@version`).

Expand Down
39 changes: 38 additions & 1 deletion workspaces/mama/src/ManifestManager.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import path from "node:path";
// Import Third-party Dependencies
import { parseAuthor } from "@nodesecure/utils";
import type {
PackumentVersion, PackageJSON, WorkspacesPackageJSON, Contact
PackumentVersion,
PackageJSON,
WorkspacesPackageJSON,
Contact
} from "@nodesecure/npm-types";

// Import Internal Dependencies
Expand Down Expand Up @@ -123,6 +126,40 @@ export class ManifestManager<
return packageJSONIntegrityHash(this.document);
}

* getEntryFiles(): IterableIterator<string> {
if (this.document.main) {
yield this.document.main;
}

if (!this.document.exports) {
return;
}

if (typeof this.document.exports === "string") {
yield this.document.exports;
}
else {
yield* this.extractNodejsExport(this.document.exports);
}
}

private* extractNodejsExport(
exports: Record<string, string | null | Record<string, string | null>>
): IterableIterator<string> {
for (const node of Object.values(exports)) {
if (node === null) {
continue;
}

if (typeof node === "string") {
yield node;
}
else {
yield* this.extractNodejsExport(node);
}
}
}

static async fromPackageJSON(
location: string
): Promise<ManifestManager> {
Expand Down
48 changes: 48 additions & 0 deletions workspaces/mama/test/ManifestManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,52 @@ describe("ManifestManager", () => {
});
});
});

describe("getEntryFiles", () => {
it("should return PackageJSON main entry files", () => {
const packageJSON: PackageJSON = {
...kMinimalPackageJSON,
main: "./dist/index.js",
exports: "./dist/foobar.js"
};

const mama = new ManifestManager(packageJSON);
const files = new Set(mama.getEntryFiles());

assert.deepEqual(
[...files],
[
"./dist/index.js",
"./dist/foobar.js"
]
);
});

it("should deep extract Node.js export fields in PackageJSON", () => {
const packageJSON: PackageJSON = {
...kMinimalPackageJSON,
exports: {
".": {
import: "./index.js"
},
"./web": {
import: "./src/web.js"
},
"./package.json": "./package.json"
}
};

const mama = new ManifestManager(packageJSON);
const files = new Set(mama.getEntryFiles());

assert.deepEqual(
[...files],
[
"./index.js",
"./src/web.js",
"./package.json"
]
);
});
});
});