UNPKG

@adp-psych/container-tools

Version:

Tools for using containers for psychology experiments

119 lines (110 loc) 4.11 kB
/* * Copyright (C) 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 a [Helm](https://helm.sh/) chart for an experiment. * * @module module:build-helm-chart * @requires build-kubernetes-configuration * @requires render-ejs * @author Anthony Di Pietro <anthony.dipietro@research.uwa.edu.au> * @copyright © 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 pkgDir = require('pkg-dir'); const readPkgUp = require('read-pkg-up'); const buildKubernetesConfiguration = require('./build-kubernetes-configuration.js'); 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', 'helm'); /* eslint-disable complexity -- Necessary. */ /** * 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?.version === 'undefined') { missingValueError('version', 'version'); } else if (typeof packageJson?.description === 'undefined') { missingValueError('description', 'description'); } else if (typeof packageJson?.keywords === 'undefined') { missingValueError('keywords', 'keywords'); } else if (typeof packageJson?.homepage === 'undefined') { missingValueError('homepage', 'homepage'); } else if (typeof packageJson?.repository?.url === 'undefined') { missingValueError('repository URL', 'repository.url'); } else if (typeof packageJson?.author?.name === 'undefined') { missingValueError('author name', 'author.name'); } else if (typeof packageJson?.author?.email === 'undefined') { missingValueError('author email', 'author.email'); } else if (typeof packageJson?.author?.url === 'undefined') { missingValueError('author URL', 'author.url'); } else if (typeof packageJson?.license === 'undefined') { missingValueError('license', 'license'); } else { /* Do nothnig; package.json contains the necessary values. */ } /* eslint-enable unicorn/prefer-switch -- Reenabling. */ }; /* eslint-enable complexity -- Reenabling. */ /** * Builds the Helm chart for an experiment. * * @static * @param {String} outputDirectory - The output directory. * @example * // Builds the Helm chart. * buildHelmChart('build/helm'); */ const buildHelmChart = async (outputDirectory) => { await checkPackageJson(); const packageBasename = pkgBasename(); const chartDirectory = path.join(outputDirectory, packageBasename); await fs.rm(chartDirectory, { 'force': true, 'recursive': true, }); await renderAllEjs(TEMPLATE_DIRECTORY, chartDirectory); await fs.copyFile( path.join(await pkgDir(), 'LICENSE.txt'), path.join(chartDirectory, 'LICENSE'), ); await buildKubernetesConfiguration( path.join(chartDirectory, 'templates'), ); }; module.exports = buildHelmChart;