Skip to content

Commit

Permalink
fix: Register Handlebars comparison helpers (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennycode authored Dec 27, 2019
1 parent 792ce81 commit a0ccb85
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 0 additions & 3 deletions src/Swaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import axios from 'axios';
import {isCI} from 'ci-info';
import {getYesNo} from 'cli-interact';
import fs from 'fs-extra';
import initializeHelpers from 'handlebars-helpers';
import path from 'path';
import yaml from 'yamljs';

Expand All @@ -13,8 +12,6 @@ import {DirEntry, generateFileIndex} from './util/FileUtil';
import * as StringUtil from './util/StringUtil';
import {validateConfig} from './validator/SwaggerValidator';

initializeHelpers(['comparison']);

export async function exportServices(swaggerJson: OpenAPIV2.Document): Promise<ResourceGenerator[]> {
const resources: ResourceGenerator[] = [];
const recordedUrls: Record<string, Record<string, OpenAPIV2.PathsObject>> = {};
Expand Down
9 changes: 8 additions & 1 deletion src/generators/TemplateGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs-extra';
import Handlebars from 'handlebars';
import helpers from 'handlebars-helpers';
import path from 'path';
import prettier from 'prettier';

Expand All @@ -9,6 +10,12 @@ export abstract class TemplateGenerator {
protected abstract name: string;
protected abstract templateFile: string;
protected abstract async getContext(): Promise<GeneratorContext>;
private readonly handlebars: typeof Handlebars;

constructor() {
this.handlebars = Handlebars.create();
helpers(['comparison'], {handlebars: this.handlebars});
}

protected getTemplateFile(): string {
return path.resolve(__dirname, '../templates', this.templateFile);
Expand All @@ -19,7 +26,7 @@ export abstract class TemplateGenerator {
const context = await this.getContext();
if (templateFile && context) {
const templateSource = await fs.readFile(templateFile, 'utf8');
const template = Handlebars.compile(templateSource);
const template = this.handlebars.compile(templateSource);
return template(context);
}
return '';
Expand Down

0 comments on commit a0ccb85

Please sign in to comment.