10c9d11e15
In certain scenarios, the front directory may not be writable. When this is the case, writing the patched `index.html` should be skipped just like when the file does not exist. For brevity, I have replaced the `existsSync` check with a try-catch that catches any errors occuring during read or write of the index file. The alternative would be `fs.accessSync` with `W_OK`, but that would still throw an error if the file is not writable so I think it is reasonable to skip it altogether and go straight for the read and write attempts. A specific scenario where the front directory is immutable is NixOS, where the directory may be located in the read-only nix store.
44 lines
1.1 KiB
TypeScript
44 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');
|
|
|
|
try {
|
|
let indexContent = fs.readFileSync(indexPath, 'utf8');
|
|
|
|
indexContent = indexContent.replace(
|
|
/<!-- BEGIN: Twenty Config -->[\s\S]*?<!-- END: Twenty Config -->/,
|
|
configString,
|
|
);
|
|
|
|
fs.writeFileSync(indexPath, indexContent, 'utf8');
|
|
} catch {
|
|
// eslint-disable-next-line no-console
|
|
console.log(
|
|
'Frontend build not found or not writable, assuming it is served independently',
|
|
);
|
|
}
|
|
}
|