@dotenvx/dotenvx
Advanced tools
Comparing version 0.16.0 to 0.17.0
{ | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"name": "@dotenvx/dotenvx", | ||
@@ -28,3 +28,3 @@ "description": "a better dotenv–from the creator of `dotenv`", | ||
}, | ||
"funding": "Have you seen dotenvx.com? run anywhere, cross-platform, and encrypted envs.", | ||
"funding": "https://dotenvx.com", | ||
"dependencies": { | ||
@@ -36,3 +36,3 @@ "@inquirer/prompts": "^3.3.0", | ||
"conf": "^10.2.0", | ||
"dotenv": "^16.4.4", | ||
"dotenv": "16.4.5", | ||
"dotenv-expand": "^11.0.6", | ||
@@ -39,0 +39,0 @@ "execa": "^5.1.1", |
const fs = require('fs') | ||
const path = require('path') | ||
@@ -10,147 +11,74 @@ const main = require('./../../lib/main') | ||
// constants | ||
const ENCODING = 'utf8' | ||
async function encrypt () { | ||
async function encrypt (directory) { | ||
spinner.start() | ||
await helpers.sleep(500) // better dx | ||
logger.debug(`directory: ${directory}`) | ||
const options = this.opts() | ||
logger.debug(`options: ${JSON.stringify(options)}`) | ||
let optionEnvFile = options.envFile | ||
if (!Array.isArray(optionEnvFile)) { | ||
optionEnvFile = [optionEnvFile] | ||
} | ||
const optionEnvFile = options.envFile || helpers.findEnvFiles(directory) | ||
const addedKeys = new Set() | ||
const addedVaults = new Set() | ||
const addedEnvFilepaths = new Set() | ||
try { | ||
const { | ||
dotenvKeys, | ||
dotenvKeysFile, | ||
addedKeys, | ||
existingKeys, | ||
dotenvVaultFile, | ||
addedVaults, | ||
existingVaults, | ||
addedDotenvFilenames | ||
} = main.encrypt(directory, optionEnvFile) | ||
// must be at least one .env* file | ||
if (optionEnvFile.length < 1) { | ||
spinner.fail('no .env* files found') | ||
logger.help('? add one with [echo "HELLO=World" > .env] and then run [dotenvx encrypt]') | ||
process.exit(1) | ||
} | ||
try { | ||
logger.verbose(`generating .env.keys from ${optionEnvFile}`) | ||
if (addedKeys.length > 0) { | ||
logger.verbose(`generated ${addedKeys}`) | ||
} | ||
if (existingKeys.length > 0) { | ||
logger.verbose(`existing ${existingKeys}`) | ||
} | ||
fs.writeFileSync(path.resolve(directory, '.env.keys'), dotenvKeysFile) | ||
const dotenvKeys = (main.configDotenv({ path: '.env.keys' }).parsed || {}) | ||
logger.verbose(`generating .env.vault from ${optionEnvFile}`) | ||
if (addedVaults.length > 0) { | ||
logger.verbose(`encrypting ${addedVaults}`) | ||
} | ||
if (existingVaults.length > 0) { | ||
logger.verbose(`existing ${existingVaults}`) | ||
} | ||
fs.writeFileSync(path.resolve(directory, '.env.vault'), dotenvVaultFile) | ||
for (const envFilepath of optionEnvFile) { | ||
const filepath = helpers.resolvePath(envFilepath) | ||
if (!fs.existsSync(filepath)) { | ||
spinner.fail(`file does not exist at [${filepath}]`) | ||
logger.help(`? add it with [echo "HELLO=World" > ${envFilepath}] and then run [dotenvx encrypt]`) | ||
process.exit(1) | ||
} | ||
if (addedDotenvFilenames.length > 0) { | ||
spinner.succeed(`encrypted to .env.vault (${addedDotenvFilenames})`) | ||
logger.help2('ℹ commit .env.vault to code: [git commit -am ".env.vault"]') | ||
} else { | ||
spinner.done(`no changes (${optionEnvFile})`) | ||
} | ||
const environment = helpers.guessEnvironment(filepath) | ||
const key = `DOTENV_KEY_${environment.toUpperCase()}` | ||
let value = dotenvKeys[key] | ||
// first time seeing new DOTENV_KEY_${environment} | ||
if (!value || value.length === 0) { | ||
logger.verbose(`generating ${key}`) | ||
value = helpers.generateDotenvKey(environment) | ||
logger.debug(`generating ${key} as ${value}`) | ||
dotenvKeys[key] = value | ||
addedKeys.add(key) // for info logging to user | ||
} else { | ||
logger.verbose(`existing ${key}`) | ||
logger.debug(`existing ${key} as ${value}`) | ||
} | ||
if (addedKeys.length > 0) { | ||
spinner.succeed(`${helpers.pluralize('key', addedKeys.length)} added to .env.keys (${addedKeys})`) | ||
logger.help2('ℹ push .env.keys up to hub: [dotenvx hub push]') | ||
} | ||
let keysData = `#/!!!!!!!!!!!!!!!!!!!.env.keys!!!!!!!!!!!!!!!!!!!!!!/ | ||
#/ DOTENV_KEYs. DO NOT commit to source control / | ||
#/ [how it works](https://dotenvx.com/env-keys) / | ||
#/--------------------------------------------------/\n` | ||
if (addedVaults.length > 0) { | ||
const DOTENV_VAULT_X = addedVaults[addedVaults.length - 1] | ||
const DOTENV_KEY_X = DOTENV_VAULT_X.replace('_VAULT_', '_KEY_') | ||
const tryKey = dotenvKeys[DOTENV_KEY_X] || '<dotenv_key_environment>' | ||
for (const key in dotenvKeys) { | ||
const value = dotenvKeys[key] | ||
keysData += `${key}="${value}"\n` | ||
logger.help2(`ℹ run [DOTENV_KEY='${tryKey}' dotenvx run -- yourcommand] to test decryption locally`) | ||
} | ||
fs.writeFileSync('.env.keys', keysData) | ||
} catch (error) { | ||
spinner.fail(error.message) | ||
process.exit(1) | ||
} | ||
// used later in logging to user | ||
const dotenvKeys = (main.configDotenv({ path: '.env.keys' }).parsed || {}) | ||
try { | ||
logger.verbose(`generating .env.vault from ${optionEnvFile}`) | ||
const dotenvVaults = (main.configDotenv({ path: '.env.vault' }).parsed || {}) | ||
for (const envFilepath of optionEnvFile) { | ||
const filepath = helpers.resolvePath(envFilepath) | ||
const environment = helpers.guessEnvironment(filepath) | ||
const vault = `DOTENV_VAULT_${environment.toUpperCase()}` | ||
let ciphertext = dotenvVaults[vault] | ||
const dotenvKey = dotenvKeys[`DOTENV_KEY_${environment.toUpperCase()}`] | ||
if (!ciphertext || ciphertext.length === 0 || helpers.changed(ciphertext, dotenvKey, filepath, ENCODING)) { | ||
logger.verbose(`encrypting ${vault}`) | ||
ciphertext = helpers.encryptFile(filepath, dotenvKey, ENCODING) | ||
logger.verbose(`encrypting ${vault} as ${ciphertext}`) | ||
dotenvVaults[vault] = ciphertext | ||
addedVaults.add(vault) // for info logging to user | ||
addedEnvFilepaths.add(envFilepath) // for info logging to user | ||
} else { | ||
logger.verbose(`existing ${vault}`) | ||
logger.debug(`existing ${vault} as ${ciphertext}`) | ||
} | ||
if (error.help) { | ||
logger.help(error.help) | ||
} | ||
let vaultData = `#/-------------------.env.vault---------------------/ | ||
#/ cloud-agnostic vaulting standard / | ||
#/ [how it works](https://dotenvx.com/env-vault) / | ||
#/--------------------------------------------------/\n\n` | ||
for (const vault in dotenvVaults) { | ||
const value = dotenvVaults[vault] | ||
const environment = vault.replace('DOTENV_VAULT_', '').toLowerCase() | ||
vaultData += `# ${environment}\n` | ||
vaultData += `${vault}="${value}"\n\n` | ||
if (error.code) { | ||
logger.debug(`ERROR_CODE: ${error.code}`) | ||
} | ||
fs.writeFileSync('.env.vault', vaultData) | ||
} catch (e) { | ||
spinner.fail(e.message) | ||
process.exit(1) | ||
} | ||
if (addedEnvFilepaths.size > 0) { | ||
spinner.succeed(`encrypted to .env.vault (${[...addedEnvFilepaths]})`) | ||
logger.help2('ℹ commit .env.vault to code: [git commit -am ".env.vault"]') | ||
} else { | ||
spinner.done(`no changes (${optionEnvFile})`) | ||
} | ||
if (addedKeys.size > 0) { | ||
spinner.succeed(`${helpers.pluralize('key', addedKeys.size)} added to .env.keys (${[...addedKeys]})`) | ||
logger.help2('ℹ push .env.keys up to hub: [dotenvx hub push]') | ||
} | ||
if (addedVaults.size > 0) { | ||
const DOTENV_VAULT_X = [...addedVaults][addedVaults.size - 1] | ||
const DOTENV_KEY_X = DOTENV_VAULT_X.replace('_VAULT_', '_KEY_') | ||
const tryKey = dotenvKeys[DOTENV_KEY_X] || '<dotenv_key_environment>' | ||
logger.help2(`ℹ run [DOTENV_KEY='${tryKey}' dotenvx run -- yourcommand] to test decryption locally`) | ||
} | ||
} | ||
module.exports = encrypt |
@@ -8,3 +8,2 @@ #!/usr/bin/env node | ||
const logger = require('./../shared/logger') | ||
const helpers = require('./helpers') | ||
const examples = require('./examples') | ||
@@ -68,3 +67,4 @@ const packageJson = require('./../shared/packageJson') | ||
.addHelpText('after', examples.encrypt) | ||
.option('-f, --env-file <paths...>', 'path(s) to your env file(s)', helpers.findEnvFiles('./')) | ||
.argument('[directory]', 'directory to encrypt', '.') | ||
.option('-f, --env-file <paths...>', 'path(s) to your env file(s)') | ||
.action(require('./actions/encrypt')) | ||
@@ -71,0 +71,0 @@ |
@@ -6,2 +6,3 @@ const logger = require('./../shared/logger') | ||
// services | ||
const Encrypt = require('./services/encrypt') | ||
const Ls = require('./services/ls') | ||
@@ -109,2 +110,6 @@ | ||
const encrypt = function (directory, envFile) { | ||
return new Encrypt(directory, envFile).run() | ||
} | ||
const ls = function (directory, envFile) { | ||
@@ -121,3 +126,4 @@ return new Ls(directory, envFile).run() | ||
inject, | ||
ls | ||
ls, | ||
encrypt | ||
} |
82942
32
1890
23
+ Addeddotenv@16.4.5(transitive)
- Removeddotenv@16.4.7(transitive)
Updateddotenv@16.4.5