@dotenvx/dotenvx 0.10.0 → 0.10.2

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/README.md CHANGED
@@ -359,7 +359,7 @@ More examples
359
359
  web: dotenvx run -- node index.js
360
360
  ```
361
361
 
362
- <sub><a href="https://github.com/dotenvx/heroku-buildpack-dotenvx">buildpack docs</a></sub>
362
+ [see heroku guide](https://dotenvx.com/docs/platforms/heroku)
363
363
 
364
364
  </details>
365
365
 
@@ -430,27 +430,21 @@ More examples
430
430
 
431
431
  You can prevent `.env` files from being committed to code with this pre-commit hook.
432
432
 
433
- Place this in `.git/hooks/pre-commit`
434
-
435
433
  ```
436
- #!/bin/sh
437
- dotenvx precommit --quiet
438
-
439
- # Check dotenvx precommit exit status
440
- if [ $? -ne 0 ]; then
441
- echo "dotenvx pre-commit failed. run [dotenvx precommit] for more information"
442
- exit 1
443
- fi
444
- exit 0
434
+ dotenvx precommit --install
445
435
  ```
446
436
 
447
- Make sure to make it executable.
437
+ Run without the `--install` flag to preview output.
448
438
 
449
439
  ```
450
- chmod +x .git/hooks/pre-commit
440
+ dotenvx precommit
451
441
  ```
452
442
 
453
- You can simulate the pre-commit hook by running `dotenvx precommit` locally.
443
+ To ignore the pre-commit hook run your git commit with the `--no-verify` flag.
444
+
445
+ ```
446
+ git commit -am "msg" --no-verify
447
+ ```
454
448
 
455
449
  ## Contributing
456
450
 
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.10.0",
2
+ "version": "0.10.2",
3
3
  "name": "@dotenvx/dotenvx",
4
4
  "description": "a better dotenv–from the creator of `dotenv`",
5
5
  "author": "@motdotla",
@@ -1,20 +1,30 @@
1
1
  const fs = require('fs')
2
+ const path = require('path')
2
3
 
3
4
  const ignore = require('ignore')
4
5
 
5
6
  const logger = require('./../../shared/logger')
7
+ const helpers = require('./../helpers')
6
8
 
7
9
  function precommit () {
8
10
  const options = this.opts()
9
11
  logger.debug(`options: ${JSON.stringify(options)}`)
10
12
 
13
+ // 0. handle the --install flag
14
+ if (options.install) {
15
+ installPrecommitHook()
16
+ return
17
+ }
18
+
11
19
  // 1. check for .gitignore file
12
20
  if (!fs.existsSync('.gitignore')) {
13
- logger.error('.gitignore missing')
14
- logger.help('? add it with [touch .gitignore]')
21
+ logger.errorvp('.gitignore missing')
22
+ logger.help2('? add it with [touch .gitignore]')
15
23
  process.exit(1)
16
24
  }
17
25
 
26
+ // 2. check .env* files against .gitignore file
27
+ let warningCount = 0
18
28
  const ig = ignore().add(fs.readFileSync('.gitignore').toString())
19
29
  const files = fs.readdirSync(process.cwd())
20
30
  const dotenvFiles = files.filter(file => file.match(/^\.env(\..+)?$/))
@@ -23,33 +33,68 @@ function precommit () {
23
33
  if (ig.ignores(file)) {
24
34
  switch (file) {
25
35
  case '.env.example':
26
- logger.warn(`${file} (currently ignored but should not be)`)
27
- logger.help(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`)
36
+ warningCount += 1
37
+ logger.warnv(`${file} (currently ignored but should not be)`)
38
+ logger.help2(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`)
28
39
  break
29
40
  case '.env.vault':
30
- logger.warn(`${file} (currently ignored but should not be)`)
31
- logger.help(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`)
41
+ warningCount += 1
42
+ logger.warnv(`${file} (currently ignored but should not be)`)
43
+ logger.help2(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`)
32
44
  break
33
45
  default:
34
- logger.success(file)
35
46
  break
36
47
  }
37
48
  } else {
38
49
  switch (file) {
39
50
  case '.env.example':
40
- logger.success('.env.example') // should not be ignored
41
51
  break
42
52
  case '.env.vault':
43
- logger.success('.env.vault') // should not be ignored
44
53
  break
45
54
  default:
46
- logger.error(`${file} not properly gitignored`)
47
- logger.help(`? add ${file} to .gitignore with [echo ".env*" >> .gitignore]`)
48
- process.exit(1)
55
+ logger.errorvp(`${file} not properly gitignored`)
56
+ logger.help2(`? add ${file} to .gitignore with [echo ".env*" >> .gitignore]`)
57
+ process.exit(1) // 3.1 exit early with error code
49
58
  break
50
59
  }
51
60
  }
52
61
  })
62
+
63
+ // 3. outpout success
64
+ if (warningCount > 0) {
65
+ logger.successvp(`success (with ${helpers.pluralize('warning', warningCount)})`)
66
+ } else {
67
+ logger.successvp('success')
68
+ }
69
+ }
70
+
71
+ function installPrecommitHook () {
72
+ const hookScript = '#!/bin/sh\ndotenvx precommit\n'
73
+ const hookPath = path.join('.git', 'hooks', 'pre-commit')
74
+
75
+ try {
76
+ // Check if the pre-commit file already exists
77
+ if (fs.existsSync(hookPath)) {
78
+ // Read the existing content of the file
79
+ const existingContent = fs.readFileSync(hookPath, 'utf8')
80
+
81
+ // Check if 'dotenvx precommit' already exists in the file
82
+ if (!existingContent.includes('dotenvx precommit')) {
83
+ // Append 'dotenvx precommit' to the existing file
84
+ fs.appendFileSync(hookPath, '\n' + hookScript)
85
+ logger.successvp(`dotenvx precommit appended [${hookPath}]`)
86
+ } else {
87
+ logger.warnvp(`dotenvx precommit exists [${hookPath}]`)
88
+ }
89
+ } else {
90
+ // If the pre-commit file doesn't exist, create a new one with the hookScript
91
+ fs.writeFileSync(hookPath, hookScript)
92
+ fs.chmodSync(hookPath, '755') // Make the file executable
93
+ logger.successvp(`dotenvx precommit installed [${hookPath}]`)
94
+ }
95
+ } catch (err) {
96
+ logger.errorvp(`Failed to modify pre-commit hook: ${err.message}`)
97
+ }
53
98
  }
54
99
 
55
100
  module.exports = precommit
@@ -90,7 +90,7 @@ async function run () {
90
90
  switch (e.code) {
91
91
  // missing .env
92
92
  case 'ENOENT':
93
- logger.warnv(`missing .env file (${filepath})`)
93
+ logger.warnv(`missing ${envFilepath} file (${filepath})`)
94
94
  logger.help(`? in development: add one with [echo "HELLO=World" > .env] and re-run [dotenvx run -- ${commandArgs.join(' ')}]`)
95
95
  logger.help('? for production: set [DOTENV_KEY] on your server and re-deploy')
96
96
  break
@@ -70,6 +70,7 @@ program.command('encrypt')
70
70
  program.command('precommit')
71
71
  .description('dotenvx precommit check')
72
72
  .addHelpText('after', examples.precommit)
73
+ .option('-i, --install', 'install to .git/hooks/pre-commit')
73
74
  .action(require('./actions/precommit'))
74
75
 
75
76
  // dotenvx hub
@@ -54,6 +54,14 @@ Examples:
54
54
 
55
55
  \`\`\`
56
56
  $ dotenvx precommit
57
+ $ dotenvx precommit --install
58
+ \`\`\`
59
+
60
+ Try it:
61
+
62
+ \`\`\`
63
+ $ dotenvx precommit
64
+ [dotenvx@0.10.0][precommit] success
57
65
  \`\`\`
58
66
  `
59
67
  }
