setupEnv.mjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 HOSTNAME :
  6. *
  7. * HOSTNAME=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.HOSTNAME || os.hostname()
  16. const environments = {
  17. app: '.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 (!Object.prototype.hasOwnProperty.call(environments, hostname)) {
  32. throw new 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(`Previous ${mainEnvFile} was deleted`)
  42. fs.symlink(targetEnvFile, '.env', 'file', (err) => {
  43. if (err) {
  44. throw err
  45. }
  46. console.log(`Symlink created : ${mainEnvFile} -> ${targetEnvFile}`)
  47. })
  48. })