Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: $.manifest resilience #41

Merged
merged 1 commit into from
May 21, 2024
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
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;
}
}
}
Loading