@trayai/tray-sync-cli 1.0.12 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/dist/api/resources/me.js +14 -0
  2. package/dist/api/resources/workflow.js +38 -0
  3. package/dist/commands/api/api.js +6 -0
  4. package/dist/commands/api/login.js +91 -0
  5. package/dist/commands/api/logout.js +39 -0
  6. package/dist/commands/api/project.js +2 -2
  7. package/dist/commands/api/shared.js +10 -4
  8. package/dist/commands/api/workflow.js +166 -0
  9. package/dist/lib/apiAuth.js +25 -2
  10. package/dist/lib/apiSessionStore.js +104 -0
  11. package/dist/lib/browserOpener.js +24 -0
  12. package/dist/lib/dotenvEdit.js +31 -0
  13. package/dist/lib/oauth.js +125 -0
  14. package/dist/lib/oauthCallbackServer.js +97 -0
  15. package/dist/lib/oauthRefresh.js +65 -0
  16. package/dist/lib/region.js +1 -0
  17. package/dist/views/api/workflow.v2.js +21 -0
  18. package/node_modules/bundle-name/index.js +5 -0
  19. package/node_modules/bundle-name/license +9 -0
  20. package/node_modules/bundle-name/package.json +45 -0
  21. package/node_modules/bundle-name/readme.md +23 -0
  22. package/node_modules/default-browser/index.d.ts +29 -0
  23. package/node_modules/default-browser/index.js +34 -0
  24. package/node_modules/default-browser/license +9 -0
  25. package/node_modules/default-browser/package.json +54 -0
  26. package/node_modules/default-browser/readme.md +24 -0
  27. package/node_modules/default-browser/windows.js +59 -0
  28. package/node_modules/default-browser-id/index.js +25 -0
  29. package/node_modules/default-browser-id/license +9 -0
  30. package/node_modules/default-browser-id/package.json +41 -0
  31. package/node_modules/default-browser-id/readme.md +19 -0
  32. package/node_modules/define-lazy-prop/index.d.ts +31 -0
  33. package/node_modules/define-lazy-prop/index.js +18 -0
  34. package/node_modules/define-lazy-prop/license +9 -0
  35. package/node_modules/define-lazy-prop/package.json +51 -0
  36. package/node_modules/define-lazy-prop/readme.md +55 -0
  37. package/node_modules/dotenv/CHANGELOG.md +643 -0
  38. package/node_modules/dotenv/LICENSE +23 -0
  39. package/node_modules/dotenv/README-es.md +757 -0
  40. package/node_modules/dotenv/README.md +812 -0
  41. package/node_modules/dotenv/SECURITY.md +1 -0
  42. package/node_modules/dotenv/config.d.ts +1 -0
  43. package/node_modules/dotenv/config.js +9 -0
  44. package/node_modules/dotenv/lib/cli-options.js +17 -0
  45. package/node_modules/dotenv/lib/env-options.js +28 -0
  46. package/node_modules/dotenv/lib/main.d.ts +179 -0
  47. package/node_modules/dotenv/lib/main.js +423 -0
  48. package/node_modules/dotenv/package.json +62 -0
  49. package/node_modules/dotenv/skills/dotenv/SKILL.md +200 -0
  50. package/node_modules/dotenv/skills/dotenvx/SKILL.md +118 -0
  51. package/node_modules/graceful-fs/LICENSE +15 -0
  52. package/node_modules/graceful-fs/README.md +143 -0
  53. package/node_modules/graceful-fs/clone.js +23 -0
  54. package/node_modules/graceful-fs/graceful-fs.js +448 -0
  55. package/node_modules/graceful-fs/legacy-streams.js +118 -0
  56. package/node_modules/graceful-fs/package.json +53 -0
  57. package/node_modules/graceful-fs/polyfills.js +355 -0
  58. package/node_modules/is-docker/cli.js +5 -0
  59. package/node_modules/is-docker/index.d.ts +13 -0
  60. package/node_modules/is-docker/index.js +29 -0
  61. package/node_modules/is-docker/license +9 -0
  62. package/node_modules/is-docker/package.json +44 -0
  63. package/node_modules/is-docker/readme.md +27 -0
  64. package/node_modules/is-in-ssh/index.d.ts +15 -0
  65. package/node_modules/is-in-ssh/index.js +7 -0
  66. package/node_modules/is-in-ssh/license +9 -0
  67. package/node_modules/is-in-ssh/package.json +43 -0
  68. package/node_modules/is-in-ssh/readme.md +43 -0
  69. package/node_modules/is-inside-container/cli.js +5 -0
  70. package/node_modules/is-inside-container/index.d.ts +13 -0
  71. package/node_modules/is-inside-container/index.js +23 -0
  72. package/node_modules/is-inside-container/license +9 -0
  73. package/node_modules/is-inside-container/package.json +51 -0
  74. package/node_modules/is-inside-container/readme.md +36 -0
  75. package/node_modules/is-wsl/index.d.ts +15 -0
  76. package/node_modules/is-wsl/index.js +36 -0
  77. package/node_modules/is-wsl/license +9 -0
  78. package/node_modules/is-wsl/package.json +58 -0
  79. package/node_modules/is-wsl/readme.md +21 -0
  80. package/node_modules/open/index.d.ts +158 -0
  81. package/node_modules/open/index.js +425 -0
  82. package/node_modules/open/license +9 -0
  83. package/node_modules/open/package.json +70 -0
  84. package/node_modules/open/readme.md +197 -0
  85. package/node_modules/open/xdg-open +1267 -0
  86. package/node_modules/powershell-utils/index.d.ts +175 -0
  87. package/node_modules/powershell-utils/index.js +79 -0
  88. package/node_modules/powershell-utils/license +9 -0
  89. package/node_modules/powershell-utils/package.json +47 -0
  90. package/node_modules/powershell-utils/readme.md +209 -0
  91. package/node_modules/proper-lockfile/CHANGELOG.md +108 -0
  92. package/node_modules/proper-lockfile/LICENSE +21 -0
  93. package/node_modules/proper-lockfile/README.md +183 -0
  94. package/node_modules/proper-lockfile/index.js +40 -0
  95. package/node_modules/proper-lockfile/lib/adapter.js +85 -0
  96. package/node_modules/proper-lockfile/lib/lockfile.js +342 -0
  97. package/node_modules/proper-lockfile/lib/mtime-precision.js +55 -0
  98. package/node_modules/proper-lockfile/package.json +71 -0
  99. package/node_modules/retry/.npmignore +3 -0
  100. package/node_modules/retry/.travis.yml +15 -0
  101. package/node_modules/retry/License +21 -0
  102. package/node_modules/retry/Makefile +18 -0
  103. package/node_modules/retry/README.md +227 -0
  104. package/node_modules/retry/equation.gif +0 -0
  105. package/node_modules/retry/example/dns.js +31 -0
  106. package/node_modules/retry/example/stop.js +40 -0
  107. package/node_modules/retry/index.js +1 -0
  108. package/node_modules/retry/lib/retry.js +100 -0
  109. package/node_modules/retry/lib/retry_operation.js +158 -0
  110. package/node_modules/retry/package.json +32 -0
  111. package/node_modules/retry/test/common.js +10 -0
  112. package/node_modules/retry/test/integration/test-forever.js +24 -0
  113. package/node_modules/retry/test/integration/test-retry-operation.js +258 -0
  114. package/node_modules/retry/test/integration/test-retry-wrap.js +101 -0
  115. package/node_modules/retry/test/integration/test-timeouts.js +69 -0
  116. package/node_modules/run-applescript/index.d.ts +66 -0
  117. package/node_modules/run-applescript/index.js +37 -0
  118. package/node_modules/run-applescript/license +9 -0
  119. package/node_modules/run-applescript/package.json +42 -0
  120. package/node_modules/run-applescript/readme.md +78 -0
  121. package/node_modules/signal-exit/LICENSE.txt +16 -0
  122. package/node_modules/signal-exit/README.md +39 -0
  123. package/node_modules/signal-exit/index.js +202 -0
  124. package/node_modules/signal-exit/package.json +38 -0
  125. package/node_modules/signal-exit/signals.js +53 -0
  126. package/node_modules/wsl-utils/index.d.ts +180 -0
  127. package/node_modules/wsl-utils/index.js +145 -0
  128. package/node_modules/wsl-utils/license +9 -0
  129. package/node_modules/wsl-utils/node_modules/powershell-utils/index.d.ts +122 -0
  130. package/node_modules/wsl-utils/node_modules/powershell-utils/index.js +58 -0
  131. package/node_modules/wsl-utils/node_modules/powershell-utils/license +9 -0
  132. package/node_modules/wsl-utils/node_modules/powershell-utils/package.json +47 -0
  133. package/node_modules/wsl-utils/node_modules/powershell-utils/readme.md +153 -0
  134. package/node_modules/wsl-utils/package.json +48 -0
  135. package/node_modules/wsl-utils/readme.md +223 -0
  136. package/node_modules/wsl-utils/utilities.js +19 -0
  137. package/package.json +8 -1
