This commit is contained in:
Philipp Kunz 2017-05-03 09:45:22 +02:00
parent ce90cf2034
commit aa31bbfb49
12 changed files with 110 additions and 35 deletions

View File

@ -7,14 +7,19 @@ export interface ScafTemplateContructorOptions {
export declare class ScafTemplate { export declare class ScafTemplate {
name: string; name: string;
description: string; description: string;
templateObject: Smartfile[]; templateSmartfileArray: Smartfile[];
requiredVariables: any[]; requiredVariables: string[];
constructor(); suppliedVariables: any;
missingVariables: string[];
/** /**
* read a template from a directory * read a template from a directory
*/ */
readTemplateFromDir(dirArg: string): Promise<void>; readTemplateFromDir(dirPathArg: string): Promise<void>;
writeWithVariables(variablesArg: any): Promise<void>; /**
* supply the variables to render the teplate with
* @param variablesArg
*/
supplyVariables(variablesArg: any): Promise<void>;
/** /**
* finds all variables in a Template * finds all variables in a Template
*/ */

View File

@ -10,18 +10,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./smartscaf.plugins"); const plugins = require("./smartscaf.plugins");
class ScafTemplate { class ScafTemplate {
constructor() { } constructor() {
this.missingVariables = [];
}
/** /**
* read a template from a directory * read a template from a directory
*/ */
readTemplateFromDir(dirArg) { readTemplateFromDir(dirPathArg) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
this.templateObject = yield plugins.smartfile.fs.fileTreeToObject(dirArg, '**/*'); let dirPath = plugins.path.resolve(dirPathArg);
this.templateSmartfileArray = yield plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*');
this._findVariablesInTemplate();
}); });
} }
writeWithVariables(variablesArg) { /**
* supply the variables to render the teplate with
* @param variablesArg
*/
supplyVariables(variablesArg) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield this._checkSuppliedVariables(variablesArg); this.suppliedVariables = variablesArg;
this.missingVariables = yield this._checkSuppliedVariables(variablesArg);
}); });
} }
/** /**
@ -29,6 +38,8 @@ class ScafTemplate {
*/ */
_findVariablesInTemplate() { _findVariablesInTemplate() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
for (let localSmartfile of this.templateSmartfileArray) {
}
}); });
} }
/** /**
@ -36,8 +47,15 @@ class ScafTemplate {
*/ */
_checkSuppliedVariables(variablesArg) { _checkSuppliedVariables(variablesArg) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
let missingVars = [];
for (let templateSmartFile of this.templateSmartfileArray) {
console.log(templateSmartFile);
let localMissingVars = yield plugins.smarthbs.checkVarsSatisfaction(templateSmartFile.contents.toString(), variablesArg);
missingVars = plugins.lodash.concat(missingVars, localMissingVars);
}
return missingVars;
}); });
} }
} }
exports.ScafTemplate = ScafTemplate; exports.ScafTemplate = ScafTemplate;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzY2FmLmNsYXNzZXMuc21hcnRzY2FmLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRzY2FmLmNsYXNzZXMuc21hcnRzY2FmLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFBQSwrQ0FBOEM7QUFXOUM7SUFLRSxnQkFBZSxDQUFDO0lBRWhCOztPQUVHO0lBQ0csbUJBQW1CLENBQUUsTUFBYzs7WUFDdkMsSUFBSSxDQUFDLGNBQWMsR0FBRyxNQUFNLE9BQU8sQ0FBQyxTQUFTLENBQUMsRUFBRSxDQUFDLGdCQUFnQixDQUFDLE1BQU0sRUFBRSxNQUFNLENBQUMsQ0FBQTtRQUNuRixDQUFDO0tBQUE7SUFFSyxrQkFBa0IsQ0FBRSxZQUFZOztZQUNwQyxNQUFNLElBQUksQ0FBQyx1QkFBdUIsQ0FBQyxZQUFZLENBQUMsQ0FBQTtRQUNsRCxDQUFDO0tBQUE7SUFFRDs7T0FFRzt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QyxJQUFJLGNBQWMsSUFBSSxJQUFJLENBQUMsc0JBQXNCLENBQUMsQ0FBQyxDQUFDO1lBRXpELENBQUM7UUFDSCxDQUFDO0tBQUE7SUFFRDs7T0FFRztJQUNXLHVCQUF1QixDQUFDLFlBQVk7O1lBQ2hELElBQUksV0FBVyxHQUFhLEVBQUUsQ0FBQTtZQUM5QixHQUFHLENBQUMsQ0FBQyxJQUFJLGlCQUFpQixJQUFJLElBQUksQ0FBQyxzQkFBc0IsQ0FBQyxDQUFDLENBQUM7Z0JBQzFELE9BQU8sQ0FBQyxHQUFHLENBQUMsaUJBQWlCLENBQUMsQ0FBQTtnQkFDOUIsSUFBSSxnQkFBZ0IsR0FBRyxNQUFNLE9BQU8sQ0FBQyxRQUFRLENBQUMscUJBQXFCLENBQ2pFLGlCQUFpQixDQUFDLFFBQVEsQ0FBQyxRQUFRLEVBQUUsRUFDckMsWUFBWSxDQUNiLENBQUE7Z0JBQ0QsV0FBVyxHQUFHLE9BQU8sQ0FBQyxNQUFNLENBQUMsTUFBTSxDQUFDLFdBQVcsRUFBRSxnQkFBZ0IsQ0FBQyxDQUFBO1lBQ3BFLENBQUM7WUFFRCxNQUFNLENBQUMsV0FBVyxDQUFBO1FBQ3BCLENBQUM7S0FBQTtDQUNGO0FBbkRELG9DQW1EQyJ9

