Skip to content

Commit b416a1f

Browse files
committed
chore: migrate to handlebars
1 parent 38fcbeb commit b416a1f

35 files changed

+135
-173
lines changed

package-lock.json

Lines changed: 66 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@
309309
"deploy": "vsce publish"
310310
},
311311
"devDependencies": {
312-
"@types/ejs": "^3.1.5",
313312
"@types/lodash-es": "^4.17.12",
314313
"@types/mocha": "^10.0.10",
315314
"@types/node": "20.x",
@@ -336,16 +335,17 @@
336335
"vsce": "^2.15.0"
337336
},
338337
"dependencies": {
338+
"@types/handlebars": "^4.1.0",
339339
"@vscode-elements/elements": "^1.11.0",
340340
"@xml-tools/ast": "^5.0.5",
341341
"@xml-tools/content-assist": "^3.1.11",
342342
"@xml-tools/parser": "^1.0.11",
343343
"@xml-tools/simple-schema": "^3.0.5",
344344
"@xml-tools/validation": "^1.0.16",
345-
"ejs": "^3.1.10",
346345
"fast-xml-parser": "^4.5.1",
347346
"formik": "^2.4.6",
348347
"glob": "^11.0.1",
348+
"handlebars": "^4.7.8",
349349
"indent-string": "^5.0.0",
350350
"lodash-es": "^4.17.21",
351351
"minimatch": "^10.0.1",
Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
import { render } from 'ejs';
1+
import { create } from 'handlebars';
22
import { resolve } from 'path';
33
import FileSystem from 'util/FileSystem';
4-
import Logger from 'util/Logger';
54
import { Uri } from 'vscode';
5+
import Logger from 'util/Logger';
66

77
export default class GenerateFromTemplate {
8-
public static async generate(template: string, data?: any): Promise<string> {
8+
public static async generate(template: string, data?: Record<string, any>): Promise<string> {
99
try {
1010
const templatePath = this.getTemplatePath(template);
1111
const templateContent = await FileSystem.readFile(Uri.file(templatePath));
12-
const content = render(templateContent, data);
12+
const handlebars = create();
13+
this.registerHelpers(handlebars);
14+
this.registerPartials(handlebars);
15+
const compiledTemplate = handlebars.compile(templateContent);
16+
const content = compiledTemplate(data);
1317
return content;
1418
} catch (error) {
15-
console.error(error);
19+
Logger.log('Failed to generate template', String(error));
1620
throw error;
1721
}
1822
}
1923

2024
protected static getTemplatePath(templateName: string): string {
21-
return resolve(FileSystem.getExtensionPath('templates'), templateName + '.ejs');
25+
return resolve(FileSystem.getExtensionPath('templates/handlebars'), templateName + '.hbs');
26+
}
27+
28+
protected static registerHelpers(handlebars: typeof Handlebars): void {
29+
handlebars.registerHelper('ifeq', (a: string, b: string, options: any) => {
30+
if (a === b) {
31+
return options.fn(this);
32+
}
33+
return options.inverse(this);
34+
});
35+
}
36+
37+
protected static registerPartials(handlebars: typeof Handlebars): void {
38+
handlebars.registerPartial('fileHeader', '{{#if fileHeader}}\n{{{fileHeader}}}\n{{/if}}');
2239
}
2340
}
File renamed without changes.
File renamed without changes.

templates/license/mit.ejs renamed to templates/handlebars/license/mit.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright © <%= year %>-present <%= copyright %>
1+
Copyright © {{ year }}-present {{ copyright }}
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal
File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
{{> fileHeader}}
4+
declare(strict_types=1);
5+
6+
use Magento\Framework\Component\ComponentRegistrar;
7+
8+
ComponentRegistrar::register(ComponentRegistrar::MODULE, '{{ vendor }}_{{ module }}', __DIR__);
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?xml version="1.0"?>
2-
<% if (fileHeader) { -%>
3-
<%- fileHeader %>
4-
<% } -%>
2+
{{> fileHeader}}
53
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
64
xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
75
</config>

0 commit comments

Comments
 (0)