2018-09-06 15:31:05 +01:00
|
|
|
const fs = require('fs');
|
2018-09-06 15:18:48 +01:00
|
|
|
const path = require('path');
|
2018-10-08 00:00:50 +01:00
|
|
|
|
2020-08-21 03:59:27 +05:30
|
|
|
const env = require('../../../config/env');
|
2021-02-26 01:32:35 +05:30
|
|
|
const { availableLangs } = require('../../../config/i18n/all-langs');
|
2020-11-25 18:45:59 +01:00
|
|
|
|
2019-06-17 05:16:35 +05:30
|
|
|
const globalConfigPath = path.resolve(__dirname, '../../../config');
|
2018-10-08 00:00:50 +01:00
|
|
|
|
2020-08-26 15:53:56 +02:00
|
|
|
const { FREECODECAMP_NODE_ENV } = process.env;
|
|
|
|
|
2020-12-16 02:02:52 -06:00
|
|
|
function checkClientLocale() {
|
|
|
|
if (!availableLangs.client.includes(process.env.CLIENT_LOCALE)) {
|
2021-01-28 13:52:39 +05:30
|
|
|
throw Error(`
|
|
|
|
|
2021-02-26 01:32:35 +05:30
|
|
|
CLIENT_LOCALE, ${process.env.CLIENT_LOCALE}, is not an available language in config/i18n/all-langs.js
|
2021-01-28 13:52:39 +05:30
|
|
|
|
|
|
|
`);
|
2020-12-16 02:02:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 20:04:22 -08:00
|
|
|
function checkCurriculumLocale() {
|
|
|
|
if (!availableLangs.curriculum.includes(process.env.CURRICULUM_LOCALE)) {
|
2021-01-28 13:52:39 +05:30
|
|
|
throw Error(`
|
|
|
|
|
2021-02-26 01:32:35 +05:30
|
|
|
CURRICULUM_LOCALE, ${process.env.CURRICULUM_LOCALE}, is not an available language in config/i18n/all-langs.js
|
2021-01-28 13:52:39 +05:30
|
|
|
|
|
|
|
`);
|
2020-12-28 20:04:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:45:59 +01:00
|
|
|
if (FREECODECAMP_NODE_ENV !== 'development') {
|
2020-08-26 15:53:56 +02:00
|
|
|
const locationKeys = [
|
|
|
|
'homeLocation',
|
|
|
|
'apiLocation',
|
|
|
|
'forumLocation',
|
2021-01-28 13:52:39 +05:30
|
|
|
'newsLocation',
|
|
|
|
'radioLocation'
|
2020-08-26 15:53:56 +02:00
|
|
|
];
|
2020-09-24 16:34:27 +02:00
|
|
|
const deploymentKeys = [
|
2020-12-16 02:02:52 -06:00
|
|
|
'clientLocale',
|
|
|
|
'curriculumLocale',
|
|
|
|
'showLocaleDropdownMenu',
|
2020-09-24 16:34:27 +02:00
|
|
|
'deploymentEnv',
|
|
|
|
'environment',
|
|
|
|
'showUpcomingChanges'
|
|
|
|
];
|
2020-08-26 15:53:56 +02:00
|
|
|
const searchKeys = ['algoliaAppId', 'algoliaAPIKey'];
|
2021-05-03 11:45:23 +03:00
|
|
|
const donationKeys = ['paypalClientId'];
|
2020-08-26 15:53:56 +02:00
|
|
|
|
|
|
|
const expectedVariables = locationKeys.concat(
|
|
|
|
deploymentKeys,
|
|
|
|
searchKeys,
|
|
|
|
donationKeys
|
|
|
|
);
|
2021-01-28 13:52:39 +05:30
|
|
|
const receivedvariables = Object.keys(env);
|
2020-08-26 15:53:56 +02:00
|
|
|
expectedVariables.sort();
|
2021-01-28 13:52:39 +05:30
|
|
|
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)
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
|
2020-08-26 15:53:56 +02:00
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of expectedVariables) {
|
2020-09-24 16:34:27 +02:00
|
|
|
if (typeof env[key] === 'undefined' || env[key] === null) {
|
2021-01-28 13:52:39 +05:30
|
|
|
throw Error(`
|
|
|
|
|
|
|
|
Env. variable ${key} is missing, build cannot continue
|
|
|
|
|
|
|
|
`);
|
2020-08-26 15:53:56 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 21:10:17 +02:00
|
|
|
|
|
|
|
if (env['environment'] !== 'production')
|
2021-01-28 13:52:39 +05:30
|
|
|
throw Error(`
|
|
|
|
|
|
|
|
Production environment should be 'production'
|
|
|
|
|
|
|
|
`);
|
2020-09-25 21:10:17 +02:00
|
|
|
|
|
|
|
if (env['showUpcomingChanges'])
|
2021-01-28 13:52:39 +05:30
|
|
|
throw Error(`
|
|
|
|
|
|
|
|
SHOW_UPCOMING_CHANGES should never be 'true' in production
|
|
|
|
|
|
|
|
`);
|
2020-12-16 02:02:52 -06:00
|
|
|
|
|
|
|
checkClientLocale();
|
2020-12-28 20:04:22 -08:00
|
|
|
checkCurriculumLocale();
|
2020-11-25 18:45:59 +01:00
|
|
|
} else {
|
2020-12-16 02:02:52 -06:00
|
|
|
checkClientLocale();
|
2020-12-28 20:04:22 -08:00
|
|
|
checkCurriculumLocale();
|
2020-08-26 15:53:56 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:46:14 +05:30
|
|
|
fs.writeFileSync(`${globalConfigPath}/env.json`, JSON.stringify(env));
|