View File

@ -1,4 +1,7 @@
import 'typings-global';
import * as lodash from 'lodash';
import * as path from 'path';
import * as smartfile from 'smartfile'; import * as smartfile from 'smartfile';
import * as smarthbs from 'smarthbs'; import * as smarthbs from 'smarthbs';
import * as smartq from 'smartq'; import * as smartq from 'smartq';
export { smartfile, smarthbs, smartq }; export { lodash, path, smartfile, smarthbs, smartq };

View File

@ -1,9 +1,14 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
require("typings-global");
const lodash = require("lodash");
exports.lodash = lodash;
const path = require("path");
exports.path = path;
const smartfile = require("smartfile"); const smartfile = require("smartfile");
exports.smartfile = smartfile; exports.smartfile = smartfile;
const smarthbs = require("smarthbs"); const smarthbs = require("smarthbs");
exports.smarthbs = smarthbs; exports.smarthbs = smarthbs;
const smartq = require("smartq"); const smartq = require("smartq");
exports.smartq = smartq; exports.smartq = smartq;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzY2FmLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHNjYWYucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLHVDQUFzQztBQUtwQyw4QkFBUztBQUpYLHFDQUFvQztBQUtsQyw0QkFBUTtBQUpWLGlDQUFnQztBQUs5Qix3QkFBTSJ9 //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRzY2FmLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHNjYWYucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDBCQUF1QjtBQUN2QixpQ0FBZ0M7QUFPOUIsd0JBQU07QUFOUiw2QkFBNEI7QUFPMUIsb0JBQUk7QUFOTix1Q0FBc0M7QUFPcEMsOEJBQVM7QUFOWCxxQ0FBb0M7QUFPbEMsNEJBQVE7QUFOVixpQ0FBZ0M7QUFPOUIsd0JBQU0ifQ==

View File

@ -26,8 +26,11 @@
"tapbundle": "^1.0.10" "tapbundle": "^1.0.10"
}, },
"dependencies": { "dependencies": {
"smartfile": "^4.1.10", "@types/lodash": "^4.14.63",
"smarthbs": "^1.0.8", "lodash": "^4.17.4",
"smartq": "^1.1.1" "smartfile": "^4.2.9",
"smarthbs": "^1.0.11",
"smartq": "^1.1.1",
"typings-global": "^1.0.16"
} }
} }

View File

@ -10,7 +10,13 @@ tap.test('should create new Smartscaf instance', async () => {
}) })
tap.test('Smartscaf instance -> should read a template directory', async () => { tap.test('Smartscaf instance -> should read a template directory', async () => {
testScafTemplate.readTemplateFromDir('./test/test_template') await testScafTemplate.readTemplateFromDir('./test/test_template')
expect(testScafTemplate.templateSmartfileArray.length).to.equal(3)
})
tap.test('smartfile -> should accept variables', async () => {
await testScafTemplate.supplyVariables({})
console.log(testScafTemplate.missingVariables)
}) })
tap.start() tap.start()

View File

@ -0,0 +1,3 @@
# this is a {{templateVar1}}
# this is a {{templateVar2}}
# this is a {{templateVar3}}

View File

@ -0,0 +1,3 @@
# this is a {{templateVar1}}
# this is a {{templateVar2}}
# this is a {{templateVar3}}

View File

@ -0,0 +1 @@
# some {{wow}}

View File

@ -15,12 +15,14 @@ export class ScafTemplate {
templateSmartfileArray: Smartfile[] templateSmartfileArray: Smartfile[]
requiredVariables: string[] requiredVariables: string[]
suppliedVariables: any suppliedVariables: any
missingVariables: string[] = []
/** /**
* read a template from a directory * read a template from a directory
*/ */
async readTemplateFromDir (dirArg: string) { async readTemplateFromDir (dirPathArg: string) {
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(dirArg, '**/*') let dirPath = plugins.path.resolve(dirPathArg)
this.templateSmartfileArray = await plugins.smartfile.fs.fileTreeToObject(dirPath, '**/*')
this._findVariablesInTemplate() this._findVariablesInTemplate()
} }
@ -29,7 +31,8 @@ export class ScafTemplate {
* @param variablesArg * @param variablesArg
*/ */
async supplyVariables (variablesArg) { async supplyVariables (variablesArg) {
await this._checkSuppliedVariables(variablesArg) this.suppliedVariables = variablesArg
this.missingVariables = await this._checkSuppliedVariables(variablesArg)
} }
/** /**
@ -45,6 +48,16 @@ export class ScafTemplate {
* checks if supplied Variables satisfy the template * checks if supplied Variables satisfy the template
*/ */
private async _checkSuppliedVariables(variablesArg) { private async _checkSuppliedVariables(variablesArg) {
let missingVars: string[] = []
for (let templateSmartFile of this.templateSmartfileArray) {
console.log(templateSmartFile)
let localMissingVars = await plugins.smarthbs.checkVarsSatisfaction(
templateSmartFile.contents.toString(),
variablesArg
)
missingVars = plugins.lodash.concat(missingVars, localMissingVars)
}
return missingVars
} }
} }

