fix(build): migrate filesystem access to smartfs and tighten TypeScript compatibility
This commit is contained in:
+16
-20
@@ -3,24 +3,20 @@ import * as plugins from './smarthbs.plugins.js';
|
||||
/**
|
||||
* registers a directory of partials to make them available within handlebars compilation
|
||||
*/
|
||||
export let registerPartialDir = (dirPathArg: string): Promise<any> => {
|
||||
let done = plugins.smartpromise.defer();
|
||||
plugins.smartfile.fs.listFileTree(dirPathArg, '**/*.hbs').then((hbsFileArrayArg) => {
|
||||
for (let hbsFilePath of hbsFileArrayArg) {
|
||||
let parsedPath = plugins.path.parse(hbsFilePath);
|
||||
let hbsFileString = plugins.smartfile.fs.toStringSync(
|
||||
plugins.path.join(dirPathArg, hbsFilePath)
|
||||
);
|
||||
if (parsedPath.dir === '/') {
|
||||
parsedPath.dir = '';
|
||||
}
|
||||
if (parsedPath.dir !== '') {
|
||||
parsedPath.dir = parsedPath.dir + '/';
|
||||
}
|
||||
let partialName = `partials/${parsedPath.dir}${parsedPath.name}`;
|
||||
plugins.handlebars.registerPartial(partialName, hbsFileString);
|
||||
done.resolve();
|
||||
}
|
||||
});
|
||||
return done.promise;
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user