smartscaf/ts/smartscaf.classes.smartscaf.ts

64 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-04-28 22:44:23 +00:00
import * as plugins from './smartscaf.plugins'
// interfaces
import { Smartfile } from 'smartfile'
export interface ScafTemplateContructorOptions {
name?: string,
description?: string
sourceDir?: string
}
export class ScafTemplate {
name: string
description: string
2017-04-30 21:58:03 +00:00
templateSmartfileArray: Smartfile[]
requiredVariables: string[]
suppliedVariables: any
2017-05-03 07:45:22 +00:00
missingVariables: string[] = []
2017-04-28 22:44:23 +00:00
/**
* read a template from a directory
*/
2017-05-03 07:45:22 +00:00
async readTemplateFromDir (dirPathArg: string) {
let dirPath = plugins.path.resolve(dirPathArg)
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*')
2017-04-30 21:58:03 +00:00
this._findVariablesInTemplate()
2017-04-28 22:44:23 +00:00
}
2017-04-30 21:58:03 +00:00
/**
* supply the variables to render the teplate with
* @param variablesArg
*/
async supplyVariables (variablesArg) {
2017-05-03 07:45:22 +00:00
this.suppliedVariables = variablesArg
this.missingVariables = await this._checkSuppliedVariables(variablesArg)
2017-04-28 22:44:23 +00:00
}
/**
* finds all variables in a Template
*/
2017-05-03 07:45:22 +00:00
private async _findVariablesInTemplate() {
2017-04-30 21:58:03 +00:00
for (let localSmartfile of this.templateSmartfileArray) {
2017-05-03 07:45:22 +00:00
2017-04-30 21:58:03 +00:00
}
2017-04-28 22:44:23 +00:00
}
/**
* checks if supplied Variables satisfy the template
*/
2017-05-03 07:45:22 +00:00
private async _checkSuppliedVariables(variablesArg) {
let missingVars: string[] = []
for (let templateSmartFile of this.templateSmartfileArray) {
console.log(templateSmartFile)
let localMissingVars = await plugins.smarthbs.checkVarsSatisfaction(
templateSmartFile.contents.toString(),
variablesArg
)
missingVars = plugins.lodash.concat(missingVars, localMissingVars)
}
2017-04-30 21:58:03 +00:00
2017-05-03 07:45:22 +00:00
return missingVars
2017-04-28 22:44:23 +00:00
}
}