@dotenvx/dotenvx 0.21.0 → 0.23.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/package.json +2 -2
- package/src/cli/actions/precommit.js +22 -2
- package/src/lib/helpers/dotenvVault.js +5 -2
- package/src/lib/helpers/installPrecommitHook.js +14 -8
- package/src/lib/helpers/pluralize.js +10 -0
- package/src/lib/helpers/removePersonal.js +9 -0
- package/src/lib/main.js +10 -1
- package/src/lib/services/precommit.js +44 -52
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
2
|
+
"version": "0.23.0",
|
|
3
3
|
"name": "@dotenvx/dotenvx",
|
|
4
4
|
"description": "a better dotenv–from the creator of `dotenv`",
|
|
5
5
|
"author": "@motdotla",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"clipboardy": "^2.3.0",
|
|
33
33
|
"commander": "^11.1.0",
|
|
34
34
|
"conf": "^10.2.0",
|
|
35
|
-
"dotenv": "16.4.5",
|
|
35
|
+
"dotenv": "^16.4.5",
|
|
36
36
|
"dotenv-expand": "^11.0.6",
|
|
37
37
|
"execa": "^5.1.1",
|
|
38
38
|
"glob": "^10.3.10",
|
|
@@ -6,8 +6,28 @@ function precommit () {
|
|
|
6
6
|
const options = this.opts()
|
|
7
7
|
logger.debug(`options: ${JSON.stringify(options)}`)
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
try {
|
|
10
|
+
const {
|
|
11
|
+
successMessage,
|
|
12
|
+
warnings
|
|
13
|
+
} = new Precommit(options).run()
|
|
14
|
+
|
|
15
|
+
for (const warning of warnings) {
|
|
16
|
+
logger.warnv(warning.message)
|
|
17
|
+
if (warning.help) {
|
|
18
|
+
logger.help(warning.help)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
logger.successvp(successMessage)
|
|
23
|
+
} catch (error) {
|
|
24
|
+
logger.errorvp(error.message)
|
|
25
|
+
if (error.help) {
|
|
26
|
+
logger.help(error.help)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
process.exit(1)
|
|
30
|
+
}
|
|
11
31
|
}
|
|
12
32
|
|
|
13
33
|
module.exports = precommit
|
|
@@ -3,6 +3,7 @@ const path = require('path')
|
|
|
3
3
|
const encrypt = require('./encrypt')
|
|
4
4
|
const changed = require('./changed')
|
|
5
5
|
const guessEnvironment = require('./guessEnvironment')
|
|
6
|
+
const removePersonal = require('./removePersonal')
|
|
6
7
|
|
|
7
8
|
class DotenvVault {
|
|
8
9
|
constructor (dotenvFiles = {}, dotenvKeys = {}, dotenvVaults = {}) {
|
|
@@ -23,8 +24,10 @@ class DotenvVault {
|
|
|
23
24
|
let ciphertext = this.dotenvVaults[vault]
|
|
24
25
|
const dotenvKey = this.dotenvKeys[`DOTENV_KEY_${environment.toUpperCase()}`]
|
|
25
26
|
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
const cleanRaw = removePersonal(raw)
|
|
28
|
+
|
|
29
|
+
if (!ciphertext || ciphertext.length === 0 || changed(ciphertext, cleanRaw, dotenvKey)) {
|
|
30
|
+
ciphertext = encrypt(cleanRaw, dotenvKey)
|
|
28
31
|
this.dotenvVaults[vault] = ciphertext
|
|
29
32
|
addedVaults.add(vault) // for info logging to user
|
|
30
33
|
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
const fs = require('fs')
|
|
2
2
|
const path = require('path')
|
|
3
3
|
|
|
4
|
-
const logger = require('./../../shared/logger')
|
|
5
|
-
|
|
6
4
|
const HOOK_SCRIPT = `#!/bin/sh
|
|
7
5
|
|
|
8
6
|
if ! command -v dotenvx &> /dev/null
|
|
@@ -21,20 +19,30 @@ class InstallPrecommitHook {
|
|
|
21
19
|
}
|
|
22
20
|
|
|
23
21
|
run () {
|
|
22
|
+
let successMessage
|
|
23
|
+
|
|
24
24
|
try {
|
|
25
25
|
// Check if the pre-commit file already exists
|
|
26
26
|
if (this._exists()) {
|
|
27
27
|
// Check if 'dotenvx precommit' already exists in the file
|
|
28
|
-
if (
|
|
29
|
-
|
|
28
|
+
if (this._currentHook().includes('dotenvx precommit')) {
|
|
29
|
+
// do nothing
|
|
30
|
+
successMessage = `dotenvx precommit exists [${this.hookPath}]`
|
|
30
31
|
} else {
|
|
31
|
-
|
|
32
|
+
this._appendHook()
|
|
33
|
+
successMessage = `dotenvx precommit appended [${this.hookPath}]`
|
|
32
34
|
}
|
|
33
35
|
} else {
|
|
34
36
|
this._createHook()
|
|
37
|
+
successMessage = `dotenvx precommit installed [${this.hookPath}]`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
successMessage
|
|
35
42
|
}
|
|
36
43
|
} catch (err) {
|
|
37
|
-
|
|
44
|
+
const error = new Error(`failed to modify pre-commit hook: ${err.message}`)
|
|
45
|
+
throw error
|
|
38
46
|
}
|
|
39
47
|
}
|
|
40
48
|
|
|
@@ -50,13 +58,11 @@ class InstallPrecommitHook {
|
|
|
50
58
|
// If the pre-commit file doesn't exist, create a new one with the hookScript
|
|
51
59
|
fs.writeFileSync(this.hookPath, HOOK_SCRIPT)
|
|
52
60
|
fs.chmodSync(this.hookPath, '755') // Make the file executable
|
|
53
|
-
logger.successvp(`dotenvx precommit installed [${this.hookPath}]`)
|
|
54
61
|
}
|
|
55
62
|
|
|
56
63
|
_appendHook () {
|
|
57
64
|
// Append 'dotenvx precommit' to the existing file
|
|
58
65
|
fs.appendFileSync(this.hookPath, '\n' + HOOK_SCRIPT)
|
|
59
|
-
logger.successvp(`dotenvx precommit appended [${this.hookPath}]`)
|
|
60
66
|
}
|
|
61
67
|
}
|
|
62
68
|
|
package/src/lib/main.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const logger = require('./../shared/logger')
|
|
2
2
|
const dotenv = require('dotenv')
|
|
3
|
+
const dotenvExpand = require('dotenv-expand')
|
|
3
4
|
|
|
4
5
|
// services
|
|
5
6
|
const Encrypt = require('./services/encrypt')
|
|
@@ -9,7 +10,15 @@ const Genexample = require('./services/genexample')
|
|
|
9
10
|
|
|
10
11
|
// proxies to dotenv
|
|
11
12
|
const config = function (options) {
|
|
12
|
-
|
|
13
|
+
const env = dotenv.config(options)
|
|
14
|
+
|
|
15
|
+
// if processEnv passed also pass to expand
|
|
16
|
+
if (options && options.processEnv) {
|
|
17
|
+
env.processEnv = options.processEnv
|
|
18
|
+
}
|
|
19
|
+
const expanded = dotenvExpand.expand(env)
|
|
20
|
+
|
|
21
|
+
return expanded
|
|
13
22
|
}
|
|
14
23
|
|
|
15
24
|
const configDotenv = function (options) {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/* istanbul ignore file */
|
|
2
2
|
const fs = require('fs')
|
|
3
3
|
const ignore = require('ignore')
|
|
4
|
-
const logger = require('./../../shared/logger')
|
|
5
|
-
const helpers = require('./../../cli/helpers')
|
|
6
4
|
|
|
5
|
+
const pluralize = require('./../helpers/pluralize')
|
|
7
6
|
const InstallPrecommitHook = require('./../helpers/installPrecommitHook')
|
|
8
7
|
|
|
9
8
|
class Precommit {
|
|
@@ -13,66 +12,59 @@ class Precommit {
|
|
|
13
12
|
|
|
14
13
|
run () {
|
|
15
14
|
if (this.install) {
|
|
16
|
-
|
|
15
|
+
const {
|
|
16
|
+
successMessage
|
|
17
|
+
} = this._installPrecommitHook()
|
|
17
18
|
|
|
18
|
-
return
|
|
19
|
-
|
|
19
|
+
return {
|
|
20
|
+
successMessage,
|
|
21
|
+
warnings: []
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
const warnings = []
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
// 1. check for .gitignore file
|
|
27
|
+
if (!fs.existsSync('.gitignore')) {
|
|
28
|
+
const error = new Error('.gitignore missing')
|
|
29
|
+
error.help = '? add it with [touch .gitignore]'
|
|
30
|
+
throw error
|
|
31
|
+
}
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
break
|
|
47
|
-
default:
|
|
48
|
-
break
|
|
49
|
-
}
|
|
50
|
-
} else {
|
|
51
|
-
switch (file) {
|
|
52
|
-
case '.env.example':
|
|
53
|
-
break
|
|
54
|
-
case '.env.vault':
|
|
55
|
-
break
|
|
56
|
-
default:
|
|
57
|
-
logger.errorvp(`${file} not properly gitignored`)
|
|
58
|
-
logger.help2(`? add ${file} to .gitignore with [echo ".env*" >> .gitignore]`)
|
|
59
|
-
process.exit(1) // 3.1 exit early with error code
|
|
60
|
-
break
|
|
33
|
+
// 2. check .env* files against .gitignore file
|
|
34
|
+
const ig = ignore().add(fs.readFileSync('.gitignore').toString())
|
|
35
|
+
const files = fs.readdirSync(process.cwd())
|
|
36
|
+
const dotenvFiles = files.filter(file => file.match(/^\.env(\..+)?$/))
|
|
37
|
+
dotenvFiles.forEach(file => {
|
|
38
|
+
// check if that file is being ignored
|
|
39
|
+
if (ig.ignores(file)) {
|
|
40
|
+
if (file === '.env.example' || file === '.env.vault') {
|
|
41
|
+
const warning = new Error(`${file} (currently ignored but should not be)`)
|
|
42
|
+
warning.help = `? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`
|
|
43
|
+
warnings.push(warning)
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
if (file !== '.env.example' && file !== '.env.vault') {
|
|
47
|
+
const error = new Error(`${file} not properly gitignored`)
|
|
48
|
+
error.help = `? add ${file} to .gitignore with [echo ".env*" >> .gitignore]`
|
|
49
|
+
throw error
|
|
50
|
+
}
|
|
61
51
|
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
let successMessage = 'success'
|
|
55
|
+
if (warnings.length > 0) {
|
|
56
|
+
successMessage = `success (with ${pluralize('warning', warnings.length)})`
|
|
62
57
|
}
|
|
63
|
-
})
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
logger.successvp('success')
|
|
59
|
+
return {
|
|
60
|
+
successMessage,
|
|
61
|
+
warnings
|
|
62
|
+
}
|
|
70
63
|
}
|
|
71
64
|
}
|
|
72
65
|
|
|
73
|
-
/* istanbul ignore next */
|
|
74
66
|
_installPrecommitHook () {
|
|
75
|
-
new InstallPrecommitHook().run()
|
|
67
|
+
return new InstallPrecommitHook().run()
|
|
76
68
|
}
|
|
77
69
|
}
|
|
78
70
|
|