passflow 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +67 -2
- package/dist/cli.mjs +1759 -0
- package/package.json +42 -3
- package/skills/REVISION +1 -0
- package/skills/index.json +405 -0
- package/skills/passflow-nextjs/SKILL.md +205 -0
- package/skills/passflow-nextjs/assets/.env.example +5 -0
- package/skills/passflow-nextjs/assets/app/account/page.tsx +29 -0
- package/skills/passflow-nextjs/assets/app/actions.example.ts +21 -0
- package/skills/passflow-nextjs/assets/app/api/me/route.ts +22 -0
- package/skills/passflow-nextjs/assets/components/auth-links.tsx +11 -0
- package/skills/passflow-nextjs/assets/lib/call-api.example.ts +15 -0
- package/skills/passflow-nextjs/assets/lib/passflow.ts +4 -0
- package/skills/passflow-nextjs/assets/proxy.ts +10 -0
- package/skills/passflow-nextjs/references/guardrails.md +87 -0
- package/skills/passflow-nextjs/scripts/index.json +405 -0
- package/skills/passflow-nextjs/scripts/lib.sh +1069 -0
- package/skills/passflow-nextjs/scripts/verify.sh +467 -0
- package/skills/passflow-node-api/SKILL.md +189 -0
- package/skills/passflow-node-api/assets/examples/explain-rejection.ts +22 -0
- package/skills/passflow-node-api/assets/examples/express-routes.ts +22 -0
- package/skills/passflow-node-api/assets/examples/fastify-routes.ts +28 -0
- package/skills/passflow-node-api/assets/examples/hono-routes.ts +19 -0
- package/skills/passflow-node-api/assets/src/express-cors.ts +11 -0
- package/skills/passflow-node-api/assets/src/express.ts +13 -0
- package/skills/passflow-node-api/assets/src/fastify-cors.ts +11 -0
- package/skills/passflow-node-api/assets/src/fastify.ts +14 -0
- package/skills/passflow-node-api/assets/src/fetch-handler.ts +13 -0
- package/skills/passflow-node-api/assets/src/hono-cors.ts +11 -0
- package/skills/passflow-node-api/assets/src/hono.ts +16 -0
- package/skills/passflow-node-api/assets/src/passflow.ts +21 -0
- package/skills/passflow-node-api/references/guardrails.md +66 -0
- package/skills/passflow-node-api/scripts/index.json +405 -0
- package/skills/passflow-node-api/scripts/lib.sh +1069 -0
- package/skills/passflow-node-api/scripts/verify.sh +283 -0
- package/skills/passflow-react-vite/SKILL.md +225 -0
- package/skills/passflow-react-vite/assets/server/express-cors.ts +9 -0
- package/skills/passflow-react-vite/assets/server/express.ts +14 -0
- package/skills/passflow-react-vite/assets/server/fastify-cors.ts +11 -0
- package/skills/passflow-react-vite/assets/server/fastify.ts +15 -0
- package/skills/passflow-react-vite/assets/server/hono-cors.ts +8 -0
- package/skills/passflow-react-vite/assets/server/hono.ts +17 -0
- package/skills/passflow-react-vite/assets/server/passflow.ts +30 -0
- package/skills/passflow-react-vite/assets/server/tsconfig.json +14 -0
- package/skills/passflow-react-vite/assets/src/passflow.tsx +28 -0
- package/skills/passflow-react-vite/assets/src/require-sign-in.tsx +35 -0
- package/skills/passflow-react-vite/assets/src/use-api.ts +23 -0
- package/skills/passflow-react-vite/references/guardrails.md +82 -0
- package/skills/passflow-react-vite/scripts/index.json +405 -0
- package/skills/passflow-react-vite/scripts/lib.sh +1069 -0
- package/skills/passflow-react-vite/scripts/verify.sh +678 -0
- package/skills/passflow-tanstack-start/SKILL.md +182 -0
- package/skills/passflow-tanstack-start/assets/.env.example +5 -0
- package/skills/passflow-tanstack-start/assets/src/components/auth-links.tsx +10 -0
- package/skills/passflow-tanstack-start/assets/src/lib/call-api.example.ts +14 -0
- package/skills/passflow-tanstack-start/assets/src/lib/passflow.ts +14 -0
- package/skills/passflow-tanstack-start/assets/src/lib/server-functions.example.ts +20 -0
- package/skills/passflow-tanstack-start/assets/src/routes/account.tsx +25 -0
- package/skills/passflow-tanstack-start/assets/src/routes/api/me.ts +25 -0
- package/skills/passflow-tanstack-start/assets/src/start.ts +14 -0
- package/skills/passflow-tanstack-start/references/guardrails.md +73 -0
- package/skills/passflow-tanstack-start/scripts/index.json +405 -0
- package/skills/passflow-tanstack-start/scripts/lib.sh +1069 -0
- package/skills/passflow-tanstack-start/scripts/verify.sh +470 -0
|
@@ -0,0 +1,1069 @@
|
|
|
1
|
+
# shellcheck shell=bash
|
|
2
|
+
# Shared helpers for the Passflow skills' verify.sh scripts.
|
|
3
|
+
#
|
|
4
|
+
# Source it from a script that runs under `set -euo pipefail`:
|
|
5
|
+
# . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib.sh"
|
|
6
|
+
#
|
|
7
|
+
# Works with bash 3.2 (macOS) and later, BSD and GNU userlands.
|
|
8
|
+
# No function here prints the value of an environment variable or a token.
|
|
9
|
+
#
|
|
10
|
+
# Return codes: 0 ok, 1 a check failed, 2 refused or usage error.
|
|
11
|
+
#
|
|
12
|
+
# Functions
|
|
13
|
+
# Output: pf_pass CHECK | pf_fail CHECK REASON | pf_skip CHECK REASON | pf_info TEXT
|
|
14
|
+
# pf_result -> prints "RESULT pass|fail", returns 0|1
|
|
15
|
+
# Tools: pf_require_tool NAME... (2 when one is missing)
|
|
16
|
+
# pf_node_at_least MAJOR.MINOR (0 when node is that version or newer)
|
|
17
|
+
# JSON: pf_json_get FILE KEY... (string, number or boolean; arrays one item per line;
|
|
18
|
+
# jq when present, else node; PF_NO_JQ=1 forces node)
|
|
19
|
+
# package.json: pf_pkg_field DIR KEY...
|
|
20
|
+
# pf_has_dep DIR NAME (dependencies or devDependencies)
|
|
21
|
+
# pf_dep_range DIR NAME (the range package.json gives NAME)
|
|
22
|
+
# pf_has_script DIR NAME
|
|
23
|
+
# Ranges: pf_range_meets SPEC '>=X.Y' (0 when every version SPEC admits is X.Y or newer;
|
|
24
|
+
# 1 for a SPEC that cannot be compared)
|
|
25
|
+
# Stacks: pf_index_file (scripts/index.json beside this file)
|
|
26
|
+
# pf_stack_get INDEX STACK_ID KEY... (a stack's field from index.json)
|
|
27
|
+
# pf_detect_stack DIR INDEX (stack id, "refused:<stack id>" or "unsupported")
|
|
28
|
+
# pf_stack_refusal DIR INDEX STACK_ID (the message for "refused:<stack id>")
|
|
29
|
+
# pf_stack_version_message STACK_ID ("Next.js 16 or newer", "TanStack Start 1.169.5 or newer with ...")
|
|
30
|
+
# Packages: pf_detect_pm DIR [INDEX] (npm | pnpm | yarn-classic | yarn-berry | bun | shrinkwrap
|
|
31
|
+
# | mismatch:<declared>:<lockfile's> | none)
|
|
32
|
+
# pf_pm_supported PM | pf_pm_refused PM
|
|
33
|
+
# pf_pm_refusal PM (the message for any result that is not a supported PM)
|
|
34
|
+
# pf_pm_tool PM (the command: npm, pnpm or yarn)
|
|
35
|
+
# pf_package_spec NAME VERSION_ENV (NAME, or NAME@version when VERSION_ENV is set)
|
|
36
|
+
# pf_install_line PM NAME VERSION_ENV (the install line to show, with the variable unexpanded)
|
|
37
|
+
# pf_install DIR [PM] (install with lifecycle scripts off)
|
|
38
|
+
# pf_run_script DIR NAME [PM]
|
|
39
|
+
# pf_exec DIR COMMAND [ARGS...] (a binary from the project's node_modules)
|
|
40
|
+
# pf_typecheck DIR [PM] (0 pass, 1 fail, 3 nothing to check)
|
|
41
|
+
# Env file: pf_env_missing_keys FILE KEY... (prints missing names)
|
|
42
|
+
# pf_load_env FILE KEY... (exports those keys only; never evaluates a value)
|
|
43
|
+
# pf_git_tracked DIR FILE
|
|
44
|
+
# Scans: pf_scan_bundle BUNDLE_DIR ENV_FILE NAME... (names and values; 0 clean, 1 found, 3 no such dir)
|
|
45
|
+
# pf_scan_values DIR ENV_FILE 'GLOB...' NAME... (values only, in files matching a GLOB)
|
|
46
|
+
# pf_guardrail_scan DIR KIND (KIND: server | spa | api; 0 clean, 1 found)
|
|
47
|
+
# HTTP: pf_http OUT_DIR METHOD URL [CURL_ARGS...] (writes status, headers, body to OUT_DIR)
|
|
48
|
+
# pf_header OUT_DIR NAME (first value of a response header)
|
|
49
|
+
# pf_wait_http URL SECONDS [PID] (1 at once when PID has exited)
|
|
50
|
+
# pf_port_free PORT | pf_port_busy_refusal PORT
|
|
51
|
+
# pf_has_token LIST TOKEN (a whole item of a comma-separated header value)
|
|
52
|
+
# pf_listener_owned PORT PID (0 yes, 1 another process, 3 cannot tell: no lsof)
|
|
53
|
+
# pf_check_listener PORT PID (prints PASS, FAIL or SKIP for pf_listener_owned)
|
|
54
|
+
# All curl calls use --noproxy '*': the checks talk to this machine only.
|
|
55
|
+
# Processes: pf_start_bg DIR LOG_FILE COMMAND [ARGS...] (sets PF_BG_PID)
|
|
56
|
+
# pf_alive PID
|
|
57
|
+
# pf_kill_tree PID (TERM, then KILL after 5 s; pgrep, ps or /proc)
|
|
58
|
+
# pf_traps CLEANUP_FUNCTION (EXIT, and INT/TERM that clean up and exit)
|
|
59
|
+
|
|
60
|
+
PF_FAILED=0
|
|
61
|
+
PF_BG_PID=''
|
|
62
|
+
PF_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
63
|
+
# Directory for temporary files, without a trailing slash.
|
|
64
|
+
PF_TMP="${TMPDIR:-/tmp}"
|
|
65
|
+
PF_TMP="${PF_TMP%/}"
|
|
66
|
+
|
|
67
|
+
# ---------------------------------------------------------------- output
|
|
68
|
+
|
|
69
|
+
pf_pass() { printf 'PASS %s\n' "$1"; }
|
|
70
|
+
|
|
71
|
+
pf_fail() {
|
|
72
|
+
PF_FAILED=1
|
|
73
|
+
printf 'FAIL %s: %s\n' "$1" "$2"
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
pf_skip() { printf 'SKIP %s: %s\n' "$1" "$2"; }
|
|
77
|
+
|
|
78
|
+
pf_info() { printf ' %s\n' "$*"; }
|
|
79
|
+
|
|
80
|
+
pf_result() {
|
|
81
|
+
if [ "$PF_FAILED" -eq 0 ]; then
|
|
82
|
+
printf 'RESULT pass\n'
|
|
83
|
+
return 0
|
|
84
|
+
fi
|
|
85
|
+
printf 'RESULT fail\n'
|
|
86
|
+
return 1
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
# ---------------------------------------------------------------- tools
|
|
90
|
+
|
|
91
|
+
pf_require_tool() {
|
|
92
|
+
local tool
|
|
93
|
+
for tool in "$@"; do
|
|
94
|
+
if ! command -v "$tool" >/dev/null 2>&1; then
|
|
95
|
+
printf 'REFUSED: %s is required and was not found on PATH\n' "$tool" >&2
|
|
96
|
+
return 2
|
|
97
|
+
fi
|
|
98
|
+
done
|
|
99
|
+
return 0
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
# pf_node_at_least 22.12
|
|
103
|
+
pf_node_at_least() {
|
|
104
|
+
local want="$1" have want_major want_minor have_major have_minor
|
|
105
|
+
have="$(node -p 'process.versions.node' 2>/dev/null)" || return 1
|
|
106
|
+
want_major="${want%%.*}"
|
|
107
|
+
want_minor="${want#*.}"
|
|
108
|
+
want_minor="${want_minor%%.*}"
|
|
109
|
+
have_major="${have%%.*}"
|
|
110
|
+
have_minor="${have#*.}"
|
|
111
|
+
have_minor="${have_minor%%.*}"
|
|
112
|
+
if [ "$have_major" -gt "$want_major" ]; then return 0; fi
|
|
113
|
+
if [ "$have_major" -eq "$want_major" ] && [ "$have_minor" -ge "$want_minor" ]; then return 0; fi
|
|
114
|
+
return 1
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
# ---------------------------------------------------------------- JSON
|
|
118
|
+
|
|
119
|
+
# jq when it is on PATH, unless PF_NO_JQ=1 (which forces the node path).
|
|
120
|
+
_pf_use_jq() {
|
|
121
|
+
[ "${PF_NO_JQ:-}" != 1 ] && command -v jq >/dev/null 2>&1
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# pf_json_get FILE KEY...: prints the value at the key path. Strings, numbers and booleans print as
|
|
125
|
+
# they are; an array prints one scalar item per line; objects, null and a missing path print nothing.
|
|
126
|
+
# Uses jq when present, else node.
|
|
127
|
+
pf_json_get() {
|
|
128
|
+
local file="$1"
|
|
129
|
+
shift
|
|
130
|
+
[ -f "$file" ] || return 0
|
|
131
|
+
if _pf_use_jq; then
|
|
132
|
+
jq -r '$ARGS.positional as $p
|
|
133
|
+
| (try getpath($p) catch null)
|
|
134
|
+
| if type == "array" then .[] | select(type == "string" or type == "number" or type == "boolean") | tostring
|
|
135
|
+
elif type == "string" or type == "number" or type == "boolean" then tostring
|
|
136
|
+
else empty end' "$file" --args "$@" 2>/dev/null || true
|
|
137
|
+
return 0
|
|
138
|
+
fi
|
|
139
|
+
node -e '
|
|
140
|
+
const fs = require("fs");
|
|
141
|
+
const [file, ...path] = process.argv.slice(1);
|
|
142
|
+
let v;
|
|
143
|
+
try { v = JSON.parse(fs.readFileSync(file, "utf8")); } catch { process.exit(0); }
|
|
144
|
+
for (const k of path) {
|
|
145
|
+
if (v === null || typeof v !== "object" || !Object.prototype.hasOwnProperty.call(v, k)) process.exit(0);
|
|
146
|
+
v = v[k];
|
|
147
|
+
}
|
|
148
|
+
const scalar = (x) => ["string", "number", "boolean"].includes(typeof x);
|
|
149
|
+
if (Array.isArray(v)) { for (const x of v) if (scalar(x)) console.log(String(x)); }
|
|
150
|
+
else if (scalar(v)) console.log(String(v));
|
|
151
|
+
' "$file" "$@" 2>/dev/null || true
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
# ---------------------------------------------------------------- package.json
|
|
155
|
+
|
|
156
|
+
pf_pkg_field() {
|
|
157
|
+
local dir="$1"
|
|
158
|
+
shift
|
|
159
|
+
pf_json_get "$dir/package.json" "$@"
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
# _pf_json_has FILE KEY...: 0 when the key path exists in FILE with a value other than null, an empty
|
|
163
|
+
# string included.
|
|
164
|
+
_pf_json_has() {
|
|
165
|
+
local file="$1"
|
|
166
|
+
shift
|
|
167
|
+
[ -f "$file" ] || return 1
|
|
168
|
+
if _pf_use_jq; then
|
|
169
|
+
jq -e '$ARGS.positional as $p | (try getpath($p) catch null) != null' "$file" --args "$@" >/dev/null 2>&1
|
|
170
|
+
return $?
|
|
171
|
+
fi
|
|
172
|
+
node -e '
|
|
173
|
+
const fs = require("fs");
|
|
174
|
+
const [file, ...path] = process.argv.slice(1);
|
|
175
|
+
let v;
|
|
176
|
+
try { v = JSON.parse(fs.readFileSync(file, "utf8")); } catch { process.exit(1); }
|
|
177
|
+
for (const k of path) {
|
|
178
|
+
if (v === null || typeof v !== "object" || !Object.prototype.hasOwnProperty.call(v, k)) process.exit(1);
|
|
179
|
+
v = v[k];
|
|
180
|
+
}
|
|
181
|
+
process.exit(v === null || v === undefined ? 1 : 0);
|
|
182
|
+
' "$file" "$@" 2>/dev/null
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
# pf_has_dep DIR NAME: NAME is a key of dependencies or devDependencies, whatever its version spec (an empty
|
|
186
|
+
# spec included: the package is present, and a range rule then refuses it).
|
|
187
|
+
pf_has_dep() {
|
|
188
|
+
local dir="$1" name="$2" field
|
|
189
|
+
for field in dependencies devDependencies; do
|
|
190
|
+
if _pf_json_has "$dir/package.json" "$field" "$name"; then
|
|
191
|
+
return 0
|
|
192
|
+
fi
|
|
193
|
+
done
|
|
194
|
+
return 1
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
# pf_dep_range DIR NAME: the range package.json gives NAME, from dependencies, else devDependencies.
|
|
198
|
+
pf_dep_range() {
|
|
199
|
+
local dir="$1" name="$2" field range
|
|
200
|
+
for field in dependencies devDependencies; do
|
|
201
|
+
range="$(pf_pkg_field "$dir" "$field" "$name")"
|
|
202
|
+
if [ -n "$range" ]; then
|
|
203
|
+
printf '%s\n' "$range"
|
|
204
|
+
return 0
|
|
205
|
+
fi
|
|
206
|
+
done
|
|
207
|
+
return 0
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
pf_has_script() {
|
|
211
|
+
[ -n "$(pf_pkg_field "$1" scripts "$2")" ]
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
# ---------------------------------------------------------------- version ranges
|
|
215
|
+
|
|
216
|
+
# _pf_version_floor SPEC: the lowest version a simple range admits, as "MAJOR MINOR PATCH PRE"
|
|
217
|
+
# (PRE is 1 for a pre-release such as 16.0.0-rc.1, else 0).
|
|
218
|
+
# Understood: 1.2.3, v1.2.3, =1.2.3, ^1.2.3, ~1.2.3, >=1.2.3, and 1, 1.2, 1.x, 1.2.x, 1.2.*.
|
|
219
|
+
# Prints nothing for anything else ("*", "latest", "workspace:^", "npm:…", a git or file URL, "a || b",
|
|
220
|
+
# "a - b", "<2", ">1", ">=1 <2"): such a range cannot be compared, so a range rule never matches it.
|
|
221
|
+
_pf_version_floor() {
|
|
222
|
+
local spec="$1" re major minor patch pre=0
|
|
223
|
+
spec="$(printf '%s' "$spec" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')"
|
|
224
|
+
re='^(\^|~|>=|=)?[[:space:]]*v?([0-9]+)(\.([0-9]+|[xX*]))?(\.([0-9]+|[xX*]))?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$'
|
|
225
|
+
[[ "$spec" =~ $re ]] || return 0
|
|
226
|
+
major="${BASH_REMATCH[2]}"
|
|
227
|
+
minor="${BASH_REMATCH[4]}"
|
|
228
|
+
patch="${BASH_REMATCH[6]}"
|
|
229
|
+
case "$minor" in '' | x | X | '*') minor=0; patch=0 ;; esac
|
|
230
|
+
case "$patch" in '' | x | X | '*') patch=0 ;; esac
|
|
231
|
+
[ -z "${BASH_REMATCH[7]}" ] || pre=1
|
|
232
|
+
printf '%s %s %s %s\n' "$((10#$major))" "$((10#$minor))" "$((10#$patch))" "$pre"
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
# pf_range_meets SPEC REQUIRED: 0 when every version SPEC admits is at least REQUIRED's floor.
|
|
236
|
+
# REQUIRED is ">=MAJOR[.MINOR[.PATCH]]" as index.json writes it. Fails safe: a SPEC or REQUIRED that cannot
|
|
237
|
+
# be compared (an empty one included) returns 1. A pre-release SPEC (16.0.0-rc.1, 16.1.0-rc.1) returns 1
|
|
238
|
+
# whatever its version: as in npm's default semver rules, a range without a pre-release, such as ">=16",
|
|
239
|
+
# admits no pre-release.
|
|
240
|
+
pf_range_meets() {
|
|
241
|
+
local spec="$1" required="$2" have want
|
|
242
|
+
case "$required" in
|
|
243
|
+
'>='*) ;;
|
|
244
|
+
*) return 1 ;;
|
|
245
|
+
esac
|
|
246
|
+
have="$(_pf_version_floor "$spec")"
|
|
247
|
+
want="$(_pf_version_floor "$required")"
|
|
248
|
+
[ -n "$have" ] && [ -n "$want" ] || return 1
|
|
249
|
+
# shellcheck disable=SC2086
|
|
250
|
+
set -- $have $want
|
|
251
|
+
[ "$4" -eq 0 ] || return 1
|
|
252
|
+
if [ "$1" -ne "$5" ]; then [ "$1" -gt "$5" ]; return $?; fi
|
|
253
|
+
if [ "$2" -ne "$6" ]; then [ "$2" -gt "$6" ]; return $?; fi
|
|
254
|
+
[ "$3" -ge "$7" ]
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
# ---------------------------------------------------------------- stacks
|
|
258
|
+
|
|
259
|
+
# The index.json beside this file (a byte-identical copy of skills/index.json).
|
|
260
|
+
pf_index_file() {
|
|
261
|
+
printf '%s\n' "$PF_LIB_DIR/index.json"
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
# pf_stack_get INDEX STACK_ID KEY...: a field of one stack in index.json.
|
|
265
|
+
pf_stack_get() {
|
|
266
|
+
local index="$1" id="$2"
|
|
267
|
+
shift 2
|
|
268
|
+
[ -f "$index" ] || return 0
|
|
269
|
+
if _pf_use_jq; then
|
|
270
|
+
jq -r --arg id "$id" '$ARGS.positional as $p
|
|
271
|
+
| (.stacks[] | select(.id == $id)) as $s
|
|
272
|
+
| ($s | try getpath($p) catch null)
|
|
273
|
+
| if type == "array" then .[] | select(type == "string" or type == "number" or type == "boolean") | tostring
|
|
274
|
+
elif type == "string" or type == "number" or type == "boolean" then tostring
|
|
275
|
+
else empty end' "$index" --args "$@" 2>/dev/null || true
|
|
276
|
+
return 0
|
|
277
|
+
fi
|
|
278
|
+
node -e '
|
|
279
|
+
const fs = require("fs");
|
|
280
|
+
const [file, id, ...path] = process.argv.slice(1);
|
|
281
|
+
let v;
|
|
282
|
+
try { v = (JSON.parse(fs.readFileSync(file, "utf8")).stacks || []).find((s) => s.id === id); } catch { process.exit(0); }
|
|
283
|
+
for (const k of path) {
|
|
284
|
+
if (v === null || typeof v !== "object" || !Object.prototype.hasOwnProperty.call(v, k)) process.exit(0);
|
|
285
|
+
v = v[k];
|
|
286
|
+
}
|
|
287
|
+
const scalar = (x) => ["string", "number", "boolean"].includes(typeof x);
|
|
288
|
+
if (Array.isArray(v)) { for (const x of v) if (scalar(x)) console.log(String(x)); }
|
|
289
|
+
else if (scalar(v)) console.log(String(v));
|
|
290
|
+
' "$index" "$id" "$@" 2>/dev/null || true
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
# The stack ids of index.json, in detection order.
|
|
294
|
+
pf_stack_ids() {
|
|
295
|
+
local index="$1"
|
|
296
|
+
[ -f "$index" ] || return 0
|
|
297
|
+
if _pf_use_jq; then
|
|
298
|
+
jq -r '.stacks[].id' "$index" 2>/dev/null || true
|
|
299
|
+
return 0
|
|
300
|
+
fi
|
|
301
|
+
node -e '
|
|
302
|
+
const fs = require("fs");
|
|
303
|
+
try { for (const s of JSON.parse(fs.readFileSync(process.argv[1], "utf8")).stacks || []) console.log(s.id); } catch {}
|
|
304
|
+
' "$index" 2>/dev/null || true
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
# _pf_detect_entries INDEX STACK_ID all|any: one "package<TAB>range" line per entry (range may be empty).
|
|
308
|
+
_pf_detect_entries() {
|
|
309
|
+
local index="$1" id="$2" kind="$3"
|
|
310
|
+
if _pf_use_jq; then
|
|
311
|
+
jq -r --arg id "$id" --arg k "$kind" \
|
|
312
|
+
'.stacks[] | select(.id == $id) | (.detect[$k] // [])[] | "\(.package)\t\(.range // "")"' \
|
|
313
|
+
"$index" 2>/dev/null || true
|
|
314
|
+
return 0
|
|
315
|
+
fi
|
|
316
|
+
node -e '
|
|
317
|
+
const fs = require("fs");
|
|
318
|
+
const [file, id, kind] = process.argv.slice(1);
|
|
319
|
+
try {
|
|
320
|
+
const s = (JSON.parse(fs.readFileSync(file, "utf8")).stacks || []).find((x) => x.id === id);
|
|
321
|
+
for (const e of (s && s.detect && s.detect[kind]) || []) console.log(e.package + "\t" + (e.range || ""));
|
|
322
|
+
} catch {}
|
|
323
|
+
' "$index" "$id" "$kind" 2>/dev/null || true
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
# _pf_stack_selected DIR INDEX STACK_ID: selection by package names alone. Every "all" package is a
|
|
327
|
+
# dependency and, when the stack has an "any" list, at least one "any" package is. A stack with no entries
|
|
328
|
+
# is never selected.
|
|
329
|
+
_pf_stack_selected() {
|
|
330
|
+
local dir="$1" index="$2" id="$3" entries pkg range tab seen=0 any_seen=0 any_hit=0
|
|
331
|
+
tab="$(printf '\t')"
|
|
332
|
+
entries="$(_pf_detect_entries "$index" "$id" all)"
|
|
333
|
+
while IFS="$tab" read -r pkg range; do
|
|
334
|
+
[ -n "$pkg" ] || continue
|
|
335
|
+
seen=1
|
|
336
|
+
pf_has_dep "$dir" "$pkg" || return 1
|
|
337
|
+
done <<EOF
|
|
338
|
+
$entries
|
|
339
|
+
EOF
|
|
340
|
+
entries="$(_pf_detect_entries "$index" "$id" any)"
|
|
341
|
+
while IFS="$tab" read -r pkg range; do
|
|
342
|
+
[ -n "$pkg" ] || continue
|
|
343
|
+
any_seen=1
|
|
344
|
+
if pf_has_dep "$dir" "$pkg"; then any_hit=1; fi
|
|
345
|
+
done <<EOF
|
|
346
|
+
$entries
|
|
347
|
+
EOF
|
|
348
|
+
[ "$seen" -eq 1 ] || [ "$any_seen" -eq 1 ] || return 1
|
|
349
|
+
[ "$any_seen" -eq 0 ] || [ "$any_hit" -eq 1 ]
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
# _pf_stack_range_failure DIR INDEX STACK_ID: prints "package<TAB>rule<TAB>spec" for the first range rule of
|
|
353
|
+
# the stack that the project's dependencies do not meet; prints nothing when all hold. An "any" entry's rule
|
|
354
|
+
# applies only when that package is a dependency. A spec that cannot be compared fails.
|
|
355
|
+
_pf_stack_range_failure() {
|
|
356
|
+
local dir="$1" index="$2" id="$3" kind entries pkg range spec tab
|
|
357
|
+
tab="$(printf '\t')"
|
|
358
|
+
for kind in all any; do
|
|
359
|
+
entries="$(_pf_detect_entries "$index" "$id" "$kind")"
|
|
360
|
+
while IFS="$tab" read -r pkg range; do
|
|
361
|
+
[ -n "$pkg" ] && [ -n "$range" ] || continue
|
|
362
|
+
pf_has_dep "$dir" "$pkg" || continue
|
|
363
|
+
# An empty spec cannot be compared, so it fails the rule.
|
|
364
|
+
spec="$(pf_dep_range "$dir" "$pkg")"
|
|
365
|
+
if ! pf_range_meets "$spec" "$range"; then
|
|
366
|
+
printf '%s\t%s\t%s\n' "$pkg" "$range" "$spec"
|
|
367
|
+
return 0
|
|
368
|
+
fi
|
|
369
|
+
done <<EOF
|
|
370
|
+
$entries
|
|
371
|
+
EOF
|
|
372
|
+
done
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
# pf_detect_stack DIR INDEX: index.json's detect rules against DIR/package.json (dependencies plus
|
|
376
|
+
# devDependencies). Stacks are tried in array order and the first one selected by package names wins
|
|
377
|
+
# (see _pf_stack_selected). Its range rules must then hold; there is no fall-through to a later stack.
|
|
378
|
+
# Prints the stack id, "refused:<stack id>" when a range rule fails, or "unsupported".
|
|
379
|
+
pf_detect_stack() {
|
|
380
|
+
local dir="$1" index="$2" id
|
|
381
|
+
if [ ! -f "$dir/package.json" ]; then
|
|
382
|
+
printf 'unsupported\n'
|
|
383
|
+
return 0
|
|
384
|
+
fi
|
|
385
|
+
for id in $(pf_stack_ids "$index"); do
|
|
386
|
+
_pf_stack_selected "$dir" "$index" "$id" || continue
|
|
387
|
+
if [ -n "$(_pf_stack_range_failure "$dir" "$index" "$id")" ]; then
|
|
388
|
+
printf 'refused:%s\n' "$id"
|
|
389
|
+
else
|
|
390
|
+
printf '%s\n' "$id"
|
|
391
|
+
fi
|
|
392
|
+
return 0
|
|
393
|
+
done
|
|
394
|
+
printf 'unsupported\n'
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
# pf_stack_version_message STACK_ID: what a stack's range rules ask for, in words.
|
|
398
|
+
pf_stack_version_message() {
|
|
399
|
+
case "$1" in
|
|
400
|
+
nextjs) printf 'Next.js 16 or newer\n' ;;
|
|
401
|
+
tanstack-start) printf 'TanStack Start 1.169.5 or newer with TanStack Router 1.170.46 or newer\n' ;;
|
|
402
|
+
*) printf 'a newer version of its framework\n' ;;
|
|
403
|
+
esac
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
# pf_stack_refusal DIR INDEX STACK_ID: the message for a project refused as "refused:<stack id>".
|
|
407
|
+
pf_stack_refusal() {
|
|
408
|
+
local dir="$1" index="$2" id="$3" failure pkg rule spec tab
|
|
409
|
+
tab="$(printf '\t')"
|
|
410
|
+
failure="$(_pf_stack_range_failure "$dir" "$index" "$id")"
|
|
411
|
+
IFS="$tab" read -r pkg rule spec <<EOF
|
|
412
|
+
$failure
|
|
413
|
+
EOF
|
|
414
|
+
printf 'this project needs %s: package.json gives %s "%s", which does not meet %s. A tag, workspace:, a URL or a compound range cannot be checked; write a version range such as "^%s".\n' \
|
|
415
|
+
"$(pf_stack_version_message "$id")" "$pkg" "$spec" "$rule" "${rule#>=}"
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
# ---------------------------------------------------------------- package managers
|
|
419
|
+
|
|
420
|
+
# pf_detect_pm DIR [INDEX]: npm | pnpm | yarn-classic | yarn-berry | bun | shrinkwrap
|
|
421
|
+
# | mismatch:<declared>:<lockfile's> | none.
|
|
422
|
+
# 1. A `packageManager` field naming yarn@2 or newer, or bun, gives yarn-berry or bun (refused).
|
|
423
|
+
# 2. A root npm-shrinkwrap.json gives shrinkwrap (refused: npm would write to it, not to package-lock.json).
|
|
424
|
+
# 3. index.json's package_managers.detect entries whose file exists in DIR: if any names a refused
|
|
425
|
+
# manager, that manager; otherwise the first match.
|
|
426
|
+
# 4. A yarn.lock with a `__metadata:` line is a Yarn Berry lockfile, whatever else matched: yarn-berry.
|
|
427
|
+
# 5. No entry matches (no lockfile): none (refused: the change needs a committed lockfile).
|
|
428
|
+
# 6. A `packageManager` field naming a supported manager other than the lockfile's gives
|
|
429
|
+
# mismatch:<declared>:<lockfile's> (refused: the project's own files disagree on the manager).
|
|
430
|
+
pf_detect_pm() {
|
|
431
|
+
local dir="$1" index="${2:-}" field declared='' file manager first='' refused='' tab
|
|
432
|
+
[ -n "$index" ] || index="$(pf_index_file)"
|
|
433
|
+
tab="$(printf '\t')"
|
|
434
|
+
field="$(pf_pkg_field "$dir" packageManager)"
|
|
435
|
+
case "$field" in
|
|
436
|
+
yarn@1 | yarn@1.* | yarn@1+*) declared=yarn-classic ;;
|
|
437
|
+
yarn@*) printf 'yarn-berry\n'; return 0 ;;
|
|
438
|
+
bun@*) printf 'bun\n'; return 0 ;;
|
|
439
|
+
npm@*) declared=npm ;;
|
|
440
|
+
pnpm@*) declared=pnpm ;;
|
|
441
|
+
esac
|
|
442
|
+
if [ -e "$dir/npm-shrinkwrap.json" ]; then
|
|
443
|
+
printf 'shrinkwrap\n'
|
|
444
|
+
return 0
|
|
445
|
+
fi
|
|
446
|
+
while IFS="$tab" read -r file manager; do
|
|
447
|
+
[ -n "$file" ] && [ -n "$manager" ] || continue
|
|
448
|
+
[ -e "$dir/$file" ] || continue
|
|
449
|
+
[ -n "$first" ] || first="$manager"
|
|
450
|
+
if [ -z "$refused" ] && pf_pm_refused "$manager" "$index"; then refused="$manager"; fi
|
|
451
|
+
done <<EOF
|
|
452
|
+
$(_pf_pm_detect_entries "$index")
|
|
453
|
+
EOF
|
|
454
|
+
if [ -n "$refused" ]; then
|
|
455
|
+
printf '%s\n' "$refused"
|
|
456
|
+
elif [ -f "$dir/yarn.lock" ] && grep -q '^__metadata:' "$dir/yarn.lock" 2>/dev/null; then
|
|
457
|
+
printf 'yarn-berry\n'
|
|
458
|
+
elif [ -z "$first" ]; then
|
|
459
|
+
printf 'none\n'
|
|
460
|
+
elif [ -n "$declared" ] && [ "$declared" != "$first" ]; then
|
|
461
|
+
printf 'mismatch:%s:%s\n' "$declared" "$first"
|
|
462
|
+
else
|
|
463
|
+
printf '%s\n' "$first"
|
|
464
|
+
fi
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
# One "file<TAB>manager" line per package_managers.detect entry, in order.
|
|
468
|
+
_pf_pm_detect_entries() {
|
|
469
|
+
local index="$1"
|
|
470
|
+
[ -f "$index" ] || return 0
|
|
471
|
+
if _pf_use_jq; then
|
|
472
|
+
jq -r '.package_managers.detect[] | "\(.file)\t\(.manager)"' "$index" 2>/dev/null || true
|
|
473
|
+
return 0
|
|
474
|
+
fi
|
|
475
|
+
node -e '
|
|
476
|
+
const fs = require("fs");
|
|
477
|
+
try {
|
|
478
|
+
for (const e of JSON.parse(fs.readFileSync(process.argv[1], "utf8")).package_managers.detect || [])
|
|
479
|
+
console.log(e.file + "\t" + e.manager);
|
|
480
|
+
} catch {}
|
|
481
|
+
' "$index" 2>/dev/null || true
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
# pf_pm_supported PM [INDEX]: PM is in package_managers.supported.
|
|
485
|
+
pf_pm_supported() {
|
|
486
|
+
local pm="$1" index="${2:-}"
|
|
487
|
+
[ -n "$index" ] || index="$(pf_index_file)"
|
|
488
|
+
pf_json_get "$index" package_managers supported | grep -qx -- "$pm"
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
# pf_pm_refused PM [INDEX]: PM is in package_managers.refused.
|
|
492
|
+
pf_pm_refused() {
|
|
493
|
+
local pm="$1" index="${2:-}"
|
|
494
|
+
[ -n "$index" ] || index="$(pf_index_file)"
|
|
495
|
+
pf_json_get "$index" package_managers refused | grep -qx -- "$pm"
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
# pf_pm_refusal PM: the message for a result of pf_detect_pm that is not a supported manager.
|
|
499
|
+
pf_pm_refusal() {
|
|
500
|
+
local declared lock
|
|
501
|
+
case "$1" in
|
|
502
|
+
none)
|
|
503
|
+
printf 'no lockfile found. Passflow skills need a committed lockfile: package-lock.json (npm), pnpm-lock.yaml (pnpm) or yarn.lock (Yarn 1, classic). Install once with your package manager, commit the lockfile, then run again.\n'
|
|
504
|
+
;;
|
|
505
|
+
shrinkwrap)
|
|
506
|
+
printf 'this project has an npm-shrinkwrap.json. Passflow skills cannot change it; they update package-lock.json, pnpm-lock.yaml or yarn.lock only. Remove the shrinkwrap or integrate by hand.\n'
|
|
507
|
+
;;
|
|
508
|
+
mismatch:*)
|
|
509
|
+
declared="${1#mismatch:}"
|
|
510
|
+
lock="${declared#*:}"
|
|
511
|
+
declared="${declared%%:*}"
|
|
512
|
+
printf 'package.json names %s in packageManager, but the lockfile belongs to %s. Make them agree (remove the stale lockfile or fix packageManager), then run again.\n' \
|
|
513
|
+
"$(pf_pm_tool "$declared")" "$(pf_pm_tool "$lock")"
|
|
514
|
+
;;
|
|
515
|
+
*)
|
|
516
|
+
printf 'this project uses %s. Passflow skills support npm, pnpm and Yarn 1 (classic) only.\n' "$1"
|
|
517
|
+
;;
|
|
518
|
+
esac
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
# pf_pm_tool PM: the command for a supported manager (yarn-classic -> yarn).
|
|
522
|
+
pf_pm_tool() {
|
|
523
|
+
case "$1" in
|
|
524
|
+
yarn-classic) printf 'yarn\n' ;;
|
|
525
|
+
*) printf '%s\n' "$1" ;;
|
|
526
|
+
esac
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
# pf_package_spec @passflow/node PASSFLOW_NODE_VERSION -> "@passflow/node" or "@passflow/node@0.2.0".
|
|
530
|
+
pf_package_spec() {
|
|
531
|
+
local name="$1" var="$2" version=''
|
|
532
|
+
case "$var" in
|
|
533
|
+
'' | *[!A-Za-z0-9_]* | [0-9]*) ;;
|
|
534
|
+
*) version="${!var:-}" ;;
|
|
535
|
+
esac
|
|
536
|
+
if [ -n "$version" ]; then
|
|
537
|
+
printf '%s@%s\n' "$name" "$version"
|
|
538
|
+
else
|
|
539
|
+
printf '%s\n' "$name"
|
|
540
|
+
fi
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
# pf_install_line npm @passflow/node PASSFLOW_NODE_VERSION prints:
|
|
544
|
+
# npm install "@passflow/node${PASSFLOW_NODE_VERSION:+@$PASSFLOW_NODE_VERSION}"
|
|
545
|
+
pf_install_line() {
|
|
546
|
+
local pm="$1" name="$2" var="$3" cmd
|
|
547
|
+
case "$pm" in
|
|
548
|
+
npm) cmd='npm install' ;;
|
|
549
|
+
pnpm) cmd='pnpm add' ;;
|
|
550
|
+
yarn-classic) cmd='yarn add' ;;
|
|
551
|
+
*) return 2 ;;
|
|
552
|
+
esac
|
|
553
|
+
# The dollar signs are literal: the line is shown to the user, who runs it.
|
|
554
|
+
# shellcheck disable=SC2016
|
|
555
|
+
printf '%s "%s${%s:+@$%s}"\n' "$cmd" "$name" "$var" "$var"
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
# pf_install DIR [PM]: installs the project's dependencies with lifecycle scripts off. PM defaults to
|
|
559
|
+
# pf_detect_pm DIR; pass it for a folder whose lockfile lives in a parent.
|
|
560
|
+
pf_install() {
|
|
561
|
+
local dir="$1" pm="${2:-}"
|
|
562
|
+
[ -n "$pm" ] || pm="$(pf_detect_pm "$dir")"
|
|
563
|
+
case "$pm" in
|
|
564
|
+
npm) (cd "$dir" && npm install --ignore-scripts --no-audit --no-fund) ;;
|
|
565
|
+
pnpm) (cd "$dir" && pnpm install --ignore-scripts) ;;
|
|
566
|
+
yarn-classic) (cd "$dir" && yarn install --ignore-scripts) ;;
|
|
567
|
+
*)
|
|
568
|
+
printf 'REFUSED: %s\n' "$(pf_pm_refusal "$pm")" >&2
|
|
569
|
+
return 2
|
|
570
|
+
;;
|
|
571
|
+
esac
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
# pf_run_script DIR NAME [PM]
|
|
575
|
+
pf_run_script() {
|
|
576
|
+
local dir="$1" name="$2" pm="${3:-}"
|
|
577
|
+
[ -n "$pm" ] || pm="$(pf_detect_pm "$dir")"
|
|
578
|
+
case "$pm" in
|
|
579
|
+
npm) (cd "$dir" && npm run --silent "$name") ;;
|
|
580
|
+
pnpm) (cd "$dir" && pnpm run --silent "$name") ;;
|
|
581
|
+
yarn-classic) (cd "$dir" && yarn run --silent "$name") ;;
|
|
582
|
+
*) return 2 ;;
|
|
583
|
+
esac
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
# pf_exec DIR COMMAND [ARGS...]: runs a binary installed in the project, never one fetched for the run.
|
|
587
|
+
pf_exec() {
|
|
588
|
+
local dir="$1" bin="$2"
|
|
589
|
+
shift 2
|
|
590
|
+
if [ ! -x "$dir/node_modules/.bin/$bin" ]; then
|
|
591
|
+
printf '%s is not installed in %s/node_modules/.bin\n' "$bin" "$dir" >&2
|
|
592
|
+
return 127
|
|
593
|
+
fi
|
|
594
|
+
(cd "$dir" && "./node_modules/.bin/$bin" "$@")
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
# pf_typecheck DIR [PM]: the `typecheck` script if there is one, else tsc when tsconfig.json exists:
|
|
598
|
+
# `tsc -b --noEmit` for a tsconfig.json with project references (a solution-style file such as Vite's,
|
|
599
|
+
# where plain `tsc --noEmit` checks nothing), `tsc -p tsconfig.json --noEmit` otherwise.
|
|
600
|
+
# Returns 3 when there is nothing to check. Under `set -e`, call it as `rc=0; pf_typecheck "$dir" || rc=$?`.
|
|
601
|
+
pf_typecheck() {
|
|
602
|
+
local dir="$1" pm="${2:-}"
|
|
603
|
+
if pf_has_script "$dir" typecheck; then
|
|
604
|
+
pf_run_script "$dir" typecheck "$pm"
|
|
605
|
+
return $?
|
|
606
|
+
fi
|
|
607
|
+
if [ -f "$dir/tsconfig.json" ]; then
|
|
608
|
+
if grep -q '"references"[[:space:]]*:' "$dir/tsconfig.json" 2>/dev/null; then
|
|
609
|
+
pf_exec "$dir" tsc -b --noEmit
|
|
610
|
+
else
|
|
611
|
+
pf_exec "$dir" tsc -p tsconfig.json --noEmit
|
|
612
|
+
fi
|
|
613
|
+
return $?
|
|
614
|
+
fi
|
|
615
|
+
return 3
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
# ---------------------------------------------------------------- env file
|
|
619
|
+
|
|
620
|
+
# Reads NAME=VALUE lines. Accepts `export NAME=VALUE` and one pair of surrounding quotes. Skips comments.
|
|
621
|
+
# Prints "NAME<TAB>VALUE" lines on stdout for internal use only: callers must never echo them.
|
|
622
|
+
_pf_env_pairs() {
|
|
623
|
+
local file="$1" line name value
|
|
624
|
+
[ -f "$file" ] || return 0
|
|
625
|
+
while IFS= read -r line || [ -n "$line" ]; do
|
|
626
|
+
line="${line%$'\r'}"
|
|
627
|
+
case "$line" in
|
|
628
|
+
'' | '#'*) continue ;;
|
|
629
|
+
esac
|
|
630
|
+
line="${line#export }"
|
|
631
|
+
case "$line" in
|
|
632
|
+
*=*) ;;
|
|
633
|
+
*) continue ;;
|
|
634
|
+
esac
|
|
635
|
+
name="${line%%=*}"
|
|
636
|
+
name="$(printf '%s' "$name" | tr -d ' \t')"
|
|
637
|
+
value="${line#*=}"
|
|
638
|
+
case "$name" in
|
|
639
|
+
'' | *[!A-Za-z0-9_]* | [0-9]*) continue ;;
|
|
640
|
+
esac
|
|
641
|
+
# Spaces around "=" and at the end do not count.
|
|
642
|
+
value="${value#"${value%%[![:space:]]*}"}"
|
|
643
|
+
case "$value" in
|
|
644
|
+
\"*)
|
|
645
|
+
# A quoted value ends at its closing quote; anything after it (a comment) is dropped.
|
|
646
|
+
value="${value#\"}"
|
|
647
|
+
value="${value%%\"*}"
|
|
648
|
+
;;
|
|
649
|
+
\'*)
|
|
650
|
+
value="${value#\'}"
|
|
651
|
+
value="${value%%\'*}"
|
|
652
|
+
;;
|
|
653
|
+
*)
|
|
654
|
+
# Unquoted: " #" starts a comment, as in the dotenv loaders of Node.js, Next.js and Vite.
|
|
655
|
+
case "$value" in
|
|
656
|
+
'#'*) value='' ;;
|
|
657
|
+
*[[:space:]]'#'*) value="$(printf '%s' "$value" | sed 's/[[:space:]]#.*$//')" ;;
|
|
658
|
+
esac
|
|
659
|
+
value="${value%"${value##*[![:space:]]}"}"
|
|
660
|
+
;;
|
|
661
|
+
esac
|
|
662
|
+
printf '%s\t%s\n' "$name" "$value"
|
|
663
|
+
done <"$file"
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
# The value of NAME in FILE (the last assignment wins), for internal use only: never print it.
|
|
667
|
+
_pf_env_value() {
|
|
668
|
+
_pf_env_pairs "$1" | awk -F '\t' -v n="$2" '$1 == n { v = substr($0, length(n) + 2) } END { printf "%s", v }'
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
# pf_env_missing_keys FILE KEY...: prints each KEY that has no non-empty value in FILE.
|
|
672
|
+
pf_env_missing_keys() {
|
|
673
|
+
local file="$1" key
|
|
674
|
+
shift
|
|
675
|
+
for key in "$@"; do
|
|
676
|
+
if [ -z "$(_pf_env_value "$file" "$key")" ]; then
|
|
677
|
+
printf '%s\n' "$key"
|
|
678
|
+
fi
|
|
679
|
+
done
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
# pf_load_env FILE KEY...: exports each KEY that FILE assigns, and nothing else from FILE. Values are never
|
|
683
|
+
# evaluated, expanded or printed. The app reads the rest of its env file itself.
|
|
684
|
+
pf_load_env() {
|
|
685
|
+
local file="$1" name value
|
|
686
|
+
shift
|
|
687
|
+
[ -f "$file" ] || return 1
|
|
688
|
+
while IFS="$(printf '\t')" read -r name value; do
|
|
689
|
+
[ -n "$name" ] || continue
|
|
690
|
+
case " $* " in
|
|
691
|
+
*" $name "*) export "$name=$value" ;;
|
|
692
|
+
esac
|
|
693
|
+
done <<EOF
|
|
694
|
+
$(_pf_env_pairs "$file")
|
|
695
|
+
EOF
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
# pf_git_tracked DIR FILE: 0 when git tracks FILE. Outside a git repo: 1.
|
|
699
|
+
pf_git_tracked() {
|
|
700
|
+
local dir="$1" file="$2"
|
|
701
|
+
command -v git >/dev/null 2>&1 || return 1
|
|
702
|
+
(cd "$dir" && git rev-parse --is-inside-work-tree >/dev/null 2>&1) || return 1
|
|
703
|
+
(cd "$dir" && git ls-files --error-unmatch -- "$file" >/dev/null 2>&1)
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
# ---------------------------------------------------------------- scans
|
|
707
|
+
|
|
708
|
+
# _pf_scan_value DIR NAME SOURCE VALUE [GREP_ARGS...]: prints "<file>: value of NAME (SOURCE)" for each file
|
|
709
|
+
# under DIR that holds VALUE; returns 1 when one does. The value reaches grep through a pipe, never a
|
|
710
|
+
# command line that `ps` could show nor a file left behind on a signal. Values shorter than 8 characters
|
|
711
|
+
# are not searched (they would match by chance).
|
|
712
|
+
_pf_scan_value() {
|
|
713
|
+
local dir="$1" name="$2" source="$3" value="$4" hits
|
|
714
|
+
shift 4
|
|
715
|
+
[ "${#value}" -ge 8 ] || return 0
|
|
716
|
+
hits="$(grep -rlF "$@" -f <(printf '%s\n' "$value") -- "$dir" 2>/dev/null || true)"
|
|
717
|
+
[ -n "$hits" ] || return 0
|
|
718
|
+
printf '%s\n' "$hits" | while IFS= read -r f; do printf ' %s: value of %s (%s)\n' "$f" "$name" "$source"; done
|
|
719
|
+
return 1
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
# _pf_scan_values DIR ENV_FILE NAME [GREP_ARGS...]: searches DIR for NAME's value from ENV_FILE and, when it
|
|
723
|
+
# differs, for the value NAME has in this process's environment (a CI secret store, for example).
|
|
724
|
+
_pf_scan_values() {
|
|
725
|
+
local dir="$1" env_file="$2" name="$3" file_value='' env_value='' found=0
|
|
726
|
+
shift 3
|
|
727
|
+
case "$name" in
|
|
728
|
+
'' | *[!A-Za-z0-9_]* | [0-9]*) return 0 ;;
|
|
729
|
+
esac
|
|
730
|
+
if [ -n "$env_file" ] && [ -f "$env_file" ]; then
|
|
731
|
+
file_value="$(_pf_env_value "$env_file" "$name")"
|
|
732
|
+
_pf_scan_value "$dir" "$name" "$(basename "$env_file")" "$file_value" "$@" || found=1
|
|
733
|
+
fi
|
|
734
|
+
env_value="${!name:-}"
|
|
735
|
+
if [ -n "$env_value" ] && [ "$env_value" != "$file_value" ]; then
|
|
736
|
+
_pf_scan_value "$dir" "$name" environment "$env_value" "$@" || found=1
|
|
737
|
+
fi
|
|
738
|
+
file_value=''
|
|
739
|
+
env_value=''
|
|
740
|
+
return "$found"
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
# pf_scan_bundle BUNDLE_DIR ENV_FILE NAME...: searches built client files for each env NAME and for each
|
|
744
|
+
# NAME's value, from ENV_FILE when it exists and from the environment when NAME is set there. Prints
|
|
745
|
+
# "<file>: <NAME>" or "<file>: value of <NAME> (<source>)" per hit, never the value. An empty ENV_FILE
|
|
746
|
+
# searches the names only. Returns 0 clean, 1 found, 3 when BUNDLE_DIR does not exist.
|
|
747
|
+
pf_scan_bundle() {
|
|
748
|
+
local bundle="$1" env_file="$2" name hits found=0
|
|
749
|
+
shift 2
|
|
750
|
+
[ -d "$bundle" ] || return 3
|
|
751
|
+
for name in "$@"; do
|
|
752
|
+
hits="$(grep -rlF -- "$name" "$bundle" 2>/dev/null || true)"
|
|
753
|
+
if [ -n "$hits" ]; then
|
|
754
|
+
found=1
|
|
755
|
+
printf '%s\n' "$hits" | while IFS= read -r f; do printf ' %s: %s\n' "$f" "$name"; done
|
|
756
|
+
fi
|
|
757
|
+
if [ -n "$env_file" ]; then
|
|
758
|
+
_pf_scan_values "$bundle" "$env_file" "$name" || found=1
|
|
759
|
+
fi
|
|
760
|
+
done
|
|
761
|
+
return "$found"
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
# pf_scan_values DIR ENV_FILE 'GLOB...' NAME...: like pf_scan_bundle, but values only and only in files whose
|
|
765
|
+
# name matches one of the space-separated GLOBs (server output that is sent to browsers, for example).
|
|
766
|
+
# Returns 0 clean, 1 found, 3 when DIR does not exist.
|
|
767
|
+
pf_scan_values() {
|
|
768
|
+
local dir="$1" env_file="$2" globs="$3" name glob includes='' found=0
|
|
769
|
+
shift 3
|
|
770
|
+
[ -d "$dir" ] || return 3
|
|
771
|
+
set -f
|
|
772
|
+
for glob in $globs; do includes="$includes --include=$glob"; done
|
|
773
|
+
for name in "$@"; do
|
|
774
|
+
# shellcheck disable=SC2086
|
|
775
|
+
_pf_scan_values "$dir" "$env_file" "$name" $includes || found=1
|
|
776
|
+
done
|
|
777
|
+
set +f
|
|
778
|
+
return "$found"
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
# Source files of the app: TypeScript and JavaScript outside dependencies and build output.
|
|
782
|
+
_pf_source_files() {
|
|
783
|
+
find "$1" \
|
|
784
|
+
\( -name node_modules -o -name .git -o -name .next -o -name .output -o -name .vinxi -o -name .nitro \
|
|
785
|
+
-o -name .tanstack -o -name dist -o -name build -o -name coverage -o -name .turbo -o -name .vercel \) -prune \
|
|
786
|
+
-o -type f \( -name '*.ts' -o -name '*.tsx' -o -name '*.js' -o -name '*.jsx' -o -name '*.mjs' -o -name '*.cjs' \) \
|
|
787
|
+
-print 2>/dev/null
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
# Prints "<file>:<line>" for each line of FILES that matches the extended regex. Never the line itself,
|
|
791
|
+
# which could hold a value.
|
|
792
|
+
_pf_grep_lines() {
|
|
793
|
+
local pattern="$1" files="$2" flags="${3:-}"
|
|
794
|
+
[ -n "$files" ] || return 0
|
|
795
|
+
printf '%s\n' "$files" | while IFS= read -r f; do
|
|
796
|
+
# shellcheck disable=SC2086
|
|
797
|
+
grep -nE $flags -- "$pattern" "$f" 2>/dev/null | cut -d: -f1 | while IFS= read -r n; do printf '%s:%s\n' "$f" "$n"; done
|
|
798
|
+
done
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
# Prints "<file>:<line>" for each <Link ...> opening tag in FILES whose href or to points at /auth/, the tag
|
|
802
|
+
# split over several lines included (Prettier puts each prop on its own line). The tag ends at the first ">"
|
|
803
|
+
# outside braces, so `onClick={() => go()}` before href does not end it.
|
|
804
|
+
_pf_auth_link_lines() {
|
|
805
|
+
local files="$1"
|
|
806
|
+
[ -n "$files" ] || return 0
|
|
807
|
+
printf '%s\n' "$files" | while IFS= read -r f; do
|
|
808
|
+
awk -v file="$f" -v target="(href|to)=[{\"'\`]*/auth/" '
|
|
809
|
+
{
|
|
810
|
+
rest = $0
|
|
811
|
+
while (rest != "") {
|
|
812
|
+
if (!inside) {
|
|
813
|
+
i = index(rest, "<Link")
|
|
814
|
+
if (i == 0) break
|
|
815
|
+
after = substr(rest, i + 5, 1)
|
|
816
|
+
rest = substr(rest, i + 5)
|
|
817
|
+
if (after != "" && after !~ /[[:space:]>]/) continue
|
|
818
|
+
inside = 1; start = NR; tag = ""; depth = 0
|
|
819
|
+
}
|
|
820
|
+
# Walk to the ">" that closes the tag, counting braces.
|
|
821
|
+
n = length(rest); j = 0
|
|
822
|
+
for (k = 1; k <= n; k++) {
|
|
823
|
+
c = substr(rest, k, 1)
|
|
824
|
+
if (c == "{") depth++
|
|
825
|
+
else if (c == "}" && depth > 0) depth--
|
|
826
|
+
else if (c == ">" && depth == 0) { j = k; break }
|
|
827
|
+
}
|
|
828
|
+
if (j == 0) { tag = tag " " rest; break }
|
|
829
|
+
tag = tag " " substr(rest, 1, j - 1)
|
|
830
|
+
rest = substr(rest, j + 1)
|
|
831
|
+
inside = 0
|
|
832
|
+
if (tag ~ target) print file ":" start
|
|
833
|
+
}
|
|
834
|
+
}' "$f" 2>/dev/null
|
|
835
|
+
done
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
_PF_SCAN_FOUND=0
|
|
839
|
+
|
|
840
|
+
# _pf_report RULE HITS: prints each hit under its rule and records that something was found.
|
|
841
|
+
_pf_report() {
|
|
842
|
+
if [ -n "$2" ]; then
|
|
843
|
+
_PF_SCAN_FOUND=1
|
|
844
|
+
printf '%s\n' "$2" | while IFS= read -r h; do printf ' %s: %s\n' "$1" "$h"; done
|
|
845
|
+
fi
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
# pf_guardrail_scan DIR KIND: source-only checks. KIND is server (Next.js, TanStack Start), spa or api.
|
|
849
|
+
# Prints " <rule>: <file>:<line>" per finding. Returns 0 clean, 1 found.
|
|
850
|
+
pf_guardrail_scan() {
|
|
851
|
+
local dir="$1" kind="$2" files hits auth_files=''
|
|
852
|
+
_PF_SCAN_FOUND=0
|
|
853
|
+
files="$(_pf_source_files "$dir")"
|
|
854
|
+
|
|
855
|
+
hits="$(_pf_grep_lines 'NEXT_PUBLIC_[A-Z0-9_]*SECRET' "$files")"
|
|
856
|
+
_pf_report 'secret in a NEXT_PUBLIC_ variable' "$hits"
|
|
857
|
+
hits="$(_pf_grep_lines 'VITE_[A-Z0-9_]*SECRET' "$files")"
|
|
858
|
+
_pf_report 'secret in a VITE_ variable' "$hits"
|
|
859
|
+
|
|
860
|
+
hits="$(_pf_grep_lines "(from|require\\()[[:space:]]*['\"](jwt-decode|jsonwebtoken)['\"]|jwtDecode[[:space:]]*\\(" "$files")"
|
|
861
|
+
_pf_report 'hand-decoded token (use createVerifier)' "$hits"
|
|
862
|
+
|
|
863
|
+
# atob() or a base64 Buffer decode, in files that handle tokens.
|
|
864
|
+
if [ -n "$files" ]; then
|
|
865
|
+
auth_files="$(printf '%s\n' "$files" | while IFS= read -r f; do
|
|
866
|
+
if grep -qiE 'token|bearer|authorization|passflow' "$f" 2>/dev/null; then printf '%s\n' "$f"; fi
|
|
867
|
+
done)"
|
|
868
|
+
fi
|
|
869
|
+
hits="$(_pf_grep_lines "atob[[:space:]]*\\(|Buffer\\.from\\([^)]*['\"]base64(url)?['\"]" "$auth_files")"
|
|
870
|
+
_pf_report 'hand-decoded token (use createVerifier)' "$hits"
|
|
871
|
+
|
|
872
|
+
if [ "$kind" = server ]; then
|
|
873
|
+
hits="$(_pf_grep_lines '(localStorage|sessionStorage).*token|token.*(localStorage|sessionStorage)' "$files" -i)"
|
|
874
|
+
_pf_report 'token in browser storage' "$hits"
|
|
875
|
+
fi
|
|
876
|
+
|
|
877
|
+
if [ "$kind" = server ] || [ "$kind" = spa ]; then
|
|
878
|
+
hits="$(_pf_auth_link_lines "$files")"
|
|
879
|
+
_pf_report 'sign-in or sign-out link must be a plain <a href>' "$hits"
|
|
880
|
+
fi
|
|
881
|
+
|
|
882
|
+
return "$_PF_SCAN_FOUND"
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
# ---------------------------------------------------------------- HTTP
|
|
886
|
+
|
|
887
|
+
# pf_http OUT_DIR METHOD URL [CURL_ARGS...]: one request, no redirects followed. Writes OUT_DIR/status,
|
|
888
|
+
# OUT_DIR/headers and OUT_DIR/body. Returns curl's exit code.
|
|
889
|
+
pf_http() {
|
|
890
|
+
local out="$1" method="$2" url="$3" rc=0
|
|
891
|
+
shift 3
|
|
892
|
+
mkdir -p "$out"
|
|
893
|
+
: >"$out/headers"
|
|
894
|
+
: >"$out/body"
|
|
895
|
+
curl --noproxy '*' -sS -X "$method" --max-time 15 -o "$out/body" -D "$out/headers" -w '%{http_code}' "$@" -- "$url" \
|
|
896
|
+
>"$out/status" 2>"$out/error" || rc=$?
|
|
897
|
+
return "$rc"
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
# pf_header OUT_DIR NAME: the first value of a response header, case-insensitive, without the CR.
|
|
901
|
+
pf_header() {
|
|
902
|
+
local out="$1" name="$2"
|
|
903
|
+
tr -d '\r' <"$out/headers" | awk -v n="$name" '
|
|
904
|
+
BEGIN { n = tolower(n) ":" }
|
|
905
|
+
{ i = index($0, ":"); if (i > 0 && tolower(substr($0, 1, i)) == n) { v = substr($0, i + 1); sub(/^[ \t]+/, "", v); print v; exit } }'
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
# pf_wait_http URL SECONDS [PID]: waits until URL answers with any HTTP status. With PID, gives up as soon
|
|
909
|
+
# as that process has exited (returns 1), so a server that failed to start is never mistaken for one that
|
|
910
|
+
# answers.
|
|
911
|
+
pf_wait_http() {
|
|
912
|
+
local url="$1" seconds="$2" pid="${3:-}" i=0 code
|
|
913
|
+
while [ "$i" -lt "$seconds" ]; do
|
|
914
|
+
if [ -n "$pid" ] && ! pf_alive "$pid"; then
|
|
915
|
+
return 1
|
|
916
|
+
fi
|
|
917
|
+
code="$(curl --noproxy '*' -s -o /dev/null -w '%{http_code}' --max-time 2 -- "$url" 2>/dev/null || true)"
|
|
918
|
+
if [ -n "$code" ] && [ "$code" != 000 ]; then
|
|
919
|
+
return 0
|
|
920
|
+
fi
|
|
921
|
+
sleep 1
|
|
922
|
+
i=$((i + 1))
|
|
923
|
+
done
|
|
924
|
+
return 1
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
# pf_port_free PORT: 0 when nothing accepts a connection on PORT at 127.0.0.1 or at localhost (which may be
|
|
928
|
+
# ::1). Any listener counts, HTTP or not.
|
|
929
|
+
pf_port_free() {
|
|
930
|
+
local port="$1" host rc
|
|
931
|
+
for host in 127.0.0.1 localhost; do
|
|
932
|
+
rc=0
|
|
933
|
+
curl --noproxy '*' -s -o /dev/null --max-time 2 -- "http://$host:$port/" 2>/dev/null || rc=$?
|
|
934
|
+
# 7: connection refused. Anything else, success included, means something listens.
|
|
935
|
+
[ "$rc" -eq 7 ] || return 1
|
|
936
|
+
done
|
|
937
|
+
return 0
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
# pf_has_token LIST TOKEN: 0 when the comma-separated header value LIST holds TOKEN as a whole item,
|
|
941
|
+
# case-insensitive ("WWW-Authenticate" is in "Content-Type, www-authenticate", not in "X-WWW-Authenticate").
|
|
942
|
+
pf_has_token() {
|
|
943
|
+
printf '%s\n' "$1" | tr ',' '\n' | sed 's/^[[:space:]]*//; s/[[:space:]]*$//' | grep -qixF -- "$2"
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
# pf_listener_owned PORT PID: 0 when every process listening on PORT is PID or runs under it, 1 when one is
|
|
947
|
+
# not (another server owns the port), 3 when that cannot be told (no lsof, or lsof sees no listener).
|
|
948
|
+
pf_listener_owned() {
|
|
949
|
+
local port="$1" pid="$2" listeners tree p
|
|
950
|
+
command -v lsof >/dev/null 2>&1 || return 3
|
|
951
|
+
listeners="$(lsof -nP -iTCP:"$port" -sTCP:LISTEN -t 2>/dev/null | LC_ALL=C sort -u || true)"
|
|
952
|
+
[ -n "$listeners" ] || return 3
|
|
953
|
+
tree=" $(_pf_tree "$pid" | tr '\n' ' ') "
|
|
954
|
+
for p in $listeners; do
|
|
955
|
+
case "$tree" in
|
|
956
|
+
*" $p "*) ;;
|
|
957
|
+
*) return 1 ;;
|
|
958
|
+
esac
|
|
959
|
+
done
|
|
960
|
+
return 0
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
# pf_check_listener PORT PID: one PASS, FAIL or SKIP line on whether the server that answers on PORT is the
|
|
964
|
+
# one verify.sh started (PID or a process under it).
|
|
965
|
+
pf_check_listener() {
|
|
966
|
+
local rc=0
|
|
967
|
+
pf_listener_owned "$1" "$2" || rc=$?
|
|
968
|
+
case "$rc" in
|
|
969
|
+
0) pf_pass "port $1 is served by the process verify.sh started" ;;
|
|
970
|
+
1) pf_fail "port $1 is served by the process verify.sh started" "another process listens on port $1" ;;
|
|
971
|
+
*) pf_skip "listener owner" "lsof is not available or saw no listener on port $1" ;;
|
|
972
|
+
esac
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
# pf_port_busy_refusal PORT: the message for a port that is already taken.
|
|
976
|
+
pf_port_busy_refusal() {
|
|
977
|
+
printf 'something already listens on port %s. Stop it or pass a free port with --port; verify.sh tests only a server it starts itself.\n' "$1"
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
# ---------------------------------------------------------------- processes
|
|
981
|
+
|
|
982
|
+
# pf_start_bg DIR LOG_FILE COMMAND [ARGS...]: starts COMMAND in DIR in the background; sets PF_BG_PID.
|
|
983
|
+
pf_start_bg() {
|
|
984
|
+
local dir="$1" log="$2"
|
|
985
|
+
shift 2
|
|
986
|
+
(cd "$dir" && exec "$@") >"$log" 2>&1 &
|
|
987
|
+
# Read by the sourcing script.
|
|
988
|
+
# shellcheck disable=SC2034
|
|
989
|
+
PF_BG_PID=$!
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
# pf_alive PID: 0 while PID runs (a zombie left by an exited child does not count).
|
|
993
|
+
pf_alive() {
|
|
994
|
+
local state
|
|
995
|
+
kill -0 "$1" 2>/dev/null || return 1
|
|
996
|
+
state="$(ps -o stat= -p "$1" 2>/dev/null || true)"
|
|
997
|
+
case "$state" in
|
|
998
|
+
Z*) return 1 ;;
|
|
999
|
+
esac
|
|
1000
|
+
return 0
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
# _pf_children PID: the direct children of PID, one per line. Uses pgrep, else ps, else /proc, so it works
|
|
1004
|
+
# in slim images without procps.
|
|
1005
|
+
_pf_children() {
|
|
1006
|
+
local pid="$1" stat_file stat ppid
|
|
1007
|
+
if command -v pgrep >/dev/null 2>&1; then
|
|
1008
|
+
pgrep -P "$pid" 2>/dev/null || true
|
|
1009
|
+
elif command -v ps >/dev/null 2>&1; then
|
|
1010
|
+
ps -A -o pid= -o ppid= 2>/dev/null | awk -v p="$pid" '$2 == p { print $1 }' || true
|
|
1011
|
+
elif [ -d /proc ]; then
|
|
1012
|
+
for stat_file in /proc/[0-9]*/stat; do
|
|
1013
|
+
[ -r "$stat_file" ] || continue
|
|
1014
|
+
stat="$(cat "$stat_file" 2>/dev/null || true)"
|
|
1015
|
+
# The command name in field 2 may hold spaces: the fields after it start at the last ")".
|
|
1016
|
+
stat="${stat##*) }"
|
|
1017
|
+
ppid="$(printf '%s' "$stat" | cut -d' ' -f2)"
|
|
1018
|
+
if [ "$ppid" = "$pid" ]; then
|
|
1019
|
+
stat_file="${stat_file#/proc/}"
|
|
1020
|
+
printf '%s\n' "${stat_file%/stat}"
|
|
1021
|
+
fi
|
|
1022
|
+
done
|
|
1023
|
+
fi
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
# _pf_tree PID: PID and every process under it, parents first.
|
|
1027
|
+
_pf_tree() {
|
|
1028
|
+
local child
|
|
1029
|
+
printf '%s\n' "$1"
|
|
1030
|
+
for child in $(_pf_children "$1"); do
|
|
1031
|
+
_pf_tree "$child"
|
|
1032
|
+
done
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
# pf_kill_tree PID: stops PID and every process under it: TERM, up to 5 s to exit, then KILL.
|
|
1036
|
+
pf_kill_tree() {
|
|
1037
|
+
local pid="$1" pids p i=0 left
|
|
1038
|
+
[ -n "$pid" ] || return 0
|
|
1039
|
+
# The whole tree is listed first: a wrapper (npm, yarn) that exits on TERM would orphan its children.
|
|
1040
|
+
pids="$(_pf_tree "$pid")"
|
|
1041
|
+
for p in $pids; do
|
|
1042
|
+
kill "$p" 2>/dev/null || true
|
|
1043
|
+
done
|
|
1044
|
+
while [ "$i" -lt 10 ]; do
|
|
1045
|
+
left=''
|
|
1046
|
+
for p in $pids; do
|
|
1047
|
+
if pf_alive "$p"; then left="$left $p"; fi
|
|
1048
|
+
done
|
|
1049
|
+
[ -n "$left" ] || break
|
|
1050
|
+
sleep 0.5
|
|
1051
|
+
i=$((i + 1))
|
|
1052
|
+
done
|
|
1053
|
+
for p in $pids; do
|
|
1054
|
+
if pf_alive "$p"; then kill -9 "$p" 2>/dev/null || true; fi
|
|
1055
|
+
done
|
|
1056
|
+
wait "$pid" 2>/dev/null || true
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
# pf_traps CLEANUP_FUNCTION: runs CLEANUP_FUNCTION on exit, and on INT or TERM runs it and exits 130 or 143,
|
|
1060
|
+
# so an interrupted run never carries on with its checks.
|
|
1061
|
+
pf_traps() {
|
|
1062
|
+
# The function name is expanded now, on purpose.
|
|
1063
|
+
# shellcheck disable=SC2064
|
|
1064
|
+
trap "$1" EXIT
|
|
1065
|
+
# shellcheck disable=SC2064
|
|
1066
|
+
trap "$1; trap - EXIT; exit 130" INT
|
|
1067
|
+
# shellcheck disable=SC2064
|
|
1068
|
+
trap "$1; trap - EXIT; exit 143" TERM
|
|
1069
|
+
}
|