Skip to content

Commit

Permalink
fix: $.manfifest resilience (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed May 21, 2024
1 parent 6e08a75 commit 080541a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/options/helpers/manifest/ManifestResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('manifest', () => {
expect(typeof version).toBe('string');
});

it('should return a fallback if the specified property is not found in the current package', async () => {
const value = await manifest((m) => m.someProperty, 'fallback');
expect(value).toBe('fallback');
});

it('should return a specific property of the package.json content of the specified package', async () => {
const version = await manifest('lodash', (m) => m.version);
expect(typeof version).toBe('string');
Expand All @@ -33,6 +38,11 @@ describe('manifest', () => {
expect(name1).toBe(name2);
});

it('should return a fallback if the specified property is not found in the specified package', async () => {
const value = await manifest('lodash', 'someProperty', 'fallback');
expect(value).toBe('fallback');
});

it('should provide more convenient access to other manifest properties', async () => {
const name1 = await manifest('lodash', 'name');
const name2 = await manifest('lodash', ['name']);
Expand All @@ -41,4 +51,9 @@ describe('manifest', () => {
expect(name1).toBe(name2);
expect(name2).toBe(name3);
});

it('should use a fallback value if the manifest is not found', async () => {
const result = await manifest('non-existing-package', ['name'], '-');
expect(result).toBe('-');
});
});
13 changes: 10 additions & 3 deletions src/options/helpers/manifest/ManifestResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,15 @@ export class ManifestResolver {
};

private async resolveManifestPath(packageName?: string) {
return packageName
? require.resolve(packageName + '/package.json')
: await pkgUp({ cwd: this.cwd });
return packageName ? this.resolveCJS(packageName) : await pkgUp({ cwd: this.cwd });
}

private resolveCJS(packageName: string) {
try {
return require.resolve(packageName + '/package.json');
} catch (error: unknown) {
log.debug(error, `Failed to resolve package.json for "${packageName}"`);
return;
}
}
}

0 comments on commit 080541a

Please sign in to comment.