BREAKING CHANGE(scope): switch to new @pushrocks scope

This commit is contained in:
2018-08-27 23:55:14 +02:00
parent c5ec7e9c24
commit a3f18c17ed
17 changed files with 1740 additions and 1405 deletions

View File

@ -1 +1 @@
export * from './smartscaf.classes.smartscaf'
export * from './smartscaf.classes.smartscaf';

View File

@ -1,120 +1,130 @@
import * as plugins from './smartscaf.plugins'
import * as helpers from './smartscaf.helpers'
import * as plugins from './smartscaf.plugins';
import * as helpers from './smartscaf.helpers';
// interfaces
import { Smartfile } from 'smartfile'
import { Smartfile } from '@pushrocks/smartfile';
export interface ScafTemplateContructorOptions {
name?: string,
description?: string
sourceDir?: string
name?: string;
description?: string;
sourceDir?: string;
}
export class ScafTemplate {
name: string
description: string
templateSmartfileArray: Smartfile[]
requiredVariables: string[]
defaultVariables: any
suppliedVariables: any = {}
missingVariables: string[] = []
name: string;
description: string;
templateSmartfileArray: Smartfile[];
requiredVariables: string[];
defaultVariables: any;
suppliedVariables: any = {};
missingVariables: string[] = [];
/**
* read a template from a directory
*/
async readTemplateFromDir (dirPathArg: string) {
let dirPath = plugins.path.resolve(dirPathArg)
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*')
await this._findVariablesInTemplate()
await this._checkSuppliedVariables()
await this._checkDefaultVariables()
async readTemplateFromDir(dirPathArg: string) {
let dirPath = plugins.path.resolve(dirPathArg);
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*');
await this._findVariablesInTemplate();
await this._checkSuppliedVariables();
await this._checkDefaultVariables();
}
/**
* supply the variables to render the teplate with
* @param variablesArg gets merged with this.suppliedVariables
*/
async supplyVariables (variablesArg) {
this.suppliedVariables = plugins.lodash.merge(this.suppliedVariables, variablesArg)
this.missingVariables = await this._checkSuppliedVariables()
async supplyVariables(variablesArg) {
this.suppliedVariables = {
...this.suppliedVariables,
...variablesArg
};
this.missingVariables = await this._checkSuppliedVariables();
}
/**
* Will ask for the missing variables by cli interaction
*/
async askCliForMissingVariables () {
this.missingVariables = await this._checkSuppliedVariables()
let localSmartInteract = new plugins.smartinteract.SmartInteract()
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: (() => {
if (this.defaultVariables && this.defaultVariables[missingVariable]) {
return this.defaultVariables[missingVariable]
} else {
return 'undefined variable'
}
})(),
message: `What is the value of ${missingVariable}?`
}])
localSmartInteract.addQuestions([
{
name: missingVariable,
type: 'input',
default: (() => {
if (this.defaultVariables && this.defaultVariables[missingVariable]) {
return this.defaultVariables[missingVariable];
} else {
return 'undefined variable';
}
})(),
message: `What is the value of ${missingVariable}?`
}
]);
}
let answerBucket = await localSmartInteract.runQueue()
let answerBucket = await localSmartInteract.runQueue();
answerBucket.answerMap.forEach(async answer => {
await helpers.deepAddToObject(this.suppliedVariables, answer.name, answer.value)
})
await helpers.deepAddToObject(this.suppliedVariables, answer.name, answer.value);
});
}
async writeToDisk (destinationDirArg) {
let smartfileArrayToWrite = plugins.lodash.cloneDeep(this.templateSmartfileArray)
async writeToDisk(destinationDirArg) {
let smartfileArrayToWrite = this.templateSmartfileArray;
for (let smartfile of smartfileArrayToWrite) {
// render the template
let template = await plugins.smarthbs.getTemplateForString(
smartfile.contents.toString()
)
let renderedTemplateString = template(this.suppliedVariables)
let template = await plugins.smarthbs.getTemplateForString(smartfile.contents.toString());
let renderedTemplateString = template(this.suppliedVariables);
// handle frontmatter
let parsedTemplate = plugins.smartfm.parse(renderedTemplateString)
const smartfmInstance = new plugins.smartfm.Smartfm({
fmType: "yaml"
});
let parsedTemplate = smartfmInstance.parse(renderedTemplateString) as any;
if (parsedTemplate.data.fileName) {
smartfile.updateFileName(parsedTemplate.data.fileName)
smartfile.updateFileName(parsedTemplate.data.fileName);
}
smartfile.contents = Buffer.from(parsedTemplate.content)
smartfile.contents = Buffer.from(parsedTemplate.content);
}
await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg)
await plugins.smartfile.memory.smartfileArrayToFs(smartfileArrayToWrite, destinationDirArg);
}
/**
* finds all variables in a Template in as string
* e.g. myobject.someKey and myobject.someOtherKey
*/
private async _findVariablesInTemplate () {
let templateVariables: string[] = []
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)
let localTemplateVariables = await plugins.smarthbs.findVarsInHbsString(
templateSmartfile.contents.toString()
);
templateVariables = [...templateVariables, ...localTemplateVariables];
}
templateVariables = plugins.lodash.uniq(templateVariables)
templateVariables = templateVariables.filter((value, index, self) => {
return self.indexOf(value) === index;
});
}
/**
* checks if supplied Variables satisfy the template
*/
private async _checkSuppliedVariables () {
let missingVars: string[] = []
private async _checkSuppliedVariables() {
let missingVars: string[] = [];
for (let templateSmartfile of this.templateSmartfileArray) {
let localMissingVars = await plugins.smarthbs.checkVarsSatisfaction(
templateSmartfile.contents.toString(),
this.suppliedVariables
)
missingVars = plugins.lodash.concat(missingVars, localMissingVars)
);
missingVars = [...missingVars, ...localMissingVars];
}
missingVars = plugins.lodash.uniq(missingVars)
return missingVars
missingVars = missingVars.filter((value, index, self) => {
return self.indexOf(value) === index;
});
return missingVars;
}
/**
@ -129,18 +139,18 @@ export class ScafTemplate {
* >> - yourKey:
* >> - yourDeeperKey: yourValue
*/
private async _checkDefaultVariables () {
private async _checkDefaultVariables() {
let defaultsSmartfile = this.templateSmartfileArray.filter(smartfileArg => {
return smartfileArg.parsedPath.base === 'defaults.yml'
})[0]
return smartfileArg.parsedPath.base === 'defaults.yml';
})[0];
if (defaultsSmartfile) {
let defaultObject = await plugins.smartyaml.yamlStringToObject(
defaultsSmartfile.contents.toString()
)
this.defaultVariables = defaultObject
);
this.defaultVariables = defaultObject;
} else {
this.defaultVariables = {}
this.defaultVariables = {};
}
}
}

