setupEnv.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Post install script: create or replace the symlink .env
  3. * to the .env file matching the current environment
  4. */
  5. import os from "os";
  6. import fs from "fs";
  7. import {fileURLToPath} from "node:url";
  8. import path from 'path';
  9. const projectDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..')
  10. const hostname = os.hostname()
  11. const environments = {
  12. 'app': '.env.docker',
  13. 'prod-v2': '.env.prod',
  14. 'test-v2': '.env.test',
  15. 'test1': '.env.test1',
  16. 'test2': '.env.test2',
  17. 'test3': '.env.test3',
  18. 'test4': '.env.test4',
  19. 'test5': '.env.test5',
  20. }
  21. if (!environments.hasOwnProperty(hostname)) {
  22. throw Error("Critical : unknown environment")
  23. }
  24. const targetEnvFile = path.join(projectDir, 'env', environments[hostname])
  25. const mainEnvFile = path.join(projectDir, '.env')
  26. fs.unlink(mainEnvFile, (err) => {
  27. // 'ENOENT' is the error code for "no such file or directory"
  28. if (err && err.code === 'ENOENT') {
  29. throw err
  30. }
  31. console.log(`${mainEnvFile} was deleted`);
  32. });
  33. fs.symlink(
  34. targetEnvFile,
  35. '.env',
  36. 'file',
  37. (err) => {
  38. if (err) {
  39. throw (err);
  40. }
  41. console.log(`Symlink created : ${mainEnvFile} -> ${targetEnvFile}`)
  42. }
  43. )