Files
freeCodeCamp/tools/scripts/build/ensure-env.js
Nicholas Carrigan (he/him) 59173b346a fix(tools): Validate curriculum_locale (#40572)
Adds validation for the `CURRICULUM_LOCALE`
environment variable in the same way the
`CLIENT_LOCALE` value is validated.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-31 12:15:35 +05:30

83 lines
2.3 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const env = require('../../../config/env');
const { availableLangs } = require('../../../client/i18n/allLangs');
const clientPath = path.resolve(__dirname, '../../../client');
const globalConfigPath = path.resolve(__dirname, '../../../config');
const { FREECODECAMP_NODE_ENV } = process.env;
function checkClientLocale() {
if (!availableLangs.client.includes(process.env.CLIENT_LOCALE)) {
throw Error(
`CLIENT_LOCALE, ${process.env.CLIENT_LOCALE}, is not an available language in client/i18n/allLangs.js`
);
}
}
function checkCurriculumLocale() {
if (!availableLangs.curriculum.includes(process.env.CURRICULUM_LOCALE)) {
throw Error(
`CURRICULUM_LOCALE, ${process.env.CURRICULUM_LOCALE}, is not an available language in client/i18n/allLangs.js`
);
}
}
if (FREECODECAMP_NODE_ENV !== 'development') {
const locationKeys = [
'homeLocation',
'apiLocation',
'forumLocation',
'newsLocation'
];
const deploymentKeys = [
'clientLocale',
'curriculumLocale',
'showLocaleDropdownMenu',
'deploymentEnv',
'environment',
'showUpcomingChanges'
];
const searchKeys = ['algoliaAppId', 'algoliaAPIKey'];
const donationKeys = ['stripePublicKey', 'paypalClientId'];
const expectedVariables = locationKeys.concat(
deploymentKeys,
searchKeys,
donationKeys
);
const variables = Object.keys(env);
expectedVariables.sort();
variables.sort();
if (expectedVariables.length !== variables.length) {
throw Error(`Env. variable validation failed. Expected
${expectedVariables}
but recieved
${variables}
`);
}
for (const key of expectedVariables) {
if (typeof env[key] === 'undefined' || env[key] === null) {
throw Error(`Env. variable ${key} is missing, build cannot continue`);
}
}
if (env['environment'] !== 'production')
throw Error("Production environment should be 'production' ");
if (env['showUpcomingChanges'])
throw Error("SHOW_UPCOMING_CHANGES should never be 'true' in production");
checkClientLocale();
checkCurriculumLocale();
} else {
checkClientLocale();
checkCurriculumLocale();
}
fs.writeFileSync(`${clientPath}/config/env.json`, JSON.stringify(env));
fs.writeFileSync(`${globalConfigPath}/env.json`, JSON.stringify(env));