feat: allow passing custom EJS options

This commit is contained in:
jorenbroekema
2021-04-19 14:34:57 +02:00
committed by Thomas Allmer
parent dbf501d458
commit dcf386a1fe
4 changed files with 32 additions and 19 deletions

View File

@@ -66,16 +66,16 @@ class Generator {
return path.join(this.options.destinationPath, destination); return path.join(this.options.destinationPath, destination);
} }
copyTemplate(from, to) { copyTemplate(from, to, ejsOptions = {}) {
copyTemplate(from, to, this.templateData); copyTemplate(from, to, this.templateData, ejsOptions);
} }
copyTemplateJsonInto(from, to, options = { mode: 'merge' }) { copyTemplateJsonInto(from, to, options = { mode: 'merge' }, ejsOptions = {}) {
copyTemplateJsonInto(from, to, this.templateData, options); copyTemplateJsonInto(from, to, this.templateData, options, ejsOptions);
} }
async copyTemplates(from, to = this.destinationPath()) { async copyTemplates(from, to = this.destinationPath(), ejsOptions = {}) {
return copyTemplates(from, to, this.templateData); return copyTemplates(from, to, this.templateData, ejsOptions);
} }
async end() { async end() {

View File

@@ -89,13 +89,16 @@ export function resetVirtualFiles() {
* processTemplate('prefix <%= name %> suffix', { name: 'foo' }) * processTemplate('prefix <%= name %> suffix', { name: 'foo' })
* // prefix foo suffix * // 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 {string} _fileContent Template as a string
* @param {object} data Object of all the variables to repalce * @param {object} data Object of all the variables to repalce
* @param {ejs.Options} ejsOptions
* @returns {string} Template with all replacements * @returns {string} Template with all replacements
*/ */
export function processTemplate(_fileContent, data = {}) { export function processTemplate(_fileContent, data = {}, ejsOptions = {}) {
let fileContent = _fileContent; let fileContent = _fileContent;
fileContent = render(fileContent, data, { debug: false, filename: 'template' }); fileContent = render(fileContent, data, { debug: false, filename: 'template', ...ejsOptions });
return fileContent; return fileContent;
} }
@@ -342,11 +345,12 @@ export function optionsToCommand(options, generatorName = '@open-wc') {
* @param {string} fromPath * @param {string} fromPath
* @param {string} toPath * @param {string} toPath
* @param {object} data * @param {object} data
* @param {ejs.Options} ejsOptions
*/ */
export function copyTemplate(fromPath, toPath, data) { export function copyTemplate(fromPath, toPath, data, ejsOptions = {}) {
const fileContent = readFileFromPath(fromPath); const fileContent = readFileFromPath(fromPath);
if (fileContent) { if (fileContent) {
const processed = processTemplate(fileContent, data); const processed = processTemplate(fileContent, data, ejsOptions);
writeFileToPath(toPath, processed); writeFileToPath(toPath, processed);
} }
} }
@@ -356,8 +360,9 @@ export function copyTemplate(fromPath, toPath, data) {
* @param {string} fromGlob * @param {string} fromGlob
* @param {string} [toDir] Directory to copy into * @param {string} [toDir] Directory to copy into
* @param {object} data Replace parameters in files * @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 => { return new Promise(resolve => {
glob(fromGlob, { dot: true }, (er, files) => { glob(fromGlob, { dot: true }, (er, files) => {
const copiedFiles = []; const copiedFiles = [];
@@ -365,7 +370,7 @@ export function copyTemplates(fromGlob, toDir = process.cwd(), data = {}) {
if (!fs.lstatSync(filePath).isDirectory()) { if (!fs.lstatSync(filePath).isDirectory()) {
const fileContent = readFileFromPath(filePath); const fileContent = readFileFromPath(filePath);
if (fileContent !== false) { if (fileContent !== false) {
const processed = processTemplate(fileContent, data); const processed = processTemplate(fileContent, data, ejsOptions);
// find path write to (force / also on windows) // find path write to (force / also on windows)
const replace = path.join(fromGlob.replace(/\*/g, '')).replace(/\\(?! )/g, '/'); const replace = path.join(fromGlob.replace(/\*/g, '')).replace(/\\(?! )/g, '/');
@@ -386,18 +391,20 @@ export function copyTemplates(fromGlob, toDir = process.cwd(), data = {}) {
* @param {string} fromPath * @param {string} fromPath
* @param {string} toPath * @param {string} toPath
* @param {object} data * @param {object} data
* @param {ejs.Options} ejsOptions
*/ */
export function copyTemplateJsonInto( export function copyTemplateJsonInto(
fromPath, fromPath,
toPath, toPath,
data = {}, data = {},
{ mode = 'merge' } = { mode: 'merge' }, { mode = 'merge' } = { mode: 'merge' },
ejsOptions = {},
) { ) {
const content = readFileFromPath(fromPath); const content = readFileFromPath(fromPath);
if (content === false) { if (content === false) {
return; return;
} }
const processed = processTemplate(content, data); const processed = processTemplate(content, data, ejsOptions);
const mergeMeObj = JSON.parse(processed); const mergeMeObj = JSON.parse(processed);
const overwriteMerge = (destinationArray, sourceArray) => sourceArray; const overwriteMerge = (destinationArray, sourceArray) => sourceArray;

View File

@@ -40,6 +40,12 @@ describe('processTemplate', () => {
expect(e).to.be.an.instanceof(ReferenceError); 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', () => { describe('writeFileToPath', () => {

View File

@@ -1,19 +1,19 @@
{ {
"devDependencies": { "devDependencies": {
"eslint": "^7.18.0", "eslint": "^7.24.0",
"@open-wc/eslint-config": "^4.2.0", "@open-wc/eslint-config": "^4.2.0",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"eslint-config-prettier": "^7.2.0", "eslint-config-prettier": "^7.2.0",
"husky": "^4.3.8", "husky": "^4.3.8",
"lint-staged": "^10.5.3", "lint-staged": "^10.5.4",
"@web/test-runner": "^0.12.7", "@web/test-runner": "^0.12.20",
"@open-wc/testing": "^2.5.32", "@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", "@open-wc/building-rollup": "^1.9.4",
"deepmerge": "^4.2.2", "deepmerge": "^4.2.2",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rollup": "^2.38.0", "rollup": "^2.45.2",
"@web/dev-server": "^0.1.5" "@web/dev-server": "^0.1.12"
}, },
"scripts": { "scripts": {
"lint": "eslint --ext .js,.html . --ignore-path .gitignore && prettier \"**/*.js\" --check --ignore-path .gitignore", "lint": "eslint --ext .js,.html . --ignore-path .gitignore && prettier \"**/*.js\" --check --ignore-path .gitignore",