@dotenvx/dotenvx 0.9.0 → 0.10.1

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/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024, Scott Motte
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -426,6 +426,26 @@ More examples
426
426
  * [dotenvx/docs](https://dotenvx.com/docs)
427
427
  * [quickstart guide](https://dotenvx.com/docs/quickstart)
428
428
 
429
+ ## Pre-commit
430
+
431
+ You can prevent `.env` files from being committed to code with this pre-commit hook.
432
+
433
+ ```
434
+ dotenvx precommit --install
435
+ ```
436
+
437
+ Run without the `--install` flag to preview output.
438
+
439
+ ```
440
+ dotenvx precommit
441
+ ```
442
+
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
+ ```
448
+
429
449
  ## Contributing
430
450
 
431
451
  You can fork this repo and create [pull requests](https://github.com/dotenvx/dotenvx/pulls) or if you have questions or feedback:
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.9.0",
2
+ "version": "0.10.1",
3
3
  "name": "@dotenvx/dotenvx",
4
4
  "description": "a better dotenv–from the creator of `dotenv`",
5
5
  "author": "@motdotla",
@@ -31,11 +31,12 @@
31
31
  "commander": "^11.1.0",
32
32
  "conf": "^10.2.0",
33
33
  "dotenv": "^16.3.1",
34
+ "execa": "^5.1.1",
35
+ "ignore": "^5.3.0",
34
36
  "open": "^8.4.2",
35
37
  "ora": "^5.4.1",
36
38
  "qrcode-terminal": "^0.12.0",
37
39
  "update-notifier": "^5.1.0",
38
- "execa": "^5.1.1",
39
40
  "winston": "^3.11.0",
40
41
  "xxhashjs": "^0.2.2"
41
42
  },
@@ -4,6 +4,7 @@ const main = require('./../../lib/main')
4
4
  const logger = require('./../../shared/logger')
5
5
  const helpers = require('./../helpers')
6
6
  const createSpinner = require('./../../shared/createSpinner')
7
+ const { AppendToIgnores } = require('./../ignores')
7
8
 
8
9
  const spinner = createSpinner('encrypting')
9
10
 
@@ -11,6 +12,8 @@ const spinner = createSpinner('encrypting')
11
12
  const ENCODING = 'utf8'
12
13
 
13
14
  async function encrypt () {
15
+ new AppendToIgnores().run()
16
+
14
17
  spinner.start()
15
18
  await helpers.sleep(500) // better dx
16
19
 
@@ -0,0 +1,100 @@
1
+ const fs = require('fs')
2
+ const path = require('path')
3
+
4
+ const ignore = require('ignore')
5
+
6
+ const logger = require('./../../shared/logger')
7
+ const helpers = require('./../helpers')
8
+
9
+ function precommit () {
10
+ const options = this.opts()
11
+ logger.debug(`options: ${JSON.stringify(options)}`)
12
+
13
+ // 0. handle the --install flag
14
+ if (options.install) {
15
+ installPrecommitHook()
16
+ return
17
+ }
18
+
19
+ // 1. check for .gitignore file
20
+ if (!fs.existsSync('.gitignore')) {
21
+ logger.errorvp('.gitignore missing')
22
+ logger.help2('? add it with [touch .gitignore]')
23
+ process.exit(1)
24
+ }
25
+
26
+ // 2. check .env* files against .gitignore file
27
+ let warningCount = 0
28
+ const ig = ignore().add(fs.readFileSync('.gitignore').toString())
29
+ const files = fs.readdirSync(process.cwd())
30
+ const dotenvFiles = files.filter(file => file.match(/^\.env(\..+)?$/))
31
+ dotenvFiles.forEach(file => {
32
+ // check if that file is being ignored
33
+ if (ig.ignores(file)) {
34
+ switch (file) {
35
+ case '.env.example':
36
+ warningCount += 1
37
+ logger.warnv(`${file} (currently ignored but should not be)`)
38
+ logger.help2(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`)
39
+ break
40
+ case '.env.vault':
41
+ warningCount += 1
42
+ logger.warnv(`${file} (currently ignored but should not be)`)
43
+ logger.help2(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`)
44
+ break
45
+ default:
46
+ break
47
+ }
48
+ } else {
49
+ switch (file) {
50
+ case '.env.example':
51
+ break
52
+ case '.env.vault':
53
+ break
54
+ default:
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
58
+ break
59
+ }
60
+ }
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
+ }
98
+ }
99
+
100
+ module.exports = precommit
@@ -2,10 +2,13 @@ const fs = require('fs')
2
2
  const logger = require('./../../shared/logger')
3
3
  const helpers = require('./../helpers')
4
4
  const main = require('./../../lib/main')
5
+ const { AppendToIgnores } = require('./../ignores')
5
6
 
6
7
  const ENCODING = 'utf8'
7
8
 
8
9
  async function run () {
10
+ new AppendToIgnores().run()
11
+
9
12
  const commandArgs = this.args
10
13
  logger.debug(`process command [${commandArgs.join(' ')}]`)
11
14
 
@@ -7,7 +7,6 @@ const program = new Command()
7
7
  const logger = require('./../shared/logger')
8
8
  const helpers = require('./helpers')
9
9
  const examples = require('./examples')
10
- const { AppendToIgnores } = require('./ignores')
11
10
  const packageJson = require('./../shared/packageJson')
12
11
 
13
12
  // once a day check for any updates
@@ -23,8 +22,6 @@ program
23
22
  .option('-v, --verbose', 'sets log level to verbose')
24
23
  .option('-d, --debug', 'sets log level to debug')
25
24
  .hook('preAction', (thisCommand, actionCommand) => {
26
- new AppendToIgnores().run()
27
-
28
25
  const options = thisCommand.opts()
29
26
 
30
27
  if (options.logLevel) {
@@ -69,6 +66,13 @@ program.command('encrypt')
69
66
  .option('-f, --env-file <paths...>', 'path(s) to your env file(s)', helpers.findEnvFiles('./'))
70
67
  .action(require('./actions/encrypt'))
71
68
 
69
+ // dotenvx precommit
70
+ program.command('precommit')
71
+ .description('dotenvx precommit check')
72
+ .addHelpText('after', examples.precommit)
73
+ .option('-i, --install', 'install to .git/hooks/pre-commit')
74
+ .action(require('./actions/precommit'))
75
+
72
76
  // dotenvx hub
73
77
  program.addCommand(require('./commands/hub'))
74
78
 
@@ -48,7 +48,26 @@ Try it:
48
48
  `
49
49
  }
50
50
 
51
+ const precommit = function () {
52
+ return `
53
+ Examples:
54
+
55
+ \`\`\`
56
+ $ dotenvx precommit
57
+ $ dotenvx precommit --install
58
+ \`\`\`
59
+
60
+ Try it:
61
+
62
+ \`\`\`
63
+ $ dotenvx precommit
64
+ [dotenvx@0.10.0][precommit] success
65
+ \`\`\`
66
+ `
67
+ }
68
+
51
69
  module.exports = {
52
70
  run,
53
- encrypt
71
+ encrypt,
72
+ precommit
54
73
  }
@@ -7,6 +7,8 @@ const createSpinner = function (initialMessage = '') {
7
7
  return {
8
8
  start: (message) => spinner.start(message),
9
9
  succeed: (message) => spinner.succeed(chalk.keyword('green')(message)),
10
+ warn: (message) => spinner.warn(chalk.keyword('orangered')(message)),
11
+ info: (message) => spinner.info(chalk.keyword('blue')(message)),
10
12
  done: (message) => spinner.succeed(message),
11
13
  fail: (message) => spinner.fail(chalk.bold.red(message))
12
14
  }
@@ -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':