-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
serverless.js
executable file
·69 lines (56 loc) · 1.94 KB
/
serverless.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
#!/usr/bin/env node
'use strict';
require('essentials');
// global graceful-fs patch
// https://github.com/isaacs/node-graceful-fs#global-patching
require('graceful-fs').gracefulify(require('fs'));
if (require('../lib/utils/tabCompletion/isSupported') && process.argv[2] === 'completion') {
require('../lib/utils/autocomplete')();
return;
}
const handleError = require('../lib/cli/handle-error');
let serverless;
process.once('uncaughtException', (error) =>
handleError(error, {
isUncaughtException: true,
isLocallyInstalled: serverless && serverless.isLocallyInstalled,
})
);
const processSpanPromise = (async () => {
try {
const wait = require('timers-ext/promise/sleep');
await wait(); // Ensure access to "processSpanPromise"
require('../lib/utils/analytics').sendPending({
serverlessExecutionSpan: processSpanPromise,
});
if (await require('../lib/cli/eventually-list-version')()) return;
const uuid = require('uuid');
const invocationId = uuid.v4();
const Serverless = require('../lib/Serverless');
serverless = new Serverless();
try {
serverless.onExitPromise = processSpanPromise;
serverless.invocationId = invocationId;
await serverless.init();
if (serverless.invokedInstance) serverless = serverless.invokedInstance;
await serverless.run();
} catch (error) {
// If Enterprise Plugin, capture error
let enterpriseErrorHandler = null;
serverless.pluginManager.plugins.forEach((p) => {
if (p.enterprise && p.enterprise.errorHandler) {
enterpriseErrorHandler = p.enterprise.errorHandler;
}
});
if (!enterpriseErrorHandler) throw error;
try {
await enterpriseErrorHandler(error, invocationId);
} catch (enterpriseErrorHandlerError) {
process.stdout.write(`${enterpriseErrorHandlerError.stack}\n`);
}
throw error;
}
} catch (error) {
handleError(error);
}
})();