Skip to content

Commit

Permalink
chore: copied in source of handlebars-directory to avoid a runtime fa…
Browse files Browse the repository at this point in the history
…ilure on node 4
  • Loading branch information
kevinwright committed Sep 28, 2018
1 parent 9758511 commit 1789bdd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
"license": "ISC",
"dependencies": {
"handlebars": "^4.0.8",
"handlebars-directory": "^1.0.0",
"marked": "^0.3.6",
"minimist": "^1.2.0",
"moment": "^2.18.1",
"snyk": "^1.99.0",
"source-map-support": "^0.5.7",
"viz.js": "^2.0.0"
"viz.js": "^2.0.0",
"web-template-file-tree": "^1.1.1"
},
"bin": {
"snyk-to-html": "dist/index.js"
Expand Down
78 changes: 78 additions & 0 deletions src/lib/handlebars-directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

// originally forked from https://github.com/reykjavikingur/node-handlebars-directory
// under the terms of the ISC licence:

// Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby
// granted, provided that the above copyright notice and this permission notice appear in all copies.

// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING
// ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
// DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
// USE OR PERFORMANCE OF THIS SOFTWARE.

import assert = require('assert');
import Handlebars = require('handlebars');
import FileTree = require('web-template-file-tree');

export function HandlebarsDirectory(path, extension, options?) {
assert(Boolean(path), 'missing required argument for directory path');
assert(Boolean(extension), 'missing required argument for file extension');
if (!options) {
options = {};
}
const handlebars = options.handlebars || Handlebars;
const loadDirectory = HandlebarsDirectoryLoader(path, extension, handlebars);

return (filePath, data) => {
return loadDirectory()
.then((hbs) => {
return hbs.compile('{{>' + filePath + '}}')(data);
});
};
}

function HandlebarsDirectoryLoader(path, extension, handlebars) {
assert(Boolean(handlebars), 'missing required argument for handlebars');
const loaded = false;
const loadFileTree = FileTreeLoader(path, extension);
return () => {
if (loaded) {
return Promise.resolve(handlebars);
} else {
return loadFileTree()
.then((fileTree) => {
registerFileTree(handlebars, fileTree);
return handlebars;
});
}
};
}

function FileTreeLoader(path, extension) {
const fileTree = new FileTree(path, {extension});
const fileTreeLoaded = false;
return () => {
return new Promise((resolve, reject) => {
if (fileTreeLoaded) {
resolve(fileTree);
} else {
fileTree.load((err) => {
if (err) {
reject(err);
} else {
resolve(fileTree);
}
});
}
});
};
}

function registerFileTree(handlebars, fileTree) {
for (const path in fileTree.cache) {
if (path) {
handlebars.registerPartial(path, fileTree.cache[path]);
}
}
}
3 changes: 2 additions & 1 deletion src/lib/hbars.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Handlebars = require('handlebars');
import HandlebarsDirectory = require('handlebars-directory');
import {HandlebarsDirectory} from './handlebars-directory';

import marked = require('marked');
import moment = require('moment');
import path = require('path');
Expand Down

0 comments on commit 1789bdd

Please sign in to comment.