UNPKG

2.78 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5const isStandaloneExecutable = require('../lib/utils/isStandaloneExecutable');
6
7if (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// global graceful-fs patch
16// https://github.com/isaacs/node-graceful-fs#global-patching
17const realFs = require('fs');
18const gracefulFs = require('graceful-fs');
19
20gracefulFs.gracefulify(realFs);
21
22const userNodeVersion = Number(process.version.split('.')[0].slice(1));
23
24// only check for components if user is running Node 8
25if (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
40require('essentials');
41
42const autocomplete = require('../lib/utils/autocomplete');
43const BbPromise = require('bluebird');
44const logError = require('../lib/classes/Error').logError;
45const uuid = require('uuid');
46
47if (process.env.SLS_DEBUG) {
48 // For performance reasons enabled only in SLS_DEBUG mode
49 BbPromise.config({
50 longStackTraces: true,
51 });
52}
53
54process.on('uncaughtException', error => logError(error, { forceExit: true }));
55
56process.noDeprecation = true;
57
58if (require('../lib/utils/tabCompletion/isSupported') && process.argv[2] === 'completion') {
59 autocomplete();
60 return;
61}
62
63let resolveServerlessExecutionSpan;
64require('../lib/utils/analytics').sendPending({
65 serverlessExecutionSpan: new BbPromise(resolve => (resolveServerlessExecutionSpan = resolve)),
66});
67
68const invocationId = uuid.v4();
69
70// requiring here so that if anything went wrong,
71// during require, it will be caught.
72const Serverless = require('../lib/Serverless');
73
74const serverless = new Serverless();
75
76serverless.invocationId = invocationId;
77
78serverless
79 .init()
80 .then(() => serverless.run())
81 .then(
82 () => resolveServerlessExecutionSpan(),
83 err => {
84 resolveServerlessExecutionSpan();
85 // If Enterprise Plugin, capture error
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 );