setupEnv.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Post install script: create or replace the symlink .env
  3. * to the .env file matching the current environment
  4. *
  5. * To force a hostname, define an env variable named HOST :
  6. *
  7. * HOST=ci node ./env/setupEnv.mjs
  8. *
  9. */
  10. import os from "os";
  11. import fs from "fs";
  12. import {fileURLToPath} from "node:url";
  13. import path from 'path';
  14. const projectDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..')
  15. const hostname = process.env.HOST ?? os.hostname()
  16. const environments = {
  17. 'site_logiciels': '.env.docker',
  18. 'prod-v2': '.env.prod',
  19. 'test-v2': '.env.test',
  20. 'test1': '.env.test1',
  21. 'test2': '.env.test2',
  22. 'test3': '.env.test3',
  23. 'test4': '.env.test4',
  24. 'test5': '.env.test5',
  25. 'test6': '.env.test6',
  26. 'test7': '.env.test7',
  27. 'test8': '.env.test8',
  28. 'test9': '.env.test9',
  29. 'ci': '.env.ci',
  30. }
  31. if (!environments.hasOwnProperty(hostname)) {
  32. throw Error("Critical : unknown environment [" + hostname + "]")
  33. }
  34. const targetEnvFile = path.join(projectDir, 'env', environments[hostname])
  35. const mainEnvFile = path.join(projectDir, '.env')
  36. fs.unlink(mainEnvFile, (err) => {
  37. // 'ENOENT' is the error code for "no such file or directory", we ignore this one
  38. if (err && err.code !== 'ENOENT') {
  39. throw err
  40. }
  41. console.log(`${mainEnvFile} was deleted`);
  42. });
  43. fs.symlink(
  44. targetEnvFile,
  45. '.env',
  46. 'file',
  47. (err) => {
  48. if (err) {
  49. throw (err);
  50. }
  51. console.log(`Symlink created : ${mainEnvFile} -> ${targetEnvFile}`)
  52. }
  53. )