winzik/tools/chrome.sh

131 lines
6.3 KiB
Shell
Executable file

#!/usr/bin/env bash
# Chrome extensions (Manifest V3) for the JumPHP IDE and the command line, on ANY folder (no JumPHP needed in it):
# bin/jumphp chrome:new <folder> ["Name"] a new extension from skeleton-chrome/ (popup, options, service worker,
# content script, _locales en + fr, icons, tests), git init
# bin/jumphp chrome:check [folder] [--json] manifest.json and its files checked (MV3 rules, files, icons, permissions,
# translations); report in var/chrome-check.json; exit 1 on errors
# bin/jumphp chrome:run [folder] [url] a headless Chrome with the extension loaded unpacked: its id, the console
# errors, screenshots var/screenshots/page.png, options.png, popup.png
# bin/jumphp chrome:test [folder] the extension's tests (tests/*.test.cjs: Node's test runner + Puppeteer)
# bin/jumphp chrome:pack [folder] dist/<name>-<version>.zip for the Chrome Web Store (check first, dev files out)
# bin/jumphp chrome:bump [folder] [patch|minor|major|x.y.z] the version in manifest.json
# bin/jumphp chrome:icons [folder] [#background] [#highlight] icons/icon-16/32/48/128.png drawn again (scripts/icons.cjs)
# bin/jumphp chrome:lint [folder] ESLint (eslint.config.mjs of the project; npm downloads it once)
# Everything runs in Docker, in Puppeteer's image (Node + Chrome for Testing, pinned), as the user: nothing on the host.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
IMAGE="${JUMPHP_CHROME_IMAGE:-${JUMPHP_PUPPETEER_IMAGE:-ghcr.io/puppeteer/puppeteer:24.10.0}}"
ESLINT_VERSION="10.15.0"
say() { printf '\033[1;36m==> %s\033[0m\n' "$*"; }
die() { printf '\033[1;31m[x] %s\033[0m\n' "$*" >&2; exit 1; }
command -v docker >/dev/null || die "Docker is needed (https://docs.docker.com/get-docker/)"
[ -n "${MSYSTEM:-}" ] && export MSYS_NO_PATHCONV=1
host_path() { if [ -n "${MSYSTEM:-}" ] && command -v cygpath >/dev/null 2>&1; then cygpath -m "$1"; else printf '%s' "$1"; fi; }
dir_of() { local d="${1:-$PWD}"; [ -d "$d" ] || die "no folder $d"; (cd "$d" && pwd); }
ext_dir() {
local d; d="$(dir_of "${1:-}")"
[ -f "$d/manifest.json" ] || die "$d has no manifest.json: not a Chrome extension (bin/jumphp chrome:new <folder> makes one)"
printf '%s' "$d"
}
ensure_image() {
docker image inspect "$IMAGE" >/dev/null 2>&1 && return
say "getting $IMAGE (Node + Chrome for Testing, once: ~1 GB)"
docker pull "$IMAGE" >&2
}
# node <args...> in the image, the extension folder at /ext (writable), the tools at /tools
node_in() {
local dir="$1"; shift
ensure_image
local tty=(); [ -t 0 ] && [ -t 1 ] && tty=(-t)
docker run --rm --init "${tty[@]}" --user "$(id -u):$(id -g)" -e HOME=/tmp \
-e PUPPETEER_CACHE_DIR=/home/pptruser/.cache/puppeteer -e NODE_PATH=/home/pptruser/node_modules \
-v "$(host_path "$dir"):/ext" -v "$(host_path "$ROOT/tools/chrome"):/tools:ro" -w /ext "$IMAGE" "$@"
}
cmd_new() {
local target="${1:-}" name="${2:-}"
[ -n "$target" ] || die "usage: bin/jumphp chrome:new <folder> [\"Name\"]"
if [ -e "$target" ] && [ -n "$(ls -A "$target" 2>/dev/null)" ]; then die "$target exists and is not empty"; fi
mkdir -p "$target"
target="$(cd "$target" && pwd)"
if [ -z "$name" ]; then
name="$(basename "$target" | sed 's/[-_]\+/ /g; s/\b\(.\)/\u\1/g')"
fi
ensure_image
docker run --rm --user "$(id -u):$(id -g)" -e HOME=/tmp -v "$(host_path "$ROOT/skeleton-chrome"):/skeleton:ro" \
-v "$(host_path "$target"):/target" -v "$(host_path "$ROOT/tools/chrome"):/tools:ro" "$IMAGE" node /tools/new.cjs /skeleton /target "$name"
if command -v git >/dev/null 2>&1 && [ ! -d "$target/.git" ]; then
git -C "$target" init -q -b main 2>/dev/null || git -C "$target" init -q
git -C "$target" add -A
fi
say "done: $target"
echo " bin/jumphp chrome:check $target its manifest and files"
echo " bin/jumphp chrome:run $target loaded in a headless Chrome, screenshots in var/screenshots/"
echo " bin/jumphp chrome:test $target its tests"
echo " Chrome: chrome://extensions > Developer mode > Load unpacked > $target"
}
cmd_check() {
local dir json=()
for a in "$@"; do [ "$a" = --json ] && json=(--json); done
dir="$(ext_dir "$(printf '%s\n' "$@" | grep -v '^--' | head -1)")"
node_in "$dir" node /tools/check.cjs /ext "${json[@]}"
}
cmd_run() {
local dir; dir="$(ext_dir "${1:-}")"
local status=0
node_in "$dir" node /tools/run.cjs /ext "${2:-}" 2>&1 | grep -vE 'GPU stall|Automatic fallback|dbus' || status=1
for f in page options popup; do [ -f "$dir/var/screenshots/$f.png" ] && echo "$dir/var/screenshots/$f.png"; done
return $status
}
cmd_test() {
local dir; dir="$(ext_dir "${1:-}")"
ls "$dir"/tests/*.test.* >/dev/null 2>&1 || die "no test in $dir/tests/ (tests/*.test.cjs)"
echo "Chrome extension tests"
node_in "$dir" sh -c 'node --test --test-reporter=spec tests/*.test.* 2>&1'
}
cmd_pack() {
local dir; dir="$(ext_dir "${1:-}")"
node_in "$dir" node /tools/pack.cjs /ext
}
cmd_bump() {
local dir; dir="$(ext_dir "${1:-}")"
node_in "$dir" node /tools/bump.cjs /ext "${2:-patch}"
}
cmd_icons() {
local dir; dir="$(ext_dir "${1:-}")"
[ -f "$dir/scripts/icons.cjs" ] || cp "$ROOT/skeleton-chrome/scripts/icons.cjs" "$dir/scripts/icons.cjs" 2>/dev/null \
|| { mkdir -p "$dir/scripts" && cp "$ROOT/skeleton-chrome/scripts/icons.cjs" "$dir/scripts/icons.cjs"; }
node_in "$dir" node scripts/icons.cjs "${2:-}" "${3:-}"
}
cmd_lint() {
local dir; dir="$(ext_dir "${1:-}")"
[ -f "$dir/eslint.config.mjs" ] || [ -f "$dir/eslint.config.js" ] || die "no eslint.config.mjs in $dir"
local cache="${XDG_CACHE_HOME:-$HOME/.cache}/jumphp/chrome-npm"
mkdir -p "$cache"
ensure_image
docker run --rm --init --user "$(id -u):$(id -g)" -e HOME=/tmp -e npm_config_cache=/npm -e npm_config_update_notifier=false \
-v "$(host_path "$cache"):/npm" -v "$(host_path "$dir"):/ext" -w /ext "$IMAGE" npx --yes "eslint@$ESLINT_VERSION" .
}
sub="${1:-help}"; shift || true
case "$sub" in
new) cmd_new "$@" ;;
check) cmd_check "$@" ;;
run) cmd_run "$@" ;;
test) cmd_test "$@" ;;
pack) cmd_pack "$@" ;;
bump) cmd_bump "$@" ;;
icons) cmd_icons "$@" ;;
lint) cmd_lint "$@" ;;
*) sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//'; [ "$sub" = help ] || exit 1 ;;
esac