/** * Post install script: create or replace the symlink .env * to the .env file matching the current environment * * To force an hostname, define an env variable named HOST : * * HOST=ci node ./env/setupEnv.mjs * */ import os from "os"; import fs from "fs"; import {fileURLToPath} from "node:url"; import path from 'path'; const projectDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..') const hostname = process.env.HOST ?? os.hostname() const environments = { 'app': '.env.docker', 'prod-v2': '.env.prod', 'test-v2': '.env.test', 'test1': '.env.test1', 'test2': '.env.test2', 'test3': '.env.test3', 'test4': '.env.test4', 'test5': '.env.test5', 'ci': '.env.ci', } if (!environments.hasOwnProperty(hostname)) { throw Error("Critical : unknown environment [" + hostname + "]") } const targetEnvFile = path.join(projectDir, 'env', environments[hostname]) const mainEnvFile = path.join(projectDir, '.env') fs.unlink(mainEnvFile, (err) => { // 'ENOENT' is the error code for "no such file or directory", we ignore this one if (err && err.code !== 'ENOENT') { throw err } console.log(`${mainEnvFile} was deleted`); }); fs.symlink( targetEnvFile, '.env', 'file', (err) => { if (err) { throw (err); } console.log(`Symlink created : ${mainEnvFile} -> ${targetEnvFile}`) } )