@dotenvx/dotenvx
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -5,4 +5,10 @@ # Changelog | ||
## [Unreleased](https://github.com/dotenvx/dotenvx/compare/v1.2.0...main) | ||
## [Unreleased](https://github.com/dotenvx/dotenvx/compare/v1.3.0...main) | ||
## 1.3.0 | ||
### Added | ||
* encrypt specified keys with `--key` option - `dotenvx encrypt -k HELLO` ([#281](https://github.com/dotenvx/dotenvx/pull/281)) | ||
## 1.2.0 | ||
@@ -9,0 +15,0 @@ |
{ | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"name": "@dotenvx/dotenvx", | ||
@@ -4,0 +4,0 @@ "description": "a better dotenv–from the creator of `dotenv`", |
@@ -1118,2 +1118,14 @@ [![dotenvx](https://dotenvx.com/better-banner.png)](https://dotenvx.com) | ||
</details> | ||
* <details><summary>`encrypt -k`</summary><br> | ||
Specify the key(s) to encrypt by passing `--key`. | ||
```sh | ||
$ echo "HELLO=World\nHELLO2=Universe" > .env | ||
$ dotenvx encrypt -k HELLO2 | ||
✔ encrypted (.env) | ||
``` | ||
</details> | ||
* <details><summary>`help`</summary><br> | ||
@@ -1120,0 +1132,0 @@ |
@@ -19,3 +19,3 @@ const fs = require('fs') | ||
unchangedFilepaths | ||
} = main.encrypt(options.envFile) | ||
} = main.encrypt(options.envFile, options.key) | ||
@@ -22,0 +22,0 @@ for (const processedEnvFile of processedEnvFiles) { |
@@ -97,2 +97,3 @@ #!/usr/bin/env node | ||
.option('-f, --env-file <paths...>', 'path(s) to your env file(s)') | ||
.option('-k, --key <keys...>', 'keys(s) to encrypt (default: all keys in file)') | ||
.action(encryptAction) | ||
@@ -99,0 +100,0 @@ |
@@ -147,4 +147,5 @@ import type { URL } from 'url'; | ||
* @param envFile - path to the .env file | ||
* @param key - keys(s) to encrypt (default: all keys in .env file) | ||
*/ | ||
export function encrypt(envFile: string): EncryptOutput; | ||
export function encrypt(envFile: string, key: string): EncryptOutput; | ||
@@ -161,2 +162,3 @@ export type VaultEncryptOutput = { | ||
envFile: string | string[]; | ||
key: string | string[]; | ||
}; | ||
@@ -163,0 +165,0 @@ |
@@ -197,4 +197,4 @@ // @ts-check | ||
/** @type {import('./main').encrypt} */ | ||
const encrypt = function (envFile) { | ||
return new Encrypt(envFile).run() | ||
const encrypt = function (envFile, key) { | ||
return new Encrypt(envFile, key).run() | ||
} | ||
@@ -201,0 +201,0 @@ |
@@ -14,4 +14,5 @@ const fs = require('fs') | ||
class Encrypt { | ||
constructor (envFile = '.env') { | ||
constructor (envFile = '.env', key = []) { | ||
this.envFile = envFile | ||
this.key = key | ||
this.processedEnvFiles = [] | ||
@@ -24,2 +25,3 @@ this.changedFilepaths = new Set() | ||
const envFilepaths = this._envFilepaths() | ||
const keys = this._keys() | ||
for (const envFilepath of envFilepaths) { | ||
@@ -57,11 +59,13 @@ const filepath = path.resolve(envFilepath) | ||
for (const [key, value] of Object.entries(parsed)) { | ||
const encrypted = isEncrypted(key, value) | ||
if (!encrypted) { | ||
row.keys.push(key) // track key(s) | ||
if (keys.length < 1 || keys.includes(key)) { // optionally control which key to encrypt | ||
const encrypted = isEncrypted(key, value) | ||
if (!encrypted) { | ||
row.keys.push(key) // track key(s) | ||
const encryptedValue = encryptValue(value, publicKey) | ||
// once newSrc is built write it out | ||
src = replace(src, key, encryptedValue) | ||
const encryptedValue = encryptValue(value, publicKey) | ||
// once newSrc is built write it out | ||
src = replace(src, key, encryptedValue) | ||
row.changed = true // track change | ||
row.changed = true // track change | ||
} | ||
} | ||
@@ -105,4 +109,12 @@ } | ||
} | ||
_keys () { | ||
if (!Array.isArray(this.key)) { | ||
return [this.key] | ||
} | ||
return this.key | ||
} | ||
} | ||
module.exports = Encrypt |
1131446
4478
1450