desirae.js/lib/utils.js

131 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-11-09 03:01:26 +00:00
(function (exports) {
"use strict";
2015-01-05 19:23:26 +00:00
2020-11-09 03:01:26 +00:00
var path = exports.path || require("path");
2015-01-05 19:23:26 +00:00
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function safeResolve(basename, target) {
basename = path.resolve(basename);
2020-11-09 03:01:26 +00:00
var targetname = path.resolve(basename, target),
re = new RegExp("^" + escapeRegExp(basename) + "(/|$)");
2015-01-05 19:23:26 +00:00
return re.test(targetname) && targetname;
}
exports.safeResolve = safeResolve;
exports.escapeRegExp = escapeRegExp;
2015-01-15 05:36:59 +00:00
function create(Desi) {
2020-11-09 03:01:26 +00:00
var fsapi = Desi.fsapi || require("./node-adapters").fsapi;
2015-01-15 05:36:59 +00:00
fsapi.getConfigs = function (confs) {
2020-11-09 03:01:26 +00:00
var opts = {
extensions: ["yml", "yaml", "json"],
dotfiles: false,
contents: true,
sha1sum: true,
};
2015-01-15 05:36:59 +00:00
return fsapi.getMeta(confs, opts).then(function (collections) {
2020-11-09 03:01:26 +00:00
var obj = {};
2015-01-15 05:36:59 +00:00
Object.keys(collections).forEach(function (key) {
2020-11-09 03:01:26 +00:00
var files = collections[key],
keyname = key.replace(/\.(json|ya?ml|\/)$/i, "");
2015-01-15 05:36:59 +00:00
obj[keyname] = obj[keyname] || {};
files.forEach(function (file) {
2020-11-09 03:01:26 +00:00
var filename = file.name.replace(/\.(json|ya?ml)$/i, ""),
data = {};
2015-01-15 05:36:59 +00:00
if (file.error) {
console.error(file);
console.error(file.error);
return;
}
if (/\.(ya?ml)$/i.test(file.name)) {
try {
data = Desi.YAML.parse(file.contents) || {};
if ("undefined" === data) {
data = {};
}
2020-11-09 03:01:26 +00:00
} catch (e) {
2015-01-15 05:36:59 +00:00
data = { error: e };
console.error("Could not parse yaml for " + filename);
console.error(file);
console.error(e);
}
2020-11-09 03:01:26 +00:00
} else if (/\.(json)$/i.test(file.name)) {
2015-01-15 05:36:59 +00:00
try {
data = JSON.parse(file.contents) || {};
2020-11-09 03:01:26 +00:00
} catch (e) {
2015-01-15 05:36:59 +00:00
data = { error: e };
console.error("Could not parse json for " + filename);
console.error(file);
console.error(e);
}
} else {
console.error("Not sure what to do with this one...");
console.error(file);
}
2017-04-07 07:45:24 +00:00
data = obj[keyname][filename] = data || obj[keyname][filename];
if (data) {
data.filename = file.name;
}
2015-01-15 05:36:59 +00:00
/*
if (!obj[keyname][filename]) {
obj[keyname][filename] = {};
}
Object.keys(data).forEach(function (key) {
obj[keyname][filename][key] = data[key];
});
*/
});
});
return obj;
});
};
fsapi.getAllPartials = function () {
2020-11-09 03:01:26 +00:00
return fsapi
.getConfigs(["partials", "partials.yml"])
.then(function (results) {
var partials = {};
Object.keys(results.partials).forEach(function (key) {
var partial = results.partials[key];
Object.keys(partial).forEach(function (prop) {
if (partials[prop]) {
console.warn("partial '" + prop + "' overwritten by " + key);
}
2015-01-15 05:36:59 +00:00
2020-11-09 03:01:26 +00:00
partials[prop] = partial[prop];
});
2015-01-15 05:36:59 +00:00
});
2020-11-09 03:01:26 +00:00
return partials;
});
2015-01-15 05:36:59 +00:00
};
fsapi.getAllConfigFiles = function () {
2020-11-09 03:01:26 +00:00
return fsapi
.getConfigs(["config.yml", "site.yml", "authors"])
.then(function (results) {
var authors = results.authors,
config = results.config.config,
site = results.site.site;
return { config: config, authors: authors, site: site };
});
2015-01-15 05:36:59 +00:00
};
return exports;
}
if (exports.Desirae) {
create(exports.Desirae);
2020-11-09 03:01:26 +00:00
} else {
2015-01-15 05:36:59 +00:00
exports.create = create;
}
2020-11-09 03:01:26 +00:00
})(("undefined" !== typeof exports && exports) || window);