From 9235b7f26848b9b9aec3e18348e38b3d0f32079b Mon Sep 17 00:00:00 2001 From: fraxken Date: Fri, 9 May 2025 17:45:05 +0200 Subject: [PATCH] feat(mama): implement license extraction --- workspaces/mama/src/ManifestManager.class.ts | 24 +++++ workspaces/mama/test/ManifestManager.spec.ts | 99 ++++++++++++++++++++ workspaces/npm-types/src/index.d.ts | 7 +- 3 files changed, 129 insertions(+), 1 deletion(-) diff --git a/workspaces/mama/src/ManifestManager.class.ts b/workspaces/mama/src/ManifestManager.class.ts index 4131f812..d96860c1 100644 --- a/workspaces/mama/src/ManifestManager.class.ts +++ b/workspaces/mama/src/ManifestManager.class.ts @@ -126,6 +126,30 @@ export class ManifestManager< return packageJSONIntegrityHash(this.document); } + get license(): string | null { + if (this.document.license) { + if (typeof this.document.license === "string") { + return this.document.license; + } + + if (typeof this.document.license === "object") { + return this.document.license.type ?? null; + } + } + + if (this.document.licenses) { + if (Array.isArray(this.document.licenses)) { + return this.document.licenses[0]?.type ?? null; + } + + if (typeof this.document.licenses === "object") { + return this.document.licenses.type ?? null; + } + } + + return null; + } + * getEntryFiles(): IterableIterator { if (this.document.main) { yield this.document.main; diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index c1b3b694..22f9fcf7 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -435,6 +435,105 @@ describe("ManifestManager", () => { }); }); + describe("get license", () => { + test("Given a minimal PackageJSON with no license field then it must return null", () => { + const mama = new ManifestManager(kMinimalPackageJSON); + + assert.strictEqual( + mama.license, + null + ); + }); + + test("Given a minimal PackageJSON with a string license then it must return the exact license value", () => { + const expectedLicense = "MIT"; + + const mama = new ManifestManager({ + ...kMinimalPackageJSON, + license: expectedLicense + }); + + assert.strictEqual( + mama.license, + expectedLicense + ); + }); + + test("Given a minimal PackageJSON with a license object then it must return the 'type' property value", () => { + const expectedLicense = "MIT"; + + const mama = new ManifestManager({ + ...kMinimalPackageJSON, + license: { + type: expectedLicense + } + }); + + assert.strictEqual( + mama.license, + expectedLicense + ); + }); + + test("Given a minimal PackageJSON with a licenses object then it must return the 'type' property value", () => { + const expectedLicense = "MIT"; + + const mama = new ManifestManager({ + ...kMinimalPackageJSON, + licenses: { + type: expectedLicense + } + }); + + assert.strictEqual( + mama.license, + expectedLicense + ); + }); + + test("Given a minimal PackageJSON with a licenses array then it must return the first license type", () => { + const expectedLicense = "MIT"; + + const mama = new ManifestManager({ + ...kMinimalPackageJSON, + licenses: [ + { + type: expectedLicense + } + ] + }); + + assert.strictEqual( + mama.license, + expectedLicense + ); + }); + + test("Given a minimal PackageJSON with an empty licenses array then it must return null", () => { + const mama = new ManifestManager({ + ...kMinimalPackageJSON, + licenses: [] + }); + + assert.strictEqual( + mama.license, + null + ); + }); + + test("Given a minimal PackageJSON with licenses array and unexpected values then it must return null", () => { + const mama = new ManifestManager({ + ...kMinimalPackageJSON, + licenses: ["MIT"] as any + }); + + assert.strictEqual( + mama.license, + null + ); + }); + }); + describe("flags", () => { test("Given a minimal PackageJSON we must verify default values", () => { const mama = new ManifestManager(kMinimalPackageJSON); diff --git a/workspaces/npm-types/src/index.d.ts b/workspaces/npm-types/src/index.d.ts index b728dc05..f0cd908b 100644 --- a/workspaces/npm-types/src/index.d.ts +++ b/workspaces/npm-types/src/index.d.ts @@ -76,6 +76,10 @@ export type NodeImport = { default: string } | { node: string, default: string }; +export interface PackageJSONLicense { + type?: string | undefined | null; +} + interface BasePackageJSON { author?: Contact | string; bin?: Record; @@ -94,7 +98,8 @@ interface BasePackageJSON { files?: string[]; homepage?: string; keywords?: string[]; - license?: string; + license?: string | PackageJSONLicense; + licenses?: PackageJSONLicense[] | PackageJSONLicense; man?: string | string[]; optionalDependencies?: Record; os?: string[];