From da09f57c19397929b4e2657f735e4795c2a44d0a Mon Sep 17 00:00:00 2001 From: davy Date: Sun, 25 May 2025 14:41:55 +0200 Subject: [PATCH 01/10] =?UTF-8?q?feat(ManifestManager):=20ajout=20de=20la?= =?UTF-8?q?=20propri=C3=A9t=C3=A9=20manifestLocation=20et=20mise=20=C3=A0?= =?UTF-8?q?=20jour=20du=20constructeur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workspaces/mama/src/ManifestManager.class.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/workspaces/mama/src/ManifestManager.class.ts b/workspaces/mama/src/ManifestManager.class.ts index 04cd7eb7..ed6a2f76 100644 --- a/workspaces/mama/src/ManifestManager.class.ts +++ b/workspaces/mama/src/ManifestManager.class.ts @@ -65,6 +65,7 @@ export class ManifestManager< ManifestManagerDocument, NonOptionalPackageJSONProperties >; + public manifestLocation?: string; public flags = Object.seal({ hasUnsafeScripts: false, @@ -72,12 +73,14 @@ export class ManifestManager< }); constructor( - document: ManifestManagerDocument + document: ManifestManagerDocument, + manifestLocation?: string ) { this.document = Object.assign( { ...ManifestManager.Default }, structuredClone(document) ); + this.manifestLocation = manifestLocation; this.flags.isNative = [ ...this.dependencies, @@ -211,7 +214,8 @@ export class ManifestManager< ) as PackageJSON | WorkspacesPackageJSON; return new ManifestManager( - packageJSON + packageJSON, + packageLocation ); } catch (cause) { From b9ee5165182c03fcfb6d689a247f3ecce534f072 Mon Sep 17 00:00:00 2001 From: davy Date: Sun, 25 May 2025 14:45:00 +0200 Subject: [PATCH 02/10] =?UTF-8?q?test(ManifestManager):=20ajout=20de=20tes?= =?UTF-8?q?ts=20pour=20la=20propri=C3=A9t=C3=A9=20manifestLocation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workspaces/mama/test/ManifestManager.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index f0010810..9cd791b7 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -79,6 +79,10 @@ describe("ManifestManager", () => { mama.spec, `${kMinimalPackageJSON.name}@${kMinimalPackageJSON.version}` ); + assert.strictEqual( + mama.manifestLocation, + location + ); }); test("Given an invalid JSON, it should throw a custom Error with the parsing error as a cause", async(t: TestContext) => { @@ -136,6 +140,12 @@ describe("ManifestManager", () => { } ); }); + + it("Should store manifestLocation if provided", () => { + const location = "/tmp/fake/path/package.json"; + const mama = new ManifestManager(kMinimalPackageJSON, location); + assert.strictEqual(mama.manifestLocation, location); + }); }); describe("get dependencies", () => { From 59c1d3f9536e081fb3e9fc365c15e939098f762c Mon Sep 17 00:00:00 2001 From: davy Date: Sun, 25 May 2025 19:10:51 +0200 Subject: [PATCH 03/10] =?UTF-8?q?refactor(ManifestManager):=20renommer=20m?= =?UTF-8?q?anifestLocation=20en=20location=20et=20mise=20=C3=A0=20jour=20d?= =?UTF-8?q?es=20tests=20associ=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workspaces/mama/src/ManifestManager.class.ts | 8 ++++---- workspaces/mama/temp-test/package.json | 1 + workspaces/mama/test/ManifestManager.spec.ts | 20 +++++++++++++++++--- 3 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 workspaces/mama/temp-test/package.json diff --git a/workspaces/mama/src/ManifestManager.class.ts b/workspaces/mama/src/ManifestManager.class.ts index ed6a2f76..9186c3d3 100644 --- a/workspaces/mama/src/ManifestManager.class.ts +++ b/workspaces/mama/src/ManifestManager.class.ts @@ -65,7 +65,7 @@ export class ManifestManager< ManifestManagerDocument, NonOptionalPackageJSONProperties >; - public manifestLocation?: string; + public location?: string; public flags = Object.seal({ hasUnsafeScripts: false, @@ -74,13 +74,13 @@ export class ManifestManager< constructor( document: ManifestManagerDocument, - manifestLocation?: string + location?: string ) { this.document = Object.assign( { ...ManifestManager.Default }, structuredClone(document) ); - this.manifestLocation = manifestLocation; + this.location = location; this.flags.isNative = [ ...this.dependencies, @@ -204,7 +204,7 @@ export class ManifestManager< } const packageLocation = location.endsWith("package.json") ? - location : + path.join(path.dirname(location), "package.json") : path.join(location, "package.json"); const packageStr = await fs.readFile(packageLocation, "utf-8"); diff --git a/workspaces/mama/temp-test/package.json b/workspaces/mama/temp-test/package.json new file mode 100644 index 00000000..0faa39ef --- /dev/null +++ b/workspaces/mama/temp-test/package.json @@ -0,0 +1 @@ +{"name":"test","version":"1.0.0"} \ No newline at end of file diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index 9cd791b7..81c36665 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -80,7 +80,7 @@ describe("ManifestManager", () => { `${kMinimalPackageJSON.name}@${kMinimalPackageJSON.version}` ); assert.strictEqual( - mama.manifestLocation, + mama.location, location ); }); @@ -141,10 +141,10 @@ describe("ManifestManager", () => { ); }); - it("Should store manifestLocation if provided", () => { + it("Should store location if provided", () => { const location = "/tmp/fake/path/package.json"; const mama = new ManifestManager(kMinimalPackageJSON, location); - assert.strictEqual(mama.manifestLocation, location); + assert.strictEqual(mama.location, location); }); }); @@ -750,3 +750,17 @@ describe("ManifestManager", () => { }); }); }); + +describe("ManifestManager.fromPackageJSON", () => { + it("should work with a path to a package.json file", async() => { + const tempDir = path.join(process.cwd(), "temp-test"); + await fs.mkdir(tempDir, { recursive: true }); + const packagePath = path.join(tempDir, "package.json"); + await fs.writeFile(packagePath, JSON.stringify({ name: "test", version: "1.0.0" })); + + const manager = await ManifestManager.fromPackageJSON(packagePath); + + assert.strictEqual(manager.document.name, "test"); + assert.strictEqual(manager.document.version, "1.0.0"); + }); +}); From 6b3dbd8b806e9b006cce2eb7a3d65c56321bace1 Mon Sep 17 00:00:00 2001 From: davy Date: Sun, 25 May 2025 19:39:36 +0200 Subject: [PATCH 04/10] =?UTF-8?q?feat(ManifestManager):=20ajout=20de=20l'i?= =?UTF-8?q?nterface=20ManifestManagerOptions=20et=20mise=20=C3=A0=20jour?= =?UTF-8?q?=20du=20constructeur=20pour=20accepter=20des=20options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workspaces/mama/src/ManifestManager.class.ts | 11 ++++++++--- workspaces/mama/test/ManifestManager.spec.ts | 7 ++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/workspaces/mama/src/ManifestManager.class.ts b/workspaces/mama/src/ManifestManager.class.ts index 9186c3d3..4d3940eb 100644 --- a/workspaces/mama/src/ManifestManager.class.ts +++ b/workspaces/mama/src/ManifestManager.class.ts @@ -50,6 +50,11 @@ export type ManifestManagerDocument = WorkspacesPackageJSON | PackumentVersion; +export interface ManifestManagerOptions { + location?: string; + // D'autres options pourront être ajoutées ici plus tard +} + export class ManifestManager< MetadataDef extends Record = Record > { @@ -74,13 +79,13 @@ export class ManifestManager< constructor( document: ManifestManagerDocument, - location?: string + options: ManifestManagerOptions = {} ) { this.document = Object.assign( { ...ManifestManager.Default }, structuredClone(document) ); - this.location = location; + this.location = options.location; this.flags.isNative = [ ...this.dependencies, @@ -215,7 +220,7 @@ export class ManifestManager< return new ManifestManager( packageJSON, - packageLocation + { location } ); } catch (cause) { diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index 81c36665..2d2b321d 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -143,9 +143,14 @@ describe("ManifestManager", () => { it("Should store location if provided", () => { const location = "/tmp/fake/path/package.json"; - const mama = new ManifestManager(kMinimalPackageJSON, location); + const mama = new ManifestManager(kMinimalPackageJSON, { location }); assert.strictEqual(mama.location, location); }); + + it("Should have location undefined if not provided", () => { + const mama = new ManifestManager(kMinimalPackageJSON); + assert.strictEqual(mama.location, undefined); + }); }); describe("get dependencies", () => { From d3c92cac0d2478efa4b14a481152190451803d73 Mon Sep 17 00:00:00 2001 From: davy Date: Sun, 25 May 2025 21:37:48 +0200 Subject: [PATCH 05/10] Removal of the assert for dirname of location in fromPackageJSON and addition of the assert in the constructor; consequently, the related test tied to the previous implementation has also been removed. --- workspaces/mama/src/ManifestManager.class.ts | 12 ++++++++---- workspaces/mama/test/ManifestManager.spec.ts | 14 -------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/workspaces/mama/src/ManifestManager.class.ts b/workspaces/mama/src/ManifestManager.class.ts index 4d3940eb..ae9c1aba 100644 --- a/workspaces/mama/src/ManifestManager.class.ts +++ b/workspaces/mama/src/ManifestManager.class.ts @@ -52,7 +52,6 @@ export type ManifestManagerDocument = export interface ManifestManagerOptions { location?: string; - // D'autres options pourront être ajoutées ici plus tard } export class ManifestManager< @@ -85,7 +84,12 @@ export class ManifestManager< { ...ManifestManager.Default }, structuredClone(document) ); - this.location = options.location; + if (typeof options.location === "string" && options.location.endsWith("package.json")) { + this.location = path.dirname(options.location); + } + else { + this.location = options.location; + } this.flags.isNative = [ ...this.dependencies, @@ -209,7 +213,7 @@ export class ManifestManager< } const packageLocation = location.endsWith("package.json") ? - path.join(path.dirname(location), "package.json") : + location : path.join(location, "package.json"); const packageStr = await fs.readFile(packageLocation, "utf-8"); @@ -220,7 +224,7 @@ export class ManifestManager< return new ManifestManager( packageJSON, - { location } + { location: packageLocation } ); } catch (cause) { diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index 2d2b321d..08b28d0b 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -755,17 +755,3 @@ describe("ManifestManager", () => { }); }); }); - -describe("ManifestManager.fromPackageJSON", () => { - it("should work with a path to a package.json file", async() => { - const tempDir = path.join(process.cwd(), "temp-test"); - await fs.mkdir(tempDir, { recursive: true }); - const packagePath = path.join(tempDir, "package.json"); - await fs.writeFile(packagePath, JSON.stringify({ name: "test", version: "1.0.0" })); - - const manager = await ManifestManager.fromPackageJSON(packagePath); - - assert.strictEqual(manager.document.name, "test"); - assert.strictEqual(manager.document.version, "1.0.0"); - }); -}); From cb8ba760d1e734589720f5e4e03c501f68ec0ee8 Mon Sep 17 00:00:00 2001 From: davy Date: Mon, 26 May 2025 09:57:16 +0200 Subject: [PATCH 06/10] Deletion of the package.json file in the temp-test directory. --- workspaces/mama/temp-test/package.json | 1 - 1 file changed, 1 deletion(-) delete mode 100644 workspaces/mama/temp-test/package.json diff --git a/workspaces/mama/temp-test/package.json b/workspaces/mama/temp-test/package.json deleted file mode 100644 index 0faa39ef..00000000 --- a/workspaces/mama/temp-test/package.json +++ /dev/null @@ -1 +0,0 @@ -{"name":"test","version":"1.0.0"} \ No newline at end of file From 6cc086771f78438ab37c6bd1871197bad609abf4 Mon Sep 17 00:00:00 2001 From: davy Date: Sun, 1 Jun 2025 12:46:09 +0200 Subject: [PATCH 07/10] **`fix(ManifestManager): update location check to use path.extname instead of checking if it ends with "package.json"`** --- workspaces/mama/src/ManifestManager.class.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workspaces/mama/src/ManifestManager.class.ts b/workspaces/mama/src/ManifestManager.class.ts index ae9c1aba..17f885b7 100644 --- a/workspaces/mama/src/ManifestManager.class.ts +++ b/workspaces/mama/src/ManifestManager.class.ts @@ -84,7 +84,7 @@ export class ManifestManager< { ...ManifestManager.Default }, structuredClone(document) ); - if (typeof options.location === "string" && options.location.endsWith("package.json")) { + if (typeof options.location === "string" && path.extname(options.location)) { this.location = path.dirname(options.location); } else { From 696ef5eac5ddc2ec7827267a928dd05696d81eb2 Mon Sep 17 00:00:00 2001 From: davy Date: Mon, 2 Jun 2025 22:53:25 +0200 Subject: [PATCH 08/10] feat(ManifestManager): add optional location path in ManifestManager --- .changeset/khaki-cloths-join.md | 5 +++++ workspaces/mama/src/ManifestManager.class.ts | 7 +------ 2 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 .changeset/khaki-cloths-join.md diff --git a/.changeset/khaki-cloths-join.md b/.changeset/khaki-cloths-join.md new file mode 100644 index 00000000..1bdf0c19 --- /dev/null +++ b/.changeset/khaki-cloths-join.md @@ -0,0 +1,5 @@ +--- +"@nodesecure/mama": patch +--- + +Add optional location path in ManifestManager.class.ts diff --git a/workspaces/mama/src/ManifestManager.class.ts b/workspaces/mama/src/ManifestManager.class.ts index 17f885b7..9653ec83 100644 --- a/workspaces/mama/src/ManifestManager.class.ts +++ b/workspaces/mama/src/ManifestManager.class.ts @@ -84,12 +84,7 @@ export class ManifestManager< { ...ManifestManager.Default }, structuredClone(document) ); - if (typeof options.location === "string" && path.extname(options.location)) { - this.location = path.dirname(options.location); - } - else { - this.location = options.location; - } + this.location = typeof options.location === "string" ? path.dirname(options.location) : this.location; this.flags.isNative = [ ...this.dependencies, From 73044e293721dafd2c177a05e8ab960e590bdf93 Mon Sep 17 00:00:00 2001 From: davy Date: Tue, 3 Jun 2025 21:39:15 +0200 Subject: [PATCH 09/10] fix(test): fix location storage test to use directory of provided path --- workspaces/mama/test/ManifestManager.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index 08b28d0b..70d80bb1 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -142,9 +142,9 @@ describe("ManifestManager", () => { }); it("Should store location if provided", () => { - const location = "/tmp/fake/path/package.json"; + const location = "package.json"; const mama = new ManifestManager(kMinimalPackageJSON, { location }); - assert.strictEqual(mama.location, location); + assert.strictEqual(mama.location, path.dirname(location)); }); it("Should have location undefined if not provided", () => { From f4eae690844fcc04ca41b858cdab2aa24417a16e Mon Sep 17 00:00:00 2001 From: davy Date: Tue, 3 Jun 2025 23:39:18 +0200 Subject: [PATCH 10/10] Remove useless assertion in the ManifestManager test. --- workspaces/mama/test/ManifestManager.spec.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/workspaces/mama/test/ManifestManager.spec.ts b/workspaces/mama/test/ManifestManager.spec.ts index 70d80bb1..c487b13f 100644 --- a/workspaces/mama/test/ManifestManager.spec.ts +++ b/workspaces/mama/test/ManifestManager.spec.ts @@ -79,10 +79,6 @@ describe("ManifestManager", () => { mama.spec, `${kMinimalPackageJSON.name}@${kMinimalPackageJSON.version}` ); - assert.strictEqual( - mama.location, - location - ); }); test("Given an invalid JSON, it should throw a custom Error with the parsing error as a cause", async(t: TestContext) => {