Files
smarthbs/ts/smarthbs.compile.ts
T

32 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2022-07-24 15:30:19 +02:00
import * as plugins from './smarthbs.plugins.js';
2017-03-19 17:14:28 +01:00
/**
* compiles a directory and outputs it
*/
export let compileDirectory = async (
originDirPathArg: string,
destinationDirPathArg: string,
dataFileNameArg: string
) => {
const originDirPath = plugins.path.resolve(originDirPathArg);
const destinationDirPath = plugins.path.resolve(destinationDirPathArg);
const hbsFileArray = await plugins.smartFs.directory(originDirPath)
.filter((entry) => entry.isFile && entry.name.endsWith('.hbs'))
.list();
const dataFileString = await plugins.smartFs.file(
plugins.path.join(originDirPath, dataFileNameArg)
).encoding('utf8').read() as string;
const data = JSON.parse(dataFileString) as Record<string, unknown>;
for (const hbsFile of hbsFileArray) {
const parsedPath = plugins.path.parse(hbsFile.path);
const hbsFileString = await plugins.smartFs.file(hbsFile.path).encoding('utf8').read() as string;
const template = plugins.handlebars.compile(hbsFileString);
const output = template(data);
console.log('hi ' + output + ' hi');
await plugins.smartFs.directory(destinationDirPath).create();
await plugins.smartFs.file(
plugins.path.join(destinationDirPath, parsedPath.name + '.html')
).encoding('utf8').write(output);
2017-03-19 17:14:28 +01:00
}
};