UNPKG

iopa-template-handlebars

Version:

Handlebars Template Engine for the IOPA Stack for Node.js

89 lines (82 loc) 2.61 kB
/* * Copyright (c) 2016 Internet of Protocols Alliance (IOPA) * Portions copyright (c) 2014, Yahoo Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var fs = require('fs'); var path = require('path'); if (typeof Object.assign != 'function') { (function () { Object.assign = function (target) { 'use strict'; if (target === undefined || target === null) { throw new TypeError('Cannot convert undefined or null to object'); } var output = Object(target); for (var index = 1; index < arguments.length; index++) { var source = arguments[index]; if (source !== undefined && source !== null) { for (var nextKey in source) { if (source.hasOwnProperty(nextKey)) { output[nextKey] = source[nextKey]; } } } } return output; }; })(); } function passError(callback) { return function (reason) { setImmediate(function () { callback(reason); }); }; } function passValue(callback) { return function (value) { setImmediate(function () { callback(null, value); }); }; } function walkSubFolders(dir, extname, done) { var self = this; var results = []; fs.readdir(dir, function(err, list) { if (err) return done(err); var pending = list.length; if (!pending) return done(null, results); list.forEach(function(file) { file = path.resolve(dir, file); fs.stat(file, function(err, stat) { if (stat && stat.isDirectory()) { walkSubFolders(file, extname, function(err, res) { results = results.concat(res); if (!--pending) done(null, results); }); } else { if (path.extname(file) == extname) results.push(file); if (!--pending) done(null, results); } }); }); }); }; exports.assign = Object.assign; exports.passError = passError; exports.passValue = passValue; exports.walkSubFolders = walkSubFolders;