smartscaf/ts/smartscaf.classes.smartscaf.ts

267 lines
8.0 KiB
TypeScript
Raw Permalink Normal View History

2022-06-25 01:19:16 +00:00
import * as plugins from './smartscaf.plugins.js';
import * as interfaces from './interfaces/index.js';
2017-04-28 22:44:23 +00:00
// interfaces
import { Smartfile } from '@pushrocks/smartfile';
2017-04-28 22:44:23 +00:00
export interface ScafTemplateContructorOptions {
name?: string;
description?: string;
sourceDir?: string;
2017-04-28 22:44:23 +00:00
}
export class ScafTemplate {
2023-06-24 22:50:25 +00:00
public static async createTemplateFromDir(dirPathArg: string) {
return new ScafTemplate(dirPathArg);
}
2018-10-04 14:04:42 +00:00
/**
* the name of the template
*/
2019-10-11 19:56:11 +00:00
public name: string;
2018-10-04 14:04:42 +00:00
/**
* the descriptions of the template
*/
2019-10-11 19:56:11 +00:00
public description: string;
2018-10-04 14:04:42 +00:00
/**
* the location on disk of the template
*/
2019-10-11 19:56:11 +00:00
public dirPath: string;
public destinationPath: string;
/**
* smartscafFile
*/
public smartscafFile: interfaces.ISmartscafFile;
2018-10-04 14:04:42 +00:00
/**
* the files of the template as array of Smartfiles
*/
2019-10-11 19:56:11 +00:00
public templateSmartfileArray: Smartfile[];
public requiredVariables: string[];
public defaultVariables: any;
public suppliedVariables: any = {};
public missingVariables: string[] = [];
2017-04-28 22:44:23 +00:00
constructor(dirPathArg: string) {
this.dirPath = plugins.path.resolve(dirPathArg);
}
2017-04-28 22:44:23 +00:00
/**
* read a template from a directory
*/
2019-10-11 19:56:11 +00:00
public async readTemplateFromDir() {
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(this.dirPath, '**/*');
2019-10-11 19:56:11 +00:00
// read .smartscaf.yml file
let smartscafFile: interfaces.ISmartscafFile = {
defaults: {},
dependencies: {
2022-06-25 01:19:16 +00:00
merge: [],
2019-10-11 19:56:11 +00:00
},
2022-06-25 01:19:16 +00:00
runafter: [],
2019-10-11 19:56:11 +00:00
};
2022-06-25 01:19:16 +00:00
const smartscafSmartfile = this.templateSmartfileArray.find((smartfileArg) => {
2019-10-11 19:56:11 +00:00
return smartfileArg.parsedPath.base === '.smartscaf.yml';
});
if (smartscafSmartfile) {
smartscafFile = {
...smartscafFile,
2022-06-25 01:19:16 +00:00
...(await plugins.smartyaml.yamlStringToObject(
smartscafSmartfile.contentBuffer.toString()
)),
2019-10-11 19:56:11 +00:00
};
}
this.smartscafFile = smartscafFile;
await this._resolveTemplateDependencies();
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
*/
2019-10-11 19:56:11 +00:00
public async supplyVariables(variablesArg) {
this.suppliedVariables = {
...this.suppliedVariables,
2022-06-25 01:19:16 +00:00
...variablesArg,
};
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
*/
2019-10-11 19:56:11 +00:00
public async askCliForMissingVariables() {
this.missingVariables = await this._checkSuppliedVariables();
2019-10-11 19:56:11 +00:00
const localSmartInteract = new plugins.smartinteract.SmartInteract();
for (const missingVariable of this.missingVariables) {
localSmartInteract.addQuestions([
{
name: missingVariable,
type: 'input',
default: (() => {
if (this.defaultVariables && this.defaultVariables[missingVariable]) {
return this.defaultVariables[missingVariable];
} else {
return 'undefined variable';
}
})(),
2022-06-25 01:19:16 +00:00
message: `What is the value of ${missingVariable}?`,
},
]);
2017-05-06 23:23:03 +00:00
}
2019-10-11 19:56:11 +00:00
const answerBucket = await localSmartInteract.runQueue();
2022-06-25 01:19:16 +00:00
const answers = answerBucket.getAllAnswers();
for (const answer of answers) {
2020-01-31 14:51:05 +00:00
await plugins.smartparam.smartAdd(this.suppliedVariables, answer.name, answer.value);
2022-06-25 01:19:16 +00:00
}
2017-05-05 22:47:27 +00:00
}
2018-10-04 14:04:42 +00:00
/**
* writes a file to disk
* @param destinationDirArg
*/
2019-10-11 19:56:11 +00:00
public async writeToDisk(destinationDirArg) {
this.destinationPath = destinationDirArg;
2018-10-04 14:04:42 +00:00
const smartfileArrayToWrite: Smartfile[] = [];
2019-10-11 19:56:11 +00:00
for (const smartfile of this.templateSmartfileArray) {
2018-10-04 14:04:42 +00:00
// lets filter out template files
2019-01-27 17:55:38 +00:00
if (smartfile.path === '.smartscaf.yml') {
2018-10-04 14:04:42 +00:00
continue;
}
// render the template
2019-10-11 19:56:11 +00:00
const template = await plugins.smarthbs.getTemplateForString(smartfile.contents.toString());
const renderedTemplateString = template(this.suppliedVariables);
// handle frontmatter
const smartfmInstance = new plugins.smartfm.Smartfm({
2022-06-25 01:19:16 +00:00
fmType: 'yaml',
});
2019-10-11 19:56:11 +00:00
const parsedTemplate = smartfmInstance.parse(renderedTemplateString) as any;
if (parsedTemplate.data.fileName) {
smartfile.updateFileName(parsedTemplate.data.fileName);
}
2023-06-24 22:59:36 +00:00
smartfile.contents = Buffer.from(await plugins.smarthbs.postprocess(parsedTemplate.content));
2018-10-04 14:04:42 +00:00
smartfileArrayToWrite.push(smartfile);
2017-05-26 13:32:50 +00:00
}
await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg);
2019-10-11 19:56:11 +00:00
await this.runScripts();
2017-05-26 13:32:50 +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
*/
private async _findVariablesInTemplate() {
let templateVariables: string[] = [];
2019-10-11 19:56:11 +00:00
for (const templateSmartfile of this.templateSmartfileArray) {
const localTemplateVariables = await plugins.smarthbs.findVarsInHbsString(
templateSmartfile.contents.toString()
);
templateVariables = [...templateVariables, ...localTemplateVariables];
2017-04-30 21:58:03 +00:00
}
templateVariables = templateVariables.filter((value, index, self) => {
return self.indexOf(value) === index;
});
2017-04-28 22:44:23 +00:00
}
/**
* checks if supplied Variables satisfy the template
*/
private async _checkSuppliedVariables() {
let missingVars: string[] = [];
2019-10-11 19:56:11 +00:00
for (const templateSmartfile of this.templateSmartfileArray) {
const 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
);
2018-10-04 14:04:42 +00:00
// combine with other missingVars
missingVars = [...missingVars, ...localMissingVars];
2017-05-03 07:45:22 +00:00
}
2018-10-04 14:04:42 +00:00
// dedupe
missingVars = missingVars.filter((value, index, self) => {
return self.indexOf(value) === index;
});
return missingVars;
2017-04-28 22:44:23 +00:00
}
2017-05-25 16:32:53 +00:00
/**
2018-10-04 14:04:42 +00:00
* checks the smartscaf.yml default values at the root of a template
2017-05-25 16:32:53 +00:00
* 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() {
2022-06-25 01:19:16 +00:00
const smartscafSmartfile = this.templateSmartfileArray.find((smartfileArg) => {
2018-10-04 14:04:42 +00:00
return smartfileArg.parsedPath.base === '.smartscaf.yml';
});
2017-05-25 16:32:53 +00:00
2018-10-04 14:04:42 +00:00
if (smartscafSmartfile) {
const smartscafObject = await plugins.smartyaml.yamlStringToObject(
smartscafSmartfile.contents.toString()
);
2018-10-04 14:04:42 +00:00
const defaultObject = smartscafObject.defaults;
this.defaultVariables = defaultObject;
2018-10-04 14:04:42 +00:00
}
// safeguard against non existent defaults
if (!this.defaultVariables) {
2019-01-27 17:55:38 +00:00
console.log('this template does not specify defaults');
this.defaultVariables = {};
2017-05-25 16:32:53 +00:00
}
}
/**
* resolve template dependencies
*/
private async _resolveTemplateDependencies() {
2018-10-04 14:04:42 +00:00
console.log('looking at templates to merge!');
2019-10-11 19:56:11 +00:00
for (const dependency of this.smartscafFile.dependencies.merge) {
2018-10-04 14:04:42 +00:00
console.log(`Now resolving ${dependency}`);
const templatePathToMerge = plugins.path.join(this.dirPath, dependency);
2019-01-27 17:55:38 +00:00
if (!plugins.smartfile.fs.isDirectory(templatePathToMerge)) {
console.log(
`dependency ${dependency} resolves to ${templatePathToMerge} which ist NOT a directory`
);
2018-10-04 14:04:42 +00:00
continue;
2019-01-27 17:55:38 +00:00
}
const templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(
templatePathToMerge,
'**/*'
);
2018-10-04 14:04:42 +00:00
this.templateSmartfileArray = this.templateSmartfileArray.concat(templateSmartfileArray);
}
}
2019-10-11 19:56:11 +00:00
2020-01-31 14:51:05 +00:00
private async runScripts() {
2019-10-11 19:56:11 +00:00
if (!this.destinationPath) {
throw new Error('cannot run scripts without an destinationdir');
}
const smartshellInstance = new plugins.smartshell.Smartshell({
2022-06-25 01:19:16 +00:00
executor: 'bash',
2019-10-11 19:56:11 +00:00
});
for (const command of this.smartscafFile.runafter) {
await smartshellInstance.exec(`cd ${this.destinationPath} && ${command}`);
}
}
2017-04-28 22:44:23 +00:00
}