View File

@ -1,8 +1,13 @@
import 'typings-global'
import * as lodash from 'lodash'
import * as path from 'path'
import * as smartfile from 'smartfile' import * as smartfile from 'smartfile'
import * as smarthbs from 'smarthbs' import * as smarthbs from 'smarthbs'
import * as smartq from 'smartq' import * as smartq from 'smartq'
export { export {
lodash,
path,
smartfile, smartfile,
smarthbs, smarthbs,
smartq smartq

View File

@ -29,9 +29,13 @@
version "4.0.32" version "4.0.32"
resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.0.32.tgz#637e8d945a9354aab47df7125005490fe9f8e592" resolved "https://registry.yarnpkg.com/@types/handlebars/-/handlebars-4.0.32.tgz#637e8d945a9354aab47df7125005490fe9f8e592"
"@types/lodash@^4.14.63":
version "4.14.63"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.63.tgz#5ac475f55bfdc62bc88c4239dbc482f2f3bead93"
"@types/node@*": "@types/node@*":
version "7.0.14" version "7.0.15"
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.14.tgz#1470fa002a113316ac9d9ad163fc738c7a0de2a4" resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.15.tgz#628429289604c5f7e56c13f3a0422f3e59df1a17"
"@types/promises-a-plus@*": "@types/promises-a-plus@*":
version "0.0.27" version "0.0.27"
@ -223,7 +227,7 @@ graceful-fs@^4.1.2, graceful-fs@^4.1.6:
version "4.1.11" version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
handlebars@^4.0.6: handlebars@^4.0.7:
version "4.0.7" version "4.0.7"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.7.tgz#e97325aeb8ea0b9e12b9c4dd73c4c312ad0ede59" resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.7.tgz#e97325aeb8ea0b9e12b9c4dd73c4c312ad0ede59"
dependencies: dependencies:
@ -306,6 +310,10 @@ left-pad@^1.1.3:
version "1.1.3" version "1.1.3"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a" resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a"
lodash@^4.17.4:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
longest@^1.0.1: longest@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
@ -447,9 +455,9 @@ smartdelay@^1.0.1:
dependencies: dependencies:
typings-global "^1.0.14" typings-global "^1.0.14"
smartfile@^4.1.10, smartfile@^4.1.8: smartfile@^4.2.9:
version "4.2.1" version "4.2.9"
resolved "https://registry.yarnpkg.com/smartfile/-/smartfile-4.2.1.tgz#2ec85f803f771a1ddff377a438ae87aab25f3e6e" resolved "https://registry.yarnpkg.com/smartfile/-/smartfile-4.2.9.tgz#afcd1160049cbe26a86e42e842c86e8b748e69e7"
dependencies: dependencies:
"@types/fs-extra" "2.x.x" "@types/fs-extra" "2.x.x"
"@types/vinyl" "^2.0.0" "@types/vinyl" "^2.0.0"
@ -464,15 +472,17 @@ smartfile@^4.1.10, smartfile@^4.1.8:
vinyl "^2.0.2" vinyl "^2.0.2"
vinyl-file "^3.0.0" vinyl-file "^3.0.0"
smarthbs@^1.0.8: smarthbs@^1.0.11:
version "1.0.8" version "1.0.11"
resolved "https://registry.yarnpkg.com/smarthbs/-/smarthbs-1.0.8.tgz#9ff4fc2fc0643c83a80a270d3c8ea160d47f5a5d" resolved "https://registry.yarnpkg.com/smarthbs/-/smarthbs-1.0.11.tgz#bb6a0c279fa6a22c4dffc2cc1a08e68e581251a3"
dependencies: dependencies:
"@types/handlebars" "^4.0.32" "@types/handlebars" "^4.0.32"
handlebars "^4.0.6" "@types/lodash" "^4.14.63"
smartfile "^4.1.8" handlebars "^4.0.7"
lodash "^4.17.4"
smartfile "^4.2.9"
smartq "^1.1.1" smartq "^1.1.1"
typings-global "^1.0.14" typings-global "^1.0.16"
smartpath@^3.2.8: smartpath@^3.2.8:
version "3.2.8" version "3.2.8"