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

70 lignes
1.6 KiB
JavaScript
Brut Vue normale Historique

2020-11-09 03:01:26 +00:00
"use strict";
2015-01-15 06:35:58 +00:00
2020-11-09 03:48:56 +00:00
var fs = require('fs').promises;
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
2020-11-09 03:48:56 +00:00
.readFile(options.blogdir, "/cache.json")
2020-11-09 03:01:26 +00:00
.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;