barbells
Version:
[//]: # ( ns__file unit: standard, comp: README.md )
38 lines (37 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerPartials = exports.registerPartial = void 0;
const expandNsbbreviations_1 = require("./expandNsbbreviations");
const path = require('path');
const fs = require('fs-extra');
async function registerPartial(path, name, Handlebars) {
let templateString = '';
try {
templateString = await fs.readFile(path, 'utf-8');
}
catch (error) {
throw new Error(`couldn't read the partial file '${path}'`);
}
templateString = expandNsbbreviations_1.expandNsAbbreviations(templateString);
Handlebars.registerPartial(name, templateString);
}
exports.registerPartial = registerPartial;
async function registerPartials(dir, Handlebars) {
// console.log(`about to list partials in ${dir}`)
if (!await fs.pathExists(dir))
return;
const partials = await fs.readdir(dir);
await Promise.all(partials.map(async (fileName) => {
const filePath = `${dir}/${fileName}`;
const fileType = path.parse(filePath).ext;
if (fs.lstatSync(filePath).isDirectory()) {
await registerPartials(filePath, Handlebars);
return;
}
if (fileType === '.hbs' || fileType === '.handlebars') {
const partialName = path.parse(filePath).name;
await registerPartial(filePath, partialName, Handlebars);
}
}));
}
exports.registerPartials = registerPartials;