Skip to content

Commit dcf386a

Browse files
jorenbroekemadaKmoR
authored andcommitted
feat: allow passing custom EJS options
1 parent dbf501d commit dcf386a

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

src/Generator.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ class Generator {
6666
return path.join(this.options.destinationPath, destination);
6767
}
6868

69-
copyTemplate(from, to) {
70-
copyTemplate(from, to, this.templateData);
69+
copyTemplate(from, to, ejsOptions = {}) {
70+
copyTemplate(from, to, this.templateData, ejsOptions);
7171
}
7272

73-
copyTemplateJsonInto(from, to, options = { mode: 'merge' }) {
74-
copyTemplateJsonInto(from, to, this.templateData, options);
73+
copyTemplateJsonInto(from, to, options = { mode: 'merge' }, ejsOptions = {}) {
74+
copyTemplateJsonInto(from, to, this.templateData, options, ejsOptions);
7575
}
7676

77-
async copyTemplates(from, to = this.destinationPath()) {
78-
return copyTemplates(from, to, this.templateData);
77+
async copyTemplates(from, to = this.destinationPath(), ejsOptions = {}) {
78+
return copyTemplates(from, to, this.templateData, ejsOptions);
7979
}
8080

8181
async end() {

src/core.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,16 @@ export function resetVirtualFiles() {
8989
* processTemplate('prefix <%= name %> suffix', { name: 'foo' })
9090
* // prefix foo suffix
9191
*
92+
* It's also possible to pass custom options to EJS render like changing the delimiter of tags.
93+
*
9294
* @param {string} _fileContent Template as a string
9395
* @param {object} data Object of all the variables to repalce
96+
* @param {ejs.Options} ejsOptions
9497
* @returns {string} Template with all replacements
9598
*/
96-
export function processTemplate(_fileContent, data = {}) {
99+
export function processTemplate(_fileContent, data = {}, ejsOptions = {}) {
97100
let fileContent = _fileContent;
98-
fileContent = render(fileContent, data, { debug: false, filename: 'template' });
101+
fileContent = render(fileContent, data, { debug: false, filename: 'template', ...ejsOptions });
99102
return fileContent;
100103
}
101104

@@ -342,11 +345,12 @@ export function optionsToCommand(options, generatorName = '@open-wc') {
342345
* @param {string} fromPath
343346
* @param {string} toPath
344347
* @param {object} data
348+
* @param {ejs.Options} ejsOptions
345349
*/
346-
export function copyTemplate(fromPath, toPath, data) {
350+
export function copyTemplate(fromPath, toPath, data, ejsOptions = {}) {
347351
const fileContent = readFileFromPath(fromPath);
348352
if (fileContent) {
349-
const processed = processTemplate(fileContent, data);
353+
const processed = processTemplate(fileContent, data, ejsOptions);
350354
writeFileToPath(toPath, processed);
351355
}
352356
}
@@ -356,16 +360,17 @@ export function copyTemplate(fromPath, toPath, data) {
356360
* @param {string} fromGlob
357361
* @param {string} [toDir] Directory to copy into
358362
* @param {object} data Replace parameters in files
363+
* @param {ejs.Options} ejsOptions
359364
*/
360-
export function copyTemplates(fromGlob, toDir = process.cwd(), data = {}) {
365+
export function copyTemplates(fromGlob, toDir = process.cwd(), data = {}, ejsOptions = {}) {
361366
return new Promise(resolve => {
362367
glob(fromGlob, { dot: true }, (er, files) => {
363368
const copiedFiles = [];
364369
files.forEach(filePath => {
365370
if (!fs.lstatSync(filePath).isDirectory()) {
366371
const fileContent = readFileFromPath(filePath);
367372
if (fileContent !== false) {
368-
const processed = processTemplate(fileContent, data);
373+
const processed = processTemplate(fileContent, data, ejsOptions);
369374

370375
// find path write to (force / also on windows)
371376
const replace = path.join(fromGlob.replace(/\*/g, '')).replace(/\\(?! )/g, '/');
@@ -386,18 +391,20 @@ export function copyTemplates(fromGlob, toDir = process.cwd(), data = {}) {
386391
* @param {string} fromPath
387392
* @param {string} toPath
388393
* @param {object} data
394+
* @param {ejs.Options} ejsOptions
389395
*/
390396
export function copyTemplateJsonInto(
391397
fromPath,
392398
toPath,
393399
data = {},
394400
{ mode = 'merge' } = { mode: 'merge' },
401+
ejsOptions = {},
395402
) {
396403
const content = readFileFromPath(fromPath);
397404
if (content === false) {
398405
return;
399406
}
400-
const processed = processTemplate(content, data);
407+
const processed = processTemplate(content, data, ejsOptions);
401408
const mergeMeObj = JSON.parse(processed);
402409

403410
const overwriteMerge = (destinationArray, sourceArray) => sourceArray;

test/core.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ describe('processTemplate', () => {
4040
expect(e).to.be.an.instanceof(ReferenceError);
4141
}
4242
});
43+
44+
it('allows passing custom EJS options like changing the delimiter', async () => {
45+
expect(
46+
processTemplate('prefix <?= name ?> suffix', { name: 'foo' }, { delimiter: '?' }),
47+
).to.equal('prefix foo suffix');
48+
});
4349
});
4450

4551
describe('writeFileToPath', () => {

test/snapshots/fully-loaded-app/package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"devDependencies": {
3-
"eslint": "^7.18.0",
3+
"eslint": "^7.24.0",
44
"@open-wc/eslint-config": "^4.2.0",
55
"prettier": "^2.2.1",
66
"eslint-config-prettier": "^7.2.0",
77
"husky": "^4.3.8",
8-
"lint-staged": "^10.5.3",
9-
"@web/test-runner": "^0.12.7",
8+
"lint-staged": "^10.5.4",
9+
"@web/test-runner": "^0.12.20",
1010
"@open-wc/testing": "^2.5.32",
11-
"@web/dev-server-storybook": "^0.3.3",
11+
"@web/dev-server-storybook": "^0.3.5",
1212
"@open-wc/building-rollup": "^1.9.4",
1313
"deepmerge": "^4.2.2",
1414
"rimraf": "^3.0.2",
15-
"rollup": "^2.38.0",
16-
"@web/dev-server": "^0.1.5"
15+
"rollup": "^2.45.2",
16+
"@web/dev-server": "^0.1.12"
1717
},
1818
"scripts": {
1919
"lint": "eslint --ext .js,.html . --ignore-path .gitignore && prettier \"**/*.js\" --check --ignore-path .gitignore",

0 commit comments

Comments
 (0)