Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit f50549e

Browse files
committed
chore(aws-lambda): convert package to typescript
1 parent 8b59562 commit f50549e

File tree

12 files changed

+2661
-216
lines changed

12 files changed

+2661
-216
lines changed

packages/serverless-components/aws-lambda/__tests__/publishVersion.test.js renamed to packages/serverless-components/aws-lambda/__tests__/publishVersion.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { createComponent, createTmpDir } = require("../test-utils");
1+
import { createComponent, createTmpDir } from "../test-utils";
22

33
const {
44
mockCreateFunction,
@@ -19,9 +19,13 @@ jest.mock("aws-sdk", () => require("../__mocks__/aws-sdk.mock"));
1919
const mockIamRole = jest.fn();
2020
jest.mock("@serverless/aws-iam-role", () =>
2121
jest.fn(() => {
22-
const iamRole = mockIamRole;
23-
iamRole.init = () => {};
24-
iamRole.default = () => {};
22+
const iamRole: any = mockIamRole;
23+
iamRole.init = () => {
24+
// intentional
25+
};
26+
iamRole.default = () => {
27+
// intentional
28+
};
2529
iamRole.context = {};
2630
return iamRole;
2731
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript"
5+
]
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
clearMocks: true
3+
};

packages/serverless-components/aws-lambda/package.json

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,28 @@
1717
"url": "https://github.com/serverless-nextjs/serverless-next.js/issues"
1818
},
1919
"homepage": "https://github.com/serverless-nextjs/serverless-next.js#readme",
20+
"scripts": {
21+
"test": "jest",
22+
"clean": "yarn rimraf dist",
23+
"build": "tsc -p tsconfig.build.json",
24+
"prepare": "yarn clean && yarn build"
25+
},
26+
"devDependencies": {
27+
"@types/archiver": "^5.1.0",
28+
"@types/fs-extra": "^9.0.11",
29+
"@types/lodash": "^4.14.170",
30+
"@types/node": "^15.12.5",
31+
"@types/ramda": "^0.27.41",
32+
"jest": "^27.0.5",
33+
"rimraf": "^3.0.2",
34+
"typescript": "^4.3.4"
35+
},
2036
"dependencies": {
2137
"@serverless/aws-iam-role": "^1.0.0",
2238
"@serverless/aws-lambda-layer": "^1.0.0",
2339
"@serverless/aws-s3": "^4.2.0",
40+
"@serverless/core": "^1.1.2",
41+
"aws-sdk": "^2.935.0",
2442
"archiver": "^5.3.0",
2543
"fs-extra": "^9.1.0",
2644
"globby": "^11.0.1",
@@ -29,6 +47,10 @@
2947
},
3048
"peerDependencies": {
3149
"@serverless/core": "^1.1.2",
32-
"aws-sdk": "^2.702.0"
50+
"aws-sdk": "^2.935.0"
51+
},
52+
"jest": {
53+
"preset": "ts-jest",
54+
"testEnvironment": "node"
3355
}
3456
}
Lines changed: 2 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,3 @@
1-
const aws = require("aws-sdk");
2-
const AwsSdkLambda = aws.Lambda;
3-
const { mergeDeepRight, pick } = require("ramda");
4-
const { Component, utils } = require("@serverless/core");
5-
const {
6-
createLambda,
7-
updateLambdaCode,
8-
updateLambdaConfig,
9-
getLambda,
10-
deleteLambda,
11-
configChanged,
12-
pack
13-
} = require("./utils");
1+
const component = require("./dist/component.js");
142

