Skip to content

Commit 08d8816

Browse files
committed
Fix input string/array
1 parent 796638d commit 08d8816

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

action.yml

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ inputs:
1111
required: false
1212
default: "latest"
1313
browsers:
14-
description: 'Browsers to install as a JSON array (e.g., ["chrome","firefox"]) or "all". Allowed: chrome, chromium, firefox, webkit, msedge.'
14+
description: 'Browsers as a JSON array (["chrome","firefox"]), "all", or a single string like "chrome". Allowed: chrome, chromium, firefox, webkit, msedge.'
1515
required: false
16-
default: '["chrome"]'
16+
default: "chrome"
1717
browsers-path:
1818
description: "Optional custom directory to store browser binaries (sets PLAYWRIGHT_BROWSERS_PATH)."
1919
required: false
@@ -45,39 +45,52 @@ runs:
4545
node - <<'NODE'
4646
const fs = require('fs');
4747
const out = process.env.GITHUB_OUTPUT;
48-
const raw = process.env.INPUT_BROWSERS || '["chrome"]';
48+
const raw = (process.env.INPUT_BROWSERS ?? 'chrome').trim();
4949
const os = process.env.RUNNER_OS || '';
50-
let cli, json;
50+
const allowed = new Set(['chrome','chromium','firefox','webkit','msedge']);
5151
52-
if (raw.trim().toLowerCase() === 'all') {
53-
cli = 'all';
54-
json = '["chromium","firefox","webkit"]';
55-
} else {
56-
let arr;
57-
try { arr = JSON.parse(raw); } catch {
58-
console.error('Input "browsers" must be a JSON array like ["chrome","firefox"] or the string "all".');
59-
process.exit(1);
52+
// "all" shortcut
53+
if (raw.toLowerCase() === 'all') {
54+
fs.appendFileSync(out, 'cli=all\njson=["chromium","firefox","webkit"]\n');
55+
process.exit(0);
56+
}
57+
58+
// Try JSON first
59+
let arr;
60+
try {
61+
const parsed = JSON.parse(raw);
62+
if (Array.isArray(parsed)) {
63+
arr = parsed;
64+
} else if (typeof parsed === 'string') {
65+
// Allow a single string JSON, e.g. "chrome"
66+
arr = [parsed];
67+
} else {
68+
throw new Error();
6069
}
61-
if (!Array.isArray(arr) || arr.length === 0) {
62-
console.error('Input "browsers" must be a non-empty JSON array, or "all".');
70+
} catch {
71+
// Fall back to treating raw as a single browser token
72+
arr = [raw];
73+
}
74+
75+
const norm = [];
76+
for (const v of arr.map(x => String(x).trim().toLowerCase()).filter(Boolean)) {
77+
if (!allowed.has(v)) {
78+
console.error(`Invalid browser "${v}". Allowed: ${Array.from(allowed).join(', ')}, or "all".`);
6379
process.exit(1);
6480
}
65-
const allowed = new Set(['chrome','chromium','firefox','webkit','msedge']);
66-
const dedup = [];
67-
for (const v of arr.map(s => String(s).trim().toLowerCase()).filter(Boolean)) {
68-
if (!allowed.has(v)) {
69-
console.error(`Invalid browser "${v}". Allowed: ${Array.from(allowed).join(', ')}, or "all".`);
70-
process.exit(1);
71-
}
72-
if (v === 'msedge' && os !== 'Windows') {
73-
console.error(`"msedge" can only be installed on Windows runners (current: ${os}).`);
74-
process.exit(1);
75-
}
76-
if (!dedup.includes(v)) dedup.push(v);
81+
if (v === 'msedge' && os !== 'Windows') {
82+
console.error(`"msedge" can only be installed on Windows runners (current: ${os}).`);
83+
process.exit(1);
7784
}
78-
cli = dedup.join(' ');
79-
json = JSON.stringify(dedup);
85+
if (!norm.includes(v)) norm.push(v);
8086
}
87+
if (norm.length === 0) {
88+
console.error('At least one browser must be specified.');
89+
process.exit(1);
90+
}
91+
92+
const cli = norm.join(' ');
93+
const json = JSON.stringify(norm);
8194
fs.appendFileSync(out, `cli=${cli}\njson=${json}\n`);
8295
NODE
8396
@@ -91,9 +104,11 @@ runs:
91104
set -euo pipefail
92105
v=$(printf '%s' "$RAW_WITH_DEPS" | tr '[:upper:]' '[:lower:]' | xargs)
93106
[ "$v" = "true" ] || [ "$v" = "false" ] || [ "$v" = "auto" ] || { echo "::error::with-deps must be true, false, or auto"; exit 1; }
94-
[ "$v" = "true" ] && echo "with_deps=--with-deps" >> "$GITHUB_OUTPUT" && exit 0
95-
[ "$v" = "auto" ] && [ "$RUNNER_OS" = "Linux" ] && echo "with_deps=--with-deps" >> "$GITHUB_OUTPUT" && exit 0
96-
echo "with_deps=" >> "$GITHUB_OUTPUT"
107+
if [ "$v" = "true" ] || { [ "$v" = "auto" ] && [ "$RUNNER_OS" = "Linux" ]; }; then
108+
echo "with_deps=--with-deps" >> "$GITHUB_OUTPUT"
109+
else
110+
echo "with_deps=" >> "$GITHUB_OUTPUT"
111+
fi
97112
98113
- name: Configure browsers path (optional)
99114
if: ${{ inputs.browsers-path != '' }}
@@ -106,7 +121,11 @@ runs:
106121
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: "1"
107122
run: |
108123
set -euo pipefail
109-
PKG="playwright@${{ inputs.playwright-version == 'latest' && 'latest' || inputs.playwright-version }}"
124+
if [ "${{ inputs.playwright-version }}" = "latest" ]; then
125+
PKG="playwright@latest"
126+
else
127+
PKG="playwright@${{ inputs.playwright-version }}"
128+
fi
110129
echo "Installing $PKG globally (skip browser download)..."
111130
npm i -g --no-audit --no-fund "$PKG" >/dev/null
112131
npm bin -g

0 commit comments

Comments
 (0)