@blundergoat/gruff-ts 0.3.1 → 0.3.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.
@@ -1,258 +0,0 @@
1
- #!/usr/bin/env bash
2
- # npm-publish.sh
3
- #
4
- # Purpose:
5
- # Release gruff-ts to npm with local preflight checks and human confirmation.
6
- #
7
- # Usage:
8
- # bash scripts/npm-publish.sh
9
- #
10
- # Behavior:
11
- # 1) reads package.json name/version
12
- # 2) verifies npm authentication
13
- # 3) checks version lockstep and runs the release preflight gate
14
- # 4) prints a dry-run publish summary
15
- # 5) asks for manual confirmation before `npm publish`
16
- #
17
- # Exit:
18
- # 0 if published or explicitly aborted, non-zero on failed auth/preflight/publish.
19
- #
20
- # Requirements:
21
- # - node, npm
22
- # - npm publish authentication:
23
- # npm package publishing requires either account 2FA for the publish
24
- # prompt or a granular access token with Bypass 2FA enabled.
25
- #
26
- # Preferred token setup for this script:
27
- # 1. In npmjs.com, open Account -> Access Tokens.
28
- # 2. Generate a Granular Access Token.
29
- # 3. Grant read/write access to gruff-ts, or to all packages for the
30
- # publishing account.
31
- # 4. Enable Bypass 2FA for write actions.
32
- # 5. Either store it in your npm user config:
33
- # npm config set //registry.npmjs.org/:_authToken=npm_...
34
- # bash scripts/npm-publish.sh
35
- #
36
- # Or keep it out of ~/.npmrc and pass it for this shell only:
37
- # export NPM_TOKEN="npm_..."
38
- # bash scripts/npm-publish.sh
39
- #
40
- # NODE_AUTH_TOKEN is also accepted for the temporary-token path. The
41
- # script writes env tokens to a temporary npm config file and removes it
42
- # on exit. Do not commit an .npmrc containing a real token.
43
- set -euo pipefail
44
-
45
- REGISTRY_URL="https://registry.npmjs.org/"
46
- AUTH_SOURCE=""
47
- TEMP_NPMRC=""
48
- PACKAGE_NAME=""
49
-
50
- usage() {
51
- cat <<'USAGE'
52
- Usage:
53
- bash scripts/npm-publish.sh
54
- bash scripts/npm-publish.sh --help
55
-
56
- Publishes the current package version to npm after:
57
- - npm auth/token validation
58
- - scripts/bump-version.sh --check
59
- - scripts/preflight-checks.sh
60
- - npm publish --dry-run
61
-
62
- The final publish still requires an interactive y/N confirmation.
63
- USAGE
64
- }
65
-
66
- die() {
67
- printf 'npm-publish: %s\n' "$*" >&2
68
- exit 1
69
- }
70
-
71
- repo_root() {
72
- local script_dir
73
- script_dir="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
74
- CDPATH='' cd -- "$script_dir/.." && pwd
75
- }
76
-
77
- read_package_name() {
78
- node -p "require('./package.json').name"
79
- }
80
-
81
- read_package_version() {
82
- node -p "require('./package.json').version"
83
- }
84
-
85
- cleanup() {
86
- if [[ -n "$TEMP_NPMRC" && -f "$TEMP_NPMRC" ]]; then
87
- rm -f -- "$TEMP_NPMRC"
88
- fi
89
- }
90
- trap cleanup EXIT
91
-
92
- print_token_instructions() {
93
- local reason="$1"
94
-
95
- printf 'Error: %s\n' "$reason" >&2
96
- cat >&2 <<EOF
97
-
98
- Publish token setup:
99
- 1. Go to npmjs.com -> Account -> Access Tokens.
100
- 2. Generate a Granular Access Token.
101
- 3. Grant read/write access to ${PACKAGE_NAME}, or to all packages for the
102
- publishing account.
103
- 4. Enable Bypass 2FA for write actions.
104
- 5. Store the token in your npm user config:
105
-
106
- npm config set //registry.npmjs.org/:_authToken=npm_...
107
- bash scripts/npm-publish.sh
108
-
109
- This is simple, but it persists the token in ~/.npmrc.
110
-
111
- To avoid storing the token, pass it for this shell only:
112
-
113
- export NPM_TOKEN="npm_..."
114
- bash scripts/npm-publish.sh
115
-
116
- NODE_AUTH_TOKEN works too:
117
-
118
- export NODE_AUTH_TOKEN="npm_..."
119
- bash scripts/npm-publish.sh
120
-
121
- Do not commit an .npmrc containing a real token.
122
- EOF
123
- }
124
-
125
- configure_token_from_env() {
126
- local token_source=""
127
- local token_value=""
128
-
129
- if [[ -n "${NPM_TOKEN:-}" ]]; then
130
- token_source="NPM_TOKEN"
131
- token_value="$NPM_TOKEN"
132
- elif [[ -n "${NODE_AUTH_TOKEN:-}" ]]; then
133
- token_source="NODE_AUTH_TOKEN"
134
- token_value="$NODE_AUTH_TOKEN"
135
- else
136
- return 1
137
- fi
138
-
139
- TEMP_NPMRC=$(mktemp)
140
- chmod 0600 "$TEMP_NPMRC"
141
- {
142
- printf 'registry=%s\n' "$REGISTRY_URL"
143
- printf '//registry.npmjs.org/:_authToken=%s\n' "$token_value"
144
- } >"$TEMP_NPMRC"
145
-
146
- export NPM_CONFIG_USERCONFIG="$TEMP_NPMRC"
147
- AUTH_SOURCE="$token_source"
148
- }
149
-
150
- has_configured_registry_token() {
151
- npm config list 2>/dev/null | grep -Eq '^//registry\.npmjs\.org/:_authToken = \(protected\)$'
152
- }
153
-
154
- trim_output() {
155
- tr -d '\r' | awk '{$1=$1; print}'
156
- }
157
-
158
- verify_publish_auth() {
159
- local npm_user
160
- local tfa_mode
161
-
162
- echo "--- Auth check ---"
163
- if configure_token_from_env; then
164
- echo "Using ${AUTH_SOURCE} via temporary npm config."
165
- fi
166
-
167
- if ! npm_user=$(npm whoami --registry="$REGISTRY_URL" 2>/dev/null); then
168
- print_token_instructions "npm is not authenticated for ${REGISTRY_URL}."
169
- exit 1
170
- fi
171
-
172
- echo "Logged in as: ${npm_user}"
173
-
174
- if [[ -n "$AUTH_SOURCE" ]]; then
175
- echo "Publish token source: ${AUTH_SOURCE}"
176
- echo "Token must be granular, read/write for ${PACKAGE_NAME}, and Bypass 2FA enabled."
177
- echo ""
178
- return 0
179
- fi
180
-
181
- if has_configured_registry_token; then
182
- AUTH_SOURCE="npm user config"
183
- echo "Publish token source: ${AUTH_SOURCE}"
184
- echo "Token must be granular, read/write for ${PACKAGE_NAME}, and Bypass 2FA enabled."
185
- echo ""
186
- return 0
187
- fi
188
-
189
- if ! tfa_mode=$(npm profile get "two-factor auth" --registry="$REGISTRY_URL" 2>/dev/null | trim_output); then
190
- print_token_instructions "unable to verify npm 2FA state and no publish token was supplied."
191
- exit 1
192
- fi
193
-
194
- if [[ -z "$tfa_mode" ]]; then
195
- print_token_instructions "npm did not report an account 2FA mode and no publish token was supplied."
196
- exit 1
197
- fi
198
-
199
- echo "Account 2FA mode: ${tfa_mode}"
200
- if [[ "$tfa_mode" == "disabled" ]]; then
201
- print_token_instructions "npm account 2FA is disabled and no publish token was supplied."
202
- exit 1
203
- fi
204
-
205
- echo "No explicit publish token supplied; npm must complete the publish with an interactive OTP."
206
- echo ""
207
- }
208
-
209
- main() {
210
- if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
211
- usage
212
- exit 0
213
- fi
214
-
215
- if [[ "$#" -ne 0 ]]; then
216
- usage >&2
217
- die "unknown argument: $1"
218
- fi
219
-
220
- cd "$(repo_root)"
221
- [[ -f package.json ]] || die "package.json not found"
222
-
223
- PACKAGE_NAME="$(read_package_name)" || die "failed to read package.json name"
224
- local version
225
- version="$(read_package_version)" || die "failed to read package.json version"
226
- [[ -n "$PACKAGE_NAME" ]] || die "package.json has no name field"
227
- [[ -n "$version" ]] || die "package.json has no version field"
228
-
229
- local publish_args=("--registry=$REGISTRY_URL")
230
- if [[ "$PACKAGE_NAME" == @*/* ]]; then
231
- publish_args+=(--access public)
232
- fi
233
-
234
- echo "Publishing ${PACKAGE_NAME}@${version}"
235
- verify_publish_auth
236
-
237
- echo "--- Preflight ---"
238
- bash scripts/bump-version.sh --check
239
- bash scripts/preflight-checks.sh
240
- echo ""
241
-
242
- echo "--- Dry run ---"
243
- npm publish --dry-run "${publish_args[@]}"
244
- echo ""
245
-
246
- local confirm
247
- read -rp "Publish ${PACKAGE_NAME}@${version} to npm? (y/N) " confirm
248
- if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
249
- echo "Aborted."
250
- exit 0
251
- fi
252
-
253
- npm publish "${publish_args[@]}"
254
- echo ""
255
- echo "Published: https://www.npmjs.com/package/${PACKAGE_NAME}/v/${version}"
256
- }
257
-
258
- main "$@"
@@ -1,395 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -uo pipefail
3
-
4
- SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
5
- REPO_ROOT="$(CDPATH='' cd -- "$SCRIPT_DIR/.." && pwd)"
6
-
7
- if [[ -t 1 && -z "${NO_COLOR:-}" ]]; then
8
- BOLD=$'\033[1m'
9
- DIM=$'\033[2m'
10
- GREEN=$'\033[32m'
11
- RED=$'\033[31m'
12
- YELLOW=$'\033[33m'
13
- BLUE=$'\033[34m'
14
- RESET=$'\033[0m'
15
- else
16
- BOLD=''
17
- DIM=''
18
- GREEN=''
19
- RED=''
20
- YELLOW=''
21
- BLUE=''
22
- RESET=''
23
- fi
24
-
25
- PASS="${GREEN}OK${RESET}"
26
- FAIL="${RED}FAIL${RESET}"
27
- SKIP="${YELLOW}SKIP${RESET}"
28
- ARROW="${BLUE}->${RESET}"
29
-
30
- TOTAL=0
31
- PASSED=0
32
- FAILED=0
33
- FAILURES=()
34
- TMP_FILES=()
35
- START_TIME=$(date +%s%N)
36
- NPM_AUDIT_LEVEL="${NPM_AUDIT_LEVEL:-moderate}"
37
-
38
- usage() {
39
- cat <<'USAGE'
40
- Usage:
41
- scripts/preflight-checks.sh
42
-
43
- Runs the local preflight gate:
44
- - version surfaces (package.json, package-lock.json, src/constants.ts, CHANGELOG.md) agree
45
- - npm dependency audit
46
- - npm run check (TypeScript compile plus unit tests)
47
- - gruff-ts full-project scan
48
- - shellcheck for scripts/*.sh when shellcheck is installed
49
-
50
- Environment:
51
- GRUFF_TS_FAIL_ON gruff-ts severity that fails static analysis (default: advisory)
52
- NPM_AUDIT_LEVEL npm audit threshold (default: moderate)
53
- USAGE
54
- }
55
-
56
- cleanup() {
57
- local temp_file
58
-
59
- for temp_file in "${TMP_FILES[@]}"; do
60
- [[ -f "$temp_file" ]] && rm -f -- "$temp_file"
61
- done
62
- }
63
-
64
- trap cleanup EXIT
65
-
66
- rule() {
67
- printf ' %s\n' "${DIM}--------------------------------------------${RESET}"
68
- }
69
-
70
- elapsed_since() {
71
- local started_at="$1"
72
- local finished_at
73
- local elapsed_ms
74
- local seconds
75
- local minutes
76
- local remainder
77
- local frac
78
-
79
- finished_at=$(date +%s%N)
80
- elapsed_ms=$(((finished_at - started_at) / 1000000))
81
-
82
- if ((elapsed_ms < 1000)); then
83
- printf '%dms' "$elapsed_ms"
84
- return
85
- fi
86
-
87
- seconds=$((elapsed_ms / 1000))
88
- frac=$(((elapsed_ms % 1000) / 100))
89
-
90
- if ((seconds < 60)); then
91
- printf '%d.%ds' "$seconds" "$frac"
92
- return
93
- fi
94
-
95
- minutes=$((seconds / 60))
96
- remainder=$((seconds % 60))
97
- printf '%dm %02d.%ds' "$minutes" "$remainder" "$frac"
98
- }
99
-
100
- header() {
101
- printf '\n'
102
- printf ' %sPreflight Check%s\n' "$BOLD" "$RESET"
103
- printf ' %s%s%s\n' "$DIM" "$(date '+%Y-%m-%d %H:%M:%S')" "$RESET"
104
- rule
105
- printf '\n'
106
- }
107
-
108
- step() {
109
- local label="$1"
110
-
111
- TOTAL=$((TOTAL + 1))
112
- printf ' %s %-36s' "$ARROW" "$label"
113
- }
114
-
115
- pass() {
116
- local detail="${1:-}"
117
-
118
- PASSED=$((PASSED + 1))
119
- if [[ -n "$detail" ]]; then
120
- printf '%s %s%s%s\n' "$PASS" "$DIM" "$detail" "$RESET"
121
- else
122
- printf '%s\n' "$PASS"
123
- fi
124
- }
125
-
126
- fail() {
127
- local label="$1"
128
-
129
- FAILED=$((FAILED + 1))
130
- FAILURES+=("$label")
131
- printf '%s\n' "$FAIL"
132
- }
133
-
134
- skip() {
135
- local reason="${1:-skipped}"
136
-
137
- printf '%s %s%s%s\n' "$SKIP" "$DIM" "$reason" "$RESET"
138
- }
139
-
140
- indent_output() {
141
- while IFS= read -r line; do
142
- printf ' %s%s%s\n' "$DIM" "$line" "$RESET"
143
- done
144
- }
145
-
146
- run_step() {
147
- local label="$1"
148
- shift
149
-
150
- local started_at
151
- local output
152
- local status
153
- local elapsed
154
-
155
- step "$label"
156
- started_at=$(date +%s%N)
157
- output=$("$@" 2>&1)
158
- status=$?
159
- elapsed=$(elapsed_since "$started_at")
160
-
161
- if ((status == 0)); then
162
- pass "${output:+$output }$elapsed"
163
- else
164
- fail "$label"
165
- if [[ -n "$output" ]]; then
166
- printf '%s\n' "$output" | tail -20 | indent_output
167
- fi
168
- printf ' %sexit %d after %s%s\n' "$DIM" "$status" "$elapsed" "$RESET"
169
- fi
170
-
171
- return "$status"
172
- }
173
-
174
- make_temp_file() {
175
- local suffix="$1"
176
- local temp_file
177
-
178
- temp_file=$(mktemp "${TMPDIR:-/tmp}/gruff-ts-preflight.XXXXXX.$suffix") || return 1
179
- TMP_FILES+=("$temp_file")
180
- printf '%s\n' "$temp_file"
181
- }
182
-
183
- version_consistency_check() {
184
- local output
185
- local status
186
-
187
- output=$(bash scripts/bump-version.sh --check 2>&1)
188
- status=$?
189
- if ((status != 0)); then
190
- printf '%s\n' "$output"
191
- return "$status"
192
- fi
193
- printf '%s' "$output"
194
- }
195
-
196
- npm_audit_check() {
197
- local output
198
- local status
199
- local summary
200
-
201
- output=$(npm audit --audit-level="$NPM_AUDIT_LEVEL" 2>&1)
202
- status=$?
203
- if ((status != 0)); then
204
- printf '%s\n' "$output"
205
- return "$status"
206
- fi
207
-
208
- summary=$(printf '%s\n' "$output" | awk '/found .* vulnerabilities|audited .* packages/ { line = $0 } END { print line }')
209
- printf '%s' "${summary:-completed}"
210
- return 0
211
- }
212
-
213
- npm_check() {
214
- local output
215
- local status
216
- local tests
217
- local passed
218
- local failed
219
-
220
- output=$(npm run check 2>&1)
221
- status=$?
222
-
223
- if ((status != 0)); then
224
- printf '%s\n' "$output"
225
- return "$status"
226
- fi
227
-
228
- tests=$(printf '%s\n' "$output" | awk '/^# tests / { print $3; exit }')
229
- passed=$(printf '%s\n' "$output" | awk '/^# pass / { print $3; exit }')
230
- failed=$(printf '%s\n' "$output" | awk '/^# fail / { print $3; exit }')
231
-
232
- if [[ -n "$tests" && -n "$passed" ]]; then
233
- printf '%s/%s tests passed' "$passed" "$tests"
234
- if [[ -n "$failed" && "$failed" != "0" ]]; then
235
- printf ', %s failed' "$failed"
236
- fi
237
- else
238
- printf 'completed'
239
- fi
240
-
241
- return 0
242
- }
243
-
244
- gruff_report_summary() {
245
- local report_path="$1"
246
-
247
- # shellcheck disable=SC2016
248
- node --input-type=module -e '
249
- import { readFileSync } from "node:fs";
250
-
251
- const report = JSON.parse(readFileSync(process.argv[1], "utf8"));
252
- const summary = report.summary ?? {};
253
- const score = report.score ?? {};
254
- const paths = report.paths ?? {};
255
- const total = Number(summary.total ?? 0);
256
- const advisory = Number(summary.advisory ?? 0);
257
- const warning = Number(summary.warning ?? 0);
258
- const error = Number(summary.error ?? 0);
259
- const grade = String(score.grade ?? "n/a");
260
- const composite = Number(score.composite ?? 0).toFixed(1);
261
- const analysedFiles = Number(paths.analysedFiles ?? 0);
262
-
263
- console.log(`${total} findings (advisory=${advisory}, warning=${warning}, error=${error}), ${grade} ${composite}/100, ${analysedFiles} files`);
264
- ' "$report_path"
265
- }
266
-
267
- gruff_ts_check() {
268
- local gruff_fail_on="${GRUFF_TS_FAIL_ON:-advisory}"
269
- local report_path
270
- local error_path
271
- local status
272
- local summary_status=0
273
- local printed=0
274
-
275
- report_path=$(make_temp_file json) || return 1
276
- error_path=$(make_temp_file err) || return 1
277
-
278
- ./bin/gruff-ts analyse . --format=json --fail-on="$gruff_fail_on" --no-baseline >"$report_path" 2>"$error_path"
279
- status=$?
280
-
281
- if [[ -s "$report_path" ]]; then
282
- gruff_report_summary "$report_path"
283
- summary_status=$?
284
- printed=1
285
- fi
286
-
287
- if [[ -s "$error_path" ]]; then
288
- if ((printed)); then
289
- printf '\n'
290
- fi
291
- cat "$error_path"
292
- fi
293
-
294
- if ((status != 0)); then
295
- return "$status"
296
- fi
297
- return "$summary_status"
298
- }
299
-
300
- shellcheck_check() {
301
- local scripts=()
302
- local script_path
303
- local output
304
- local status
305
-
306
- while IFS= read -r -d '' script_path; do
307
- scripts+=("$script_path")
308
- done < <(find scripts -maxdepth 1 -type f -name '*.sh' -print0 | sort -z)
309
-
310
- if [[ "${#scripts[@]}" -eq 0 ]]; then
311
- printf 'no scripts/*.sh files found'
312
- return 0
313
- fi
314
-
315
- output=$(shellcheck "${scripts[@]}" 2>&1)
316
- status=$?
317
-
318
- if ((status == 0)); then
319
- printf '%d scripts checked' "${#scripts[@]}"
320
- else
321
- printf '%s\n' "$output"
322
- fi
323
-
324
- return "$status"
325
- }
326
-
327
- summary() {
328
- local elapsed
329
-
330
- elapsed=$(elapsed_since "$START_TIME")
331
- printf '\n'
332
- rule
333
- printf '\n'
334
-
335
- if ((FAILED == 0)); then
336
- printf ' %sAll %d/%d checks passed%s %s(%s)%s\n' "$GREEN$BOLD" "$PASSED" "$TOTAL" "$RESET" "$DIM" "$elapsed" "$RESET"
337
- printf '\n'
338
- return 0
339
- fi
340
-
341
- printf ' %s%d/%d checks failed%s %s(%s)%s\n' "$RED$BOLD" "$FAILED" "$TOTAL" "$RESET" "$DIM" "$elapsed" "$RESET"
342
- printf '\n'
343
-
344
- local failure
345
- for failure in "${FAILURES[@]}"; do
346
- printf ' %s %s\n' "$FAIL" "$failure"
347
- done
348
- printf '\n'
349
-
350
- return 1
351
- }
352
-
353
- main() {
354
- if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
355
- usage
356
- return 0
357
- fi
358
-
359
- if [[ "$#" -ne 0 ]]; then
360
- printf '%sUnknown argument:%s %s\n' "$RED" "$RESET" "$1" >&2
361
- usage >&2
362
- return 64
363
- fi
364
-
365
- cd "$REPO_ROOT" || return 1
366
-
367
- header
368
-
369
- if [[ ! -x ./bin/gruff-ts ]]; then
370
- step "gruff-ts binary"
371
- fail "gruff-ts binary"
372
- printf ' %s./bin/gruff-ts is missing or not executable%s\n' "$DIM" "$RESET"
373
- summary
374
- return 127
375
- fi
376
-
377
- run_step "Version consistency" version_consistency_check
378
-
379
- run_step "Dependency audit" npm_audit_check
380
-
381
- run_step "TypeScript + tests" npm_check
382
-
383
- run_step "Gruff full-project scan" gruff_ts_check
384
-
385
- if command -v shellcheck >/dev/null 2>&1; then
386
- run_step "Shell scripts (shellcheck)" shellcheck_check
387
- else
388
- step "Shell scripts (shellcheck)"
389
- skip "shellcheck not found"
390
- fi
391
-
392
- summary
393
- }
394
-
395
- main "$@"
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
-
4
- HOST="${GRUFF_HOST:-127.0.0.1}"
5
- PORT="${GRUFF_PORT:-8767}"
6
- PROJECT_ROOT="${GRUFF_PROJECT_ROOT:-$(pwd)}"
7
-
8
- npm run start-dev -- --host "$HOST" --port "$PORT" --project-root "$PROJECT_ROOT"