Skip to content

Commit

Permalink
chore(lambda-nodejs): improve node modules versions extraction
Browse files Browse the repository at this point in the history
Lookup the version in the `package.json` and then fallback to requiring
the module's `package.json`. This allows to mark transitive dependencies
as externals and install them.

See aws#11289 (comment)
  • Loading branch information
jogold committed Nov 18, 2020
1 parent 592a8aa commit 9ae662a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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

0 comments on commit 9ae662a

Please sign in to comment.