Skip to content

Commit

Permalink
feat: allow passing custom EJS options
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored and daKmoR committed Apr 19, 2021
1 parent dbf501d commit dcf386a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
12 changes: 6 additions & 6 deletions src/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ class Generator {
return path.join(this.options.destinationPath, destination);
}

copyTemplate(from, to) {
copyTemplate(from, to, this.templateData);
copyTemplate(from, to, ejsOptions = {}) {
copyTemplate(from, to, this.templateData, ejsOptions);
}

copyTemplateJsonInto(from, to, options = { mode: 'merge' }) {
copyTemplateJsonInto(from, to, this.templateData, options);
copyTemplateJsonInto(from, to, options = { mode: 'merge' }, ejsOptions = {}) {
copyTemplateJsonInto(from, to, this.templateData, options, ejsOptions);
}

async copyTemplates(from, to = this.destinationPath()) {
return copyTemplates(from, to, this.templateData);
async copyTemplates(from, to = this.destinationPath(), ejsOptions = {}) {
return copyTemplates(from, to, this.templateData, ejsOptions);
}

async end() {
Expand Down
21 changes: 14 additions & 7 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@ export function resetVirtualFiles() {
* processTemplate('prefix <%= name %> suffix', { name: 'foo' })
* // prefix foo suffix
*
* It's also possible to pass custom options to EJS render like changing the delimiter of tags.
*
* @param {string} _fileContent Template as a string
* @param {object} data Object of all the variables to repalce
* @param {ejs.Options} ejsOptions
* @returns {string} Template with all replacements
*/
export function processTemplate(_fileContent, data = {}) {
export function processTemplate(_fileContent, data = {}, ejsOptions = {}) {
let fileContent = _fileContent;
fileContent = render(fileContent, data, { debug: false, filename: 'template' });
fileContent = render(fileContent, data, { debug: false, filename: 'template', ...ejsOptions });
return fileContent;
}

Expand Down Expand Up @@ -342,11 +345,12 @@ export function optionsToCommand(options, generatorName = '@open-wc') {
* @param {string} fromPath
* @param {string} toPath
* @param {object} data
* @param {ejs.Options} ejsOptions
*/
export function copyTemplate(fromPath, toPath, data) {
export function copyTemplate(fromPath, toPath, data, ejsOptions = {}) {
const fileContent = readFileFromPath(fromPath);
if (fileContent) {
const processed = processTemplate(fileContent, data);
const processed = processTemplate(fileContent, data, ejsOptions);
writeFileToPath(toPath, processed);
}
}
Expand All @@ -356,16 +360,17 @@ export function copyTemplate(fromPath, toPath, data) {
* @param {string} fromGlob
* @param {string} [toDir] Directory to copy into
* @param {object} data Replace parameters in files
* @param {ejs.Options} ejsOptions
*/
export function copyTemplates(fromGlob, toDir = process.cwd(), data = {}) {
export function copyTemplates(fromGlob, toDir = process.cwd(), data = {}, ejsOptions = {}) {
return new Promise(resolve => {
glob(fromGlob, { dot: true }, (er, files) => {
const copiedFiles = [];
files.forEach(filePath => {
if (!fs.lstatSync(filePath).isDirectory()) {
const fileContent = readFileFromPath(filePath);
if (fileContent !== false) {
const processed = processTemplate(fileContent, data);
const processed = processTemplate(fileContent, data, ejsOptions);

// find path write to (force / also on windows)
const replace = path.join(fromGlob.replace(/\*/g, '')).replace(/\\(?! )/g, '/');
Expand All @@ -386,18 +391,20 @@ export function copyTemplates(fromGlob, toDir = process.cwd(), data = {}) {
* @param {string} fromPath
* @param {string} toPath
* @param {object} data
* @param {ejs.Options} ejsOptions
*/
export function copyTemplateJsonInto(
fromPath,
toPath,
data = {},
{ mode = 'merge' } = { mode: 'merge' },
ejsOptions = {},
) {
const content = readFileFromPath(fromPath);
if (content === false) {
return;
}
const processed = processTemplate(content, data);
const processed = processTemplate(content, data, ejsOptions);
const mergeMeObj = JSON.parse(processed);

const overwriteMerge = (destinationArray, sourceArray) => sourceArray;
Expand Down
6 changes: 6 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ describe('processTemplate', () => {
expect(e).to.be.an.instanceof(ReferenceError);
}
});

it('allows passing custom EJS options like changing the delimiter', async () => {
expect(
processTemplate('prefix <?= name ?> suffix', { name: 'foo' }, { delimiter: '?' }),
).to.equal('prefix foo suffix');
});
});

describe('writeFileToPath', () => {
Expand Down
12 changes: 6 additions & 6 deletions test/snapshots/fully-loaded-app/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"devDependencies": {
"eslint": "^7.18.0",
"eslint": "^7.24.0",
"@open-wc/eslint-config": "^4.2.0",
"prettier": "^2.2.1",
"eslint-config-prettier": "^7.2.0",
"husky": "^4.3.8",
"lint-staged": "^10.5.3",
"@web/test-runner": "^0.12.7",
"lint-staged": "^10.5.4",
"@web/test-runner": "^0.12.20",
"@open-wc/testing": "^2.5.32",
"@web/dev-server-storybook": "^0.3.3",
"@web/dev-server-storybook": "^0.3.5",
"@open-wc/building-rollup": "^1.9.4",
"deepmerge": "^4.2.2",
"rimraf": "^3.0.2",
"rollup": "^2.38.0",
"@web/dev-server": "^0.1.5"
"rollup": "^2.45.2",
"@web/dev-server": "^0.1.12"
},
"scripts": {
"lint": "eslint --ext .js,.html . --ignore-path .gitignore && prettier \"**/*.js\" --check --ignore-path .gitignore",
Expand Down

0 comments on commit dcf386a

Please sign in to comment.