desirae.js/lib/node-adapters/fsapi-real.js

68 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-11-09 03:01:26 +00:00
"use strict";
2015-01-15 06:35:58 +00:00
function create(Desi, options) {
2020-11-09 03:01:26 +00:00
var fsapi = Desi.fsapi;
2015-01-16 00:19:25 +00:00
options.blogdir = options.blogdir || options.working_path;
2015-01-15 06:35:58 +00:00
fsapi.getMeta = function (dirnames, opts) {
opts = opts || {};
2020-11-09 03:01:26 +00:00
var extensions = "",
dotfiles = "",
contents = "",
sha1sum = "";
2015-01-15 06:35:58 +00:00
if (Array.isArray(opts.extensions)) {
2020-11-09 03:01:26 +00:00
extensions = "&extensions=" + opts.extensions.join(","); // md,markdown,jade,htm,html
2015-01-15 06:35:58 +00:00
}
if (opts.dotfiles) {
2020-11-09 03:01:26 +00:00
dotfiles = "&dotfiles=true";
2015-01-15 06:35:58 +00:00
}
if (opts.contents) {
2020-11-09 03:01:26 +00:00
contents = "&contents=true";
2015-01-15 06:35:58 +00:00
}
if (false === opts.sha1sum) {
2020-11-09 03:01:26 +00:00
sha1sum = "&sha1sum=false";
2015-01-15 06:35:58 +00:00
}
return fsapi.walk.walkDirs(options.blogdir, dirnames, opts);
};
fsapi.getContents = function (filepaths) {
return fsapi.getfs(options.blogdir, filepaths);
};
fsapi.getCache = function () {
2020-11-09 03:01:26 +00:00
return fs
.readFileAsync(options.blogdir, "/cache.json")
.catch(function (/*e*/) {
return {};
})
.then(function (obj) {
return obj;
});
2015-01-15 06:35:58 +00:00
};
fsapi.copy = function (files) {
// TODO size
return fsapi.copyfs(options.blogdir, files);
};
2015-01-16 00:19:25 +00:00
fsapi.putFiles = function (files, opts) {
2015-01-15 06:35:58 +00:00
files.forEach(function (file) {
2020-11-09 03:01:26 +00:00
if (!file.contents || "string" === typeof file.contents) {
2015-01-15 06:35:58 +00:00
return;
}
if (/\.json$/i.test(file.path)) {
file.contents = JSON.stringify(file.contents);
2020-11-09 03:01:26 +00:00
} else if (/\.ya?ml$/i.test(file.path)) {
file.contents = Desi.YAML.stringify(file.contents);
2015-01-15 06:35:58 +00:00
}
});
// TODO size
2015-01-16 00:19:25 +00:00
return fsapi.putfs(options.blogdir, files, opts);
2015-01-15 06:35:58 +00:00
};
}
exports.create = create;