smartscaf/ts/smartscaf.classes.smartscaf.ts

80 lines
2.2 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
2017-05-06 23:23:03 +00:00
this.missingVariables = await this._checkSuppliedVariables()
2017-04-28 22:44:23 +00:00
}
2017-05-05 22:47:27 +00:00
/**
* Will ask for the missing variables by cli interaction
*/
2017-05-06 23:23:03 +00:00
async askCliForMissingVariables () {
this.missingVariables = await this._checkSuppliedVariables()
let localSmartInteract = new plugins.smartinteract.SmartInteract()
for (let missingVariable of this.missingVariables) {
localSmartInteract.addQuestions([{
name: missingVariable,
type: 'input',
default: `undefined ${missingVariable}`,
message: `What is the value of ${missingVariable}?`
}])
}
let answers = await localSmartInteract.runQueue()
2017-05-05 22:47:27 +00:00
}
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-06 23:23:03 +00:00
private async _checkSuppliedVariables() {
2017-05-03 07:45:22 +00:00
let missingVars: string[] = []
for (let templateSmartFile of this.templateSmartfileArray) {
let localMissingVars = await plugins.smarthbs.checkVarsSatisfaction(
templateSmartFile.contents.toString(),
2017-05-06 23:23:03 +00:00
this.suppliedVariables
2017-05-03 07:45:22 +00:00
)
missingVars = plugins.lodash.concat(missingVars, localMissingVars)
2017-05-06 23:23:03 +00:00
missingVars = plugins.lodash.uniq(missingVars)
2017-05-03 07:45:22 +00:00
}
return missingVars
2017-04-28 22:44:23 +00:00
}
}