serverless/scripts/serverless.js

70 lines
1.9 KiB
JavaScript
Executable File

#!/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);
}
})();