fix(core): update
This commit is contained in:
1
ts/interfaces/index.ts
Normal file
1
ts/interfaces/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './smartscaf';
|
7
ts/interfaces/smartscaf.ts
Normal file
7
ts/interfaces/smartscaf.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export interface ISmartscafFile {
|
||||
defaults: {[key:string]: string};
|
||||
dependencies: {
|
||||
merge: string[];
|
||||
};
|
||||
runafter: string[];
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
import * as plugins from './smartscaf.plugins';
|
||||
import * as interfaces from './interfaces';
|
||||
import * as helpers from './smartscaf.helpers';
|
||||
|
||||
// interfaces
|
||||
@ -11,31 +12,37 @@ export interface ScafTemplateContructorOptions {
|
||||
}
|
||||
|
||||
export class ScafTemplate {
|
||||
static async createTemplateFromDir() {}
|
||||
public static async createTemplateFromDir() {}
|
||||
|
||||
/**
|
||||
* the name of the template
|
||||
*/
|
||||
name: string;
|
||||
public name: string;
|
||||
|
||||
/**
|
||||
* the descriptions of the template
|
||||
*/
|
||||
description: string;
|
||||
public description: string;
|
||||
|
||||
/**
|
||||
* the location on disk of the template
|
||||
*/
|
||||
dirPath: string;
|
||||
public dirPath: string;
|
||||
public destinationPath: string;
|
||||
|
||||
/**
|
||||
* smartscafFile
|
||||
*/
|
||||
public smartscafFile: interfaces.ISmartscafFile;
|
||||
|
||||
/**
|
||||
* the files of the template as array of Smartfiles
|
||||
*/
|
||||
templateSmartfileArray: Smartfile[];
|
||||
requiredVariables: string[];
|
||||
defaultVariables: any;
|
||||
suppliedVariables: any = {};
|
||||
missingVariables: string[] = [];
|
||||
public templateSmartfileArray: Smartfile[];
|
||||
public requiredVariables: string[];
|
||||
public defaultVariables: any;
|
||||
public suppliedVariables: any = {};
|
||||
public missingVariables: string[] = [];
|
||||
|
||||
constructor(dirPathArg: string) {
|
||||
this.dirPath = plugins.path.resolve(dirPathArg);
|
||||
@ -44,8 +51,30 @@ export class ScafTemplate {
|
||||
/**
|
||||
* read a template from a directory
|
||||
*/
|
||||
async readTemplateFromDir() {
|
||||
public async readTemplateFromDir() {
|
||||
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(this.dirPath, '**/*');
|
||||
|
||||
// read .smartscaf.yml file
|
||||
let smartscafFile: interfaces.ISmartscafFile = {
|
||||
defaults: {},
|
||||
dependencies: {
|
||||
merge: []
|
||||
},
|
||||
runafter: []
|
||||
};
|
||||
|
||||
const smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => {
|
||||
return smartfileArg.parsedPath.base === '.smartscaf.yml';
|
||||
});
|
||||
|
||||
if (smartscafSmartfile) {
|
||||
smartscafFile = {
|
||||
...smartscafFile,
|
||||
...await plugins.smartyaml.yamlStringToObject(smartscafSmartfile.contentBuffer.toString())
|
||||
};
|
||||
}
|
||||
this.smartscafFile = smartscafFile;
|
||||
|
||||
await this._resolveTemplateDependencies();
|
||||
await this._findVariablesInTemplate();
|
||||
await this._checkSuppliedVariables();
|
||||
@ -56,7 +85,7 @@ export class ScafTemplate {
|
||||
* supply the variables to render the teplate with
|
||||
* @param variablesArg gets merged with this.suppliedVariables
|
||||
*/
|
||||
async supplyVariables(variablesArg) {
|
||||
public async supplyVariables(variablesArg) {
|
||||
this.suppliedVariables = {
|
||||
...this.suppliedVariables,
|
||||
...variablesArg
|
||||
@ -67,10 +96,10 @@ export class ScafTemplate {
|
||||
/**
|
||||
* Will ask for the missing variables by cli interaction
|
||||
*/
|
||||
async askCliForMissingVariables() {
|
||||
public async askCliForMissingVariables() {
|
||||
this.missingVariables = await this._checkSuppliedVariables();
|
||||
let localSmartInteract = new plugins.smartinteract.SmartInteract();
|
||||
for (let missingVariable of this.missingVariables) {
|
||||
const localSmartInteract = new plugins.smartinteract.SmartInteract();
|
||||
for (const missingVariable of this.missingVariables) {
|
||||
localSmartInteract.addQuestions([
|
||||
{
|
||||
name: missingVariable,
|
||||
@ -86,7 +115,7 @@ export class ScafTemplate {
|
||||
}
|
||||
]);
|
||||
}
|
||||
let answerBucket = await localSmartInteract.runQueue();
|
||||
const answerBucket = await localSmartInteract.runQueue();
|
||||
await answerBucket.answerMap.forEach(async answer => {
|
||||
await helpers.deepAddToObject(this.suppliedVariables, answer.name, answer.value);
|
||||
});
|
||||
@ -96,23 +125,24 @@ export class ScafTemplate {
|
||||
* writes a file to disk
|
||||
* @param destinationDirArg
|
||||
*/
|
||||
async writeToDisk(destinationDirArg) {
|
||||
public async writeToDisk(destinationDirArg) {
|
||||
this.destinationPath = destinationDirArg;
|
||||
const smartfileArrayToWrite: Smartfile[] = [];
|
||||
for (let smartfile of this.templateSmartfileArray) {
|
||||
for (const smartfile of this.templateSmartfileArray) {
|
||||
// lets filter out template files
|
||||
if (smartfile.path === '.smartscaf.yml') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// render the template
|
||||
let template = await plugins.smarthbs.getTemplateForString(smartfile.contents.toString());
|
||||
let renderedTemplateString = template(this.suppliedVariables);
|
||||
const template = await plugins.smarthbs.getTemplateForString(smartfile.contents.toString());
|
||||
const renderedTemplateString = template(this.suppliedVariables);
|
||||
|
||||
// handle frontmatter
|
||||
const smartfmInstance = new plugins.smartfm.Smartfm({
|
||||
fmType: 'yaml'
|
||||
});
|
||||
let parsedTemplate = smartfmInstance.parse(renderedTemplateString) as any;
|
||||
const parsedTemplate = smartfmInstance.parse(renderedTemplateString) as any;
|
||||
if (parsedTemplate.data.fileName) {
|
||||
smartfile.updateFileName(parsedTemplate.data.fileName);
|
||||
}
|
||||
@ -122,6 +152,7 @@ export class ScafTemplate {
|
||||
}
|
||||
|
||||
await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg);
|
||||
await this.runScripts();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -130,8 +161,8 @@ export class ScafTemplate {
|
||||
*/
|
||||
private async _findVariablesInTemplate() {
|
||||
let templateVariables: string[] = [];
|
||||
for (let templateSmartfile of this.templateSmartfileArray) {
|
||||
let localTemplateVariables = await plugins.smarthbs.findVarsInHbsString(
|
||||
for (const templateSmartfile of this.templateSmartfileArray) {
|
||||
const localTemplateVariables = await plugins.smarthbs.findVarsInHbsString(
|
||||
templateSmartfile.contents.toString()
|
||||
);
|
||||
templateVariables = [...templateVariables, ...localTemplateVariables];
|
||||
@ -146,8 +177,8 @@ export class ScafTemplate {
|
||||
*/
|
||||
private async _checkSuppliedVariables() {
|
||||
let missingVars: string[] = [];
|
||||
for (let templateSmartfile of this.templateSmartfileArray) {
|
||||
let localMissingVars = await plugins.smarthbs.checkVarsSatisfaction(
|
||||
for (const templateSmartfile of this.templateSmartfileArray) {
|
||||
const localMissingVars = await plugins.smarthbs.checkVarsSatisfaction(
|
||||
templateSmartfile.contents.toString(),
|
||||
this.suppliedVariables
|
||||
);
|
||||
@ -176,7 +207,7 @@ export class ScafTemplate {
|
||||
* >> - yourDeeperKey: yourValue
|
||||
*/
|
||||
private async _checkDefaultVariables() {
|
||||
let smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => {
|
||||
const smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => {
|
||||
return smartfileArg.parsedPath.base === '.smartscaf.yml';
|
||||
});
|
||||
|
||||
@ -199,23 +230,8 @@ export class ScafTemplate {
|
||||
* resolve template dependencies
|
||||
*/
|
||||
private async _resolveTemplateDependencies() {
|
||||
const smartscafSmartfile = this.templateSmartfileArray.find(smartfileArg => {
|
||||
return smartfileArg.parsedPath.base === '.smartscaf.yml';
|
||||
});
|
||||
if (!smartscafSmartfile) {
|
||||
console.log('No further template dependencies defined!');
|
||||
return;
|
||||
}
|
||||
console.log('Found template dependencies! Resolving them now!');
|
||||
console.log('looking at templates to merge!');
|
||||
const smartscafYamlObject = await plugins.smartyaml.yamlStringToObject(
|
||||
smartscafSmartfile.contentBuffer.toString()
|
||||
);
|
||||
if (!smartscafYamlObject) {
|
||||
console.log('Something seems strange about the supplied dependencies.yml file.');
|
||||
return;
|
||||
}
|
||||
for (const dependency of smartscafYamlObject.dependencies.merge) {
|
||||
for (const dependency of this.smartscafFile.dependencies.merge) {
|
||||
console.log(`Now resolving ${dependency}`);
|
||||
const templatePathToMerge = plugins.path.join(this.dirPath, dependency);
|
||||
if (!plugins.smartfile.fs.isDirectory(templatePathToMerge)) {
|
||||
@ -231,4 +247,16 @@ export class ScafTemplate {
|
||||
this.templateSmartfileArray = this.templateSmartfileArray.concat(templateSmartfileArray);
|
||||
}
|
||||
}
|
||||
|
||||
private async runScripts () {
|
||||
if (!this.destinationPath) {
|
||||
throw new Error('cannot run scripts without an destinationdir');
|
||||
}
|
||||
const smartshellInstance = new plugins.smartshell.Smartshell({
|
||||
executor: 'bash'
|
||||
});
|
||||
for (const command of this.smartscafFile.runafter) {
|
||||
await smartshellInstance.exec(`cd ${this.destinationPath} && ${command}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,5 +6,6 @@ import * as smarthbs from '@pushrocks/smarthbs';
|
||||
import * as smartinteract from '@pushrocks/smartinteract';
|
||||
import * as smartq from '@pushrocks/smartpromise';
|
||||
import * as smartyaml from '@pushrocks/smartyaml';
|
||||
import * as smartshell from '@pushrocks/smartshell';
|
||||
|
||||
export { path, lik, smartfile, smartfm, smarthbs, smartinteract, smartq, smartyaml };
|
||||
export { path, lik, smartfile, smartfm, smarthbs, smartinteract, smartq, smartyaml, smartshell };
|
||||
|
Reference in New Issue
Block a user