Skip to content

Commit

Permalink
feat: add install package step
Browse files Browse the repository at this point in the history
  • Loading branch information
theBenForce committed Oct 3, 2019
1 parent d0d73a5 commit b567387
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"@aws-cdk/aws-lambda": "^1.11.0",
"execa": "^2.0.4",
"rollup": "^1.22.0"
},
"scripts": {
Expand Down
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@ import { Code } from "@aws-cdk/aws-lambda";
import * as tmp from "tmp";

import { join as joinPath } from "path";
import execa from "execa";

/**
* Writes rollup results to a temporary directory, then
* returns a @Code instance referencing that directory.
*/
export default async function RollupCode(codePath: string, config: RollupOptions): Promise<Code> {
export default async function RollupCode(codePath: string, config: RollupOptions, installedPackages: Array<string> = []): Promise<Code> {
const bundle = await rollup({ ...config, input: codePath });

// Create tmp directory
const tmpDir = tmp.dirSync();

await bundle.write({ ...config.output, file: joinPath(tmpDir.name, 'index.js') });

if (installedPackages.length > 0) {
console.info(`Installing packages`);

await execa(`npm init -y`, {
cwd: tmpDir.name
});

await execa(`npm install ${installedPackages.join(" ")}`, {
cwd: tmpDir.name
});
}

return Code.fromAsset(tmpDir.name);
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"declaration": true
"declaration": true,
"esModuleInterop": true
}
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ execa@^1.0.0:
signal-exit "^3.0.0"
strip-eof "^1.0.0"

execa@^2.0.2:
execa@^2.0.2, execa@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/execa/-/execa-2.0.4.tgz#2f5cc589c81db316628627004ea4e37b93391d8e"
integrity sha512-VcQfhuGD51vQUQtKIq2fjGDLDbL6N1DTQVpYzxZ7LPIXw3HqTuIz6uxRmpV1qf8i31LHf2kjiaGI+GdHwRgbnQ==
Expand Down

0 comments on commit b567387

Please sign in to comment.