## Context Config was programmatically loaded in our datasources however the default behavior of dotenv is to ignore vars if they are already defined. This means we need to be careful about the order of env injection and sometimes it's done at a higher level (for example db:reset will depend on build). To make things easier I'm using the override flag to properly override the PG_DATABASE_URL if different (and to properly work with the 'test' DB instead of 'default' during testing).
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
import { config } from 'dotenv';
|
|
config({
|
|
path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
|
|
override: true,
|
|
});
|
|
|
|
export function generateFrontConfig(): void {
|
|
const configObject = {
|
|
window: {
|
|
_env_: {
|
|
REACT_APP_SERVER_BASE_URL: process.env.SERVER_URL,
|
|
},
|
|
},
|
|
};
|
|
|
|
const configString = `<!-- BEGIN: Twenty Config -->
|
|
<script id="twenty-env-config">
|
|
window._env_ = ${JSON.stringify(configObject.window._env_, null, 2)};
|
|
</script>
|
|
<!-- END: Twenty Config -->`;
|
|
|
|
const distPath = path.join(__dirname, '../..', 'front');
|
|
const indexPath = path.join(distPath, 'index.html');
|
|
|
|
if (!fs.existsSync(indexPath)) {
|
|
// eslint-disable-next-line no-console
|
|
console.log(
|
|
'Frontend build not found, assuming it is served independently',
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
let indexContent = fs.readFileSync(indexPath, 'utf8');
|
|
|
|
indexContent = indexContent.replace(
|
|
/<!-- BEGIN: Twenty Config -->[\s\S]*?<!-- END: Twenty Config -->/,
|
|
configString,
|
|
);
|
|
|
|
fs.writeFileSync(indexPath, indexContent, 'utf8');
|
|
}
|