1 | 'use strict';
|
2 |
|
3 | const path = require('path');
|
4 | const BbPromise = require('bluebird');
|
5 | const os = require('os');
|
6 | const chalk = require('chalk');
|
7 | const updateNotifier = require('update-notifier');
|
8 | const pkg = require('../package.json');
|
9 | const CLI = require('./classes/CLI');
|
10 | const Config = require('./classes/Config');
|
11 | const YamlParser = require('./classes/YamlParser');
|
12 | const PluginManager = require('./classes/PluginManager');
|
13 | const Utils = require('./classes/Utils');
|
14 | const Service = require('./classes/Service');
|
15 | const Variables = require('./classes/Variables');
|
16 | const ServerlessError = require('./classes/Error').ServerlessError;
|
17 | const Version = require('./../package.json').version;
|
18 | const isStandaloneExecutable = require('./utils/isStandaloneExecutable');
|
19 | const resolveCliInput = require('./utils/resolveCliInput');
|
20 |
|
21 | const installationMaintananceCommands = new Set(['uninstall', 'upgrade']);
|
22 |
|
23 | class Serverless {
|
24 | constructor(config) {
|
25 | let configObject = config;
|
26 | configObject = configObject || {};
|
27 |
|
28 | this.providers = {};
|
29 |
|
30 | this.version = Version;
|
31 |
|
32 | this.yamlParser = new YamlParser(this);
|
33 | this.utils = new Utils(this);
|
34 | this.service = new Service(this);
|
35 | this.variables = new Variables(this);
|
36 | this.pluginManager = new PluginManager(this);
|
37 |
|
38 |
|
39 | this.cliInputArgv = process.argv.slice(2);
|
40 | configObject.servicePath =
|
41 | configObject.servicePath ||
|
42 | this.utils.findServicePath(resolveCliInput(this.cliInputArgv).options.config);
|
43 |
|
44 | this.config = new Config(this, configObject);
|
45 |
|
46 | this.classes = {};
|
47 | this.classes.CLI = CLI;
|
48 | this.classes.YamlParser = YamlParser;
|
49 | this.classes.Utils = Utils;
|
50 | this.classes.Service = Service;
|
51 | this.classes.Variables = Variables;
|
52 | this.classes.Error = ServerlessError;
|
53 | this.classes.PluginManager = PluginManager;
|
54 |
|
55 | this.serverlessDirPath = path.join(os.homedir(), '.serverless');
|
56 | this.isStandaloneExecutable = isStandaloneExecutable;
|
57 | }
|
58 |
|
59 | init() {
|
60 |
|
61 | this.instanceId = new Date().getTime().toString();
|
62 |
|
63 |
|
64 | this.cli = new this.classes.CLI(this, this.cliInputArgv);
|
65 |
|
66 |
|
67 | this.processedInput = this.cli.processInput();
|
68 |
|
69 |
|
70 | return this.pluginManager
|
71 | .loadConfigFile()
|
72 | .then(() => {
|
73 |
|
74 | this.pluginManager.setCliOptions(this.processedInput.options);
|
75 | this.pluginManager.setCliCommands(this.processedInput.commands);
|
76 |
|
77 | if (!installationMaintananceCommands.has(this.processedInput.commands[0])) {
|
78 |
|
79 | const notifier = updateNotifier({ pkg });
|
80 | notifier.notify({
|
81 | message:
|
82 | isStandaloneExecutable && notifier.update
|
83 | ? `Update available ${chalk.dim(notifier.update.current)}${chalk.reset(
|
84 | ' → '
|
85 | )}${chalk.green(notifier.update.latest)} \nRun ${chalk.cyan(
|
86 | 'serverless upgrade'
|
87 | )} to update`
|
88 | : null,
|
89 | });
|
90 | }
|
91 |
|
92 | return this.service.load(this.processedInput.options);
|
93 | })
|
94 | .then(() => {
|
95 |
|
96 | return this.pluginManager.loadAllPlugins(this.service.plugins);
|
97 | })
|
98 | .then(() => {
|
99 |
|
100 |
|
101 | this.cli.setLoadedPlugins(this.pluginManager.getPlugins());
|
102 | this.cli.setLoadedCommands(this.pluginManager.getCommands());
|
103 | return this.pluginManager.updateAutocompleteCacheFile();
|
104 | });
|
105 | }
|
106 |
|
107 | run() {
|
108 | this.utils.logStat(this).catch(() => BbPromise.resolve());
|
109 |
|
110 | if (this.cli.displayHelp(this.processedInput)) {
|
111 | return BbPromise.resolve();
|
112 | }
|
113 | this.cli.suppressLogIfPrintCommand(this.processedInput);
|
114 |
|
115 |
|
116 | this.pluginManager.validateCommand(this.processedInput.commands);
|
117 |
|
118 |
|
119 |
|
120 | return this.variables.populateService(this.pluginManager.cliOptions).then(() => {
|
121 |
|
122 |
|
123 | this.service.mergeArrays();
|
124 |
|
125 |
|
126 |
|
127 | this.service.setFunctionNames(this.processedInput.options);
|
128 |
|
129 |
|
130 | this.service.validate();
|
131 |
|
132 |
|
133 | return this.pluginManager.run(this.processedInput.commands);
|
134 | });
|
135 | }
|
136 |
|
137 | setProvider(name, provider) {
|
138 | this.providers[name] = provider;
|
139 | }
|
140 |
|
141 | getProvider(name) {
|
142 | return this.providers[name] ? this.providers[name] : false;
|
143 | }
|
144 |
|
145 | getVersion() {
|
146 | return this.version;
|
147 | }
|
148 | }
|
149 |
|
150 | module.exports = Serverless;
|