@dotenvx/dotenvx
Advanced tools
Comparing version
{ | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"name": "@dotenvx/dotenvx", | ||
@@ -4,0 +4,0 @@ "description": "a better dotenv–from the creator of `dotenv`", |
@@ -9,6 +9,26 @@ const logger = require('./../../shared/logger') | ||
const precommit = new Precommit(options) | ||
precommit.run() | ||
try { | ||
const { | ||
successMessage, | ||
warnings | ||
} = new Precommit(options).run() | ||
for (const warning of warnings) { | ||
logger.warnv(warning.message) | ||
if (warning.help) { | ||
logger.help(warning.help) | ||
} | ||
} | ||
logger.successvp(successMessage) | ||
} catch (error) { | ||
logger.errorvp(error.message) | ||
if (error.help) { | ||
logger.help(error.help) | ||
} | ||
process.exit(1) | ||
} | ||
} | ||
module.exports = precommit |
@@ -6,2 +6,3 @@ const path = require('path') | ||
const guessEnvironment = require('./guessEnvironment') | ||
const removePersonal = require('./removePersonal') | ||
@@ -27,4 +28,6 @@ class DotenvVault { | ||
if (!ciphertext || ciphertext.length === 0 || changed(ciphertext, raw, dotenvKey)) { | ||
ciphertext = encrypt(raw, dotenvKey) | ||
const cleanRaw = removePersonal(raw) | ||
if (!ciphertext || ciphertext.length === 0 || changed(ciphertext, cleanRaw, dotenvKey)) { | ||
ciphertext = encrypt(cleanRaw, dotenvKey) | ||
this.dotenvVaults[vault] = ciphertext | ||
@@ -31,0 +34,0 @@ addedVaults.add(vault) // for info logging to user |
const fs = require('fs') | ||
const path = require('path') | ||
const logger = require('./../../shared/logger') | ||
const HOOK_SCRIPT = `#!/bin/sh | ||
@@ -24,2 +22,4 @@ | ||
run () { | ||
let successMessage | ||
try { | ||
@@ -29,12 +29,20 @@ // Check if the pre-commit file already exists | ||
// Check if 'dotenvx precommit' already exists in the file | ||
if (!this._currentHook().includes('dotenvx precommit')) { | ||
if (this._currentHook().includes('dotenvx precommit')) { | ||
// do nothing | ||
successMessage = `dotenvx precommit exists [${this.hookPath}]` | ||
} else { | ||
this._appendHook() | ||
} else { | ||
logger.warnvp(`dotenvx precommit exists [${this.hookPath}]`) | ||
successMessage = `dotenvx precommit appended [${this.hookPath}]` | ||
} | ||
} else { | ||
this._createHook() | ||
successMessage = `dotenvx precommit installed [${this.hookPath}]` | ||
} | ||
return { | ||
successMessage | ||
} | ||
} catch (err) { | ||
logger.errorvp(`failed to modify pre-commit hook: ${err.message}`) | ||
const error = new Error(`failed to modify pre-commit hook: ${err.message}`) | ||
throw error | ||
} | ||
@@ -55,3 +63,2 @@ } | ||
fs.chmodSync(this.hookPath, '755') // Make the file executable | ||
logger.successvp(`dotenvx precommit installed [${this.hookPath}]`) | ||
} | ||
@@ -62,3 +69,2 @@ | ||
fs.appendFileSync(this.hookPath, '\n' + HOOK_SCRIPT) | ||
logger.successvp(`dotenvx precommit appended [${this.hookPath}]`) | ||
} | ||
@@ -65,0 +71,0 @@ } |
/* istanbul ignore file */ | ||
const fs = require('fs') | ||
const ignore = require('ignore') | ||
const logger = require('./../../shared/logger') | ||
const helpers = require('./../../cli/helpers') | ||
const pluralize = require('./../helpers/pluralize') | ||
const InstallPrecommitHook = require('./../helpers/installPrecommitHook') | ||
@@ -16,62 +15,55 @@ | ||
if (this.install) { | ||
this._installPrecommitHook() | ||
const { | ||
successMessage | ||
} = this._installPrecommitHook() | ||
return true | ||
} | ||
return { | ||
successMessage, | ||
warnings: [] | ||
} | ||
} else { | ||
const warnings = [] | ||
// 1. check for .gitignore file | ||
if (!fs.existsSync('.gitignore')) { | ||
logger.errorvp('.gitignore missing') | ||
logger.help2('? add it with [touch .gitignore]') | ||
process.exit(1) | ||
} | ||
// 1. check for .gitignore file | ||
if (!fs.existsSync('.gitignore')) { | ||
const error = new Error('.gitignore missing') | ||
error.help = '? add it with [touch .gitignore]' | ||
throw error | ||
} | ||
// 2. check .env* files against .gitignore file | ||
let warningCount = 0 | ||
const ig = ignore().add(fs.readFileSync('.gitignore').toString()) | ||
const files = fs.readdirSync(process.cwd()) | ||
const dotenvFiles = files.filter(file => file.match(/^\.env(\..+)?$/)) | ||
dotenvFiles.forEach(file => { | ||
// check if that file is being ignored | ||
if (ig.ignores(file)) { | ||
switch (file) { | ||
case '.env.example': | ||
warningCount += 1 | ||
logger.warnv(`${file} (currently ignored but should not be)`) | ||
logger.help2(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`) | ||
break | ||
case '.env.vault': | ||
warningCount += 1 | ||
logger.warnv(`${file} (currently ignored but should not be)`) | ||
logger.help2(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`) | ||
break | ||
default: | ||
break | ||
// 2. check .env* files against .gitignore file | ||
const ig = ignore().add(fs.readFileSync('.gitignore').toString()) | ||
const files = fs.readdirSync(process.cwd()) | ||
const dotenvFiles = files.filter(file => file.match(/^\.env(\..+)?$/)) | ||
dotenvFiles.forEach(file => { | ||
// check if that file is being ignored | ||
if (ig.ignores(file)) { | ||
if (file === '.env.example' || file === '.env.vault') { | ||
const warning = new Error(`${file} (currently ignored but should not be)`) | ||
warning.help = `? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]` | ||
warnings.push(warning) | ||
} | ||
} else { | ||
if (file !== '.env.example' && file !== '.env.vault') { | ||
const error = new Error(`${file} not properly gitignored`) | ||
error.help = `? add ${file} to .gitignore with [echo ".env*" >> .gitignore]` | ||
throw error | ||
} | ||
} | ||
} else { | ||
switch (file) { | ||
case '.env.example': | ||
break | ||
case '.env.vault': | ||
break | ||
default: | ||
logger.errorvp(`${file} not properly gitignored`) | ||
logger.help2(`? add ${file} to .gitignore with [echo ".env*" >> .gitignore]`) | ||
process.exit(1) // 3.1 exit early with error code | ||
break | ||
} | ||
}) | ||
let successMessage = 'success' | ||
if (warnings.length > 0) { | ||
successMessage = `success (with ${pluralize('warning', warnings.length)})` | ||
} | ||
}) | ||
// 3. outpout success | ||
if (warningCount > 0) { | ||
logger.successvp(`success (with ${helpers.pluralize('warning', warningCount)})`) | ||
} else { | ||
logger.successvp('success') | ||
return { | ||
successMessage, | ||
warnings | ||
} | ||
} | ||
} | ||
/* istanbul ignore next */ | ||
_installPrecommitHook () { | ||
new InstallPrecommitHook().run() | ||
return new InstallPrecommitHook().run() | ||
} | ||
@@ -78,0 +70,0 @@ } |
90171
0.71%50
4.17%2154
1.41%