threepipe
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

generate-example-env.mjs 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import fs from 'fs'
  2. import dotenv from 'dotenv'
  3. // read .env file if exists
  4. const envFilePath = './examples/.env'
  5. if (fs.existsSync(envFilePath)) {
  6. dotenv.config({ path: envFilePath })
  7. }
  8. // Parse DOT_ENV_CONTENT environment variable as .env file if it exists(for ci)
  9. if (process.env.DOT_ENV_CONTENT) {
  10. const envConfig = dotenv.parse(process.env.DOT_ENV_CONTENT)
  11. for (const key in envConfig) {
  12. process.env[key] = envConfig[key]
  13. }
  14. }
  15. // Filter EX_ variables from environment
  16. const envVars = Object.entries(process.env)
  17. .filter(([key]) => key.startsWith('TP_EX_'))
  18. .reduce((acc, [key, value]) => {
  19. acc[key.replace('TP_EX_', '')] = value
  20. return acc
  21. }, {'DUMMY': '1'}) // dummy to prevent empty file which is not a module apparently
  22. // Generate globals.js
  23. const jsOutput = Object.entries(envVars)
  24. .map(([key, val]) => `export const ${key} = ${JSON.stringify(val)}`)
  25. .join('\n') + '\n'
  26. fs.writeFileSync('./examples/globals.js', jsOutput, 'utf8')
  27. // Generate globals.d.ts
  28. const dtsOutput = Object.entries(envVars)
  29. .map(([key, value]) => `export const ${key}: ${typeof value}`)
  30. .join('\n') + '\n'
  31. fs.writeFileSync('./examples/globals.d.ts', dtsOutput, 'utf8')
  32. console.log('✅ globals.js and globals.d.ts generated.')