@dotenvx/dotenvx 1.13.3 → 1.14.0

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.
package/CHANGELOG.md CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
- ## [Unreleased](https://github.com/dotenvx/dotenvx/compare/v1.13.3...main)
5
+ ## [Unreleased](https://github.com/dotenvx/dotenvx/compare/v1.14.0...main)
6
+
7
+ ## 1.14.0
8
+
9
+ ### Added
10
+
11
+ * add `dotenvx keypair` command for printing your public/private keypairs ([#375](https://github.com/dotenvx/dotenvx/pull/375))
6
12
 
7
13
  ## 1.13.3
8
14
 
package/README.md CHANGED
@@ -1273,6 +1273,45 @@ More examples
1273
1273
  $ dotenvx decrypt --stdout > somefile.txt
1274
1274
  ```
1275
1275
 
1276
+ </details>
1277
+ * <details><summary>`keypair`</summary><br>
1278
+
1279
+ Print public/private keys for `.env` file.
1280
+
1281
+ ```sh
1282
+ $ echo "HELLO=World" > .env
1283
+ $ dotenvx encrypt
1284
+
1285
+ $ dotenvx keypair
1286
+ {"DOTENV_PUBLIC_KEY":"<publicKey>","DOTENV_PRIVATE_KEY":"<privateKey>"}
1287
+ ```
1288
+
1289
+ </details>
1290
+ * <details><summary>`keypair -f .env.production`</summary><br>
1291
+
1292
+ Print public/private keys for `.env.production` file.
1293
+
1294
+ ```sh
1295
+ $ echo "HELLO=Production" > .env.production
1296
+ $ dotenvx encrypt -f .env.production
1297
+
1298
+ $ dotenvx keypair -f .env.production
1299
+ {"DOTENV_PUBLIC_KEY_PRODUCTION":"<publicKey>","DOTENV_PRIVATE_KEY_PRODUCTION":"<privateKey>"}
1300
+ ```
1301
+
1302
+ </details>
1303
+ * <details><summary>`keypair DOTENV_PRIVATE_KEY`</summary><br>
1304
+
1305
+ Print specific key for `.env` file.
1306
+
1307
+ ```sh
1308
+ $ echo "HELLO=World" > .env
1309
+ $ dotenvx encrypt
1310
+
1311
+ $ dotenvx keypair DOTENV_PRIVATE_KEY
1312
+ <privateKey>
1313
+ ```
1314
+
1276
1315
  </details>
1277
1316
  * <details><summary>`ls`</summary><br>
1278
1317
 
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.13.3",
2
+ "version": "1.14.0",
3
3
  "name": "@dotenvx/dotenvx",
4
4
  "description": "a better dotenv–from the creator of `dotenv`",
5
5
  "author": "@motdotla",
@@ -0,0 +1,32 @@
1
+ const { logger } = require('./../../shared/logger')
2
+
3
+ const main = require('./../../lib/main')
4
+
5
+ function keypair (key) {
6
+ if (key) {
7
+ logger.debug(`key: ${key}`)
8
+ }
9
+
10
+ const options = this.opts()
11
+ logger.debug(`options: ${JSON.stringify(options)}`)
12
+
13
+ const results = main.keypair(options.envFile, key)
14
+
15
+ if (typeof results === 'object' && results !== null) {
16
+ let space = 0
17
+ if (options.prettyPrint) {
18
+ space = 2
19
+ }
20
+
21
+ process.stdout.write(JSON.stringify(results, null, space))
22
+ } else {
23
+ if (results === undefined) {
24
+ process.stdout.write('')
25
+ process.exit(1)
26
+ } else {
27
+ process.stdout.write(results)
28
+ }
29
+ }
30
+ }
31
+
32
+ module.exports = keypair
@@ -115,6 +115,15 @@ program.command('decrypt')
115
115
  .option('--stdout', 'send to stdout')
116
116
  .action(decryptAction)
117
117
 
118
+ // dotenvx keypair
119
+ const keypairAction = require('./actions/keypair')
120
+ program.command('keypair')
121
+ .description('print public/private keys for .env file(s)')
122
+ .argument('[key]', 'environment variable key name')
123
+ .option('-f, --env-file <paths...>', 'path(s) to your env file(s)')
124
+ .option('-pp, --pretty-print', 'pretty print output')
125
+ .action(keypairAction)
126
+
118
127
  // dotenvx ls
119
128
  const lsAction = require('./actions/ls')
120
129
  program.command('ls')
@@ -0,0 +1,44 @@
1
+ const fs = require('fs')
2
+ const dotenv = require('dotenv')
3
+
4
+ const ENCODING = 'utf8'
5
+
6
+ const guessPublicKeyName = require('./guessPublicKeyName')
7
+
8
+ function searchProcessEnv (publicKeyName) {
9
+ if (process.env[publicKeyName] && process.env[publicKeyName].length > 0) {
10
+ return process.env[publicKeyName]
11
+ }
12
+ }
13
+
14
+ function searchEnvFile (publicKeyName, envFilepath) {
15
+ if (fs.existsSync(envFilepath)) {
16
+ const keysSrc = fs.readFileSync(envFilepath, { encoding: ENCODING })
17
+ const keysParsed = dotenv.parse(keysSrc)
18
+
19
+ if (keysParsed[publicKeyName] && keysParsed[publicKeyName].length > 0) {
20
+ return keysParsed[publicKeyName]
21
+ }
22
+ }
23
+ }
24
+
25
+ function smartDotenvPublicKey (envFilepath) {
26
+ let publicKey = null
27
+ const publicKeyName = guessPublicKeyName(envFilepath) // DOTENV_PUBLIC_KEY_${ENVIRONMENT}
28
+
29
+ // 1. attempt process.env first
30
+ publicKey = searchProcessEnv(publicKeyName)
31
+ if (publicKey) {
32
+ return publicKey
33
+ }
34
+
35
+ // 2. attempt .env.keys second (path/to/.env.keys)
36
+ publicKey = searchEnvFile(publicKeyName, envFilepath)
37
+ if (publicKey) {
38
+ return publicKey
39
+ }
40
+
41
+ return null
42
+ }
43
+
44
+ module.exports = smartDotenvPublicKey
package/src/lib/main.js CHANGED
@@ -11,6 +11,7 @@ const Ls = require('./services/ls')
11
11
  const Get = require('./services/get')
12
12
  const Run = require('./services/run')
13
13
  const Sets = require('./services/sets')
14
+ const Keypair = require('./services/keypair')
14
15
  const Encrypt = require('./services/encrypt')
15
16
  const Decrypt = require('./services/decrypt')
16
17
  const Genexample = require('./services/genexample')
@@ -210,6 +211,11 @@ const decrypt = function (envFile, key, excludeKey) {
210
211
  return new Decrypt(envFile, key, excludeKey).run()
211
212
  }
212
213
 
214
+ /** @type {import('./main').keypair} */
215
+ const keypair = function (envFile, key) {
216
+ return new Keypair(envFile, key).run()
217
+ }
218
+
213
219
  module.exports = {
214
220
  // dotenv proxies
215
221
  config,
@@ -221,6 +227,7 @@ module.exports = {
221
227
  ls,
222
228
  get,
223
229
  set,
230
+ keypair,
224
231
  genexample,
225
232
  // expose for libs depending on @dotenvx/dotenvx - like dotenvx-pro
226
233
  setLogLevel,
@@ -0,0 +1,45 @@
1
+ const guessPublicKeyName = require('./../helpers/guessPublicKeyName')
2
+ const smartDotenvPublicKey = require('./../helpers/smartDotenvPublicKey')
3
+ const guessPrivateKeyName = require('./../helpers/guessPrivateKeyName')
4
+ const smartDotenvPrivateKey = require('./../helpers/smartDotenvPrivateKey')
5
+
6
+ class Keypair {
7
+ constructor (envFile = '.env', key = undefined) {
8
+ this.envFile = envFile
9
+ this.key = key
10
+ }
11
+
12
+ run () {
13
+ const out = {}
14
+
15
+ const envFilepaths = this._envFilepaths()
16
+ for (const envFilepath of envFilepaths) {
17
+ // public key
18
+ const publicKeyName = guessPublicKeyName(envFilepath)
19
+ const publicKeyValue = smartDotenvPublicKey(envFilepath)
20
+ out[publicKeyName] = publicKeyValue
21
+
22
+ // private key
23
+ const privateKeyName = guessPrivateKeyName(envFilepath)
24
+ const privateKeyValue = smartDotenvPrivateKey(envFilepath)
25
+
26
+ out[privateKeyName] = privateKeyValue
27
+ }
28
+
29
+ if (this.key) {
30
+ return out[this.key]
31
+ } else {
32
+ return out
33
+ }
34
+ }
35
+
36
+ _envFilepaths () {
37
+ if (!Array.isArray(this.envFile)) {
38
+ return [this.envFile]
39
+ }
40
+
41
+ return this.envFile
42
+ }
43
+ }
44
+
45
+ module.exports = Keypair