From 4e9d65e253e3d4bf25a9c750e4d9de453c6f7f56 Mon Sep 17 00:00:00 2001 From: ac360 Date: Mon, 11 Jan 2016 14:51:42 -0800 Subject: [PATCH] ServerlessState: create class and add to Serverless instance --- lib/Serverless.js | 25 ++++++++++++------ lib/ServerlessState.js | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 lib/ServerlessState.js diff --git a/lib/Serverless.js b/lib/Serverless.js index 1788af039..bb5fca432 100644 --- a/lib/Serverless.js +++ b/lib/Serverless.js @@ -28,7 +28,8 @@ class Serverless { constructor(config) { // Add version - this._version = require('./../package.json').version; + this._version = require('./../package.json').version; + this._run = false; // Set Default Config this.config = { @@ -46,6 +47,7 @@ class Serverless { this.hooks = {}; this.commands = {}; this.classes = { + State: require('./ServerlessState'), Meta: require('./ServerlessMeta'), Project: require('./ServerlessProject'), Component: require('./ServerlessComponent'), @@ -158,7 +160,6 @@ class Serverless { // Set CLI _this.cli = { - set: false, context: null, action: null, options: {}, @@ -314,12 +315,20 @@ class Serverless { let _this = this; - // If CLI and first Action, prepare evt object - if (_this.cli && !_this.cli.set) { - evt = { - options: extend(_this.cli.options, _this.cli.params) - }; - _this.cli.set = true; + // If first Action, init + if (!_this._run) { + + // Init State + _this.S.state = new _this.S.classes.State(_this.S); + + // If CLI command, init CLI + if (_this.cli) { + evt = { + options: extend(_this.cli.options, _this.cli.params) + }; + } + + _this._run = true; } // If not CLI and no options object, auto-set options diff --git a/lib/ServerlessState.js b/lib/ServerlessState.js new file mode 100644 index 000000000..630b407a0 --- /dev/null +++ b/lib/ServerlessState.js @@ -0,0 +1,57 @@ +'use strict'; + +/** + * Serverless State Class + */ + +const SError = require('./ServerlessError'), + SUtils = require('./utils/index'), + _ = require('lodash'), + path = require('path'), + fs = require('fs'), + BbPromise = require('bluebird'); + +class ServerlessState +{ + + /** + * Constructor + */ + + constructor(Serverless) { + this.S = Serverless; + this.load(); + } + + /** + * Load + * - Load from source (i.e., file system); + */ + + load() { + this.data = { + meta: this.S.classes.Meta(this.S), + project: this.S.classes.Project(this.S) + } + } + + /** + * Get + * - Returns clone of data + */ + + get() { + return _.cloneDeep(this.data); + } + + /** + * Set + * - Set data + */ + + set(data) { + this.data = _.merge(this.data, data); + } +} + +module.exports = ServerlessMeta; \ No newline at end of file