smartscaf/ts/smartscaf.classes.smartscaf.ts

125 lines
3.9 KiB
TypeScript
Raw Normal View History

2017-04-28 22:44:23 +00:00
import * as plugins from './smartscaf.plugins'
2017-05-25 16:32:53 +00:00
import * as helpers from './smartscaf.helpers'
2017-04-28 22:44:23 +00:00
// 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[]
2017-05-25 16:32:53 +00:00
defaultVariables: any
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-05-25 16:32:53 +00:00
await this._findVariablesInTemplate()
await this._checkSuppliedVariables()
await this._checkDefaultVariables()
2017-04-28 22:44:23 +00:00
}
2017-04-30 21:58:03 +00:00
/**
* supply the variables to render the teplate with
2017-05-25 16:32:53 +00:00
* @param variablesArg gets merged with this.suppliedVariables
2017-04-30 21:58:03 +00:00
*/
async supplyVariables (variablesArg) {
2017-05-25 16:32:53 +00:00
this.suppliedVariables = plugins.lodash.merge(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',
2017-05-25 16:32:53 +00:00
default: (() => {
if (this.defaultVariables[missingVariable]) {
return this.defaultVariables[missingVariable]
} else {
return 'undefined variable'
}
})(),
2017-05-06 23:23:03 +00:00
message: `What is the value of ${missingVariable}?`
}])
}
2017-05-25 16:32:53 +00:00
let answerBucket = await localSmartInteract.runQueue()
answerBucket.answerMap.forEach(async answer => {
await helpers.deepAddToObject(this.suppliedVariables, answer.name, answer.value)
})
2017-05-05 22:47:27 +00:00
}
2017-04-28 22:44:23 +00:00
/**
2017-05-25 16:32:53 +00:00
* finds all variables in a Template in as string
* e.g. myobject.someKey and myobject.someOtherKey
2017-04-28 22:44:23 +00:00
*/
2017-05-25 16:32:53 +00:00
private async _findVariablesInTemplate () {
let templateVariables: string[] = []
for (let templateSmartfile of this.templateSmartfileArray) {
let localTemplateVariables = await plugins.smarthbs.findVarsInHbsString(templateSmartfile.contents.toString())
templateVariables = plugins.lodash.concat(templateVariables, localTemplateVariables)
2017-04-30 21:58:03 +00:00
}
2017-05-25 16:32:53 +00:00
templateVariables = plugins.lodash.uniq(templateVariables)
2017-04-28 22:44:23 +00:00
}
/**
* checks if supplied Variables satisfy the template
*/
2017-05-25 16:32:53 +00:00
private async _checkSuppliedVariables () {
2017-05-03 07:45:22 +00:00
let missingVars: string[] = []
2017-05-25 16:32:53 +00:00
for (let templateSmartfile of this.templateSmartfileArray) {
2017-05-03 07:45:22 +00:00
let localMissingVars = await plugins.smarthbs.checkVarsSatisfaction(
2017-05-25 16:32:53 +00:00
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-25 16:32:53 +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
}
2017-05-25 16:32:53 +00:00
/**
* checks the default.yml at the root of a template for default variables
* allows 2 ways of notation in YAML:
* >> myObject.myKey.someDeeperKey: someValue
* >> myObject.yourKey.yourDeeperKey: yourValue
* or
* >> myObject:
* >> - someKey:
* >> - someDeeperKey: someValue
* >> - yourKey:
* >> - yourDeeperKey: yourValue
*/
private async _checkDefaultVariables () {
let defaultsSmartfile = this.templateSmartfileArray.filter(smartfileArg => {
return smartfileArg.parsedPath.base === 'defaults.yml'
})[0]
if (defaultsSmartfile) {
let defaultObject = await plugins.smartyaml.yamlStringToObject(
defaultsSmartfile.contents.toString()
)
this.defaultVariables = defaultObject
} else {
this.defaultVariables = {}
}
}
2017-04-28 22:44:23 +00:00
}