smarthbs/dist/index.js

57 lines
5.2 KiB
JavaScript
Raw Normal View History

2017-02-19 03:12:17 +00:00
"use strict";
2017-02-26 01:08:25 +00:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
2017-03-14 18:33:04 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
2017-02-19 03:12:17 +00:00
require("typings-global");
const handlebars = require("handlebars");
const smartfile = require("smartfile");
2017-03-14 18:33:04 +00:00
const smartq = require("smartq");
2017-02-19 03:12:17 +00:00
const path = require("path");
2017-02-26 01:08:25 +00:00
/**
* registers a directory of partials to make them available within handlebars compilation
*/
2017-02-19 03:12:17 +00:00
exports.registerPartialDir = (dirPathArg) => {
2017-03-14 18:33:04 +00:00
let done = smartq.defer();
2017-02-19 03:12:17 +00:00
smartfile.fs.listFileTree(dirPathArg, '**/*.hbs').then(hbsFileArrayArg => {
for (let hbsFilePath of hbsFileArrayArg) {
let parsedPath = path.parse(hbsFilePath);
let hbsFileString = smartfile.fs.toStringSync(path.join(dirPathArg, hbsFilePath));
if (parsedPath.dir === '') {
parsedPath.name = '/' + parsedPath.name;
}
let partialName = `partials${parsedPath.dir}${parsedPath.name}`;
handlebars.registerPartial(partialName, hbsFileString);
2017-03-14 18:33:04 +00:00
done.resolve();
2017-02-19 03:12:17 +00:00
}
});
2017-03-14 18:33:04 +00:00
return done.promise;
2017-02-19 03:12:17 +00:00
};
2017-02-26 01:08:25 +00:00
/**
* compiles a directory and outputs it
*/
exports.compileDirectory = (originDirPathArg, destinationDirPathArg, dataFileNameArg) => __awaiter(this, void 0, void 0, function* () {
2017-02-19 03:12:17 +00:00
let hbsFilePathArray = smartfile.fs.listFilesSync(originDirPathArg, /.hbs/);
let data = smartfile.fs.toObjectSync(path.join(originDirPathArg, dataFileNameArg));
for (let hbsFilePath of hbsFilePathArray) {
let parsedPath = path.parse(hbsFilePath);
let hbsFileString = smartfile.fs.toStringSync(path.join(originDirPathArg, hbsFilePath));
let template = handlebars.compile(hbsFileString);
let output = template(data);
console.log('hi ' + output + ' hi');
smartfile.memory.toFsSync(output, path.join(destinationDirPathArg, parsedPath.name + '.html'));
}
2017-02-26 01:08:25 +00:00
});
/**
* get a template for a file on disk
*/
exports.getTemplateForFile = (filePathArg) => __awaiter(this, void 0, void 0, function* () {
let filePathAbsolute = path.resolve(filePathArg);
return handlebars.compile(smartfile.fs.toStringSync(filePathAbsolute));
});
2017-03-14 18:33:04 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsMEJBQXVCO0FBQ3ZCLHlDQUF3QztBQUN4Qyx1Q0FBc0M7QUFDdEMsaUNBQWdDO0FBQ2hDLDZCQUE0QjtBQUc1Qjs7R0FFRztBQUNRLFFBQUEsa0JBQWtCLEdBQUcsQ0FBQyxVQUFrQjtJQUNqRCxJQUFJLElBQUksR0FBRyxNQUFNLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDekIsU0FBUyxDQUFDLEVBQUUsQ0FBQyxZQUFZLENBQUMsVUFBVSxFQUFFLFVBQVUsQ0FBQyxDQUFDLElBQUksQ0FBQyxlQUFlO1FBQ3BFLEdBQUcsQ0FBQyxDQUFDLElBQUksV0FBVyxJQUFJLGVBQWUsQ0FBQyxDQUFDLENBQUM7WUFDeEMsSUFBSSxVQUFVLEdBQUcsSUFBSSxDQUFDLEtBQUssQ0FBQyxXQUFXLENBQUMsQ0FBQTtZ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