@clouden-cdk/aws-lambda-typescript
Advanced tools
Comparing version 1.6.1 to 1.35.0
import { Construct } from '@aws-cdk/core'; | ||
import { Code, AssetCode } from '@aws-cdk/aws-lambda'; | ||
interface CopyFileItem { | ||
sourcePath: string; | ||
targetPath: string; | ||
} | ||
export interface TypeScriptAssetCodeOptions { | ||
npmInstallCommand?: string; | ||
npmInstallArguments?: string[]; | ||
copyFiles?: CopyFileItem[]; | ||
} | ||
/** | ||
@@ -7,3 +16,3 @@ * Wrapper for the Code abstract class, which provides some static helper methods. | ||
export declare abstract class TypeScriptCode extends Code { | ||
static asset(path: string): TypeScriptAssetCode; | ||
static asset(path: string, options?: TypeScriptAssetCodeOptions): TypeScriptAssetCode; | ||
} | ||
@@ -15,5 +24,7 @@ /** | ||
private typeScriptSourcePath; | ||
constructor(path: string); | ||
private typeScriptAssetCodeOptions; | ||
constructor(path: string, options?: TypeScriptAssetCodeOptions); | ||
bind(construct: Construct): import("@aws-cdk/aws-lambda").CodeConfig; | ||
private typeScriptBuild; | ||
} | ||
export {}; |
@@ -14,3 +14,9 @@ "use strict"; | ||
const fs = __importStar(require("fs")); | ||
const mkdirp = __importStar(require("mkdirp")); | ||
const typeScriptAlreadyBuilt = []; // list of source code paths already built in this session | ||
const defaultTypeScriptAssetCodeOptions = { | ||
npmInstallCommand: 'npm', | ||
npmInstallArguments: ['install', '--production'], | ||
copyFiles: [], | ||
}; | ||
/** | ||
@@ -20,4 +26,4 @@ * Wrapper for the Code abstract class, which provides some static helper methods. | ||
class TypeScriptCode extends aws_lambda_1.Code { | ||
static asset(path) { | ||
return new TypeScriptAssetCode(path); | ||
static asset(path, options) { | ||
return new TypeScriptAssetCode(path, options); | ||
} | ||
@@ -30,3 +36,3 @@ } | ||
class TypeScriptAssetCode extends aws_lambda_1.AssetCode { | ||
constructor(path) { | ||
constructor(path, options) { | ||
// Add a .deploy subfolder which contains the built files and is deployed to S3 | ||
@@ -36,2 +42,3 @@ super(pathModule.join(path, '.deploy')); | ||
this.typeScriptSourcePath = path; | ||
this.typeScriptAssetCodeOptions = Object.assign({}, defaultTypeScriptAssetCodeOptions, options || {}); | ||
} | ||
@@ -66,2 +73,10 @@ bind(construct) { | ||
} | ||
// Copy additional files if specified | ||
for (const copyFile of this.typeScriptAssetCodeOptions.copyFiles || []) { | ||
const sourcePath = pathModule.join(this.typeScriptSourcePath, copyFile.sourcePath); // relative to user specified path | ||
const targetPath = pathModule.join(this.path, copyFile.targetPath); // relative to .deploy | ||
const targetPathParts = pathModule.parse(targetPath); | ||
mkdirp.sync(targetPathParts.dir); | ||
fs.copyFileSync(sourcePath, targetPath); | ||
} | ||
function readFileSyncOrNull(filepath, encoding) { | ||
@@ -90,3 +105,3 @@ try { | ||
// Execute npm install | ||
const npmChild = child_process.spawnSync('npm', ['install', '--production'], { | ||
const npmChild = child_process.spawnSync(this.typeScriptAssetCodeOptions.npmInstallCommand, this.typeScriptAssetCodeOptions.npmInstallArguments, { | ||
cwd: this.path, | ||
@@ -93,0 +108,0 @@ stdio: 'inherit', |
{ | ||
"name": "@clouden-cdk/aws-lambda-typescript", | ||
"version": "1.6.1", | ||
"version": "1.35.0", | ||
"private": false, | ||
@@ -17,9 +17,12 @@ "description": "TypeScript Build Step for AWS CDK Lambda Functions", | ||
"devDependencies": { | ||
"@aws-cdk/aws-lambda": "^1.6.1", | ||
"@aws-cdk/core": "^1.6.1", | ||
"aws-cdk": "^1.6.1" | ||
"@aws-cdk/aws-lambda": "^1.35.0", | ||
"@aws-cdk/core": "^1.35.0", | ||
"@types/mkdirp": "^1.0.0", | ||
"@types/node": "^13.13.4", | ||
"aws-cdk": "^1.35.0" | ||
}, | ||
"dependencies": { | ||
"typescript": "^3.6.2" | ||
"mkdirp": "^1.0.4", | ||
"typescript": "^3.8.3" | ||
} | ||
} |
# TypeScript Build Step for AWS CDK Lambda Functions | ||
Copyright (C) Clouden Oy 2019, author Kenneth Falck <kennu@clouden.net>. | ||
Copyright (C) Clouden Oy 2019-2020, author Kenneth Falck <kennu@clouden.net>. | ||
Released under the MIT license. | ||
Versioning indicates compatibility with AWS CDK major and minor versions. 1.5.x will be compatible with AWS CDK 1.5.x and so on. | ||
Versioning indicates compatibility with AWS CDK major and minor versions. 1.35.x will be compatible with | ||
AWS CDK 1.35.x and so on. AWS CDK has recently stabilized significantly and usually this module is compatible | ||
with the latest version. | ||
@@ -26,3 +28,26 @@ ## Overview | ||
## Options | ||
You can specify an optional options object as a second parameter to customize the npm install | ||
command or to copy additional files to the .deploy directory before deploying the Lambda function. | ||
The default npm install command is `npm install --production`. | ||
The source paths specified with copyFiles are relative to the source directory (given as the first parameter). | ||
The target paths specified with copyFiles are relative to the .deploy directory (Lambda root path). | ||
```typescript | ||
TypeScriptCode.asset('path/to/lambda-source-code', { | ||
npmInstallCommand: 'npm', | ||
npmInstallArguments: ['install', '--production'], | ||
copyFiles: [{ | ||
sourcePath: 'data/file.dat', // relative to source path, can specify a single file only | ||
targetPath: 'data/file.dat', // relative to .deploy path, can specify a single file only | ||
}], | ||
}) | ||
``` | ||
## Example | ||
```typescript | ||
import { Function } from '@aws-cdk/aws-lambda' | ||
@@ -35,3 +60,3 @@ import { TypeScriptCode } from '@clouden-cdk/aws-lambda-typescript' | ||
handler: 'handler.default', | ||
runtime: lambda.Runtime.NODEJS_10_X, | ||
runtime: lambda.Runtime.NODEJS_12_X, | ||
}) | ||
@@ -71,6 +96,7 @@ ``` | ||
The TypeScript build involves two steps: | ||
The TypeScript build involves three steps: | ||
1. Run tsc to build the files in the source path and save output to the deploy path. | ||
2. Copy package.json and package-lock.json from the source path to the deploy path and run npm install --production there. | ||
2. Copy any additional files specified with copyFiles to the deploy path. | ||
3. Copy package.json and package-lock.json from the source path to the deploy path and run npm install --production there. | ||
@@ -77,0 +103,0 @@ The end result of these steps is that the deploy path contains everything needed to deploy the Lambda function. |
@@ -6,5 +6,23 @@ import { Construct } from '@aws-cdk/core' | ||
import * as fs from 'fs' | ||
import * as mkdirp from 'mkdirp' | ||
const typeScriptAlreadyBuilt: string[] = [] // list of source code paths already built in this session | ||
interface CopyFileItem { | ||
sourcePath: string | ||
targetPath: string | ||
} | ||
export interface TypeScriptAssetCodeOptions { | ||
npmInstallCommand?: string | ||
npmInstallArguments?: string[] | ||
copyFiles?: CopyFileItem[] | ||
} | ||
const defaultTypeScriptAssetCodeOptions = { | ||
npmInstallCommand: 'npm', | ||
npmInstallArguments: ['install', '--production'], | ||
copyFiles: [], | ||
} | ||
/** | ||
@@ -14,4 +32,4 @@ * Wrapper for the Code abstract class, which provides some static helper methods. | ||
export abstract class TypeScriptCode extends Code { | ||
public static asset(path: string): TypeScriptAssetCode { | ||
return new TypeScriptAssetCode(path) | ||
public static asset(path: string, options?: TypeScriptAssetCodeOptions): TypeScriptAssetCode { | ||
return new TypeScriptAssetCode(path, options) | ||
} | ||
@@ -25,4 +43,5 @@ } | ||
private typeScriptSourcePath: string // original source code path | ||
private typeScriptAssetCodeOptions: TypeScriptAssetCodeOptions | ||
constructor(path: string) { | ||
constructor(path: string, options?: TypeScriptAssetCodeOptions) { | ||
// Add a .deploy subfolder which contains the built files and is deployed to S3 | ||
@@ -32,2 +51,3 @@ super(pathModule.join(path, '.deploy')) | ||
this.typeScriptSourcePath = path | ||
this.typeScriptAssetCodeOptions = Object.assign({}, defaultTypeScriptAssetCodeOptions, options || {}) | ||
} | ||
@@ -66,2 +86,11 @@ | ||
// Copy additional files if specified | ||
for (const copyFile of this.typeScriptAssetCodeOptions.copyFiles || []) { | ||
const sourcePath = pathModule.join(this.typeScriptSourcePath, copyFile.sourcePath) // relative to user specified path | ||
const targetPath = pathModule.join(this.path, copyFile.targetPath) // relative to .deploy | ||
const targetPathParts = pathModule.parse(targetPath) | ||
mkdirp.sync(targetPathParts.dir) | ||
fs.copyFileSync(sourcePath, targetPath) | ||
} | ||
function readFileSyncOrNull(filepath: string, encoding: string) { | ||
@@ -92,3 +121,3 @@ try { | ||
// Execute npm install | ||
const npmChild = child_process.spawnSync('npm', ['install', '--production'], { | ||
const npmChild = child_process.spawnSync(this.typeScriptAssetCodeOptions.npmInstallCommand!, this.typeScriptAssetCodeOptions.npmInstallArguments, { | ||
cwd: this.path, | ||
@@ -95,0 +124,0 @@ stdio: 'inherit', |
16751
266
105
2
5
+ Addedmkdirp@^1.0.4
+ Addedmkdirp@1.0.4(transitive)
Updatedtypescript@^3.8.3