serverless/lib/Serverless.js
2016-07-25 09:58:15 +02:00

89 lines
2.8 KiB
JavaScript

'use strict';
require('shelljs/global');
const path = require('path');
const BbPromise = require('bluebird');
const CLI = require('./classes/CLI');
const Config = require('./classes/Config');
const YamlParser = require('./classes/YamlParser');
const PluginManager = require('./classes/PluginManager');
const Utils = require('./classes/Utils');
const Service = require('./classes/Service');
const SError = require('./classes/Error').SError;
const Version = require('./../package.json').version;
class Serverless {
constructor(config) {
let configObject = config;
configObject = configObject || {};
this.version = Version;
this.yamlParser = new YamlParser(this);
this.pluginManager = new PluginManager(this);
this.utils = new Utils(this);
this.service = new Service(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.PluginManager = PluginManager;
this.classes.Utils = Utils;
this.classes.Service = Service;
this.classes.Error = SError;
}
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(() => {
// set the provider of the service (so that the PluginManager takes care to
// execute the correct provider specific plugins)
this.pluginManager.setProvider(this.service.provider.name || this.service.provider);
// load all plugins
this.pluginManager.loadAllPlugins(this.service.plugins);
// give the CLI the plugins so that it can print out plugin information
// such as options when the user enters --help
this.cli.setLoadedPlugins(this.pluginManager.getPlugins());
});
}
run() {
// check if tracking is enabled (and track if it's enabled)
const serverlessPath = this.config.serverlessPath;
if (!this.utils.fileExistsSync(path.join(serverlessPath, 'do-not-track'))) {
this.utils.track(this);
}
if (!this.cli.displayHelp(this.processedInput) && this.processedInput.commands.length) {
// trigger the plugin lifecycle when there's something which should be processed
return this.pluginManager.run(this.processedInput.commands);
}
return BbPromise.resolve();
}
getVersion() {
return this.version;
}
}
module.exports = Serverless;