@@ -0,0 +1 @@
1
+ Please report any security vulnerabilities to security@dotenvx.com.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ (function () {
2
+ require('./lib/main').config(
3
+ Object.assign(
4
+ {},
5
+ require('./lib/env-options'),
6
+ require('./lib/cli-options')(process.argv)
7
+ )
8
+ )
9
+ })()
@@ -0,0 +1,17 @@
1
+ const re = /^dotenv_config_(encoding|path|quiet|debug|override|DOTENV_KEY)=(.+)$/
2
+
3
+ module.exports = function optionMatcher (args) {
4
+ const options = args.reduce(function (acc, cur) {
5
+ const matches = cur.match(re)
6
+ if (matches) {
7
+ acc[matches[1]] = matches[2]
8
+ }
9
+ return acc
10
+ }, {})
11
+
12
+ if (!('quiet' in options)) {
13
+ options.quiet = 'true'
14
+ }
15
+
16
+ return options
17
+ }
@@ -0,0 +1,28 @@
1
+ // ../config.js accepts options via environment variables
2
+ const options = {}
3
+
4
+ if (process.env.DOTENV_CONFIG_ENCODING != null) {
5
+ options.encoding = process.env.DOTENV_CONFIG_ENCODING
6
+ }
7
+
8
+ if (process.env.DOTENV_CONFIG_PATH != null) {
9
+ options.path = process.env.DOTENV_CONFIG_PATH
10
+ }
11
+
12
+ if (process.env.DOTENV_CONFIG_QUIET != null) {
13
+ options.quiet = process.env.DOTENV_CONFIG_QUIET
14
+ }
15
+
16
+ if (process.env.DOTENV_CONFIG_DEBUG != null) {
17
+ options.debug = process.env.DOTENV_CONFIG_DEBUG
18
+ }
19
+
20
+ if (process.env.DOTENV_CONFIG_OVERRIDE != null) {
21
+ options.override = process.env.DOTENV_CONFIG_OVERRIDE
22
+ }
23
+
24
+ if (process.env.DOTENV_CONFIG_DOTENV_KEY != null) {
25
+ options.DOTENV_KEY = process.env.DOTENV_CONFIG_DOTENV_KEY
26
+ }
27
+
28
+ module.exports = options
@@ -0,0 +1,179 @@
1
+ // TypeScript Version: 3.0
2
+ /// <reference types="node" />
3
+ import type { URL } from 'url';
4
+
5
+ export interface DotenvParseOutput {
6
+ [name: string]: string;
7
+ }
8
+
9
+ export interface DotenvPopulateOutput {
10
+ [name: string]: string;
11
+ }
12
+
13
+ /**
14
+ * Parses a string or buffer in the .env file format into an object.
15
+ *
16
+ * See https://dotenvx.com/docs
17
+ *
18
+ * @param src - contents to be parsed. example: `'DB_HOST=localhost'`
19
+ * @returns an object with keys and values based on `src`. example: `{ DB_HOST : 'localhost' }`
20
+ */
21
+ export function parse<T extends DotenvParseOutput = DotenvParseOutput>(
22
+ src: string | Buffer
23
+ ): T;
24
+
25
+ export interface DotenvConfigOptions {
26
+ /**
27
+ * Default: `path.resolve(process.cwd(), '.env')`
28
+ *
29
+ * Specify a custom path if your file containing environment variables is located elsewhere.
30
+ * Can also be an array of strings, specifying multiple paths.
31
+ *
32
+ * example: `require('dotenv').config({ path: '/custom/path/to/.env' })`
33
+ * example: `require('dotenv').config({ path: ['/path/to/first.env', '/path/to/second.env'] })`
34
+ */
35
+ path?: string | string[] | URL;
36
+
37
+ /**
38
+ * Default: `utf8`
39
+ *
40
+ * Specify the encoding of your file containing environment variables.
41
+ *
42
+ * example: `require('dotenv').config({ encoding: 'latin1' })`
43
+ */
44
+ encoding?: string;
45
+
46
+ /**
47
+ * Default: `false`
48
+ *
49
+ * Suppress all output (except errors).
50
+ *
51
+ * example: `require('dotenv').config({ quiet: true })`
52
+ */
53
+ quiet?: boolean;
54
+
55
+ /**
56
+ * Default: `false`
57
+ *
58
+ * Turn on logging to help debug why certain keys or values are not being set as you expect.
59
+ *
60
+ * example: `require('dotenv').config({ debug: process.env.DEBUG })`
61
+ */
62
+ debug?: boolean;
63
+
64
+ /**
65
+ * Default: `false`
66
+ *
67
+ * Override any environment variables that have already been set on your machine with values from your .env file.
68
+ *
69
+ * example: `require('dotenv').config({ override: true })`
70
+ */
71
+ override?: boolean;
72
+
73
+ /**
74
+ * Default: `process.env`
75
+ *
76
+ * Specify an object to write your secrets to. Defaults to process.env environment variables.
77
+ *
78
+ * example: `const processEnv = {}; require('dotenv').config({ processEnv: processEnv })`
79
+ */
80
+ processEnv?: DotenvPopulateInput;
81
+
82
+ /**
83
+ * Default: `undefined`
84
+ *
85
+ * Pass the DOTENV_KEY directly to config options. Defaults to looking for process.env.DOTENV_KEY environment variable. Note this only applies to decrypting .env.vault files. If passed as null or undefined, or not passed at all, dotenv falls back to its traditional job of parsing a .env file.
86
+ *
87
+ * example: `require('dotenv').config({ DOTENV_KEY: 'dotenv://:key_1234…@dotenvx.com/vault/.env.vault?environment=production' })`
88
+ */
89
+ DOTENV_KEY?: string;
90
+ }
91
+
92
+ export interface DotenvConfigOutput {
93
+ error?: DotenvError;
94
+ parsed?: DotenvParseOutput;
95
+ }
96
+
97
+ type DotenvError = Error & {
98
+ code:
99
+ | 'MISSING_DATA'
100
+ | 'INVALID_DOTENV_KEY'
101
+ | 'NOT_FOUND_DOTENV_ENVIRONMENT'
102
+ | 'DECRYPTION_FAILED'
103
+ | 'OBJECT_REQUIRED';
104
+ }
105
+
106
+ export interface DotenvPopulateOptions {
107
+ /**
108
+ * Default: `false`
109
+ *
110
+ * Turn on logging to help debug why certain keys or values are not being set as you expect.
111
+ *
112
+ * example: `require('dotenv').populate(processEnv, parsed, { debug: true })`
113
+ */
114
+ debug?: boolean;
115
+
116
+ /**
117
+ * Default: `false`
118
+ *
119
+ * Override any environment variables that have already been set on your machine with values from your .env file.
120
+ *
121
+ * example: `require('dotenv').populate(processEnv, parsed, { override: true })`
122
+ */
123
+ override?: boolean;
124
+ }
125
+
126
+ export interface DotenvPopulateInput {
127
+ [name: string]: string | undefined;
128
+ }
129
+
130
+ /**
131
+ * Loads `.env` file contents into process.env by default. If `DOTENV_KEY` is present, it smartly attempts to load encrypted `.env.vault` file contents into process.env.
132
+ *
133
+ * See https://dotenvx.com/docs
134
+ *
135
+ * @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', quiet: false, debug: true, override: false }`
136
+ * @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } }
137
+ *
138
+ */
139
+ export function config(options?: DotenvConfigOptions): DotenvConfigOutput;
140
+
141
+ /**
142
+ * Loads `.env` file contents into process.env.
143
+ *
144
+ * See https://dotenvx.com/docs
145
+ *
146
+ * @param options - additional options. example: `{ path: './custom/path', encoding: 'latin1', quiet: false, debug: true, override: false }`
147
+ * @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } }
148
+ *
149
+ */
150
+ export function configDotenv(options?: DotenvConfigOptions): DotenvConfigOutput;
151
+
152
+ /**
153
+ * Loads `source` json contents into `target` like process.env.
154
+ *
155
+ * See https://dotenvx.com/docs
156
+ *
157
+ * @param processEnv - the target JSON object. in most cases use process.env but you can also pass your own JSON object
158
+ * @param parsed - the source JSON object
159
+ * @param options - additional options. example: `{ debug: true, override: false }`
160
+ * @returns an object with the keys and values that were actually set
161
+ *
162
+ */
163
+ export function populate(
164
+ processEnv: DotenvPopulateInput,
165
+ parsed: DotenvPopulateInput,
166
+ options?: DotenvPopulateOptions
167
+ ): DotenvPopulateOutput;
168
+
169
+ /**
170
+ * Decrypt ciphertext
171
+ *
172
+ * See https://dotenvx.com/docs
173
+ *
174
+ * @param encrypted - the encrypted ciphertext string
175
+ * @param keyStr - the decryption key string
176
+ * @returns {string}
177
+ *
178
+ */
179
+ export function decrypt(encrypted: string, keyStr: string): string;
@@ -0,0 +1,423 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+ const os = require('os')
4
+ const crypto = require('crypto')
5
+
6
+ // Array of tips to display randomly
7
+ const TIPS = [
8
+ '◈ encrypted .env [www.dotenvx.com]',
9
+ '◈ secrets for agents [www.dotenvx.com]',
10
+ '⌁ auth for agents [www.vestauth.com]',
11
+ '⌘ custom filepath { path: \'/custom/path/.env\' }',
12
+ '⌘ enable debugging { debug: true }',
13
+ '⌘ override existing { override: true }',
14
+ '⌘ suppress logs { quiet: true }',
15
+ '⌘ multiple files { path: [\'.env.local\', \'.env\'] }'
16
+ ]
17
+
18
+ // Get a random tip from the tips array
19
+ function _getRandomTip () {
20
+ return TIPS[Math.floor(Math.random() * TIPS.length)]
21
+ }
22
+
23
+ function parseBoolean (value) {
24
+ if (typeof value === 'string') {
25
+ return !['false', '0', 'no', 'off', ''].includes(value.toLowerCase())
26
+ }
27
+ return Boolean(value)
28
+ }
29
+
30
+ function supportsAnsi () {
31
+ return process.stdout.isTTY // && process.env.TERM !== 'dumb'
32
+ }
33
+
34
+ function dim (text) {
35
+ return supportsAnsi() ? `\x1b[2m${text}\x1b[0m` : text
36
+ }
37
+
38
+ const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg
39
+
40
+ // Parse src into an Object
41
+ function parse (src) {
42
+ const obj = {}
43
+
44
+ // Convert buffer to string
45
+ let lines = src.toString()
46
+
47
+ // Convert line breaks to same format
48
+ lines = lines.replace(/\r\n?/mg, '\n')
49
+
50
+ let match
51
+ while ((match = LINE.exec(lines)) != null) {
52
+ const key = match[1]
53
+
54
+ // Default undefined or null to empty string
55
+ let value = (match[2] || '')
56
+
57
+ // Remove whitespace
58
+ value = value.trim()
59
+
60
+ // Check if double quoted
61
+ const maybeQuote = value[0]
62
+
63
+ // Remove surrounding quotes
64
+ value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2')
65
+
66
+ // Expand newlines if double quoted
67
+ if (maybeQuote === '"') {
68
+ value = value.replace(/\\n/g, '\n')
69
+ value = value.replace(/\\r/g, '\r')
70
+ }
71
+
72
+ // Add to object
73
+ obj[key] = value
74
+ }
75
+
76
+ return obj
77
+ }
78
+
79
+ function _parseVault (options) {
80
+ options = options || {}
81
+
82
+ const vaultPath = _vaultPath(options)
83
+ options.path = vaultPath // parse .env.vault
84
+ const result = DotenvModule.configDotenv(options)
85
+ if (!result.parsed) {
86
+ const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`)
87
+ err.code = 'MISSING_DATA'
88
+ throw err
89
+ }
90
+
91
+ // handle scenario for comma separated keys - for use with key rotation
92
+ // example: DOTENV_KEY="dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=prod,dotenv://:key_7890@dotenvx.com/vault/.env.vault?environment=prod"
93
+ const keys = _dotenvKey(options).split(',')
94
+ const length = keys.length
95
+
96
+ let decrypted
97
+ for (let i = 0; i < length; i++) {
98
+ try {
99
+ // Get full key
100
+ const key = keys[i].trim()
101
+
102
+ // Get instructions for decrypt
103
+ const attrs = _instructions(result, key)
104
+
105
+ // Decrypt
106
+ decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key)
107
+
108
+ break
109
+ } catch (error) {
110
+ // last key
111
+ if (i + 1 >= length) {
112
+ throw error
113
+ }
114
+ // try next key
115
+ }
116
+ }
117
+
118
+ // Parse decrypted .env string
119
+ return DotenvModule.parse(decrypted)
120
+ }
121
+
122
+ function _warn (message) {
123
+ console.error(`⚠ ${message}`)
124
+ }
125
+
126
+ function _debug (message) {
127
+ console.log(`┆ ${message}`)
128
+ }
129
+
130
+ function _log (message) {
131
+ console.log(`◇ ${message}`)
132
+ }
133
+
134
+ function _dotenvKey (options) {
135
+ // prioritize developer directly setting options.DOTENV_KEY
136
+ if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
137
+ return options.DOTENV_KEY
138
+ }
139
+
140
+ // secondary infra already contains a DOTENV_KEY environment variable
141
+ if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
142
+ return process.env.DOTENV_KEY
143
+ }
144
+
145
+ // fallback to empty string
146
+ return ''
147
+ }
148
+
149
+ function _instructions (result, dotenvKey) {
150
+ // Parse DOTENV_KEY. Format is a URI
151
+ let uri
152
+ try {
153
+ uri = new URL(dotenvKey)
154
+ } catch (error) {
155
+ if (error.code === 'ERR_INVALID_URL') {
156
+ const err = new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development')
157
+ err.code = 'INVALID_DOTENV_KEY'
158
+ throw err
159
+ }
160
+
161
+ throw error
162
+ }
163
+
164
+ // Get decrypt key
165
+ const key = uri.password
166
+ if (!key) {
167
+ const err = new Error('INVALID_DOTENV_KEY: Missing key part')
168
+ err.code = 'INVALID_DOTENV_KEY'
169
+ throw err
170
+ }
171
+
172
+ // Get environment
173
+ const environment = uri.searchParams.get('environment')
174
+ if (!environment) {
175
+ const err = new Error('INVALID_DOTENV_KEY: Missing environment part')
176
+ err.code = 'INVALID_DOTENV_KEY'
177
+ throw err
178
+ }
179
+
180
+ // Get ciphertext payload
181
+ const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`
182
+ const ciphertext = result.parsed[environmentKey] // DOTENV_VAULT_PRODUCTION
183
+ if (!ciphertext) {
184
+ const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`)
185
+ err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT'
186
+ throw err
187
+ }
188
+
189
+ return { ciphertext, key }
190
+ }
191
+
192
+ function _vaultPath (options) {
193
+ let possibleVaultPath = null
194
+
195
+ if (options && options.path && options.path.length > 0) {
196
+ if (Array.isArray(options.path)) {
197
+ for (const filepath of options.path) {
198
+ if (fs.existsSync(filepath)) {
199
+ possibleVaultPath = filepath.endsWith('.vault') ? filepath : `${filepath}.vault`
200
+ }
201
+ }
202
+ } else {
203
+ possibleVaultPath = options.path.endsWith('.vault') ? options.path : `${options.path}.vault`
204
+ }
205
+ } else {
206
+ possibleVaultPath = path.resolve(process.cwd(), '.env.vault')
207
+ }
208
+
209
+ if (fs.existsSync(possibleVaultPath)) {
210
+ return possibleVaultPath
211
+ }
212
+
213
+ return null
214
+ }
215
+
216
+ function _resolveHome (envPath) {
217
+ return envPath[0] === '~' ? path.join(os.homedir(), envPath.slice(1)) : envPath
218
+ }
219
+
220
+ function _configVault (options) {
221
+ const debug = parseBoolean(process.env.DOTENV_CONFIG_DEBUG || (options && options.debug))
222
+ const quiet = parseBoolean(process.env.DOTENV_CONFIG_QUIET || (options && options.quiet))
223
+
224
+ if (debug || !quiet) {
225
+ _log('loading env from encrypted .env.vault')
226
+ }
227
+
228
+ const parsed = DotenvModule._parseVault(options)
229
+
230
+ let processEnv = process.env
231
+ if (options && options.processEnv != null) {
232
+ processEnv = options.processEnv
233
+ }
234
+
235
+ DotenvModule.populate(processEnv, parsed, options)
236
+
237
+ return { parsed }
238
+ }
239
+
240
+ function configDotenv (options) {
241
+ const dotenvPath = path.resolve(process.cwd(), '.env')
242
+ let encoding = 'utf8'
243
+ let processEnv = process.env
244
+ if (options && options.processEnv != null) {
245
+ processEnv = options.processEnv
246
+ }
247
+ let debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || (options && options.debug))
248
+ let quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || (options && options.quiet))
249
+
250
+ if (options && options.encoding) {
251
+ encoding = options.encoding
252
+ } else {
253
+ if (debug) {
254
+ _debug('no encoding is specified (UTF-8 is used by default)')
255
+ }
256
+ }
257
+
258
+ let optionPaths = [dotenvPath] // default, look for .env
259
+ if (options && options.path) {
260
+ if (!Array.isArray(options.path)) {
261
+ optionPaths = [_resolveHome(options.path)]
262
+ } else {
263
+ optionPaths = [] // reset default
264
+ for (const filepath of options.path) {
265
+ optionPaths.push(_resolveHome(filepath))
266
+ }
267
+ }
268
+ }
269
+
270
+ // Build the parsed data in a temporary object (because we need to return it). Once we have the final
271
+ // parsed data, we will combine it with process.env (or options.processEnv if provided).
272
+ let lastError
273
+ const parsedAll = {}
274
+ for (const path of optionPaths) {
275
+ try {
276
+ // Specifying an encoding returns a string instead of a buffer
277
+ const parsed = DotenvModule.parse(fs.readFileSync(path, { encoding }))
278
+
279
+ DotenvModule.populate(parsedAll, parsed, options)
280
+ } catch (e) {
281
+ if (debug) {
282
+ _debug(`failed to load ${path} ${e.message}`)
283
+ }
284
+ lastError = e
285
+ }
286
+ }
287
+
288
+ const populated = DotenvModule.populate(processEnv, parsedAll, options)
289
+
290
+ // handle user settings DOTENV_CONFIG_ options inside .env file(s)
291
+ debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || debug)
292
+ quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || quiet)
293
+
294
+ if (debug || !quiet) {
295
+ const keysCount = Object.keys(populated).length
296
+ const shortPaths = []
297
+ for (const filePath of optionPaths) {
298
+ try {
299
+ const relative = path.relative(process.cwd(), filePath)
300
+ shortPaths.push(relative)
301
+ } catch (e) {
302
+ if (debug) {
303
+ _debug(`failed to load ${filePath} ${e.message}`)
304
+ }
305
+ lastError = e
306
+ }
307
+ }
308
+
309
+ _log(`injected env (${keysCount}) from ${shortPaths.join(',')} ${dim(`// tip: ${_getRandomTip()}`)}`)
310
+ }
311
+
312
+ if (lastError) {
313
+ return { parsed: parsedAll, error: lastError }
314
+ } else {
315
+ return { parsed: parsedAll }
316
+ }
317
+ }
318
+
319
+ // Populates process.env from .env file
320
+ function config (options) {
321
+ // fallback to original dotenv if DOTENV_KEY is not set
322
+ if (_dotenvKey(options).length === 0) {
323
+ return DotenvModule.configDotenv(options)
324
+ }
325
+
326
+ const vaultPath = _vaultPath(options)
327
+
328
+ // dotenvKey exists but .env.vault file does not exist
329
+ if (!vaultPath) {
330
+ _warn(`you set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}`)
331
+
332
+ return DotenvModule.configDotenv(options)
333
+ }
334
+
335
+ return DotenvModule._configVault(options)
336
+ }
337
+
338
+ function decrypt (encrypted, keyStr) {
339
+ const key = Buffer.from(keyStr.slice(-64), 'hex')
340
+ let ciphertext = Buffer.from(encrypted, 'base64')
341
+
342
+ const nonce = ciphertext.subarray(0, 12)
343
+ const authTag = ciphertext.subarray(-16)
344
+ ciphertext = ciphertext.subarray(12, -16)
345
+
346
+ try {
347
+ const aesgcm = crypto.createDecipheriv('aes-256-gcm', key, nonce)
348
+ aesgcm.setAuthTag(authTag)
349
+ return `${aesgcm.update(ciphertext)}${aesgcm.final()}`
350
+ } catch (error) {
351
+ const isRange = error instanceof RangeError
352
+ const invalidKeyLength = error.message === 'Invalid key length'
353
+ const decryptionFailed = error.message === 'Unsupported state or unable to authenticate data'
354
+
355
+ if (isRange || invalidKeyLength) {
356
+ const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)')
357
+ err.code = 'INVALID_DOTENV_KEY'
358
+ throw err
359
+ } else if (decryptionFailed) {
360
+ const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY')
361
+ err.code = 'DECRYPTION_FAILED'
362
+ throw err
363
+ } else {
364
+ throw error
365
+ }
366
+ }
367
+ }
368
+
369
+ // Populate process.env with parsed values
370
+ function populate (processEnv, parsed, options = {}) {
371
+ const debug = Boolean(options && options.debug)
372
+ const override = Boolean(options && options.override)
373
+ const populated = {}
374
+
375
+ if (typeof parsed !== 'object') {
376
+ const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate')
377
+ err.code = 'OBJECT_REQUIRED'
378
+ throw err
379
+ }
380
+
381
+ // Set process.env
382
+ for (const key of Object.keys(parsed)) {
383
+ if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
384
+ if (override === true) {
385
+ processEnv[key] = parsed[key]
386
+ populated[key] = parsed[key]
387
+ }
388
+
389
+ if (debug) {
390
+ if (override === true) {
391
+ _debug(`"${key}" is already defined and WAS overwritten`)
392
+ } else {
393
+ _debug(`"${key}" is already defined and was NOT overwritten`)
394
+ }
395
+ }
396
+ } else {
397
+ processEnv[key] = parsed[key]
398
+ populated[key] = parsed[key]
399
+ }
400
+ }
401
+
402
+ return populated
403
+ }
404
+
405
+ const DotenvModule = {
406
+ configDotenv,
407
+ _configVault,
408
+ _parseVault,
409
+ config,
410
+ decrypt,
411
+ parse,
412
+ populate
413
+ }
414
+
415
+ module.exports.configDotenv = DotenvModule.configDotenv
416
+ module.exports._configVault = DotenvModule._configVault
417
+ module.exports._parseVault = DotenvModule._parseVault
418
+ module.exports.config = DotenvModule.config
419
+ module.exports.decrypt = DotenvModule.decrypt
420
+ module.exports.parse = DotenvModule.parse
421
+ module.exports.populate = DotenvModule.populate
422
+
423
+ module.exports = DotenvModule