bugfix Promise and fs

This commit is contained in:
AJ ONeal 2020-11-08 20:48:56 -07:00
parent 215cef976f
commit 5ee8ea9b60
7 changed files with 25 additions and 28 deletions

View File

@ -242,7 +242,7 @@ JavaScript module (or make requests to an API service that renders them for you)
```javascript ```javascript
var slim = exports.slimjs || require("slimjs"); var slim = exports.slimjs || require("slimjs");
function render(contentstr /*, desi*/) { function render(contentstr /*, desi*/) {
return PromiseA.resolve(slim(contentstr)); return Promise.resolve(slim(contentstr));
} }
Desirae.registerRenderer(".slim", render); Desirae.registerRenderer(".slim", render);

View File

@ -5,7 +5,7 @@
function create(Desi) { function create(Desi) {
// Chrome, Firefox, and even MSIE11+ all support crypto // Chrome, Firefox, and even MSIE11+ all support crypto
var crypto = window.crypto || window.msCrypto, var crypto = window.crypto || window.msCrypto,
PromiseA = window.Promise, Promise = window.Promise,
algos; algos;
// convenience mappings for common digest algorithms // convenience mappings for common digest algorithms
@ -111,7 +111,7 @@
function request() {} function request() {}
request.get = function (url /*, query*/) { request.get = function (url /*, query*/) {
// Return a new promise. // Return a new promise.
return new PromiseA(function (resolve, reject) { return new Promise(function (resolve, reject) {
// Do the usual XHR stuff // Do the usual XHR stuff
var req = new XMLHttpRequest(); var req = new XMLHttpRequest();
req.onload = function () { req.onload = function () {
@ -139,7 +139,7 @@
}; };
request.post = function (url /*, query*/, body) { request.post = function (url /*, query*/, body) {
// Return a new promise. // Return a new promise.
return new PromiseA(function (resolve, reject) { return new Promise(function (resolve, reject) {
// Do the usual XHR stuff // Do the usual XHR stuff
var req = new XMLHttpRequest(); var req = new XMLHttpRequest();
req.onload = function () { req.onload = function () {

View File

@ -1,7 +1,7 @@
"use strict"; "use strict";
var PromiseA = require("bluebird").Promise, var fs = require('fs').promises;
fs = PromiseA.promisifyAll(require("fs"));
function create(Desi, options) { function create(Desi, options) {
var fsapi = Desi.fsapi; var fsapi = Desi.fsapi;
options.blogdir = options.blogdir || options.working_path; options.blogdir = options.blogdir || options.working_path;
@ -35,7 +35,7 @@ function create(Desi, options) {
fsapi.getCache = function () { fsapi.getCache = function () {
return fs return fs
.readFileAsync(options.blogdir, "/cache.json") .readFile(options.blogdir, "/cache.json")
.catch(function (/*e*/) { .catch(function (/*e*/) {
return {}; return {};
}) })

View File

@ -10,7 +10,7 @@ var safeResolve = require("../utils").safeResolve;
var sha1sum = function (str) { var sha1sum = function (str) {
return require("secret-utils").hashsum("sha1", str); return require("secret-utils").hashsum("sha1", str);
}; };
var fsExtra = PromiseA.promisifyAll(require("fs.extra")); var copyAll = require("util").promisify(require("fs.extra").copy);
//, tmpdir = require('os').tmpdir() //, tmpdir = require('os').tmpdir()
function strip(prefix, pathname) { function strip(prefix, pathname) {
@ -48,7 +48,7 @@ function walkDir(parent, sub, opts) {
return true; return true;
} }
return new PromiseA(function (resolve) { return new Promise(function (resolve) {
var walker = walk.walk(trueRoot); var walker = walk.walk(trueRoot);
walker.on("nodeError", function (filepath, stat, next) { walker.on("nodeError", function (filepath, stat, next) {
//stats.forEach(function (stat) { //stats.forEach(function (stat) {
@ -76,7 +76,7 @@ function walkDir(parent, sub, opts) {
var file; var file;
if (!filter(stat.name)) { if (!filter(stat.name)) {
return PromiseA.resolve(); return Promise.resolve();
} }
file = { file = {
@ -93,12 +93,12 @@ function walkDir(parent, sub, opts) {
files.push(file); files.push(file);
if (!(opts.sha1sum || opts.content)) { if (!(opts.sha1sum || opts.content)) {
return PromiseA.resolve(); return Promise.resolve();
} }
// TODO stream sha1 (for assets) // TODO stream sha1 (for assets)
return fs return fs
.readFileAsync(path.join(root, stat.name), null) .readFile(path.join(root, stat.name), null)
.then(function (buffer) { .then(function (buffer) {
var contents = buffer.toString("utf8"); var contents = buffer.toString("utf8");
file.sha1 = sha1sum(contents); file.sha1 = sha1sum(contents);
@ -144,7 +144,7 @@ function getfs(blogdir, filepaths) {
return forEachAsync(filepaths, function (filepath) { return forEachAsync(filepaths, function (filepath) {
var pathname = safeResolve(blogdir, filepath); var pathname = safeResolve(blogdir, filepath);
return fs return fs
.lstatAsync(pathname) .lstat(pathname)
.then(function (stat) { .then(function (stat) {
return fs.readFile(pathname, null).then(function (buffer) { return fs.readFile(pathname, null).then(function (buffer) {
files.push({ files.push({
@ -208,7 +208,7 @@ function copyfs(blogdir, files) {
//, filename = path.basename(dest) //, filename = path.basename(dest)
dirpaths[pathname] = true; dirpaths[pathname] = true;
return PromiseA.resolve(); return Promise.resolve();
}) })
.then(function () { .then(function () {
// TODO is it better to do this lazy-like or as a batch? // TODO is it better to do this lazy-like or as a batch?
@ -223,8 +223,7 @@ function copyfs(blogdir, files) {
.then(function () { .then(function () {
// TODO allow delete? // TODO allow delete?
return forEachAsync(sources, function (source) { return forEachAsync(sources, function (source) {
return fsExtra return copyAll(
.copyAsync(
safeResolve(blogdir, source), safeResolve(blogdir, source),
safeResolve(blogdir, files[source]), safeResolve(blogdir, files[source]),
{ replace: true } { replace: true }
@ -276,7 +275,7 @@ function putfs(blogdir, files, options) {
dirpaths[pathname] = true; dirpaths[pathname] = true;
return PromiseA.resolve(); return Promise.resolve();
}) })
.then(function () { .then(function () {
// TODO is it better to do this lazy-like or as a batch? // TODO is it better to do this lazy-like or as a batch?
@ -304,13 +303,9 @@ function putfs(blogdir, files, options) {
return forEachAsync(files, function (file) { return forEachAsync(files, function (file) {
// TODO use lastModifiedDate as per client request? // TODO use lastModifiedDate as per client request?
// TODO compare sha1 sums for integrity // TODO compare sha1 sums for integrity
// NOTE existsAsync is backwards
return fs return fs
.existsAsync(file.realPath) .access(file.realPath)
.then(function () { .then(function () {
return fs.writeFile(file.realPath, file.contents, "utf8");
})
.catch(function (/*exists*/) {
if (file.delete || !file.contents) { if (file.delete || !file.contents) {
return fs.unlink(file.realPath); return fs.unlink(file.realPath);
} }
@ -321,6 +316,9 @@ function putfs(blogdir, files, options) {
return fs.writeFile(file.realPath, file.contents, "utf8"); return fs.writeFile(file.realPath, file.contents, "utf8");
}) })
.catch(function () {
return fs.writeFile(file.realPath, file.contents, "utf8");
})
.catch(function (e) { .catch(function (e) {
putfsResults.errors.push({ putfsResults.errors.push({
type: "file", type: "file",

View File

@ -2,13 +2,12 @@
(function (exports) { (function (exports) {
"use strict"; "use strict";
var PromiseA = exports.Promise || require("bluebird").Promise;
function renderMd(contentstr /*, desi*/) { function renderMd(contentstr /*, desi*/) {
var markitdown = (exports.markdownit || require("markdown-it"))({ var markitdown = (exports.markdownit || require("markdown-it"))({
html: true, html: true,
linkify: true, linkify: true,
}); });
return PromiseA.resolve( return Promise.resolve(
markitdown.render(contentstr) markitdown.render(contentstr)
//.replace('"', '"') //.replace('"', '"')
//.replace(''', "'") //.replace(''', "'")
@ -18,7 +17,7 @@
function renderNoop(contentstr /*, desi*/) { function renderNoop(contentstr /*, desi*/) {
// hmmm... that was easy // hmmm... that was easy
return PromiseA.resolve(contentstr); return Promise.resolve(contentstr);
} }
function renderJade(contentstr, desi, options) { function renderJade(contentstr, desi, options) {
@ -30,7 +29,7 @@
var jade = exports.jade || require("jade"), var jade = exports.jade || require("jade"),
fn = jade.compile(contentstr, options), fn = jade.compile(contentstr, options),
html = fn(desi); html = fn(desi);
return PromiseA.resolve(html); return Promise.resolve(html);
} }
exports.DesiraeRenderMarkdown = renderMd.DesiraeRenderMarkdown = renderMd; exports.DesiraeRenderMarkdown = renderMd.DesiraeRenderMarkdown = renderMd;

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "desirae", "name": "desirae",
"version": "0.12.0", "version": "0.12.3",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "desirae", "name": "desirae",
"version": "0.12.0", "version": "0.12.3",
"description": "An in-browser static blog library and static site generator. Similar to Jekyll, Octopress, Nanoc, etc", "description": "An in-browser static blog library and static site generator. Similar to Jekyll, Octopress, Nanoc, etc",
"main": "desirae.js", "main": "desirae.js",
"scripts": { "scripts": {