View File

@ -1,33 +1,33 @@
import * as plugins from './smartscaf.plugins'
import * as plugins from './smartscaf.plugins';
/**
* adds a variable in string dot notation to an already more or less expanded object
*/
export let deepAddToObject = async (objectArg, varStringArg: string, valueArg: string) => {
let varNamesArray = varStringArg.split('.')
let referencePointer = objectArg
let varNamesArray = varStringArg.split('.');
let referencePointer = objectArg;
for (let i = 0; i !== varNamesArray.length; i++) {
let varName = varNamesArray[i]
let varName = varNamesArray[i];
// is there a next variable ?
let varNameNext: string = (() => {
if (varNamesArray[i + 1]) {
return varNamesArray[i + 1]
return varNamesArray[i + 1];
}
return null
})()
return null;
})();
// build the tree in suppliedVariables
if (!referencePointer[varName] && !varNameNext) {
referencePointer[varName] = valueArg
referencePointer = null
referencePointer[varName] = valueArg;
referencePointer = null;
} else if (!referencePointer[varName] && varNameNext) {
referencePointer[varName] = {}
referencePointer = referencePointer[varName]
referencePointer[varName] = {};
referencePointer = referencePointer[varName];
} else if (referencePointer[varName] && varNameNext) {
referencePointer = referencePointer[varName]
referencePointer = referencePointer[varName];
} else {
throw new Error('Something is strange!')
throw new Error('Something is strange!');
}
}
}
};

View File

@ -1,20 +1,9 @@
import 'typings-global'
import * as lodash from 'lodash'
import * as path from 'path'
import * as smartfile from 'smartfile'
import * as smartfm from 'smartfm'
import * as smarthbs from 'smarthbs'
import * as smartinteract from 'smartinteract'
import * as smartq from 'smartq'
import * as smartyaml from 'smartyaml'
import * as path from 'path';
import * as smartfile from '@pushrocks/smartfile';
import * as smartfm from '@pushrocks/smartfm';
import * as smarthbs from '@pushrocks/smarthbs';
import * as smartinteract from '@pushrocks/smartinteract';
import * as smartq from '@pushrocks/smartpromise';
import * as smartyaml from '@pushrocks/smartyaml';
export {
lodash,
path,
smartfile,
smartfm,
smarthbs,
smartinteract,
smartq,
smartyaml
}
export { path, smartfile, smartfm, smarthbs, smartinteract, smartq, smartyaml };