-
Notifications
You must be signed in to change notification settings - Fork 412
/
validate.js
330 lines (288 loc) · 12.2 KB
/
validate.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict';
const BbPromise = require('bluebird');
const path = require('path');
const fse = require('fs-extra');
const glob = require('glob');
const lib = require('./index');
const _ = require('lodash');
const Configuration = require('./Configuration');
const { getAllNodeFunctions, isNodeRuntime, isProviderGoogle } = require('./utils');
/**
* For automatic entry detection we sort the found files to solve ambiguities.
* This should cover most of the cases. For complex setups the user should
* build his own entries with help of the other exports.
*/
const preferredExtensions = ['.js', '.ts', '.jsx', '.tsx'];
module.exports = {
validate() {
const getHandlerFileAndFunctionName = functionDefinition => {
const { handler: handlerProp, image: imageProp, entrypoint: entryPoint } = functionDefinition;
// CASE: The lambda handler is inside lambda layer. Use functionDefinition `entrypoint`
// to define the original handler.
if (entryPoint) return entryPoint;
if (handlerProp) {
return handlerProp;
}
if (imageProp && typeof imageProp == 'string') return imageProp;
if (!imageProp || !imageProp.command || imageProp.command.length < 1) {
const docsLink = 'https://www.serverless.com/blog/container-support-for-lambda';
throw new this.serverless.classes.Error(
`Either function.handler or function.image must be defined. Pass the handler name (i.e. 'index.handler') as the value for function.image.command[0]. For help see: ${docsLink}`
);
}
return imageProp.command[0];
};
const getHandlerFile = handler => {
// Check if handler is a well-formed path based handler.
const handlerEntry = /(.*)\..*?$/.exec(handler);
if (handlerEntry) {
return handlerEntry[1];
}
};
const getEntryExtension = fileName => {
const files = glob.sync(`${fileName}.*`, {
cwd: this.serverless.config.servicePath,
nodir: true,
ignore: this.configuration.excludeFiles ? this.configuration.excludeFiles : undefined
});
if (_.isEmpty(files)) {
// If we cannot find any handler we should terminate with an error
throw new this.serverless.classes.Error(
`No matching handler found for '${fileName}' in '${this.serverless.config.servicePath}'. Check your service definition.`
);
}
// Move preferred file extensions to the beginning
const sortedFiles = _.uniq(
_.concat(
_.sortBy(
_.filter(files, file => _.includes(preferredExtensions, path.extname(file))),
a => _.size(a)
),
files
)
);
if (_.size(sortedFiles) > 1) {
if (this.log) {
this.log.warning(`More than one matching handlers found for "${fileName}". Using "${_.first(sortedFiles)}"`);
} else {
this.serverless.cli.log(
`WARNING: More than one matching handlers found for '${fileName}'. Using '${_.first(sortedFiles)}'.`
);
}
}
return path.extname(_.first(sortedFiles));
};
const getEntryForFunction = (name, serverlessFunction) => {
const handler = getHandlerFileAndFunctionName(serverlessFunction);
const handlerFile = getHandlerFile(handler);
if (!handlerFile) {
if (!isProviderGoogle(this.serverless)) {
if (this.log) {
this.log.warning();
this.log.warning(
`Entry for ${name}@${handler} could not be retrieved.\nPlease check your service config if you want to use lib.entries.`
);
} else {
this.serverless.cli.log(
`\nWARNING: Entry for ${name}@${handler} could not be retrieved.\nPlease check your service config if you want to use lib.entries.`
);
}
}
return {};
}
const ext = getEntryExtension(handlerFile);
// Create a valid entry key
return {
[handlerFile]: `./${handlerFile}${ext}`
};
};
// Initialize plugin configuration
this.configuration = new Configuration(this.serverless.service.custom);
if (this.log) {
this.log.verbose(`Using configuration:\n${JSON.stringify(this.configuration, null, 2)}`);
} else {
this.options.verbose &&
this.serverless.cli.log(`Using configuration:\n${JSON.stringify(this.configuration, null, 2)}`);
}
if (this.configuration.hasLegacyConfig) {
if (this.log) {
this.log.warning('Legacy configuration detected. Consider to use "custom.webpack" as object (see README).');
} else {
this.serverless.cli.log(
'Legacy configuration detected. Consider to use "custom.webpack" as object (see README).'
);
}
}
this.webpackConfig = this.configuration.config || this.configuration.webpackConfig;
if (this.webpackConfig.includeModules && this.webpackConfig.packagerOptions.noInstall) {
throw new this.serverless.classes.Error(
'"includeModules" requires an installation, and cannot be used with "packagerOptions.noInstall".'
);
}
// Expose entries - must be done before requiring the webpack configuration
const entries = {};
const functions = getAllNodeFunctions.call(this);
if (this.options.function) {
const serverlessFunction = this.serverless.service.getFunction(this.options.function);
const entry = getEntryForFunction.call(this, this.options.function, serverlessFunction);
_.merge(entries, entry);
} else {
_.forEach(functions, (func, index) => {
const loadedFunc = this.serverless.service.getFunction(func);
const runtime = loadedFunc.runtime || this.serverless.service.provider.runtime || 'nodejs';
if (isNodeRuntime(runtime)) {
// runtimes can be 'nodejsX.Y' (AWS, Azure) or 'google-nodejs' (Google Cloud)
const entry = getEntryForFunction.call(this, functions[index], loadedFunc);
_.merge(entries, entry);
}
if (runtime === 'provided' && loadedFunc.allowCustomRuntime) {
// allow custom runtime if the user has specified it
const entry = getEntryForFunction.call(this, functions[index], loadedFunc);
_.merge(entries, entry);
}
});
}
// Expose service file and options
lib.serverless = this.serverless;
lib.options = this.options;
lib.entries = entries;
if (_.isString(this.webpackConfig)) {
const webpackConfigFilePath = path.join(this.serverless.config.servicePath, this.webpackConfig);
if (!this.serverless.utils.fileExistsSync(webpackConfigFilePath)) {
return BbPromise.reject(
new this.serverless.classes.Error(
'The webpack plugin could not find the configuration file at: ' + webpackConfigFilePath
)
);
}
try {
const webpackConfig = require(webpackConfigFilePath);
this.webpackConfig = webpackConfig.default || webpackConfig;
} catch (err) {
if (this.log) {
this.log.error(`Could not load webpack config "${webpackConfigFilePath}"`);
} else {
this.serverless.cli.log(`Could not load webpack config '${webpackConfigFilePath}'`);
}
return BbPromise.reject(err);
}
}
// Intermediate function to handle async webpack config
const processConfig = _config => {
this.webpackConfig = _config;
// Default context
if (!this.webpackConfig.context) {
this.webpackConfig.context = this.serverless.config.servicePath;
}
// Default target
if (!this.webpackConfig.target) {
this.webpackConfig.target = 'node';
}
// Default output
if (!this.webpackConfig.output || _.isEmpty(this.webpackConfig.output)) {
const outputPath = path.join(this.serverless.config.servicePath, '.webpack');
this.webpackConfig.output = {
libraryTarget: 'commonjs',
path: outputPath,
filename: '[name].js'
};
}
// Default node
if (!this.webpackConfig.node || _.isEmpty(this.webpackConfig.node)) {
this.webpackConfig.node = false;
}
// Custom output path
if (this.options.out) {
this.webpackConfig.output.path = path.join(this.serverless.config.servicePath, this.options.out);
}
this.skipCompile =
_.get(this.serverless, 'service.custom.webpack.noBuild') === true || _.get(this.options, 'skip-build') === true;
// Skip compilation with --skip-build or noBuild
if (this.skipCompile) {
if (this.log) {
this.log('Skipping build and using existing compiled output');
} else {
this.serverless.cli.log('Skipping build and using existing compiled output');
}
if (!fse.pathExistsSync(this.webpackConfig.output.path)) {
return BbPromise.reject(new this.serverless.classes.Error('No compiled output found'));
}
this.keepOutputDirectory = true;
}
if (!this.keepOutputDirectory) {
if (this.log) {
this.log.verbose(`Removing ${this.webpackConfig.output.path}`);
} else {
this.options.verbose && this.serverless.cli.log(`Removing ${this.webpackConfig.output.path}`);
}
fse.removeSync(this.webpackConfig.output.path);
}
this.webpackOutputPath = this.webpackConfig.output.path;
// In case of individual packaging we have to create a separate config for each function
if (_.has(this.serverless, 'service.package') && this.serverless.service.package.individually) {
if (this.log) {
this.log.verbose(
`Individually packaging with concurrency at ${this.configuration.concurrency} entries a time.`
);
} else {
this.options.verbose &&
this.serverless.cli.log(
`Individually packaging with concurrency at ${this.configuration.concurrency} entries a time.`
);
}
if (this.webpackConfig.entry && !_.isEqual(this.webpackConfig.entry, entries)) {
return BbPromise.reject(
new this.serverless.classes.Error(
'Webpack entry must be automatically resolved when package.individually is set to true. ' +
'In webpack.config.js, remove the entry declaration or set entry to slsw.lib.entries.'
)
);
}
// Lookup associated Serverless functions
const allEntryFunctions = _.map(getAllNodeFunctions.call(this), funcName => {
const func = this.serverless.service.getFunction(funcName);
const handler = getHandlerFileAndFunctionName(func);
const handlerFile = path.relative('.', getHandlerFile(handler));
return {
handlerFile,
funcName,
func
};
});
this.entryFunctions = _.flatMap(entries, (value, key) => {
const entry = path.relative('.', value);
const entryFile = _.replace(entry, new RegExp(`${path.extname(entry)}$`), '');
const entryFuncs = _.filter(allEntryFunctions, ['handlerFile', entryFile]);
if (_.isEmpty(entryFuncs)) {
// We have to make sure that for each entry there is an entry function item.
entryFuncs.push({});
}
_.forEach(entryFuncs, entryFunc => {
entryFunc.entry = {
key,
value
};
});
return entryFuncs;
});
this.webpackConfig = _.map(this.entryFunctions, entryFunc => {
const config = _.cloneDeep(this.webpackConfig);
config.entry = {
[entryFunc.entry.key]: entryFunc.entry.value
};
const compileName = entryFunc.funcName || _.camelCase(entryFunc.entry.key);
config.output.path = path.join(config.output.path, compileName);
return config;
});
} else {
this.webpackConfig.output.path = path.join(this.webpackConfig.output.path, 'service');
}
return BbPromise.resolve();
};
// Webpack config can be a Promise, If it's a Promise wait for resolved config object.
if (this.webpackConfig && _.isFunction(this.webpackConfig.then)) {
return BbPromise.resolve(this.webpackConfig.then(config => processConfig(config)));
}
return processConfig(this.webpackConfig);
}
};