Skip to content

Commit

Permalink
move convert to encrypt and deprecate convert naming
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Jun 21, 2024
1 parent 7d299c0 commit 1c1b0f2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 50 deletions.
8 changes: 4 additions & 4 deletions src/cli/actions/convert.js → src/cli/actions/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const isIgnoringDotenvKeys = require('../../lib/helpers/isIgnoringDotenvKeys')

const ENCODING = 'utf8'

async function convert () {
async function encrypt () {
const options = this.opts()
logger.debug(`options: ${JSON.stringify(options)}`)

Expand All @@ -16,14 +16,14 @@ async function convert () {
processedEnvFiles,
changedFilepaths,
unchangedFilepaths
} = main.convert(options.envFile)
} = main.encrypt(options.envFile)

for (const processedEnvFile of processedEnvFiles) {
logger.verbose(`encrypting ${processedEnvFile.envFilepath} (${processedEnvFile.filepath})`)
if (processedEnvFile.error) {
if (processedEnvFile.error.code === 'MISSING_ENV_FILE') {
logger.warn(processedEnvFile.error)
logger.help(`? add one with [echo "HELLO=World" > ${processedEnvFile.envFilepath}] and re-run [dotenvx convert]`)
logger.help(`? add one with [echo "HELLO=World" > ${processedEnvFile.envFilepath}] and re-run [dotenvx encrypt]`)
} else {
logger.warn(processedEnvFile.error)
}
Expand Down Expand Up @@ -70,4 +70,4 @@ async function convert () {
}
}

module.exports = convert
module.exports = encrypt
2 changes: 1 addition & 1 deletion src/cli/actions/vault/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function encrypt (directory) {
existingVaults,
addedDotenvFilenames,
envFile
} = main.encrypt(directory, options.envFile)
} = main.vaultEncrypt(directory, options.envFile)

logger.verbose(`generating .env.keys from ${envFile}`)
if (addedKeys.length > 0) {
Expand Down
15 changes: 12 additions & 3 deletions src/cli/dotenvx.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,20 @@ program.command('set')
.option('-c, --encrypt', 'encrypt value')
.action(require('./actions/set'))

// dotenvx convert
program.command('convert')
// dotenvx encrypt
const encryptAction = require('./actions/encrypt')
program.command('encrypt')
.description('convert env file(s) to encrypted env file(s)')
.option('-f, --env-file <paths...>', 'path(s) to your env file(s)')
.action(require('./actions/convert'))
.action(encryptAction)
program.command('convert')
.description('DEPRECATED: moved to [dotenvx encrypt]')
.option('-f, --env-file <paths...>', 'path(s) to your env file(s)')
.action(function (...args) {
logger.warn('DEPRECATION NOTICE: [dotenvx convert] has moved to [dotenvx encrypt]')

encryptAction.apply(this, args)
})

// dotenvx ls
program.command('ls')
Expand Down
8 changes: 1 addition & 7 deletions src/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ const parse = function (src) {
return dotenv.parse(src)
}

// DEPRECATED: will became the same function as convert
const encrypt = function (directory, envFile) {
return new VaultEncrypt(directory, envFile).run()
}

const vaultEncrypt = function (directory, envFile) {
return new VaultEncrypt(directory, envFile).run()
}
Expand All @@ -159,7 +154,7 @@ const set = function (key, value, envFile, encrypt) {
return new Sets(key, value, envFile, encrypt).run()
}

const convert = function (envFile) {
const encrypt = function (envFile) {
return new Encrypt(envFile).run()
}

Expand Down Expand Up @@ -201,7 +196,6 @@ module.exports = {
ls,
get,
set,
convert,
status,
genexample,
// settings
Expand Down
38 changes: 19 additions & 19 deletions tests/cli/actions/vault/encrypt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ const sinon = require('sinon')
const dotenv = require('dotenv')

const main = require('../../../../src/lib/main')
const encrypt = require('../../../../src/cli/actions/vault/encrypt')
const vaultEncrypt = require('../../../../src/cli/actions/vault/encrypt')

t.test('encrypt (when .env file does not exist)', async ct => {
t.test('vaultEncrypt (when .env file does not exist)', async ct => {
const optsStub = sinon.stub().returns({})
const exitStub = sinon.stub(process, 'exit')
const fakeContext = {
opts: optsStub
}

// Call the encrypt function with the fake context
await encrypt.call(fakeContext, '.')
await vaultEncrypt.call(fakeContext, '.')

ct.ok(exitStub.calledWith(1), 'process.exit was called with code 1')

Expand All @@ -23,17 +23,17 @@ t.test('encrypt (when .env file does not exist)', async ct => {
ct.end()
})

t.test('encrypt (when different error not having help or code occurs)', async ct => {
const mainStub = sinon.stub(main, 'encrypt').throws(new Error('other error'))
t.test('vaultEncrypt (when different error not having help or code occurs)', async ct => {
const mainStub = sinon.stub(main, 'vaultEncrypt').throws(new Error('other error'))
const exitStub = sinon.stub(process, 'exit')

const optsStub = sinon.stub().returns({})
const fakeContext = {
opts: optsStub
}

// Call the encrypt function with the fake context
await encrypt.call(fakeContext, '.')
// Call the vaultEncrypt function with the fake context
await vaultEncrypt.call(fakeContext, '.')

ct.ok(exitStub.calledWith(1), 'process.exit was called with code 1')

Expand All @@ -43,15 +43,15 @@ t.test('encrypt (when different error not having help or code occurs)', async ct
ct.end()
})

t.test('encrypt (when .env file exists)', async ct => {
t.test('vaultEncrypt (when .env file exists)', async ct => {
// setup
try { fs.rmdirSync('tmp', { recursive: true }) } catch (_e) {}
fs.mkdirSync('tmp')
fs.writeFileSync('tmp/.env', 'HELLO=World')

// run encrypt
// run vaultEncrypt
const fakeContext = { opts: sinon.stub().returns({}) }
await encrypt.call(fakeContext, 'tmp')
await vaultEncrypt.call(fakeContext, 'tmp')

const envVault = dotenv.configDotenv({ path: 'tmp/.env.vault' }).parsed
const envKeys = dotenv.configDotenv({ path: 'tmp/.env.keys' }).parsed
Expand All @@ -65,16 +65,16 @@ t.test('encrypt (when .env file exists)', async ct => {
ct.end()
})

t.test('encrypt (when .env and .env.keys exists)', async ct => {
t.test('vaultEncrypt (when .env and .env.keys exists)', async ct => {
// setup
try { fs.rmdirSync('tmp', { recursive: true }) } catch (_e) {}
fs.mkdirSync('tmp')
fs.writeFileSync('tmp/.env', 'HELLO=World')
fs.writeFileSync('tmp/.env.keys', 'DOTENV_KEY_DEVELOPMENT="dotenv://:key_3a0eefe9cdda9b597825ebabc7c8c2e455963ca1efad639a0a6a143d9f4dd84b@dotenvx.com/vault/.env.vault?environment=development"')

// run encrypt
// run vaultEncrypt
const fakeContext = { opts: sinon.stub().returns({}) }
await encrypt.call(fakeContext, 'tmp')
await vaultEncrypt.call(fakeContext, 'tmp')

const envVault = dotenv.configDotenv({ path: 'tmp/.env.vault' }).parsed
const envKeys = dotenv.configDotenv({ path: 'tmp/.env.keys' }).parsed
Expand All @@ -88,17 +88,17 @@ t.test('encrypt (when .env and .env.keys exists)', async ct => {
ct.end()
})

t.test('encrypt (when .env, .env.keys, and .env.vault exists)', async ct => {
t.test('vaultEncrypt (when .env, .env.keys, and .env.vault exists)', async ct => {
// setup
try { fs.rmdirSync('tmp', { recursive: true }) } catch (_e) {}
fs.mkdirSync('tmp')
fs.writeFileSync('tmp/.env', 'HELLO=World')
fs.writeFileSync('tmp/.env.keys', 'DOTENV_KEY_DEVELOPMENT="dotenv://:key_3a0eefe9cdda9b597825ebabc7c8c2e455963ca1efad639a0a6a143d9f4dd84b@dotenvx.com/vault/.env.vault?environment=development"')
fs.writeFileSync('tmp/.env.vault', 'DOTENV_VAULT_DEVELOPMENT="VLl5KNUU23zt4inKfw7eLx4/CkJGhf51z5lpTiWkWPeH6433Yq0r"')

// run encrypt
// run vaultEncrypt
const fakeContext = { opts: sinon.stub().returns({}) }
await encrypt.call(fakeContext, 'tmp')
await vaultEncrypt.call(fakeContext, 'tmp')

const envVault = dotenv.configDotenv({ path: 'tmp/.env.vault' }).parsed
const envKeys = dotenv.configDotenv({ path: 'tmp/.env.keys' }).parsed
Expand All @@ -112,7 +112,7 @@ t.test('encrypt (when .env, .env.keys, and .env.vault exists)', async ct => {
ct.end()
})

t.test('encrypt (when .env, .env.keys, and .env.vault exists but .env.vault ciphertext is invalid)', async ct => {
t.test('vaultEncrypt (when .env, .env.keys, and .env.vault exists but .env.vault ciphertext is invalid)', async ct => {
// setup
try { fs.rmdirSync('tmp', { recursive: true }) } catch (_e) {}
fs.mkdirSync('tmp')
Expand All @@ -123,8 +123,8 @@ t.test('encrypt (when .env, .env.keys, and .env.vault exists but .env.vault ciph
const exitStub = sinon.stub(process, 'exit')
const fakeContext = { opts: sinon.stub().returns({}) }

// run encrypt
await encrypt.call(fakeContext, 'tmp')
// run vaultEncrypt
await vaultEncrypt.call(fakeContext, 'tmp')

ct.ok(exitStub.calledWith(1), 'process.exit was called with code 1')

Expand Down
19 changes: 3 additions & 16 deletions tests/lib/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ t.test('parse calls dotenv.parse', ct => {
ct.end()
})

t.test('encrypt calls VaultEncrypt.run', ct => {
const stub = sinon.stub(VaultEncrypt.prototype, 'run')
t.test('encrypt calls Encrypt.run', ct => {
const stub = sinon.stub(Encrypt.prototype, 'run')
stub.returns({})

main.encrypt()

t.ok(stub.called, 'new VaultEncrypt().run() called')
t.ok(stub.called, 'new Encrypt().run() called')

stub.restore()

Expand Down Expand Up @@ -118,19 +118,6 @@ t.test('set calls Sets.run', ct => {
ct.end()
})

t.test('convert calls Encrypt.run', ct => {
const stub = sinon.stub(Encrypt.prototype, 'run')
stub.returns({})

main.convert()

t.ok(stub.called, 'new Encrypt().run() called')

stub.restore()

ct.end()
})

t.test('set calls Status.run', ct => {
const stub = sinon.stub(Status.prototype, 'run')
stub.returns({})
Expand Down

0 comments on commit 1c1b0f2

Please sign in to comment.