Merge pull request #935 from HyperBrain/929-add-yaml-template-support

#929 Added YAML to serializer
This commit is contained in:
Eslam λ Hefnawy 2016-04-07 19:30:21 +07:00
commit 996bdac8dc
2 changed files with 54 additions and 26 deletions

View File

@ -6,6 +6,7 @@ const SError = require('./Error'),
glob = require('glob'),
async = require('async'),
path = require('path'),
yaml = require('js-yaml'),
_ = require('lodash');
// TODO: Add Variables de/serialization methods
@ -44,15 +45,22 @@ module.exports = function(S) {
// Load Templates
.then(function () {
let templatesFilePath = project.getRootPath('s-templates.json');
return BbPromise.mapSeries([ 's-templates.json', 's-templates.yaml' ], (filename) => {
// Load Templates
let templatesFilePath = project.getRootPath(filename);
if (S.utils.fileExistsSync(templatesFilePath)) {
let templates = new S.classes.Templates({}, templatesFilePath);
return templates.load()
.then(function (instance) {
project.setTemplates(instance);
});
}
if (S.utils.fileExistsSync(templatesFilePath)) {
let template = new S.classes.Templates({}, templatesFilePath);
return template.load();
}
return null;
})
.filter((template) => { return template != null })
.then(function (templates) {
if (templates.length > 0) {
project.setTemplates(templates[0]);
}
});
})
.then(function () {
S.utils.sDebug(`deserializeProject: Load functions`);
@ -212,16 +220,22 @@ module.exports = function(S) {
})
.then(function () {
// Load Templates
let templatesFilePath = func.getRootPath('s-templates.json');
return BbPromise.mapSeries([ 's-templates.json', 's-templates.yaml' ], (filename) => {
// Load Templates
let templatesFilePath = func.getRootPath(filename);
if (S.utils.fileExistsSync(templatesFilePath)) {
let template = new S.classes.Templates({}, templatesFilePath);
return template.load()
.then(function (template) {
func.setTemplate(template);
});
}
if (S.utils.fileExistsSync(templatesFilePath)) {
let template = new S.classes.Templates({}, templatesFilePath);
return template.load();
}
return null;
})
.filter(template => { return template != null })
.then(function (templates) {
if (templates.length > 0) {
func.setTemplate(templates[0]);
}
});
})
.then(function () {
return func;
@ -427,7 +441,12 @@ module.exports = function(S) {
if (!S.hasProject()) throw new SError('Templates could not be loaded because no project path has been set on Serverless instance');
// Set Data
templates.fromObject(S.utils.readFileSync(templates.getFilePath()));
let filePath = templates.getFilePath();
if (_.endsWith(filePath, '.yaml')) {
templates.fromObject(yaml.safeLoad(S.utils.readFileSync(filePath)));
} else {
templates.fromObject(S.utils.readFileSync(filePath));
}
})
.then(function () {
@ -445,13 +464,18 @@ module.exports = function(S) {
while (notRoot) {
parentDir = path.dirname(parentDir);
notRoot = !S.utils.fileExistsSync(path.join(parentDir, 's-project.json'));
if (notRoot && S.utils.fileExistsSync(path.join(parentDir, 's-templates.json'))) {
parents.push(new S.classes.Templates(
S.utils.readFileSync(path.join(parentDir, 's-templates.json')),
path.join(parentDir, 's-templates.json')))
if (notRoot) {
if (S.utils.fileExistsSync(path.join(parentDir, 's-templates.json'))) {
parents.push(new S.classes.Templates(
S.utils.readFileSync(path.join(parentDir, 's-templates.json')),
path.join(parentDir, 's-templates.json')))
} else if (S.utils.fileExistsSync(path.join(parentDir, 's-templates.yaml'))) {
parents.push(new S.classes.Templates(
yaml.safeLoad(S.utils.readFileSync(path.join(parentDir, 's-templates.yaml'))),
path.join(parentDir, 's-templates.yaml')))
}
}
}
templates.setParents(parents);
})
.then(function () {
@ -475,14 +499,17 @@ module.exports = function(S) {
// Validate: Check project path is set
if (!S.hasProject()) throw new SError('Templates could not be saved because no project path has been set on Serverless instance');
let tempaltesObj = templates.toObject()
let templatesObj = templates.toObject()
if (_.isEmpty(tempaltesObj)) {
if (_.isEmpty(templatesObj)) {
if (S.utils.fileExistsSync(templates.getFilePath())) {
return fs.unlinkAsync(templates.getFilePath());
}
} else {
return S.utils.writeFile(templates.getFilePath(), tempaltesObj);
if (_.endsWith(templates.getFilePath(), '.yaml')) {
return S.utils.writeFile(templates.getFilePath(), yaml.safeDump(templatesObj));
}
return S.utils.writeFile(templates.getFilePath(), templatesObj);
}
})

View File

@ -53,6 +53,7 @@
"fs-extra": "^0.26.4",
"glob": "^7.0.0",
"https-proxy-agent": "^1.0.0",
"js-yaml": "^3.5.5",
"json-diff": "^0.3.1",
"keypress": "^0.2.1",
"lodash": "^4.2.1",