fix(build): migrate filesystem access to smartfs and tighten TypeScript compatibility

This commit is contained in:
2026-04-30 10:20:07 +00:00
parent a71a53092b
commit 8c6e8d9c96
14 changed files with 7298 additions and 3761 deletions
+1 -1
View File
@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smarthbs',
version: '3.0.4',
version: '3.0.5',
description: 'Enhances Handlebars with advanced filesystem support, template compilation, and efficient management of partials and variables.'
}
+18 -16
View File
@@ -8,22 +8,24 @@ export let compileDirectory = async (
destinationDirPathArg: string,
dataFileNameArg: string
) => {
let hbsFilePathArray = plugins.smartfile.fs.listFilesSync(originDirPathArg, /.hbs/);
let data = plugins.smartfile.fs.toObjectSync(
plugins.path.join(originDirPathArg, dataFileNameArg)
);
for (let hbsFilePath of hbsFilePathArray) {
let parsedPath = plugins.path.parse(hbsFilePath);
let hbsFileString = plugins.smartfile.fs.toStringSync(
plugins.path.join(originDirPathArg, hbsFilePath)
);
let template = plugins.handlebars.compile(hbsFileString);
let output = template(data);
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.smartfile.fs.ensureDir(destinationDirPathArg);
plugins.smartfile.memory.toFsSync(
output,
plugins.path.join(destinationDirPathArg, parsedPath.name + '.html')
);
await plugins.smartFs.directory(destinationDirPath).create();
await plugins.smartFs.file(
plugins.path.join(destinationDirPath, parsedPath.name + '.html')
).encoding('utf8').write(output);
}
};
+7 -4
View File
@@ -4,7 +4,7 @@ import * as plugins from './smarthbs.plugins.js';
* Helper:
* Allows you to analyze a context
*/
plugins.handlebars.registerHelper('__analyze', (analyzeContext) => {
plugins.handlebars.registerHelper('__analyze', (analyzeContext: unknown) => {
if (typeof analyzeContext === 'string') {
if (plugins.handlebars.partials[analyzeContext]) {
console.log(`The analyzed partial ${analyzeContext} looks like this`);
@@ -20,12 +20,15 @@ plugins.handlebars.registerHelper('__analyze', (analyzeContext) => {
* Helper:
* logs all registered partials to console
*/
plugins.handlebars.registerHelper('__allPartialsLog', (analyzeContext) => {
plugins.handlebars.registerHelper('__allPartialsLog', (_analyzeContext: unknown) => {
console.log(plugins.handlebars.partials);
return 'analyzed';
});
plugins.handlebars.registerHelper('__compile', (evaluationString, evaluationContext) => {
let template = plugins.handlebars.compile(evaluationString);
plugins.handlebars.registerHelper('__compile', (evaluationString: unknown, evaluationContext: unknown) => {
if (typeof evaluationString !== 'string') {
return '';
}
const template = plugins.handlebars.compile(evaluationString);
return template(evaluationContext);
});
+16 -20
View File
@@ -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);
}
};
+4 -4
View File
@@ -3,11 +3,11 @@ import * as path from 'path';
export { path };
import * as smartpath from '@pushrocks/smartpath';
import * as smartfile from '@pushrocks/smartfile';
import * as smartpromise from '@pushrocks/smartpromise';
import * as smartfs from '@push.rocks/smartfs';
export { smartpath, smartfile, smartpromise };
export const smartFs = new smartfs.SmartFs(new smartfs.SmartFsProviderNode());
export { smartfs };
// third party
import handlebars from 'handlebars';
+3 -2
View File
@@ -4,8 +4,9 @@ import * as plugins from './smarthbs.plugins.js';
* get a template for a file on disk
*/
export let getTemplateForFile = async (filePathArg: string) => {
let filePathAbsolute = plugins.path.resolve(filePathArg);
return plugins.handlebars.compile(plugins.smartfile.fs.toStringSync(filePathAbsolute));
const filePathAbsolute = plugins.path.resolve(filePathArg);
const fileString = await plugins.smartFs.file(filePathAbsolute).encoding('utf8').read() as string;
return plugins.handlebars.compile(fileString);
};
/**
+9 -5
View File
@@ -27,7 +27,8 @@ export let findVarsInHbsString = async (hbsStringArg: string) => {
// make sure we are clean from curly brackets
varNameArray = varNameArray.map((x) => {
return x.match(nameInCurlsRegex)[0];
const match = x.match(nameInCurlsRegex);
return match ? match[0] : '';
});
// make sure are uniq
@@ -44,7 +45,7 @@ export let findVarsInHbsString = async (hbsStringArg: string) => {
* @param varObjectArg
* @return string array with missing variable names
*/
export let checkVarsSatisfaction = async (hbsStringArg: string, varObjectArg: any) => {
export let checkVarsSatisfaction = async (hbsStringArg: string, varObjectArg: Record<string, unknown>) => {
// required vars as combined deep string with . notation
let requiredVarStrings = await findVarsInHbsString(hbsStringArg);
@@ -55,14 +56,17 @@ export let checkVarsSatisfaction = async (hbsStringArg: string, varObjectArg: an
// building the
for (let stringVar of requiredVarStrings) {
let splittedVars = stringVar.split('.');
let requiredPointer = suppliedVarsObject;
let requiredPointer: unknown = suppliedVarsObject;
for (let i = 0; i < splittedVars.length; i++) {
let splitVar = splittedVars[i];
let varAvailable = requiredPointer[splitVar] !== undefined;
const pointerObject = requiredPointer && typeof requiredPointer === 'object'
? requiredPointer as Record<string, unknown>
: {};
let varAvailable = pointerObject[splitVar] !== undefined;
if (varAvailable && splittedVars.length === i + 1) {
// ok
} else if (varAvailable) {
requiredPointer = requiredPointer[splitVar];
requiredPointer = pointerObject[splitVar];
} else {
missingVarsObject.push(stringVar);
i = splittedVars.length;