UNPKG

@adp-psych/container-tools

Version:

Tools for using containers for psychology experiments

94 lines (85 loc) 3.07 kB
/* * 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 Builds the [Kubernetes](https://kubernetes.io/) configuration * for an experiment. * * @module module:build-kubernetes-configuration * @requires missing-value-error * @requires render-ejs * @author Anthony Di Pietro <anthony.dipietro@research.uwa.edu.au> * @copyright © 2020, 2021 Anthony Di Pietro * @license AGPL-3.0-or-later */ const fs = require('node:fs/promises'); const path = require('node:path'); const pkgBasename = require('@adp-psych/pkg-basename'); const readPkgUp = require('read-pkg-up'); const missingValueError = require('./missing-value-error.js'); const {renderAllEjs} = require('./render-ejs.js'); /** * The directory containing the [EJS](https://ejs.co/) templates. * * @constant {String} * @static * @access protected */ const TEMPLATE_DIRECTORY = path.join(__dirname, '..', 'ejs', 'kubernetes'); /** * Checks that `package.json` contains the necessary values. * * @static * @access protected * @throws {Error} Throws an error if a value is missing. * @example * checkPackageJson(); // Checks package.json. */ const checkPackageJson = async () => { const packageBasename = pkgBasename(); const {packageJson} = await readPkgUp(); /* eslint-disable unicorn/prefer-switch -- False positive. */ if (packageBasename === '') { missingValueError('package name', 'name'); } else if (typeof packageJson?.containerRegistry === 'undefined') { missingValueError('container registry', 'containerRegistry'); } else if (typeof packageJson?.author?.email === 'undefined') { missingValueError('author email', 'author'); } else if (typeof packageJson?.hostname === 'undefined') { missingValueError('hostname', 'hostname'); } else { /* Do nothing; package.json contains the necessary values. */ } /* eslint-enable unicorn/prefer-switch -- Reenabling. */ }; /** * Builds the Kubernetes configuration for an experiment. * * @static * @param {String} outputDirectory - The output directory. * @example * // Builds the Kubernetes configuration. * buildKubernetesConfiguration('build/kubernetes'); */ const buildKubernetesConfiguration = async (outputDirectory) => { await checkPackageJson(); await fs.rm(outputDirectory, { 'force': true, 'recursive': true, }); await renderAllEjs(TEMPLATE_DIRECTORY, outputDirectory); }; module.exports = buildKubernetesConfiguration;