Skip to content

Commit ac24b87

Browse files
committed
Change "view" to "parameters" in the API and template definitions
1 parent 1a7c518 commit ac24b87

File tree

5 files changed

+103
-103
lines changed

5 files changed

+103
-103
lines changed

cli.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,30 @@ const loadTemplate = (templatePath) => {
2424
});
2525
};
2626

27-
const loadView = (viewPath) => {
28-
if (!viewPath) return Promise.resolve({});
29-
return fs.readFile(viewPath, 'utf8')
30-
.then(viewData => JSON.parse(viewData));
27+
const loadParameters = (parametersPath) => {
28+
if (!parametersPath) return Promise.resolve({});
29+
return fs.readFile(parametersPath, 'utf8')
30+
.then(paramsData => JSON.parse(paramsData));
3131
};
3232

33-
const loadTemplateAndView = (templatePath, viewPath) => Promise.all([
33+
const loadTemplateAndParameters = (templatePath, parametersPath) => Promise.all([
3434
loadTemplate(templatePath),
35-
loadView(viewPath)
35+
loadParameters(parametersPath)
3636
]);
3737

3838
const validateTemplate = templatePath => loadTemplate(templatePath)
3939
.then(() => {
4040
console.log(`template source at ${templatePath} is valid`);
4141
});
4242

43-
const templateToViewSchema = templatePath => loadTemplate(templatePath)
43+
const templateToParametersSchema = templatePath => loadTemplate(templatePath)
4444
.then((tmpl) => {
45-
console.log(JSON.stringify(tmpl.getViewSchema(), null, 2));
45+
console.log(JSON.stringify(tmpl.getParametersSchema(), null, 2));
4646
});
4747

48-
const validateViewData = (tmpl, view) => {
48+
const validateParamData = (tmpl, parameters) => {
4949
try {
50-
tmpl.validateView(view);
50+
tmpl.validateParameters(parameters);
5151
} catch (e) {
5252
console.error('parameters failed validation:');
5353
if (e.stack) {
@@ -59,15 +59,15 @@ const validateViewData = (tmpl, view) => {
5959
}
6060
};
6161

62-
const validateView = (templatePath, viewPath) => loadTemplateAndView(templatePath, viewPath)
63-
.then(([tmpl, view]) => {
64-
validateViewData(tmpl, view);
62+
const validateParameters = (templatePath, parametersPath) => loadTemplateAndParameters(templatePath, parametersPath)
63+
.then(([tmpl, parameters]) => {
64+
validateParamData(tmpl, parameters);
6565
});
6666

67-
const renderTemplate = (templatePath, viewPath) => loadTemplateAndView(templatePath, viewPath)
68-
.then(([tmpl, view]) => {
69-
validateViewData(tmpl, view);
70-
console.log(tmpl.render(view));
67+
const renderTemplate = (templatePath, parametersPath) => loadTemplateAndParameters(templatePath, parametersPath)
68+
.then(([tmpl, parameters]) => {
69+
validateParamData(tmpl, parameters);
70+
console.log(tmpl.render(parameters));
7171
});
7272

7373
const validateTemplateSet = (tsPath) => {
@@ -82,10 +82,10 @@ const validateTemplateSet = (tsPath) => {
8282
});
8383
};
8484

85-
const htmlPreview = (templatePath, viewPath) => loadTemplateAndView(templatePath, viewPath)
86-
.then(([tmpl, view]) => generateHtmlPreview(
87-
tmpl.getViewSchema(),
88-
tmpl.getCombinedView(view)
85+
const htmlPreview = (templatePath, parametersPath) => loadTemplateAndParameters(templatePath, parametersPath)
86+
.then(([tmpl, parameters]) => generateHtmlPreview(
87+
tmpl.getParametersSchema(),
88+
tmpl.getCombinedParameters(parameters)
8989
))
9090
.then(htmlData => console.log(htmlData));
9191

@@ -117,23 +117,23 @@ require('yargs')
117117
.positional('file', {
118118
describe: 'template source file to parse'
119119
});
120-
}, argv => templateToViewSchema(argv.file))
121-
.command('validateView <tmplFile> <parameterFile>', 'validate supplied template parameters with given template', (yargs) => {
120+
}, argv => templateToParametersSchema(argv.file))
121+
.command('validateParameters <tmplFile> <parameterFile>', 'validate supplied template parameters with given template', (yargs) => {
122122
yargs
123123
.positional('tmplFile', {
124124
describe: 'template to get template parameters schema from'
125125
})
126126
.positional('parameterFile', {
127127
describe: 'file with template parameters to validate'
128128
});
129-
}, argv => validateView(argv.tmplFile, argv.parameterFile))
129+
}, argv => validateParameters(argv.tmplFile, argv.parameterFile))
130130
.command('render <tmplFile> [parameterFile]', 'render given template file with supplied parameters', (yargs) => {
131131
yargs
132132
.positional('tmplFile', {
133133
describe: 'template source file to render'
134134
})
135135
.positional('parameterFile', {
136-
describe: 'optional file with template parameters to use in addition to any defined in the view in the template source file'
136+
describe: 'optional file with template parameters to use in addition to any defined in the parameters in the template source file'
137137
});
138138
}, argv => renderTemplate(argv.tmplFile, argv.parameterFile))
139139
.command('validateTemplateSet <templateSetPath>', 'validate supplied template set', (yargs) => {
@@ -148,7 +148,7 @@ require('yargs')
148148
describe: 'template source file to render'
149149
})
150150
.positional('parameterFile', {
151-
describe: 'optional file with template parameters to use in addition to any defined in the view in the template source file'
151+
describe: 'optional file with template parameters to use in addition to any defined in the parameters in the template source file'
152152
});
153153
}, argv => htmlPreview(argv.tmplFile, argv.parameterFile))
154154
.command('packageTemplateSet <templateSetPath> [dst]', 'build a package for a given template set', (yargs) => {

lib/template.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ validator.addFormat('mustache', {
3030
});
3131
const _validateSchema = validator.compile(tmplSchema);
3232

33-
class JSONViewTransform {
33+
class JSONParametersTransform {
3434
transform(schema, value) {
3535
if (typeof value === 'undefined') {
3636
return value;
@@ -59,14 +59,14 @@ class Template {
5959
this.description = '';
6060
this.definitions = {};
6161
this.typeDefinitions = {};
62-
this._viewSchema = {};
62+
this._parametersSchema = {};
6363
this.target = 'as3';
6464
this.templateText = '';
65-
this.defaultView = {};
65+
this.defaultParameters = {};
6666
this.sourceType = 'UNKNOWN';
6767
this.sourceText = '';
6868
this.sourceHash = '';
69-
this._viewValidator = undefined;
69+
this._parametersValidator = undefined;
7070
}
7171

7272
_loadTypeSchemas(schemaProvider) {
@@ -340,13 +340,13 @@ class Template {
340340
return schema;
341341
}
342342

343-
_viewSchemaFromTemplate(typeSchemas) {
344-
this._viewSchema = this._handleParsed(Mustache.parse(this.templateText), typeSchemas);
343+
_parametersSchemaFromTemplate(typeSchemas) {
344+
this._parametersSchema = this._handleParsed(Mustache.parse(this.templateText), typeSchemas);
345345

346346
// If we just ended up with an empty string type, then we have no types and we
347347
// should return an empty object instead.
348-
if (this._viewSchema.type === 'string' && !this._viewSchema.properties) {
349-
this._viewSchema.type = 'object';
348+
if (this._parametersSchema.type === 'string' && !this._parametersSchema.properties) {
349+
this._parametersSchema.type = 'object';
350350
}
351351
}
352352

@@ -359,7 +359,7 @@ class Template {
359359
this.sourceHash = hash.digest('hex');
360360
}
361361

362-
_createViewValidator() {
362+
_createParametersValidator() {
363363
const loadSchema = (uri) => {
364364
uri = url.parse(uri);
365365
const opts = {
@@ -383,9 +383,9 @@ class Template {
383383
ajv.addFormat('text', /.*/);
384384
ajv.addFormat('hidden', /.*/);
385385
ajv.addFormat('password', /.*/);
386-
return ajv.compileAsync(this.getViewSchema())
386+
return ajv.compileAsync(this.getParametersSchema())
387387
.then((validate) => {
388-
this._viewValidator = validate;
388+
this._parametersValidator = validate;
389389
return Promise.resolve();
390390
});
391391
}
@@ -398,9 +398,9 @@ class Template {
398398
return tmpl._loadTypeSchemas(schemaProvider)
399399
.then((typeSchemas) => {
400400
tmpl._descriptionFromTemplate();
401-
tmpl._viewSchemaFromTemplate(typeSchemas);
401+
tmpl._parametersSchemaFromTemplate(typeSchemas);
402402
})
403-
.then(() => tmpl._createViewValidator())
403+
.then(() => tmpl._createParametersValidator())
404404
.then(() => tmpl);
405405
}
406406

@@ -414,13 +414,13 @@ class Template {
414414
if (yamldata.title) tmpl.title = yamldata.title;
415415
if (yamldata.description) tmpl.description = yamldata.description;
416416
if (yamldata.definitions) tmpl.definitions = yamldata.definitions;
417-
if (yamldata.view) tmpl.defaultView = yamldata.view;
417+
if (yamldata.parameters) tmpl.defaultParameters = yamldata.parameters;
418418

419419
return tmpl._loadTypeSchemas(schemaProvider)
420420
.then((typeSchemas) => {
421-
tmpl._viewSchemaFromTemplate(typeSchemas);
421+
tmpl._parametersSchemaFromTemplate(typeSchemas);
422422
})
423-
.then(() => tmpl._createViewValidator())
423+
.then(() => tmpl._createParametersValidator())
424424
.then(() => tmpl);
425425
}
426426

@@ -431,7 +431,7 @@ class Template {
431431
const tmpl = new this();
432432
Object.assign(tmpl, obj);
433433
return Promise.resolve()
434-
.then(() => tmpl._createViewValidator())
434+
.then(() => tmpl._createParametersValidator())
435435
.then(() => tmpl);
436436
}
437437

@@ -449,24 +449,24 @@ class Template {
449449
}
450450
}
451451

452-
getViewSchema() {
453-
return Object.assign({}, this._viewSchema, {
452+
getParametersSchema() {
453+
return Object.assign({}, this._parametersSchema, {
454454
title: this.title,
455455
description: this.description,
456456
definitions: this.typeDefinitions
457457
});
458458
}
459459

460-
getCombinedView(view) {
461-
const typeProps = this.getViewSchema().properties;
460+
getCombinedParameters(parameters) {
461+
const typeProps = this.getParametersSchema().properties;
462462
const typeDefaults = typeProps && Object.keys(typeProps).reduce((acc, key) => {
463463
const value = typeProps[key];
464464
if (value.default !== undefined) {
465465
acc[key] = value.default;
466466
}
467467
return acc;
468468
}, {});
469-
return Object.assign({}, typeDefaults || {}, this.defaultView, view || {});
469+
return Object.assign({}, typeDefaults || {}, this.defaultParameters, parameters || {});
470470
}
471471

472472
_getPartials() {
@@ -479,18 +479,18 @@ class Template {
479479
}, {});
480480
}
481481

482-
validateView(view) {
483-
const combView = this.getCombinedView(view);
484-
if (!this._viewValidator(combView)) {
485-
throw new Error(JSON.stringify(this._viewValidator.errors, null, 2));
482+
validateParameters(parameters) {
483+
const combParams = this.getCombinedParameters(parameters);
484+
if (!this._parametersValidator(combParams)) {
485+
throw new Error(JSON.stringify(this._parametersValidator.errors, null, 2));
486486
}
487487
}
488488

489-
transformView(view) {
490-
const schema = this.getViewSchema();
491-
const transform = new JSONViewTransform();
492-
return Object.keys(view).reduce((acc, curr) => {
493-
const value = view[curr];
489+
transformParameters(parameters) {
490+
const schema = this.getParametersSchema();
491+
const transform = new JSONParametersTransform();
492+
return Object.keys(parameters).reduce((acc, curr) => {
493+
const value = parameters[curr];
494494
const valueSchema = schema.properties && schema.properties[curr];
495495

496496
if (valueSchema) {
@@ -507,11 +507,11 @@ class Template {
507507
return text.replace(/{{([_a-zA-Z0-9]+):.*}}/g, '{{$1}}');
508508
}
509509

510-
render(view) {
511-
this.validateView(view);
512-
const xfview = this.transformView(this.getCombinedView(view));
510+
render(parameters) {
511+
this.validateParameters(parameters);
512+
const xfparams = this.transformParameters(this.getCombinedParameters(parameters));
513513
const partials = this._getPartials();
514-
return Mustache.render(this._cleanTemplateText(this.templateText), xfview, partials);
514+
return Mustache.render(this._cleanTemplateText(this.templateText), xfparams, partials);
515515
}
516516
}
517517

0 commit comments

Comments
 (0)