fix(env): update variable validations (#40813)

pull/40464/head
Mrugesh Mohapatra 2021-01-28 13:52:39 +05:30 committed by Mrugesh Mohapatra
parent 58c6c54c67
commit 0b944ddfde
3 changed files with 51 additions and 21 deletions

View File

@ -1,7 +1,7 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
const { environment, clientLocale } = require('../config/env');
const { clientLocale } = require('../config/env');
const { i18nextCodes } = require('./allLangs');
const i18nextCode = i18nextCodes[clientLocale];
@ -20,7 +20,8 @@ i18n.use(initReactI18next).init({
ns: ['translations', 'trending', 'intro'],
defaultNS: 'translations',
returnObjects: true,
debug: environment === 'development',
// Uncomment the next line for debug logging
// debug: true,
interpolation: {
escapeValue: false
},

View File

@ -30,11 +30,13 @@ const {
const locations = {
homeLocation,
chineseHome: !chineseHome ? 'https://chinese.freecodecamp.org' : chineseHome,
apiLocation,
forumLocation,
newsLocation,
radioLocation,
chineseHome
radioLocation: !radioLocation
? 'https://coderadio.freecodecamp.org'
: radioLocation
};
module.exports = Object.assign(locations, {

View File

@ -11,26 +11,32 @@ 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`
);
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`
);
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',
'chineseHome',
'apiLocation',
'forumLocation',
'newsLocation'
'newsLocation',
'radioLocation'
];
const deploymentKeys = [
'clientLocale',
@ -48,28 +54,49 @@ if (FREECODECAMP_NODE_ENV !== 'development') {
searchKeys,
donationKeys
);
const variables = Object.keys(env);
const receivedvariables = Object.keys(env);
expectedVariables.sort();
variables.sort();
if (expectedVariables.length !== variables.length) {
throw Error(`Env. variable validation failed. Expected
${expectedVariables}
but recieved
${variables}
receivedvariables.sort();
if (expectedVariables.length !== receivedvariables.length) {
throw Error(`
Env. variable validation failed. Make sure these keys are used and configured.
Mismatch:
${expectedVariables
.filter(expected => !receivedvariables.includes(expected))
.concat(
receivedvariables.filter(
received => !expectedVariables.includes(received)
)
)}
`);
}
for (const key of expectedVariables) {
if (typeof env[key] === 'undefined' || env[key] === null) {
throw Error(`Env. variable ${key} is missing, build cannot continue`);
throw Error(`
Env. variable ${key} is missing, build cannot continue
`);
}
}
if (env['environment'] !== 'production')
throw Error("Production environment should be 'production' ");
throw Error(`
Production environment should be 'production'
`);
if (env['showUpcomingChanges'])
throw Error("SHOW_UPCOMING_CHANGES should never be 'true' in production");
throw Error(`
SHOW_UPCOMING_CHANGES should never be 'true' in production
`);
checkClientLocale();
checkCurriculumLocale();