@adp-psych/container-tools
Version:
Tools for using containers for psychology experiments
113 lines (104 loc) • 3.45 kB
JavaScript
/*
* Copyright (C) 2020, 2021 Anthony Di Pietro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @file Renders [EJS](https://ejs.co/) templates.
*
* @module module:render-ejs
* @author Anthony Di Pietro <anthony.dipietro@research.uwa.edu.au>
* @copyright © 2020, 2021 Anthony Di Pietro
* @license AGPL-3.0-or-later
*/
/* eslint-disable security/detect-non-literal-fs-filename -- Trusted source. */
const fs = require('node:fs/promises');
const path = require('node:path');
const pkgBasename = require('@adp-psych/pkg-basename');
const ejs = require('ejs');
const readPkgUp = require('read-pkg-up');
/**
* Renders a template to an output file.
*
* @static
* @access protected
* @param {String} templatePath - The path of the template file.
* @param {String} outputPath - The path of the output file.
* @param {String} packageBasename - The basename of the current package.
* @param {Object} packageJson - The `package.json` data of the current package.
* @example
* // Renders experiment.yaml.ejs to experiment.yaml.
* render(
* 'experiment.yaml.ejs',
* 'experiment.yaml',
* pkgBasename(),
* await readPkgUp(),
* );
*/
const render = async (
templatePath, outputPath, packageBasename, packageJson,
) => {
const output = await ejs.renderFile(
templatePath,
{packageBasename, packageJson},
{},
);
await fs.writeFile(outputPath, output);
};
/**
* Renders a template to an output file.
*
* @static
* @param {String} templatePath - The path of the template file.
* @param {String} outputPath - The path of the output file.
* @example
* // Renders experiment.yaml.ejs to experiment.yaml.
* renderEjs('experiment.yaml.ejs', 'experiment.yaml');
*/
const renderEjs = async (templatePath, outputPath) => {
const packageBasename = pkgBasename();
const {packageJson} = await readPkgUp();
const output = await ejs.renderFile(
templatePath,
{packageBasename, packageJson},
{},
);
await fs.writeFile(outputPath, output);
};
/**
* Renders all templates in a directory to an output directory.
*
* @static
* @param {String} templateDirectory - The template directory.
* @param {String} outputDirectory - The output directory.
* @example
* // Renders all templates in kubernetes to build/kubernetes.
* renderEjs('kubernetes', 'build/kubernetes');
*/
const renderAllEjs = async (templateDirectory, outputDirectory) => {
const packageBasename = pkgBasename();
const {packageJson} = await readPkgUp();
await fs.mkdir(outputDirectory, {'recursive': true});
const filenames = await fs.readdir(templateDirectory);
await Promise.all(filenames.map((filename) => render(
path.join(templateDirectory, filename),
path.join(outputDirectory, path.basename(filename, '.ejs')),
packageBasename,
packageJson,
)));
};
module.exports = {
renderAllEjs,
renderEjs,
};