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
5 changes: 5 additions & 0 deletions .changeset/eight-cows-ask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/mama": patch
---

Add optional ManifestManager dirname location
25 changes: 24 additions & 1 deletion workspaces/mama/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,38 @@ Load a new instance using a `package.json` from the filesystem.

The **location** parameter can either be a full path or the path to the directory where the `package.json` is located.

### constructor(document: ManifestManagerDocument)
> [!NOTE]
> `location` is automatically dispatched to the ManifestManager constructor options.

### constructor(document: ManifestManagerDocument, options?: ManifestManagerOptions)

document is described by the following type:
```ts
import type {
PackumentVersion,
PackageJSON,
WorkspacesPackageJSON
} from "@nodesecure/npm-types";

type ManifestManagerDocument =
PackageJSON |
WorkspacesPackageJSON |
PackumentVersion;
```

And the `options` interface

```ts
export interface ManifestManagerOptions {
/**
* Optional absolute location (directory) to the manifest
*/
location?: string;
}
```

---

Default values are injected if they are not present in the document. This behavior is necessary for the correct operation of certain functions, such as integrity recovery.

```js
Expand Down
22 changes: 19 additions & 3 deletions workspaces/mama/src/ManifestManager.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export type ManifestManagerDefaultProperties = Required<
Pick<PackumentVersion, NonOptionalPackageJSONProperties>
>;

export interface ManifestManagerOptions {
/**
* Optional absolute location (directory) to the manifest
*/
location?: string;
}

export type ManifestManagerDocument =
PackageJSON |
WorkspacesPackageJSON |
Expand All @@ -68,19 +75,27 @@ export class ManifestManager<
ManifestManagerDocument,
NonOptionalPackageJSONProperties
>;

public location: string | undefined;
public flags = Object.seal({
hasUnsafeScripts: false,
isNative: false
});

constructor(
document: ManifestManagerDocument
document: ManifestManagerDocument,
options: ManifestManagerOptions = {}
) {
const { location } = options;

this.document = Object.assign(
{ ...ManifestManager.Default },
structuredClone(document)
);
if (location) {
this.location = path.extname(location) || path.basename(location).startsWith(".") ?
path.dirname(location) :
location;
}

this.flags.isNative = [
...this.dependencies,
Expand Down Expand Up @@ -218,7 +233,8 @@ export class ManifestManager<
) as PackageJSON | WorkspacesPackageJSON;

return new ManifestManager(
packageJSON
packageJSON,
{ location }
);
}
catch (cause) {
Expand Down
22 changes: 22 additions & 0 deletions workspaces/mama/test/ManifestManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe("ManifestManager", () => {
location
);

assert.equal(mama.location, location);
assert.equal(readfile.mock.callCount(), 1);
assert.ok(mama instanceof ManifestManager);

Expand All @@ -68,6 +69,7 @@ describe("ManifestManager", () => {
location
);

assert.equal(mama.location, path.dirname(location));
assert.equal(readFile.mock.callCount(), 1);
assert.ok(mama instanceof ManifestManager);

Expand Down Expand Up @@ -136,6 +138,26 @@ describe("ManifestManager", () => {
}
);
});

it("Should store location as it if that's not a file", () => {
for (const location of ["/tmp/path", "./tmp/path"]) {
const mama = new ManifestManager(kMinimalPackageJSON, { location });
assert.strictEqual(mama.location, location);
}
});

it("Should store location dirname if provided with a filename", () => {
for (const fileName of ["package.json", ".gitignore"]) {
const location = `/tmp/path/${fileName}`;
const mama = new ManifestManager(kMinimalPackageJSON, { location });
assert.strictEqual(mama.location, path.dirname(location));
}
});

it("Should have location undefined if not provided", () => {
const mama = new ManifestManager(kMinimalPackageJSON);
assert.strictEqual(mama.location, undefined);
});
});

describe("get moduleType", () => {
Expand Down