@kaynooo/cli-env 0.15.2 → 0.15.4

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/dist/ev.js CHANGED
@@ -1,5 +1,5 @@
1
- #!/usr/bin/env -S bun --no-env-file --config=/dev/null
2
- // @bun
1
+ #!/bin/sh
2
+ // @bun 2>/dev/null; exec bun --no-env-file --config=/dev/null "$0" "$@"
3
3
  var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
4
4
  var __esm = (fn, res, err) => () => {
5
5
  if (fn)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kaynooo/cli-env",
3
3
  "type": "module",
4
- "version": "0.15.2",
4
+ "version": "0.15.4",
5
5
  "description": "ev lists and edits .env files with secrets masked, and ev redact masks secrets in any piped output.",
6
6
  "author": "Kaynooo <contact@kaynooo.fr>",
7
7
  "license": "MIT",
@@ -31,11 +31,11 @@
31
31
  "registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
32
32
  },
33
33
  "scripts": {
34
- "build": "$npm_execpath build src/ev.ts --target bun --outdir dist",
34
+ "build": "$npm_execpath build src/ev.ts --target bun --outdir dist && $npm_execpath ../../scripts/shell-shebang.ts",
35
35
  "ev": "$npm_execpath src/ev.ts"
36
36
  },
37
37
  "dependencies": {
38
- "@kaynooo/cli-kit": "0.46.0"
38
+ "@kaynooo/cli-kit": "0.46.4"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@decimalturn/toml-patch": "3.3.1",
@@ -132,3 +132,45 @@ export function printsTrackedEnv(program: string, args: readonly string[]): bool
132
132
 
133
133
  return !rest.some(arg => GIT_NO_PATCH.has(arg))
134
134
  }
135
+
136
+ // A redirection word: the fd it moves (none for stdout, `&` for both), the operator, and its target when written in the same word.
137
+ const REDIRECT = /^(\d*|&)(>>|>&|>|<)(.*)$/
138
+ // A redirection operator alone, its target being the next word.
139
+ const BARE_REDIRECT = /^(?:\d*|&)(?:>>|>&|>|<)$/
140
+ const FD = /^\d$/
141
+
142
+ // The words less the redirections and their targets, so `printenv > out.txt` names no variable.
143
+ export function withoutRedirects(words: readonly string[]): string[] {
144
+ return words.filter((word, at) => !REDIRECT.test(word) && !BARE_REDIRECT.test(words[at - 1] ?? ''))
145
+ }
146
+
147
+ // Where a redirection sends its fd: an fd copied from the current targets, a closed one (`>&-`) counting as /dev/null.
148
+ function resolveTarget(operator: string, to: string, target: Readonly<Record<string, string>>): string {
149
+ if (operator === '>&' && FD.test(to))
150
+ return target[to] ?? 'other'
151
+
152
+ return to === '-' ? '/dev/null' : to
153
+ }
154
+
155
+ // Whether the words send the piece's stdout to /dev/null (or close it), followed through `2>&1` and `1>&2` in the order bash applies them.
156
+ export function discardsStdout(words: readonly string[]): boolean {
157
+ const target: Record<string, string> = { 1: 'tty', 2: 'tty' }
158
+
159
+ for (let at = 0; at < words.length; at++) {
160
+ const match = REDIRECT.exec(words[at] ?? '')
161
+
162
+ if (match === null || match[2] === '<')
163
+ continue
164
+
165
+ const [, fd = '', operator, written] = match
166
+ const to = written === '' ? (words[++at] ?? '') : written as string
167
+ const resolved = resolveTarget(operator as string, to, target)
168
+
169
+ if (fd === '&' || (operator === '>&' && fd === '' && !FD.test(to)))
170
+ target[1] = target[2] = resolved
171
+ else
172
+ target[fd === '' ? 1 : fd] = resolved
173
+ }
174
+
175
+ return target[1] === '/dev/null'
176
+ }
package/src/guard.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Piece } from '@kaynooo/cli-kit/shell'
2
2
  import { parseCommand, splitCommand, substitutions, tokenize } from '@kaynooo/cli-kit/shell'
3
- import { dumpsContainerEnv, dumpsEnvironment, printsTrackedEnv } from './guard-dumps'
3
+ import { discardsStdout, dumpsContainerEnv, dumpsEnvironment, printsTrackedEnv, withoutRedirects } from './guard-dumps'
4
4
  import { readsEnv, variablesOf } from './guard-names'
5
5
  import { loopReadsListedEnv, runnerReadsListedEnv } from './guard-pipes'
6
6
  import { searchesEnv } from './guard-search'
@@ -27,9 +27,11 @@ export function touchingPieces(command: string, options: { hasTrackedEnv?: boole
27
27
  if (readsEnv(piece, variables) || searchesEnv(program, args) || runnerReadsListedEnv(pieces, index, variables) || loopReadsListedEnv(pieces, index, variables))
28
28
  return true
29
29
 
30
- const isDump = dumpsContainerEnv(program, args) || dumpsEnvironment(program, args, tokenize(piece.command)) || (options.hasTrackedEnv === true && printsTrackedEnv(program, args))
30
+ const operands = withoutRedirects(args)
31
+ const isDump = dumpsContainerEnv(program, operands) || dumpsEnvironment(program, operands, tokenize(piece.command)) || (options.hasTrackedEnv === true && printsTrackedEnv(program, operands))
31
32
 
32
- if (!isDump)
33
+ // a dump whose stdout goes to /dev/null prints nothing, as a parse gate like `docker compose config >/dev/null`
34
+ if (!isDump || discardsStdout(tokenize(piece.command)))
33
35
  return false
34
36
 
35
37
  // an `ev redact` further down its pipeline masks what the command prints
@@ -44,7 +46,7 @@ export function touchingPieces(command: string, options: { hasTrackedEnv?: boole
44
46
  })
45
47
  }
46
48
 
47
- // True when the command would read, write or print an env file or the whole environment; an env file named only inside a quoted message or a heredoc body is text, and a container's environment piped through `ev redact` is masked.
49
+ // True when the command would read, write or print an env file or the whole environment; an env file named only inside a quoted message or a heredoc body is text, a container's environment piped through `ev redact` is masked, and one sent to /dev/null is never printed.
48
50
  export function touchesEnv(command: string, options: { hasTrackedEnv?: boolean } = {}): boolean {
49
51
  return touchingPieces(command, options).length > 0
50
52
  }