BREAKING CHANGE(update scope to @pushrocks): update
This commit is contained in:
18
ts/index.ts
18
ts/index.ts
@ -1,10 +1,10 @@
|
||||
import * as plugins from './smarthbs.plugins'
|
||||
export type TTemplateStringType = 'filePath' | 'code'
|
||||
import * as plugins from './smarthbs.plugins';
|
||||
export type TTemplateStringType = 'filePath' | 'code';
|
||||
|
||||
export let handlebars = plugins.handlebars
|
||||
export * from './smarthbs.compile'
|
||||
import './smarthbs.helpers'
|
||||
export * from './smarthbs.partials'
|
||||
export * from './smarthbs.template'
|
||||
export * from './smarthbs.variables'
|
||||
export * from './smarthbs.postprocess'
|
||||
export let handlebars = plugins.handlebars;
|
||||
export * from './smarthbs.compile';
|
||||
import './smarthbs.helpers';
|
||||
export * from './smarthbs.partials';
|
||||
export * from './smarthbs.template';
|
||||
export * from './smarthbs.variables';
|
||||
export * from './smarthbs.postprocess';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import * as plugins from './smarthbs.plugins'
|
||||
import * as plugins from './smarthbs.plugins';
|
||||
|
||||
/**
|
||||
* compiles a directory and outputs it
|
||||
@ -8,15 +8,22 @@ 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))
|
||||
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)
|
||||
console.log('hi ' + output + ' hi')
|
||||
await plugins.smartfile.fs.ensureDir(destinationDirPathArg)
|
||||
plugins.smartfile.memory.toFsSync(output, plugins.path.join(destinationDirPathArg, parsedPath.name + '.html'))
|
||||
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);
|
||||
console.log('hi ' + output + ' hi');
|
||||
await plugins.smartfile.fs.ensureDir(destinationDirPathArg);
|
||||
plugins.smartfile.memory.toFsSync(
|
||||
output,
|
||||
plugins.path.join(destinationDirPathArg, parsedPath.name + '.html')
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1,32 +1,31 @@
|
||||
import * as plugins from './smarthbs.plugins'
|
||||
import * as plugins from './smarthbs.plugins';
|
||||
|
||||
/**
|
||||
* Helper:
|
||||
* Allows you to analyze a context
|
||||
*/
|
||||
plugins.handlebars.registerHelper('__analyze', (analyzeContext) => {
|
||||
plugins.handlebars.registerHelper('__analyze', analyzeContext => {
|
||||
if (typeof analyzeContext === 'string') {
|
||||
if (plugins.handlebars.partials[analyzeContext]) {
|
||||
plugins.beautylog.log(`The analyzed partial ${analyzeContext} looks like this`)
|
||||
console.log(plugins.handlebars.partials[analyzeContext])
|
||||
console.log(`The analyzed partial ${analyzeContext} looks like this`);
|
||||
console.log(plugins.handlebars.partials[analyzeContext]);
|
||||
} else {
|
||||
plugins.beautylog.error(`The Partial ${analyzeContext} cannot be found`)
|
||||
console.error(`The Partial ${analyzeContext} cannot be found`);
|
||||
}
|
||||
return 'analyzed'
|
||||
return 'analyzed';
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
/**
|
||||
* Helper:
|
||||
* logs all registered partials to console
|
||||
*/
|
||||
plugins.handlebars.registerHelper('__allPartialsLog', (analyzeContext) => {
|
||||
console.log(plugins.handlebars.partials)
|
||||
return 'analyzed'
|
||||
})
|
||||
plugins.handlebars.registerHelper('__allPartialsLog', analyzeContext => {
|
||||
console.log(plugins.handlebars.partials);
|
||||
return 'analyzed';
|
||||
});
|
||||
|
||||
plugins.handlebars.registerHelper('__compile', (evaluationString, evaluationContext) => {
|
||||
let template = plugins.handlebars.compile(evaluationString)
|
||||
return template(evaluationContext)
|
||||
})
|
||||
|
||||
let template = plugins.handlebars.compile(evaluationString);
|
||||
return template(evaluationContext);
|
||||
});
|
||||
|
@ -1,24 +1,26 @@
|
||||
import * as plugins from './smarthbs.plugins'
|
||||
import * as plugins from './smarthbs.plugins';
|
||||
|
||||
/**
|
||||
* registers a directory of partials to make them available within handlebars compilation
|
||||
*/
|
||||
export let registerPartialDir = (dirPathArg: string): Promise<any> => {
|
||||
let done = plugins.smartq.defer()
|
||||
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))
|
||||
let parsedPath = plugins.path.parse(hbsFilePath);
|
||||
let hbsFileString = plugins.smartfile.fs.toStringSync(
|
||||
plugins.path.join(dirPathArg, hbsFilePath)
|
||||
);
|
||||
if (parsedPath.dir === '/') {
|
||||
parsedPath.dir = ''
|
||||
parsedPath.dir = '';
|
||||
}
|
||||
if (parsedPath.dir !== '') {
|
||||
parsedPath.dir = parsedPath.dir + '/'
|
||||
parsedPath.dir = parsedPath.dir + '/';
|
||||
}
|
||||
let partialName = `partials/${parsedPath.dir}${parsedPath.name}`
|
||||
plugins.handlebars.registerPartial(partialName, hbsFileString)
|
||||
done.resolve()
|
||||
let partialName = `partials/${parsedPath.dir}${parsedPath.name}`;
|
||||
plugins.handlebars.registerPartial(partialName, hbsFileString);
|
||||
done.resolve();
|
||||
}
|
||||
})
|
||||
return done.promise
|
||||
}
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
|
@ -1,16 +1,7 @@
|
||||
import 'typings-global'
|
||||
import * as beautylog from 'beautylog'
|
||||
import * as handlebars from 'handlebars'
|
||||
import * as lodash from 'lodash'
|
||||
import * as path from 'path'
|
||||
import * as smartfile from 'smartfile'
|
||||
import * as smartq from 'smartq'
|
||||
import * as handlebars from 'handlebars';
|
||||
import * as lodash from 'lodash';
|
||||
import * as path from 'path';
|
||||
import * as smartfile from '@pushrocks/smartfile';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
export {
|
||||
beautylog,
|
||||
handlebars,
|
||||
lodash,
|
||||
path,
|
||||
smartfile,
|
||||
smartq
|
||||
}
|
||||
export { handlebars, lodash, path, smartfile, smartpromise };
|
||||
|
@ -1,16 +1,15 @@
|
||||
import * as plugins from './smarthbs.plugins'
|
||||
import * as plugins from './smarthbs.plugins';
|
||||
|
||||
|
||||
let safeSyntaxBeginRegex = /{-{/g
|
||||
let safeSyntaxEndRegex = /}-}/g
|
||||
let safeSyntaxBeginRegex = /{-{/g;
|
||||
let safeSyntaxEndRegex = /}-}/g;
|
||||
|
||||
/**
|
||||
* allows you to keep handlebars in place across multiple iterations
|
||||
* helpful when handlebars syntax is used by more than one tool in a build chain
|
||||
*/
|
||||
export let postprocess = async (stringArg: string): Promise<string> => {
|
||||
let processedString = stringArg
|
||||
processedString = processedString.replace(safeSyntaxBeginRegex, '{{')
|
||||
processedString = processedString.replace(safeSyntaxEndRegex, '}}')
|
||||
return processedString
|
||||
}
|
||||
let processedString = stringArg;
|
||||
processedString = processedString.replace(safeSyntaxBeginRegex, '{{');
|
||||
processedString = processedString.replace(safeSyntaxEndRegex, '}}');
|
||||
return processedString;
|
||||
};
|
||||
|
@ -1,16 +1,16 @@
|
||||
import * as plugins from './smarthbs.plugins'
|
||||
import * as plugins from './smarthbs.plugins';
|
||||
|
||||
/**
|
||||
* 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))
|
||||
}
|
||||
let filePathAbsolute = plugins.path.resolve(filePathArg);
|
||||
return plugins.handlebars.compile(plugins.smartfile.fs.toStringSync(filePathAbsolute));
|
||||
};
|
||||
|
||||
/**
|
||||
* get a template for string
|
||||
*/
|
||||
export let getTemplateForString = async (fileStringArg: string) => {
|
||||
return plugins.handlebars.compile(fileStringArg)
|
||||
}
|
||||
return plugins.handlebars.compile(fileStringArg);
|
||||
};
|
||||
|
@ -1,39 +1,39 @@
|
||||
// This file contains code that makes it easy to search handlebar templates for variables.
|
||||
// Why? To get a clue if you are missing some.
|
||||
|
||||
import * as plugins from './smarthbs.plugins'
|
||||
import * as plugins from './smarthbs.plugins';
|
||||
|
||||
// the curly regex objects
|
||||
let tripleCurlyRegex = /{{{\s*[\w\.]+\s*}}}/g
|
||||
let doubleCurlyRegex = /{{\s*[\w\.]+\s*}}/g
|
||||
let nameInCurlsRegex = /[\w\.]+/
|
||||
let tripleCurlyRegex = /{{{\s*[\w\.]+\s*}}}/g;
|
||||
let doubleCurlyRegex = /{{\s*[\w\.]+\s*}}/g;
|
||||
let nameInCurlsRegex = /[\w\.]+/;
|
||||
|
||||
/**
|
||||
* finds all variables in a handlebars template
|
||||
* @param hbsStringArg
|
||||
*/
|
||||
export let findVarsInHbsString = async (hbsStringArg: string) => {
|
||||
let hbsString = hbsStringArg // make sure we have a new string object that we may destroy
|
||||
let varNameArray: string[] = []
|
||||
let tripleCurlyMatches = hbsString.match(tripleCurlyRegex)
|
||||
let hbsString = hbsStringArg; // make sure we have a new string object that we may destroy
|
||||
let varNameArray: string[] = [];
|
||||
let tripleCurlyMatches = hbsString.match(tripleCurlyRegex);
|
||||
if (tripleCurlyMatches) {
|
||||
hbsString = hbsString.replace(tripleCurlyRegex, '[[[replaced]]]')
|
||||
varNameArray = plugins.lodash.concat(varNameArray, tripleCurlyMatches)
|
||||
hbsString = hbsString.replace(tripleCurlyRegex, '[[[replaced]]]');
|
||||
varNameArray = plugins.lodash.concat(varNameArray, tripleCurlyMatches);
|
||||
}
|
||||
let doubleCurlyMatches = hbsString.match(doubleCurlyRegex)
|
||||
let doubleCurlyMatches = hbsString.match(doubleCurlyRegex);
|
||||
if (doubleCurlyMatches) {
|
||||
varNameArray = plugins.lodash.concat(varNameArray, doubleCurlyMatches)
|
||||
varNameArray = plugins.lodash.concat(varNameArray, doubleCurlyMatches);
|
||||
}
|
||||
|
||||
// make sure we are clean from curly brackets
|
||||
varNameArray = varNameArray.map((x) => {
|
||||
return x.match(nameInCurlsRegex)[ 0 ]
|
||||
})
|
||||
varNameArray = varNameArray.map(x => {
|
||||
return x.match(nameInCurlsRegex)[0];
|
||||
});
|
||||
|
||||
// make sure are uniq
|
||||
varNameArray = plugins.lodash.uniq(varNameArray)
|
||||
return varNameArray
|
||||
}
|
||||
varNameArray = plugins.lodash.uniq(varNameArray);
|
||||
return varNameArray;
|
||||
};
|
||||
|
||||
/**
|
||||
* checks if supplied variables satisfy an handlebars template
|
||||
@ -43,28 +43,28 @@ export let findVarsInHbsString = async (hbsStringArg: string) => {
|
||||
*/
|
||||
export let checkVarsSatisfaction = async (hbsStringArg: string, varObjectArg: any) => {
|
||||
// required vars as combined deep string with . notation
|
||||
let requiredVarStrings = await findVarsInHbsString(hbsStringArg)
|
||||
let requiredVarStrings = await findVarsInHbsString(hbsStringArg);
|
||||
|
||||
// comparison objects
|
||||
let suppliedVarsObject = varObjectArg
|
||||
let missingVarsObject: string[] = []
|
||||
let suppliedVarsObject = varObjectArg;
|
||||
let missingVarsObject: string[] = [];
|
||||
|
||||
// building the
|
||||
// building the
|
||||
for (let stringVar of requiredVarStrings) {
|
||||
let splittedVars = stringVar.split('.')
|
||||
let requiredPointer = suppliedVarsObject
|
||||
let splittedVars = stringVar.split('.');
|
||||
let requiredPointer = suppliedVarsObject;
|
||||
for (let i = 0; i < splittedVars.length; i++) {
|
||||
let splitVar = splittedVars[ i ]
|
||||
let varAvailable = (requiredPointer[ splitVar ] !== undefined)
|
||||
if (varAvailable && splittedVars.length === (i + 1)) {
|
||||
let splitVar = splittedVars[i];
|
||||
let varAvailable = requiredPointer[splitVar] !== undefined;
|
||||
if (varAvailable && splittedVars.length === i + 1) {
|
||||
// ok
|
||||
} else if (varAvailable) {
|
||||
requiredPointer = requiredPointer[ splitVar ]
|
||||
requiredPointer = requiredPointer[splitVar];
|
||||
} else {
|
||||
missingVarsObject.push(stringVar)
|
||||
i = splittedVars.length
|
||||
missingVarsObject.push(stringVar);
|
||||
i = splittedVars.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
return missingVarsObject
|
||||
}
|
||||
return missingVarsObject;
|
||||
};
|
||||
|
Reference in New Issue
Block a user