@dotenvx/dotenvx 0.8.4 → 0.10.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/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
@@ -347,10 +347,20 @@ More examples
347
347
 
348
348
  * <details><summary>Heroku</summary><br>
349
349
 
350
+ > Add the buildpack, installing the `dotenvx` binary to your heroku deployment.
351
+
350
352
  ```sh
351
- coming soon
353
+ heroku buildpacks:add https://github.com/dotenvx/heroku-buildpack-dotenvx
352
354
  ```
353
355
 
356
+ > Use it in your Procfile.
357
+
358
+ ```sh
359
+ web: dotenvx run -- node index.js
360
+ ```
361
+
362
+ <sub><a href="https://github.com/dotenvx/heroku-buildpack-dotenvx">buildpack docs</a></sub>
363
+
354
364
  </details>
355
365
 
356
366
  * <details><summary>Laravel Forge</summary><br>
@@ -416,6 +426,32 @@ More examples
416
426
  * [dotenvx/docs](https://dotenvx.com/docs)
417
427
  * [quickstart guide](https://dotenvx.com/docs/quickstart)
418
428
 
429
+ ## Pre-commit
430
+
431
+ You can prevent `.env` files from being committed to code with this pre-commit hook.
432
+
433
+ Place this in `.git/hooks/pre-commit`
434
+
435
+ ```
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
445
+ ```
446
+
447
+ Make sure to make it executable.
448
+
449
+ ```
450
+ chmod +x .git/hooks/pre-commit
451
+ ```
452
+
453
+ You can simulate the pre-commit hook by running `dotenvx precommit` locally.
454
+
419
455
  ## Contributing
420
456
 
421
457
  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.8.4",
2
+ "version": "0.10.0",
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,55 @@
1
+ const fs = require('fs')
2
+
3
+ const ignore = require('ignore')
4
+
5
+ const logger = require('./../../shared/logger')
6
+
7
+ function precommit () {
8
+ const options = this.opts()
9
+ logger.debug(`options: ${JSON.stringify(options)}`)
10
+
11
+ // 1. check for .gitignore file
12
+ if (!fs.existsSync('.gitignore')) {
13
+ logger.error('.gitignore missing')
14
+ logger.help('? add it with [touch .gitignore]')
15
+ process.exit(1)
16
+ }
17
+
18
+ const ig = ignore().add(fs.readFileSync('.gitignore').toString())
19
+ const files = fs.readdirSync(process.cwd())
20
+ const dotenvFiles = files.filter(file => file.match(/^\.env(\..+)?$/))
21
+ dotenvFiles.forEach(file => {
22
+ // check if that file is being ignored
23
+ if (ig.ignores(file)) {
24
+ switch (file) {
25
+ case '.env.example':
26
+ logger.warn(`${file} (currently ignored but should not be)`)
27
+ logger.help(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`)
28
+ break
29
+ case '.env.vault':
30
+ logger.warn(`${file} (currently ignored but should not be)`)
31
+ logger.help(`? add !${file} to .gitignore with [echo "!${file}" >> .gitignore]`)
32
+ break
33
+ default:
34
+ logger.success(file)
35
+ break
36
+ }
37
+ } else {
38
+ switch (file) {
39
+ case '.env.example':
40
+ logger.success('.env.example') // should not be ignored
41
+ break
42
+ case '.env.vault':
43
+ logger.success('.env.vault') // should not be ignored
44
+ break
45
+ default:
46
+ logger.error(`${file} not properly gitignored`)
47
+ logger.help(`? add ${file} to .gitignore with [echo ".env*" >> .gitignore]`)
48
+ process.exit(1)
49
+ break
50
+ }
51
+ }
52
+ })
53
+ }
54
+
55
+ module.exports = precommit
@@ -2,10 +2,16 @@ 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
+
12
+ const commandArgs = this.args
13
+ logger.debug(`process command [${commandArgs.join(' ')}]`)
14
+
9
15
  const options = this.opts()
10
16
  logger.debug(`options: ${JSON.stringify(options)}`)
11
17
 
@@ -81,7 +87,19 @@ async function run () {
81
87
  readableFilepaths.add(envFilepath)
82
88
  result.injected.forEach(key => injected.add(key))
83
89
  } catch (e) {
84
- logger.warn(e)
90
+ switch (e.code) {
91
+ // missing .env
92
+ case 'ENOENT':
93
+ logger.warnv(`missing .env file (${filepath})`)
94
+ logger.help(`? in development: add one with [echo "HELLO=World" > .env] and re-run [dotenvx run -- ${commandArgs.join(' ')}]`)
95
+ logger.help('? for production: set [DOTENV_KEY] on your server and re-deploy')
96
+ break
97
+
98
+ // unhandled error
99
+ default:
100
+ logger.warn(e)
101
+ break
102
+ }
85
103
  }
86
104
  }
87
105
 
@@ -99,9 +117,8 @@ async function run () {
99
117
  logger.error(' or try: [dotenvx run -- npm run dev]')
100
118
  process.exit(1)
101
119
  } else {
102
- const subCommand = process.argv.slice(commandIndex + 1)
103
-
104
- await helpers.executeCommand(subCommand, process.env)
120
+ // const commandArgs = process.argv.slice(commandIndex + 1)
121
+ await helpers.executeCommand(commandArgs, process.env)
105
122
  }
106
123
  }
107
124
 
@@ -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) {
@@ -56,7 +53,7 @@ program
56
53
 
57
54
  // dotenvx run -- node index.js
58
55
  program.command('run')
59
- .description('inject env at runtime [dotenvx run -- your-command-here]')
56
+ .description('inject env at runtime [dotenvx run -- yourcommand]')
60
57
  .addHelpText('after', examples.run)
61
58
  .option('-f, --env-file <paths...>', 'path(s) to your env file(s)', '.env')
62
59
  .option('-o, --overload', 'override existing env variables')
@@ -69,6 +66,12 @@ 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
+ .action(require('./actions/precommit'))
74
+
72
75
  // dotenvx hub
73
76
  program.addCommand(require('./commands/hub'))
74
77
 
@@ -48,7 +48,18 @@ Try it:
48
48
  `
49
49
  }
50
50
 
51
+ const precommit = function () {
52
+ return `
53
+ Examples:
54
+
55
+ \`\`\`
56
+ $ dotenvx precommit
57
+ \`\`\`
58
+ `
59
+ }
60
+
51
61
  module.exports = {
52
62
  run,
53
- encrypt
63
+ encrypt,
64
+ precommit
54
65
  }
@@ -23,26 +23,26 @@ const resolvePath = function (filepath) {
23
23
  return path.resolve(process.cwd(), filepath)
24
24
  }
25
25
 
26
- const executeCommand = async function (subCommand, env) {
26
+ const executeCommand = async function (commandArgs, env) {
27
27
  const signals = [
28
28
  'SIGHUP', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
29
29
  'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
30
30
  ]
31
31
 
32
- logger.debug(`executing subcommand ${subCommand}`)
32
+ logger.debug(`executing process command [${commandArgs.join(' ')}]`)
33
33
 
34
34
  // handler for SIGINT
35
- let subprocess
35
+ let commandProcess
36
36
  const sigintHandler = () => {
37
37
  logger.debug('received SIGINT')
38
- logger.debug('checking subprocess')
39
- logger.debug(subprocess)
38
+ logger.debug('checking command process')
39
+ logger.debug(commandProcess)
40
40
 
41
- if (subprocess) {
42
- logger.debug('sending SIGINT to subprocess')
43
- subprocess.kill('SIGINT') // Send SIGINT to the subprocess
41
+ if (commandProcess) {
42
+ logger.debug('sending SIGINT to command process')
43
+ commandProcess.kill('SIGINT') // Send SIGINT to the command process
44
44
  } else {
45
- logger.debug('no subprocess to send SIGINT to')
45
+ logger.debug('no command process to send SIGINT to')
46
46
  }
47
47
  }
48
48
 
@@ -51,7 +51,7 @@ const executeCommand = async function (subCommand, env) {
51
51
  }
52
52
 
53
53
  try {
54
- subprocess = execa(subCommand[0], subCommand.slice(1), {
54
+ commandProcess = execa(commandArgs[0], commandArgs.slice(1), {
55
55
  stdio: 'inherit',
56
56
  env: { ...process.env, ...env }
57
57
  })
@@ -62,8 +62,8 @@ const executeCommand = async function (subCommand, env) {
62
62
  process.on(signal, () => handleOtherSignal(signal))
63
63
  })
64
64
 
65
- // Wait for the subprocess to finish
66
- const { exitCode } = await subprocess
65
+ // Wait for the command process to finish
66
+ const { exitCode } = await commandProcess
67
67
 
68
68
  if (exitCode !== 0) {
69
69
  logger.debug(`received exitCode ${exitCode}`)
@@ -72,15 +72,15 @@ const executeCommand = async function (subCommand, env) {
72
72
  } catch (error) {
73
73
  if (error.signal !== 'SIGINT') {
74
74
  logger.error(error.message)
75
- logger.error(`command [${subCommand.join(' ')}] failed`)
75
+ logger.error(`command [${commandArgs.join(' ')}] failed`)
76
76
  logger.error('')
77
- logger.error(` try without dotenvx: [${subCommand.join(' ')}]`)
77
+ logger.error(` try without dotenvx: [${commandArgs.join(' ')}]`)
78
78
  logger.error('')
79
79
  logger.error('if that succeeds, then dotenvx is the culprit. report issue:')
80
80
  logger.error(`<${REPORT_ISSUE_LINK}>`)
81
81
  }
82
82
 
83
- // Exit with the error code from the subprocess, or 1 if unavailable
83
+ // Exit with the error code from the command process, or 1 if unavailable
84
84
  process.exit(error.exitCode || 1)
85
85
  } finally {
86
86
  // Clean up: Remove the SIGINT handler
@@ -1,6 +1,6 @@
1
1
  const fs = require('fs')
2
2
 
3
- const FORMATS = ['.env*', '!.env.vault', '.flaskenv*']
3
+ const FORMATS = ['.env*', '!.env.vault']
4
4
 
5
5
  class Generic {
6
6
  constructor (filename, touchFile = false) {
@@ -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
  }
@@ -11,6 +11,7 @@ const packageJson = require('./packageJson')
11
11
  const levels = {
12
12
  error: 0,
13
13
  warn: 1,
14
+ warnv: 1,
14
15
  success: 2,
15
16
  successv: 2,
16
17
  info: 2,
@@ -41,6 +42,8 @@ const dotenvxFormat = printf(({ level, message, label, timestamp }) => {
41
42
  return error(formattedMessage)
42
43
  case 'warn':
43
44
  return warn(formattedMessage)
45
+ case 'warnv':
46
+ return warn(`[dotenvx@${packageJson.version}] ${formattedMessage}`)
44
47
  case 'success':
45
48
  return success(formattedMessage)
46
49
  case 'successv': // success with 'version'