ensure the path to the decree file exists before attempting to write to it

pull/1/head
ansuz 4 years ago
parent c2918ac888
commit 998f2267c4

@ -140,18 +140,25 @@ var Path = require("path");
var readFileBin = require("./stream-file").readFileBin; var readFileBin = require("./stream-file").readFileBin;
var Util = require("./common-util"); var Util = require("./common-util");
var Schedule = require("./schedule"); var Schedule = require("./schedule");
var Fse = require("fs-extra");
var nThen = require("nthen");
Decrees.load = function (Env, cb) { Decrees.load = function (Env, cb) {
Env.scheduleDecree = Env.scheduleDecree || Schedule(); Env.scheduleDecree = Env.scheduleDecree || Schedule();
var decreeName = Path.join(Env.paths.decree, 'decree.ndjson'); // XXX mkdirp
var stream = Fs.createReadStream(decreeName, {start: 0});
var handler = Decrees.createLineHandler(Env);
Env.scheduleDecree.blocking('', function (unblock) { Env.scheduleDecree.blocking('', function (unblock) {
var done = Util.once(Util.both(cb, unblock)); var done = Util.once(Util.both(cb, unblock));
nThen(function (w) {
// ensure that the path to the decree log exists
Fse.mkdirp(Env.paths.decree, w(function (err) {
if (!err) { return; }
w.abort();
done(err);
}));
}).nThen(function () {
var decreeName = Path.join(Env.paths.decree, 'decree.ndjson');
var stream = Fs.createReadStream(decreeName, {start: 0});
var handler = Decrees.createLineHandler(Env);
readFileBin(stream, function (msgObj, next) { readFileBin(stream, function (msgObj, next) {
var text = msgObj.buff.toString('utf8'); var text = msgObj.buff.toString('utf8');
try { try {
@ -164,6 +171,7 @@ Decrees.load = function (Env, cb) {
done(err); done(err);
}); });
}); });
});
}; };
Decrees.write = function (Env, decree, _cb) { Decrees.write = function (Env, decree, _cb) {

Loading…
Cancel
Save