knex/lib/migrations/util/template.js

53 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

const template = require('lodash/template');
const { readFile, writeFile } = require('./fs');
/**
* Light wrapper over lodash templates making it safer to be used with javascript source code.
*
* In particular, doesn't interfere with use of interpolated strings in javascript.
*
* @param {string} content Template source
* @param {_.TemplateOptions} options Template options
*/
const jsSourceTemplate = (content, options) =>
template(content, {
interpolate: /<%=([\s\S]+?)%>/g,
...options,
});
/**
* Compile the contents of specified (javascript) file as a lodash template
*
* @param {string} filePath Path of file to be used as template
* @param {_.TemplateOptions} options Lodash template options
*/
const jsFileTemplate = async (filePath, options) => {
const contentBuffer = await readFile(filePath);
return jsSourceTemplate(contentBuffer.toString(), options);
};
/**
* Write a javascript file using another file as a (lodash) template
2019-10-25 20:17:26 +02:00
*
* @param {string} targetFilePath
* @param {string} sourceFilePath
* @param {_.TemplateOptions} options options passed to lodash templates
*/
const writeJsFileUsingTemplate = async (
targetFilePath,
sourceFilePath,
options,
variables
2019-10-25 20:17:26 +02:00
) =>
writeFile(
targetFilePath,
(await jsFileTemplate(sourceFilePath, options))(variables)
);
module.exports = {
jsSourceTemplate,
jsFileTemplate,
2019-10-25 20:17:26 +02:00
writeJsFileUsingTemplate,
};