-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda.js
More file actions
93 lines (77 loc) · 2.2 KB
/
Copy pathlambda.js
File metadata and controls
93 lines (77 loc) · 2.2 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint-disable global-require, import/no-unresolved, no-underscore-dangle */
const path = require('path');
const chalk = require('chalk');
module.exports = (handler, webpackConf) => {
console.log(chalk.green('Running nova:lambda'));
const webpack = require('webpack');
const ServerlessOffline = require('serverless-offline/src/ServerlessOffline');
const [serverConf] = webpackConf.filter((conf) => conf.target === 'node');
if (!serverConf) {
throw Error('Missing webpack configuration with target set as "node"');
}
const compiler = webpack({
...serverConf,
output: {
...(serverConf.output || {}),
libraryTarget: 'commonjs',
},
mode: 'development',
});
const outputPath = (serverConf.output && serverConf.output.path) || path.join(process.pwd(), 'dist');
let fileName = (serverConf.output && serverConf.output.fileName) || 'server.js';
fileName = fileName.split('.').shift();
const handlerPath = `${path.relative(process.cwd(), outputPath)}/${fileName}`;
let isWatching = false;
const serverless = {
version: '1.48.1',
service: {
service: 'hypernova-lambda',
provider: {
runtime: 'nodejs8.10',
},
functions: {
batch: {
handler: `${handlerPath}.${handler}`,
events: [
{
http: {
path: 'batch',
method: 'POST',
},
},
],
},
},
},
cli: {
log: (...args) => console.log(chalk.yellow(...args)),
},
config: {
servicePath: process.cwd(),
},
};
serverless.service.getFunction = (key) => {
const fun = serverless.service.functions[key];
if (!fun) {
return null;
}
return {
...fun,
name: key,
};
};
compiler.watch({}, (error) => {
if (error) {
throw error;
}
if (isWatching) {
console.log(chalk.yellow('Lambda code compiled.'));
return Promise.resolve();
}
const slsOffline = new ServerlessOffline(serverless, { stage: 'ara', region: 'local' });
isWatching = true;
return Promise.resolve(slsOffline._buildServer())
.then((server) => server.start());
});
return compiler;
};