2021-11-19 14:19:40 +00:00
|
|
|
import { spawn } from 'child_process';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2018-10-07 23:00:50 +00:00
|
|
|
|
2021-11-19 14:19:40 +00:00
|
|
|
import { availableLangs } from '../../../config/i18n/all-langs';
|
|
|
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
const env = require('../../../config/read-env.js');
|
2020-11-25 17:45:59 +00:00
|
|
|
|
2019-06-16 23:46:35 +00:00
|
|
|
const globalConfigPath = path.resolve(__dirname, '../../../config');
|
2018-10-07 23:00:50 +00:00
|
|
|
|
2020-08-26 13:53:56 +00:00
|
|
|
const { FREECODECAMP_NODE_ENV } = process.env;
|
|
|
|
|
2020-12-16 08:02:52 +00:00
|
|
|
function checkClientLocale() {
|
2022-01-25 10:34:16 +00:00
|
|
|
if (!process.env.CLIENT_LOCALE) throw Error('CLIENT_LOCALE is not set');
|
2020-12-16 08:02:52 +00:00
|
|
|
if (!availableLangs.client.includes(process.env.CLIENT_LOCALE)) {
|
2021-01-28 08:22:39 +00:00
|
|
|
throw Error(`
|
|
|
|
|
2021-11-25 11:01:50 +00:00
|
|
|
CLIENT_LOCALE, ${process.env.CLIENT_LOCALE}, is not an available language in config/i18n/all-langs.ts
|
2021-01-28 08:22:39 +00:00
|
|
|
|
|
|
|
`);
|
2020-12-16 08:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-29 04:04:22 +00:00
|
|
|
function checkCurriculumLocale() {
|
2022-01-25 10:34:16 +00:00
|
|
|
if (!process.env.CURRICULUM_LOCALE)
|
|
|
|
throw Error('CURRICULUM_LOCALE is not set');
|
2020-12-29 04:04:22 +00:00
|
|
|
if (!availableLangs.curriculum.includes(process.env.CURRICULUM_LOCALE)) {
|
2021-01-28 08:22:39 +00:00
|
|
|
throw Error(`
|
|
|
|
|
2021-11-25 11:01:50 +00:00
|
|
|
CURRICULUM_LOCALE, ${process.env.CURRICULUM_LOCALE}, is not an available language in config/i18n/all-langs.ts
|
2021-01-28 08:22:39 +00:00
|
|
|
|
|
|
|
`);
|
2020-12-29 04:04:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 17:45:59 +00:00
|
|
|
if (FREECODECAMP_NODE_ENV !== 'development') {
|
2020-08-26 13:53:56 +00:00
|
|
|
const locationKeys = [
|
|
|
|
'homeLocation',
|
|
|
|
'apiLocation',
|
|
|
|
'forumLocation',
|
2021-01-28 08:22:39 +00:00
|
|
|
'newsLocation',
|
|
|
|
'radioLocation'
|
2020-08-26 13:53:56 +00:00
|
|
|
];
|
2020-09-24 14:34:27 +00:00
|
|
|
const deploymentKeys = [
|
2020-12-16 08:02:52 +00:00
|
|
|
'clientLocale',
|
|
|
|
'curriculumLocale',
|
|
|
|
'showLocaleDropdownMenu',
|
2020-09-24 14:34:27 +00:00
|
|
|
'deploymentEnv',
|
|
|
|
'environment',
|
2021-12-09 18:31:03 +00:00
|
|
|
'showUpcomingChanges',
|
|
|
|
'showNewCurriculum'
|
2020-09-24 14:34:27 +00:00
|
|
|
];
|
2020-08-26 13:53:56 +00:00
|
|
|
const searchKeys = ['algoliaAppId', 'algoliaAPIKey'];
|
2021-11-10 16:21:50 +00:00
|
|
|
const donationKeys = ['stripePublicKey', 'paypalClientId', 'patreonClientId'];
|
2022-04-15 14:54:02 +00:00
|
|
|
const loggingKeys = ['sentryClientDSN'];
|
2020-08-26 13:53:56 +00:00
|
|
|
|
|
|
|
const expectedVariables = locationKeys.concat(
|
|
|
|
deploymentKeys,
|
|
|
|
searchKeys,
|
2022-04-15 14:54:02 +00:00
|
|
|
donationKeys,
|
|
|
|
loggingKeys
|
2020-08-26 13:53:56 +00:00
|
|
|
);
|
2021-11-24 14:09:45 +00:00
|
|
|
const receivedvariables = Object.keys(env as Record<string, unknown>);
|
2020-08-26 13:53:56 +00:00
|
|
|
expectedVariables.sort();
|
2021-01-28 08:22:39 +00:00
|
|
|
receivedvariables.sort();
|
|
|
|
if (expectedVariables.length !== receivedvariables.length) {
|
2021-11-19 14:19:40 +00:00
|
|
|
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
2021-01-28 08:22:39 +00:00
|
|
|
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 13:53:56 +00:00
|
|
|
`);
|
2021-11-19 14:19:40 +00:00
|
|
|
/* eslint-enable @typescript-eslint/restrict-template-expressions */
|
2020-08-26 13:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of expectedVariables) {
|
2021-11-19 14:19:40 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
2020-09-24 14:34:27 +00:00
|
|
|
if (typeof env[key] === 'undefined' || env[key] === null) {
|
2021-01-28 08:22:39 +00:00
|
|
|
throw Error(`
|
|
|
|
|
|
|
|
Env. variable ${key} is missing, build cannot continue
|
|
|
|
|
|
|
|
`);
|
2020-08-26 13:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 19:10:17 +00:00
|
|
|
|
|
|
|
if (env['environment'] !== 'production')
|
2021-01-28 08:22:39 +00:00
|
|
|
throw Error(`
|
|
|
|
|
2021-11-19 14:19:40 +00:00
|
|
|
Production environment should be 'production'
|
2021-01-28 08:22:39 +00:00
|
|
|
|
2021-11-19 14:19:40 +00:00
|
|
|
`);
|
2020-09-25 19:10:17 +00:00
|
|
|
|
|
|
|
if (env['showUpcomingChanges'])
|
2021-01-28 08:22:39 +00:00
|
|
|
throw Error(`
|
|
|
|
|
2021-11-19 14:19:40 +00:00
|
|
|
SHOW_UPCOMING_CHANGES should never be 'true' in production
|
2021-01-28 08:22:39 +00:00
|
|
|
|
2021-11-19 14:19:40 +00:00
|
|
|
`);
|
2020-12-16 08:02:52 +00:00
|
|
|
|
|
|
|
checkClientLocale();
|
2020-12-29 04:04:22 +00:00
|
|
|
checkCurriculumLocale();
|
2020-11-25 17:45:59 +00:00
|
|
|
} else {
|
2020-12-16 08:02:52 +00:00
|
|
|
checkClientLocale();
|
2020-12-29 04:04:22 +00:00
|
|
|
checkCurriculumLocale();
|
2021-10-06 15:37:08 +00:00
|
|
|
if (fs.existsSync(`${globalConfigPath}/env.json`)) {
|
2021-12-09 18:31:03 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment */
|
|
|
|
const {
|
|
|
|
showNewCurriculum,
|
|
|
|
showUpcomingChanges
|
|
|
|
} = require(`${globalConfigPath}/env.json`);
|
|
|
|
/* eslint-enable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment */
|
|
|
|
if (
|
|
|
|
env['showUpcomingChanges'] !== showUpcomingChanges ||
|
|
|
|
env['showNewCurriculum'] !== showNewCurriculum
|
|
|
|
) {
|
2021-11-19 14:19:40 +00:00
|
|
|
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
|
2021-12-09 18:31:03 +00:00
|
|
|
console.log('Feature flags have been changed, cleaning client cache.');
|
2021-10-06 15:37:08 +00:00
|
|
|
const child = spawn('npm', ['run', 'clean:client']);
|
|
|
|
child.stdout.setEncoding('utf8');
|
|
|
|
child.stdout.on('data', function (data) {
|
|
|
|
console.log(data);
|
|
|
|
});
|
|
|
|
child.on('error', err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-08-26 13:53:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 11:16:14 +00:00
|
|
|
fs.writeFileSync(`${globalConfigPath}/env.json`, JSON.stringify(env));
|