mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
143 lines
2.5 KiB
JavaScript
143 lines
2.5 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Serverless State Class
|
|
*/
|
|
|
|
const SError = require('./ServerlessError'),
|
|
SUtils = require('./utils/index'),
|
|
_ = require('lodash'),
|
|
path = require('path'),
|
|
fs = require('fs');
|
|
|
|
class ServerlessState {
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
|
|
constructor(Serverless) {
|
|
|
|
this._S = Serverless;
|
|
this.meta = new this._S.classes.Meta(this._S);
|
|
}
|
|
|
|
/**
|
|
* Load
|
|
* - Load from source (i.e., file system);
|
|
* - Returns promise
|
|
*/
|
|
|
|
load() {
|
|
|
|
let _this = this;
|
|
|
|
return _this._S.getProject().load()
|
|
.then(function() {
|
|
return _this.meta.load();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Save
|
|
* - Load from source (i.e., file system);
|
|
*/
|
|
|
|
save() {
|
|
|
|
let _this = this;
|
|
|
|
return _this._S.getProject().save({ deep: true })
|
|
.then(function() {
|
|
return _this.meta.save({ deep: true });
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Set
|
|
* - Set data from a javascript object
|
|
*/
|
|
|
|
set(data) {
|
|
this.meta = data.meta ? this.meta.set(data.meta) : this.meta;
|
|
this.project = data.project ? this._S.getProject().set(data.project, { deep: true }) : this._S.getProject();
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Get
|
|
* - Returns clone of data
|
|
*/
|
|
|
|
get() {
|
|
return {
|
|
meta: this.meta.get(),
|
|
project: this._S.getProject().get()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Populated
|
|
* - Returns clone of data
|
|
*/
|
|
|
|
getPopulated(options) {
|
|
|
|
options = options || {};
|
|
|
|
// Validate: Check Stage & Region
|
|
if (!options.stage || !options.region) throw new SError('Both "stage" and "region" params are required');
|
|
|
|
return {
|
|
meta: this.meta.get(),
|
|
project: this._S.getProject().getPopulated(options)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Meta
|
|
* - Returns meta data from state
|
|
*/
|
|
|
|
getMeta() {
|
|
return this.meta;
|
|
}
|
|
|
|
/**
|
|
* Get Stages
|
|
* - Returns array of stages in project
|
|
*/
|
|
|
|
getStages() {
|
|
return this.meta.getStages();
|
|
}
|
|
|
|
/**
|
|
* Get Regions (in stage)
|
|
* - Returns array of regions in a stage
|
|
*/
|
|
|
|
getRegions(stage) {
|
|
return this.meta.getRegions(stage);
|
|
}
|
|
|
|
/**
|
|
* Validate Stage Exists
|
|
* - Checks to see if a stage exists in your project
|
|
*/
|
|
|
|
validateStageExists(stage) {
|
|
return this.meta.validateStageExists(stage);
|
|
}
|
|
|
|
/**
|
|
* Validate Region Exists
|
|
* - Checks to see if a stage exists in your project
|
|
*/
|
|
|
|
validateRegionExists(stage, region) {
|
|
return this.meta.validateRegionExists(stage, region);
|
|
}
|
|
}
|
|
|
|
module.exports = ServerlessState; |