15-
const outputsList = [
16-
"name",
17-
"hash",
18-
"description",
19-
"memory",
20-
"timeout",
21-
"code",
22-
"bucket",
23-
"shims",
24-
"handler",
25-
"runtime",
26-
"env",
27-
"role",
28-
"arn",
29-
"region",
30-
"tags"
31-
];
32-
33-
const defaults = {
34-
description: "AWS Lambda Component",
35-
memory: 512,
36-
timeout: 10,
37-
code: process.cwd(),
38-
bucket: undefined,
39-
shims: [],
40-
handler: "handler.hello",
41-
runtime: "nodejs10.x",
42-
env: {},
43-
region: "us-east-1",
44-
tags: undefined
45-
};
46-
47-
class AwsLambda extends Component {
48-
async default(inputs = {}) {
49-
this.context.status(`Deploying`);
50-
51-
const config = mergeDeepRight(defaults, inputs);
52-
53-
config.name = inputs.name || this.state.name || this.context.resourceId();
54-
config.tags = inputs.tags || this.state.tags;
55-
56-
this.context.debug(
57-
`Starting deployment of lambda ${config.name} to the ${config.region} region.`
58-
);
59-
60-
const lambda = new AwsSdkLambda({
61-
region: config.region,
62-
credentials: this.context.credentials.aws
63-
});
64-
65-
if (!config.role || !config.role.arn) {
66-
const awsIamRole = await this.load("@serverless/aws-iam-role");
67-
const outputsAwsIamRole = await awsIamRole(config.role);
68-
config.role = { arn: outputsAwsIamRole.arn };
69-
}
70-
71-
this.context.status("Packaging");
72-
this.context.debug(`Packaging lambda code from ${config.code}.`);
73-
config.zipPath = await pack(config.code, config.shims);
74-
75-
config.hash = await utils.hashFile(config.zipPath);
76-
77-
const prevLambda = await getLambda({ lambda, ...config });
78-
79-
if (!prevLambda) {
80-
this.context.status(`Creating`);
81-
this.context.debug(
82-
`Creating lambda ${config.name} in the ${config.region} region.`
83-
);
84-
85-
const createResult = await createLambda({ lambda, ...config });
86-
config.arn = createResult.arn;
87-
config.hash = createResult.hash;
88-
} else {
89-
config.arn = prevLambda.arn;
90-
91-
if (configChanged(prevLambda, config)) {
92-
if (prevLambda.hash !== config.hash) {
93-
this.context.status(`Uploading code`);
94-
this.context.debug(`Uploading ${config.name} lambda code.`);
95-
await updateLambdaCode({ lambda, ...config });
96-
}
97-
98-
this.context.status(`Updating`);
99-
this.context.debug(`Updating ${config.name} lambda config.`);
100-
101-
const updateResult = await updateLambdaConfig({ lambda, ...config });
102-
config.hash = updateResult.hash;
103-
}
104-
}
105-
106-
// todo we probably don't need this logic now that we auto generate names
107-
if (this.state.name && this.state.name !== config.name) {
108-
this.context.status(`Replacing`);
109-
await deleteLambda({ lambda, name: this.state.name });
110-
}
111-
112-
this.context.debug(
113-
`Successfully deployed lambda ${config.name} in the ${config.region} region.`
114-
);
115-
116-
const outputs = pick(outputsList, config);
117-
118-
this.state = outputs;
119-
await this.save();
120-
121-
return outputs;
122-
}
123-
124-
async publishVersion() {
125-
const { name, region, hash } = this.state;
126-
127-
const lambda = new AwsSdkLambda({
128-
region,
129-
credentials: this.context.credentials.aws
130-
});
131-
132-
const { Version } = await lambda
133-
.publishVersion({
134-
FunctionName: name,
135-
CodeSha256: hash
136-
})
137-
.promise();
138-
139-
return { version: Version };
140-
}
141-
142-
async remove() {
143-
this.context.status(`Removing`);
144-
145-
if (!this.state.name) {
146-
this.context.debug(`Aborting removal. Function name not found in state.`);
147-
return;
148-
}
149-
150-
const { name, region } = this.state;
151-
152-
const lambda = new AwsSdkLambda({
153-
region,
154-
credentials: this.context.credentials.aws
155-
});
156-
157-
const awsIamRole = await this.load("@serverless/aws-iam-role");
158-
159-
await awsIamRole.remove();
160-
161-
this.context.debug(`Removing lambda ${name} from the ${region} region.`);
162-
await deleteLambda({ lambda, name });
163-
this.context.debug(
164-
`Successfully removed lambda ${name} from the ${region} region.`
165-
);
166-
167-
const outputs = pick(outputsList, this.state);
168-
169-
this.state = {};
170-
await this.save();
171-
172-
return outputs;
173-
}
174-
}
175-
176-
module.exports = AwsLambda;
3+
module.exports = component.default;

0 commit comments

Comments
 (0)