Skip to content

Commit

Permalink
Stop using a well-know install location
Browse files Browse the repository at this point in the history
This does not work, because the subsequent `require()` call fails to load the installed module, because it can't find the aws-cdk-lib and constructs peer-dependencies. Installing both of those seems like a performance cost we do not want to pay.
  • Loading branch information
madeline-k committed Aug 29, 2022
1 parent 7f18fbc commit 852bdad
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/@aws-cdk/lambda-layer-awscli/lib/awscli-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as lambda from '@aws-cdk/aws-lambda';
import { RemovalPolicy, ResourceEnvironment, Stack } from '@aws-cdk/core';
import { Construct, Node } from 'constructs';
import * as childproc from 'child_process';
import * as os from 'os';
import * as path from 'path';

/**
Expand All @@ -20,12 +19,8 @@ export class AwsCliLayer implements lambda.ILayerVersion {
private readonly packageName: string = 'lambda-layer-awscli-v1';
private readonly npmPackage: any;
private readonly layer: lambda.LayerVersion;
private readonly wellKnownInstallDir: string;

constructor(scope: Construct, id: string) {
const installParentDir = os.homedir() ?? os.tmpdir();
this.wellKnownInstallDir = path.join(installParentDir, '.awscdk/npm-cache');

const version = this.requireWrapper(path.join(__dirname, '../package.json')).devDependencies[this.packageName];
const pathOfModuleIfAlreadyInstalled = require.resolve(`${this.packageName}`);
const versionAlreadyInstalled = this.requireWrapper(path.join(pathOfModuleIfAlreadyInstalled, '../../package.json')).version;
Expand Down Expand Up @@ -74,9 +69,13 @@ export class AwsCliLayer implements lambda.ILayerVersion {
}

private installNpmPackage(): any {
console.log(`Shelling out to run npm install ${this.packageName} --no-save --prefix ${this.wellKnownInstallDir}`);
const result = childproc.execSync(`pwd; npm prefix; npm install ${this.packageName} --silent --no-save --prefix ${this.wellKnownInstallDir}`);
const installDir = require.main?.paths[0].split('/').slice(0, -1).join('/');
if (!installDir) {
return;
}
console.log(`Shelling out to run npm install ${this.packageName} --no-save --prefix ${installDir}`);
const result = childproc.execSync(`pwd; npm prefix; npm install ${this.packageName} --no-save --prefix ${installDir}`);
console.log(result.toString('utf8'));
return this.requireWrapper(path.join(this.wellKnownInstallDir, 'node_modules', this.packageName, 'lib/index.js'));
return this.requireWrapper(path.join(installDir, 'node_modules', this.packageName, 'lib/index.js'));
}
}

0 comments on commit 852bdad

Please sign in to comment.