@@ -10,10 +10,14 @@ const packageJson = require('./packageJson')
10
10
 
11
11
  const levels = {
12
12
  error: 0,
13
+ errorv: 0,
14
+ errorvp: 0,
13
15
  warn: 1,
14
16
  warnv: 1,
17
+ warnvp: 1,
15
18
  success: 2,
16
19
  successv: 2,
20
+ successvp: 2,
17
21
  info: 2,
18
22
  help: 2,
19
23
  help2: 2,
@@ -40,14 +44,22 @@ const dotenvxFormat = printf(({ level, message, label, timestamp }) => {
40
44
  switch (level.toLowerCase()) {
41
45
  case 'error':
42
46
  return error(formattedMessage)
47
+ case 'errorv':
48
+ return error(`[dotenvx@${packageJson.version}] ${formattedMessage}`)
49
+ case 'errorvp':
50
+ return error(`[dotenvx@${packageJson.version}][precommit] ${formattedMessage}`)
43
51
  case 'warn':
44
52
  return warn(formattedMessage)
45
53
  case 'warnv':
46
54
  return warn(`[dotenvx@${packageJson.version}] ${formattedMessage}`)
55
+ case 'warnvp':
56
+ return warn(`[dotenvx@${packageJson.version}][precommit] ${formattedMessage}`)
47
57
  case 'success':
48
58
  return success(formattedMessage)
49
59
  case 'successv': // success with 'version'
50
60
  return successv(`[dotenvx@${packageJson.version}] ${formattedMessage}`)
61
+ case 'successvp': // success with 'version' and precommit
62
+ return success(`[dotenvx@${packageJson.version}][precommit] ${formattedMessage}`)
51
63
  case 'info':
52
64
  return formattedMessage
53
65
  case 'help':