class Serverless {
constructor(config) {
let configObject = config;
configObject = configObject || {};
this.providers = {};
this.version = Version;
this.yamlParser = new YamlParser(this);
this.utils = new Utils(this);
this.service = new Service(this);
this.variables = new Variables(this);
this.pluginManager = new PluginManager(this);
// use the servicePath from the options or try to find it in the CWD
configObject.servicePath = configObject.servicePath || this.utils.findServicePath();
this.config = new Config(this, configObject);
this.classes = {};
this.classes.CLI = CLI;
this.classes.YamlParser = YamlParser;
this.classes.Utils = Utils;
this.classes.Service = Service;
this.classes.Variables = Variables;
this.classes.Error = ServerlessError;
this.classes.PluginManager = PluginManager;
this.serverlessDirPath = path.join(os.homedir(), '.serverless');
}
init() {
// create a new CLI instance
this.cli = new CLI(this);
// get an array of commands and options that should be processed
this.processedInput = this.cli.processInput();
// set the options and commands which were processed by the CLI
this.pluginManager.setCliOptions(this.processedInput.options);
this.pluginManager.setCliCommands(this.processedInput.commands);
return this.service.load(this.processedInput.options)
.then(() => {
// load all plugins
this.pluginManager.loadAllPlugins(this.service.plugins);
this.pluginManager.autoloadServerlessAlphaPlugin();
// give the CLI the plugins and commands so that it can print out
// information such as options when the user enters --help
this.cli.setLoadedPlugins(this.pluginManager.getPlugins());
this.cli.setLoadedCommands(this.pluginManager.getCommands());
});
}
run() {
this.utils.logStat(this).catch(() => BbPromise.resolve());
if (this.cli.displayHelp(this.processedInput)) {
return BbPromise.resolve();
}
// make sure the command exists before doing anything else
this.pluginManager.validateCommand(this.processedInput.commands);
// populate variables after --help, otherwise help may fail to print
// (https://github.com/serverless/serverless/issues/2041)
this.variables.populateService(this.pluginManager.cliOptions);
// populate function names after variables are loaded in case functions were externalized
// (https://github.com/serverless/serverless/issues/2997)
this.service.setFunctionNames(this.processedInput.options);
// validate the service configuration, now that variables are loaded
this.service.validate();
// trigger the plugin lifecycle when there's something which should be processed
return this.pluginManager.run(this.processedInput.commands);
}
setProvider(name, provider) {
this.providers[name] = provider;
}
getProvider(name) {
return this.providers[name] ? this.providers[name] : false;
}
getVersion() {
return this.version;
}
}n/a
class ServerlessError extends Error {
constructor(message, statusCode) {
super(message);
this.name = this.constructor.name;
this.message = message;
this.statusCode = statusCode;
Error.captureStackTrace(this, this.constructor);
}
}n/a
class ServerlessError extends Error {
constructor(message, statusCode) {
super(message);
this.name = this.constructor.name;
this.message = message;
this.statusCode = statusCode;
Error.captureStackTrace(this, this.constructor);
}
}n/a
(e) => {
try {
const errorType = e.name.replace(/([A-Z])/g, ' $1');
writeMessage(errorType, e.message);
if (e.name !== 'ServerlessError') {
const errorMessage = [
' ',
' For debugging logs, run again after setting the',
' "SLS_DEBUG=*" environment variable.',
].join('');
consoleLog(chalk.red(errorMessage));
consoleLog(' ');
}
if (process.env.SLS_DEBUG) {
consoleLog(chalk.yellow(' Stack Trace --------------------------------------------'));
consoleLog(' ');
consoleLog(e.stack);
consoleLog(' ');
}
consoleLog(chalk.yellow(' Get Support --------------------------------------------'));
consoleLog(`${chalk.yellow(' Docs: ')}${chalk.white('docs.serverless.com')}`);
consoleLog(`${chalk.yellow(' Bugs: ')}${chalk
.white('github.com/serverless/serverless/issues')}`);
consoleLog(`${chalk.yellow(' Forums: ')}${chalk.white('forum.serverless.com')}`);
consoleLog(`${chalk.yellow(' Chat: ')}${chalk
.white('gitter.im/serverless/serverless')}`);
consoleLog(' ');
consoleLog(chalk.yellow(' Your Environment Information -----------------------------'));
consoleLog(chalk.yellow(` OS: ${process.platform}`));
consoleLog(chalk.yellow(` Node Version: ${process.version.replace(/^[v|V]/, '')}`));
consoleLog(chalk.yellow(` Serverless Version: ${version}`));
consoleLog(' ');
// Failure exit
process.exit(1);
} catch (errorHandlingError) {
throw new Error(e);
}
}n/a
(message) => {
writeMessage('Serverless Warning', message);
}n/a