Skip to content

Commit

Permalink
fix(runtime): fix default runtime
Browse files Browse the repository at this point in the history
fix #10
  • Loading branch information
floydspace committed Jul 19, 2021
1 parent efc7f11 commit 6fc103d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
"typescript"
],
"devDependencies": {
"@aws-cdk/assert": "^1.70.0",
"@aws-cdk/aws-lambda": "^1.70.0",
"@aws-cdk/core": "^1.70.0",
"@aws-cdk/assert": "^1.114.0",
"@aws-cdk/aws-lambda": "^1.114.0",
"@aws-cdk/core": "^1.114.0",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@types/fs-extra": "^9.0.2",
Expand Down
21 changes: 15 additions & 6 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ import { mergeRight, union, without } from 'ramda';

import { packExternalModules } from './pack-externals';
import { NodejsFunctionProps } from './props';
import { extractFileName, findProjectRoot, NodeMajorESMap, nodeMajorVersion } from './utils';
import { extractFileName, findProjectRoot, nodeMajorVersion } from './utils';

const BUILD_FOLDER = '.build';
const DEFAULT_BUILD_OPTIONS: es.BuildOptions = {
bundle: true,
target: NodeMajorESMap[nodeMajorVersion()],
target: `node${nodeMajorVersion()}`,
};

const NodeMajorMap = {
8: lambda.Runtime.NODEJS_8_10,
9: lambda.Runtime.NODEJS_8_10,
10: lambda.Runtime.NODEJS_10_X,
11: lambda.Runtime.NODEJS_10_X,
12: lambda.Runtime.NODEJS_12_X,
13: lambda.Runtime.NODEJS_12_X,
14: lambda.Runtime.NODEJS_14_X,
15: lambda.Runtime.NODEJS_14_X,
};

/**
Expand All @@ -33,9 +44,7 @@ export class NodejsFunction extends lambda.Function {
const exclude = props.exclude ?? ['aws-sdk'];
const packager = props.packager ?? true;
const handler = props.handler ?? 'index.handler';
const defaultRuntime = nodeMajorVersion() >= 12
? lambda.Runtime.NODEJS_12_X
: lambda.Runtime.NODEJS_10_X;
const defaultRuntime = NodeMajorMap[nodeMajorVersion()];
const runtime = props.runtime ?? defaultRuntime;
const entry = extractFileName(projectRoot, handler);

Expand All @@ -52,7 +61,7 @@ export class NodejsFunction extends lambda.Function {
without(exclude, buildOptions.external || []),
projectRoot,
path.join(projectRoot, BUILD_FOLDER),
packager !== true ? packager : undefined,
packager !== true ? packager : undefined
);
}

Expand Down
20 changes: 3 additions & 17 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ export function findUp(name: string, directory: string = process.cwd()): string
* Forwards `rootDir` or finds project root folder.
*/
export function findProjectRoot(rootDir?: string): string | undefined {
return rootDir
?? findUp('yarn.lock')
?? findUp('package-lock.json')
?? findUp('package.json')
?? findUp(`.git${path.sep}`);
return (
rootDir ?? findUp('yarn.lock') ?? findUp('package-lock.json') ?? findUp('package.json') ?? findUp(`.git${path.sep}`)
);
}

/**
Expand All @@ -105,18 +103,6 @@ export function nodeMajorVersion(): number {
return parseInt(process.versions.node.split('.')[0], 10);
}

export const NodeMajorESMap = {
8: 'es2016',
9: 'es2017',
10: 'es2018',
11: 'es2018',
12: 'es2019',
13: 'es2019',
14: 'es2020',
15: 'es2020',
16: 'esnext',
};

/**
* Returns the package manager currently active if the program is executed
* through an npm or yarn script like:
Expand Down

0 comments on commit 6fc103d

Please sign in to comment.