Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

chore(lambda-nodejs): improve node modules versions extraction #11548

Merged
merged 3 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 10 additions & 4 deletions packages/@aws-cdk/aws-lambda-nodejs/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export function exec(cmd: string, args: string[], options?: SpawnSyncOptions) {
}

/**
* Extract dependencies from a package.json
* Extract versions for a list of modules.
*
* First lookup the version in the package.json and then fallback to requiring
* the module's package.json. The fallback is needed for transitive dependencies.
*/
export function extractDependencies(pkgPath: string, modules: string[]): { [key: string]: string } {
const dependencies: { [key: string]: string } = {};
Expand All @@ -107,10 +110,13 @@ export function extractDependencies(pkgPath: string, modules: string[]): { [key:
};

for (const mod of modules) {
if (!pkgDependencies[mod]) {
throw new Error(`Cannot extract version for module '${mod}' in package.json`);
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const version = pkgDependencies[mod] ?? require(`${mod}/package.json`).version;
dependencies[mod] = version;
} catch (err) {
throw new Error(`Cannot extract version for module '${mod}'. Check that it's referenced in your package.json or installed.`);
}
dependencies[mod] = pkgDependencies[mod];
}

return dependencies;
Expand Down
15 changes: 13 additions & 2 deletions packages/@aws-cdk/aws-lambda-nodejs/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('exec', () => {
});

describe('extractDependencies', () => {
test('with depencies referenced in package.json', () => {
test('with dependencies referenced in package.json', () => {
expect(extractDependencies(
path.join(__dirname, '../package.json'),
['@aws-cdk/aws-lambda', '@aws-cdk/core'],
Expand All @@ -99,11 +99,22 @@ describe('extractDependencies', () => {
});
});

test('with transitive dependencies', () => {
expect(extractDependencies(
path.join(__dirname, '../package.json'),
['typescript'],
)).toEqual({
// eslint-disable-next-line @typescript-eslint/no-require-imports, import/no-extraneous-dependencies
typescript: require('typescript/package.json').version,
});
/* eslint-enable @typescript-eslint/no-require-imports */
});

test('with unknown dependency', () => {
expect(() => extractDependencies(
path.join(__dirname, '../package.json'),
['unknown'],
)).toThrow(/Cannot extract version for module 'unknown' in package.json/);
)).toThrow(/Cannot extract version for module 'unknown'/);
});
});

Expand Down