serverless/lib/ServerlessFunction.js
ac360 96f1af8661 Many updates:
Project/Stage/RegionCreate: Finish refactor
Classes: Rely on Serverless Project Path, instead of passing in class
Utils.populate: Fix
Utils.getResoruces: Finish
ResourcesDeploy: Finish refactor
2015-12-31 14:35:46 -08:00

110 lines
2.3 KiB
JavaScript

'use strict';
/**
* Serverless Function Class
* - options.path format is: "moduleFolder/functionFolder#functionName"
*/
const SError = require('./ServerlessError'),
SUtils = require('./utils/index'),
extend = require('util')._extend,
path = require('path'),
fs = require('fs'),
_ = require('lodash'),
BbPromise = require('bluebird');
class ServerlessFunction {
/**
* Constructor
*/
constructor(Serverless, options) {
this.S = Serverless;
this.options = options || {};
this.load(options);
}
/**
* Load
* - Load from source (i.e., file system);
*/
load(module, func) {
let _this = this;
if (module) {
_this.options.module = module;
_this.options.function = func;
_this.options.functionPath = path.join(
_this.S._projectRootPath,
'back',
'modules',
_this.options.module,
'functions',
_this.options.function,
's-function.json');
}
// TODO: Validate function exists
// Defaults
_this.data = {};
_this.data.endpoints = [];
// If no function path exists, return
if (!_this.options.functionPath) return;
let functionJson = SUtils.readAndParseJsonSync(path.join(_this.options.functionPath, 's-function.json'));
_this.data = extend(_this.data, functionJson);
}
/**
* Get
* - Return data
*/
get() {
return this.data;
}
/**
* getPopulated
* - Fill in templates then variables
*/
getPopulated(stage, region) {
let data = this.get();
data = SUtils.populate(data, this.S._projectRootPath, stage, region);
}
/**
* save
* - Saves data to file system
*/
save() {
let _this = this;
// If file exists, do a diff and skip if equal
if (SUtils.fileExistsSync(path.join(_this.options.functionPath, 's-function.json'))) {
let functionJson = SUtils.readAndParseJsonSync(path.join(_this.options.functionPath, 's-function.json'));
// check if data changed
if (_.isEqual(functionJson, this.data)) return;
}
// overwrite function JSON file
fs.writeFileSync(path.join(_this.options.functionPath, 's-function.json'),
JSON.stringify(this.data, null, 2));
return;
}
}
module.exports = ServerlessFunction;