版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.js 2.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const fs = require('fs')
  2. const path = require('path')
  3. const os = require('os')
  4. const packageJson = require('../package.json')
  5. const version = packageJson.version
  6. const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg
  7. // Parser src into an Object
  8. function parse (src) {
  9. const obj = {}
  10. // Convert buffer to string
  11. let lines = src.toString()
  12. // Convert line breaks to same format
  13. lines = lines.replace(/\r\n?/mg, '\n')
  14. let match
  15. while ((match = LINE.exec(lines)) != null) {
  16. const key = match[1]
  17. // Default undefined or null to empty string
  18. let value = (match[2] || '')
  19. // Remove whitespace
  20. value = value.trim()
  21. // Check if double quoted
  22. const maybeQuote = value[0]
  23. // Remove surrounding quotes
  24. value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2')
  25. // Expand newlines if double quoted
  26. if (maybeQuote === '"') {
  27. value = value.replace(/\\n/g, '\n')
  28. value = value.replace(/\\r/g, '\r')
  29. }
  30. // Add to object
  31. obj[key] = value
  32. }
  33. return obj
  34. }
  35. function _log (message) {
  36. console.log(`[dotenv@${version}][DEBUG] ${message}`)
  37. }
  38. function _resolveHome (envPath) {
  39. return envPath[0] === '~' ? path.join(os.homedir(), envPath.slice(1)) : envPath
  40. }
  41. // Populates process.env from .env file
  42. function config (options) {
  43. let dotenvPath = path.resolve(process.cwd(), '.env')
  44. let encoding = 'utf8'
  45. const debug = Boolean(options && options.debug)
  46. const override = Boolean(options && options.override)
  47. if (options) {
  48. if (options.path != null) {
  49. dotenvPath = _resolveHome(options.path)
  50. }
  51. if (options.encoding != null) {
  52. encoding = options.encoding
  53. }
  54. }
  55. try {
  56. // Specifying an encoding returns a string instead of a buffer
  57. const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }))
  58. Object.keys(parsed).forEach(function (key) {
  59. if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
  60. process.env[key] = parsed[key]
  61. } else {
  62. if (override === true) {
  63. process.env[key] = parsed[key]
  64. }
  65. if (debug) {
  66. if (override === true) {
  67. _log(`"${key}" is already defined in \`process.env\` and WAS overwritten`)
  68. } else {
  69. _log(`"${key}" is already defined in \`process.env\` and was NOT overwritten`)
  70. }
  71. }
  72. }
  73. })
  74. return { parsed }
  75. } catch (e) {
  76. if (debug) {
  77. _log(`Failed to load ${dotenvPath} ${e.message}`)
  78. }
  79. return { error: e }
  80. }
  81. }
  82. const DotenvModule = {
  83. config,
  84. parse
  85. }
  86. module.exports.config = DotenvModule.config
  87. module.exports.parse = DotenvModule.parse
  88. module.exports = DotenvModule