freeCodeCamp/tools/scripts/ensure-env.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
2018-09-06 15:18:48 +01:00
const path = require('path');
const debug = require('debug');
const envPath = path.resolve(__dirname, '../../.env');
require('dotenv').config({ path: envPath });
const { createRedirects } = require('./createRedirects');
const log = debug('fcc:tools:ensure-env');
const {
HOME_LOCATION: home,
API_LOCATION: api,
FORUM_LOCATION: forum,
FORUM_PROXY_LOCATION: forumProxy,
2018-10-20 23:59:07 +05:30
LOCALE: locale,
NODE_ENV: NODE_ENV
} = process.env;
const locations = {
homeLocation: home,
apiLocation: api,
forumLocation: forum,
forumProxyLocation: forumProxy
};
2018-09-27 20:21:53 +05:30
const apiPath = path.resolve(__dirname, '../../api-server');
const clientPath = path.resolve(__dirname, '../../client');
const clientStaticPath = path.resolve(clientPath, 'static');
const globalConfigPath = path.resolve(__dirname, '../../config');
const env = Object.assign(locations, {locale});
2018-10-20 23:59:07 +05:30
if (NODE_ENV === 'production') {
const redirects = createRedirects({ api, home, forum, forumProxy });
fs.writeFile(`${clientStaticPath}/_redirects`, redirects, function(err) {
if (err) {
log('Error');
console.error(err);
}
log('_redirects written');
});
} else {
log(`ignoring creation of redirect file in ${NODE_ENV}`);
}
2018-09-06 15:18:48 +01:00
fs.access(`${apiPath}/server/resources/pathMigration.json`, err => {
if (err) {
log('creating pathMigration');
return fs.writeFileSync(
`${apiPath}/server/resources/pathMigration.json`,
'{}'
);
}
log('pathMigration present');
return null;
});
2018-09-27 12:28:31 +05:30
fs.writeFileSync(`${clientPath}/config/env.json`, JSON.stringify(env));
fs.writeFileSync(`${globalConfigPath}/env.json`, JSON.stringify(env));