23 lines
1009 B
TypeScript
23 lines
1009 B
TypeScript
import * as plugins from './smarthbs.plugins.js';
|
|
|
|
/**
|
|
* registers a directory of partials to make them available within handlebars compilation
|
|
*/
|
|
export let registerPartialDir = async (dirPathArg: string): Promise<void> => {
|
|
const dirPath = plugins.path.resolve(dirPathArg);
|
|
const hbsFileArray = await plugins.smartFs.directory(dirPath)
|
|
.recursive()
|
|
.filter((entry) => entry.isFile && entry.name.endsWith('.hbs'))
|
|
.list();
|
|
for (const hbsFile of hbsFileArray) {
|
|
const relativeFilePath = plugins.path.relative(dirPath, hbsFile.path);
|
|
const parsedPath = plugins.path.parse(relativeFilePath);
|
|
const hbsFileString = await plugins.smartFs.file(hbsFile.path).encoding('utf8').read() as string;
|
|
const partialDir = parsedPath.dir === '.' || parsedPath.dir === ''
|
|
? ''
|
|
: `${parsedPath.dir.split(plugins.path.sep).join('/')}/`;
|
|
const partialName = `partials/${partialDir}${parsedPath.name}`;
|
|
plugins.handlebars.registerPartial(partialName, hbsFileString);
|
|
}
|
|
};
|