1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const isStandaloneExecutable = require('../lib/utils/isStandaloneExecutable');
|
6 |
|
7 | if (isStandaloneExecutable) {
|
8 | require('../lib/utils/standalone-patch');
|
9 | if (process.argv[2] === 'binary-postinstall' && process.argv.length === 3) {
|
10 | require('../scripts/postinstall');
|
11 | return;
|
12 | }
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 | const realFs = require('fs');
|
18 | const gracefulFs = require('graceful-fs');
|
19 |
|
20 | gracefulFs.gracefulify(realFs);
|
21 |
|
22 | const userNodeVersion = Number(process.version.split('.')[0].slice(1));
|
23 |
|
24 |
|
25 | if (userNodeVersion >= 8) {
|
26 | const componentsV1 = require('../lib/components-v1');
|
27 | const componentsV2 = require('../lib/components-v2');
|
28 |
|
29 | if (componentsV1 && componentsV1.runningComponents()) {
|
30 | componentsV1.runComponents();
|
31 | return;
|
32 | }
|
33 |
|
34 | if (componentsV2 && componentsV2.runningComponents()) {
|
35 | componentsV2.runComponents();
|
36 | return;
|
37 | }
|
38 | }
|
39 |
|
40 | require('essentials');
|
41 |
|
42 | const autocomplete = require('../lib/utils/autocomplete');
|
43 | const BbPromise = require('bluebird');
|
44 | const logError = require('../lib/classes/Error').logError;
|
45 | const uuid = require('uuid');
|
46 |
|
47 | if (process.env.SLS_DEBUG) {
|
48 |
|
49 | BbPromise.config({
|
50 | longStackTraces: true,
|
51 | });
|
52 | }
|
53 |
|
54 | process.on('uncaughtException', error => logError(error, { forceExit: true }));
|
55 |
|
56 | process.noDeprecation = true;
|
57 |
|
58 | if (require('../lib/utils/tabCompletion/isSupported') && process.argv[2] === 'completion') {
|
59 | autocomplete();
|
60 | return;
|
61 | }
|
62 |
|
63 | let resolveServerlessExecutionSpan;
|
64 | require('../lib/utils/analytics').sendPending({
|
65 | serverlessExecutionSpan: new BbPromise(resolve => (resolveServerlessExecutionSpan = resolve)),
|
66 | });
|
67 |
|
68 | const invocationId = uuid.v4();
|
69 |
|
70 |
|
71 |
|
72 | const Serverless = require('../lib/Serverless');
|
73 |
|
74 | const serverless = new Serverless();
|
75 |
|
76 | serverless.invocationId = invocationId;
|
77 |
|
78 | serverless
|
79 | .init()
|
80 | .then(() => serverless.run())
|
81 | .then(
|
82 | () => resolveServerlessExecutionSpan(),
|
83 | err => {
|
84 | resolveServerlessExecutionSpan();
|
85 |
|
86 | let enterpriseErrorHandler = null;
|
87 | serverless.pluginManager.plugins.forEach(p => {
|
88 | if (p.enterprise && p.enterprise.errorHandler) {
|
89 | enterpriseErrorHandler = p.enterprise.errorHandler;
|
90 | }
|
91 | });
|
92 | if (!enterpriseErrorHandler) {
|
93 | logError(err);
|
94 | return null;
|
95 | }
|
96 | return enterpriseErrorHandler(err, invocationId)
|
97 | .catch(error => {
|
98 | process.stdout.write(`${error.stack}\n`);
|
99 | })
|
100 | .then(() => {
|
101 | logError(err);
|
102 | });
|
103 | }
|
104 | );
|