This repository was archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (49 loc) · 1.72 KB
/
index.js
File metadata and controls
62 lines (49 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const path = require('path');
const cproc = require('child_process');
class PackageNodeNativeAwsLambda {
constructor(serverless, options) {
const { config: { servicePath } } = serverless;
this.servicePath = servicePath;
this.modulesPath = path.join(servicePath, 'node_modules');
this.serverless = serverless;
this.hooks = {
'before:package:createDeploymentArtifacts': () => this.beforeDeploy(),
'after:package:finalize': () => this.afterFinalize()
};
this.handleExit();
}
get nativeModules() {
return this.serverless.service.custom.nativeModules;
}
get nativeModuleImageTag() {
return this.serverless.service.custom.nativeModuleImageTag || 'latest';
}
get nativeModuleImage() {
return 'iopipe/awslambda-npm-install';
}
beforeDeploy() {
const image = `${this.nativeModuleImage}:${this.nativeModuleImageTag}`;
const baseCmd = `docker run -v ${this.servicePath}:/var/task ${image}`;
return Promise.all(this.nativeModules.map(module => {
return new Promise((resolve, reject) => {
// Install native module with awslambda-npm-install (via Docker)
cproc.exec(`${baseCmd} ${module}`, error => { error ? reject(error) : resolve() });
});
}));
}
afterFinalize() {
return Promise.all(this.nativeModules.map(module => {
return new Promise((resolve, reject) => {
// Rebuild native module with local node
cproc.exec(`npm rebuild ${module}`, error => { error ? reject(error) : resolve() });
});
}));
}
handleExit() {
['SIGINT', 'SIGTERM', 'SIGQUIT'].forEach(
signal => process.on(signal, () => { this.afterFinalize(); })
);
}
}
module.exports = PackageNodeNativeAwsLambda;