Skip to content

Commit ad41d7d

Browse files
authored
chore: improve consumer scripts (#19)
1 parent 4d76aca commit ad41d7d

5 files changed

Lines changed: 56 additions & 29 deletions

File tree

docs/_pages/consume.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ <h2 id="step-1-npmrc">Step 1: Point npm at GitHub Packages</h2>
8282
<p>Create a project-local <code>.npmrc</code> beside the <code>package.json</code> where you want to consume the skill. The same file works in local dev and GitHub Actions; the only thing that changes is where <code>NODE_AUTH_TOKEN</code> comes from.</p>
8383

8484
<pre><code class="language-ini">@bcgov:registry=https://npm.pkg.github.com
85-
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}
86-
always-auth=true</code></pre>
85+
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}</code></pre>
8786

8887
<p><code>${NODE_AUTH_TOKEN}</code> is expanded by npm at install time from the environment variable; the literal string lives in the file, so it's safe to commit.</p>
8988

@@ -262,8 +261,7 @@ <h2 id="troubleshooting">Troubleshooting</h2>
262261
<div class="accordion-content">
263262
<p>Make sure the scope and the auth line both point at GitHub Packages; they must both be present in <code>.npmrc</code>:</p>
264263
<pre><code>@bcgov:registry=https://npm.pkg.github.com
265-
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}
266-
always-auth=true</code></pre>
264+
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}</code></pre>
267265
<p>If you also have a global <code>~/.npmrc</code> with a different <code>registry=</code>, the scoped registry line wins, but the auth line must match the URL of whatever registry actually serves the install.</p>
268266
</div>
269267
</details>

docs/assets/search-index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/consume.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,7 @@ <h2 id="step-1-npmrc">Step 1: Point npm at GitHub Packages</h2>
11171117
<p>Create a project-local <code>.npmrc</code> beside the <code>package.json</code> where you want to consume the skill. The same file works in local dev and GitHub Actions; the only thing that changes is where <code>NODE_AUTH_TOKEN</code> comes from.</p>
11181118

11191119
<pre><code class="language-ini">@bcgov:registry=https://npm.pkg.github.com
1120-
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}
1121-
always-auth=true</code></pre>
1120+
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}</code></pre>
11221121

11231122
<p><code>${NODE_AUTH_TOKEN}</code> is expanded by npm at install time from the environment variable; the literal string lives in the file, so it's safe to commit.</p>
11241123

@@ -1297,8 +1296,7 @@ <h2 id="troubleshooting">Troubleshooting</h2>
12971296
<div class="accordion-content">
12981297
<p>Make sure the scope and the auth line both point at GitHub Packages; they must both be present in <code>.npmrc</code>:</p>
12991298
<pre><code>@bcgov:registry=https://npm.pkg.github.com
1300-
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}
1301-
always-auth=true</code></pre>
1299+
//npm.pkg.github.com/:_authToken=${NODE_AUTH_TOKEN}</code></pre>
13021300
<p>If you also have a global <code>~/.npmrc</code> with a different <code>registry=</code>, the scoped registry line wins, but the auth line must match the URL of whatever registry actually serves the install.</p>
13031301
</div>
13041302
</details>

scripts/install-skill.ps1

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ function Confirm-Npmrc {
162162
# ----- step 5: package.json bootstrap -----
163163
function Confirm-PackageJson {
164164
if (Test-Path 'package.json') { return }
165+
if ($script:NonInteractive) {
166+
Write-Info "No package.json here; running 'npm init -y' (non-interactive)"
167+
npm init -y | Out-Null
168+
Write-Ok 'Created package.json'
169+
return
170+
}
165171
if (Confirm-YesNo "No package.json here. Run 'npm init -y' first?" 'y') {
166172
npm init -y | Out-Null
167173
Write-Ok 'Created package.json'
@@ -350,22 +356,27 @@ Confirm-Npm
350356
Confirm-Gh
351357
Confirm-GhAuth
352358
Confirm-Npmrc
353-
Confirm-PackageJson
354359

355-
# Capture original parameter binding BEFORE Select-Skill / Read-WithDefault
356-
# might mutate $Skill -- used to decide whether to run interactive wire-up.
360+
# Capture original parameter binding BEFORE any function mutates $Skill /
361+
# $Version. When -Skill was supplied the caller wants hands-off scripted
362+
# behavior; ditto when running in a non-interactive host. $NonInteractive
363+
# silences the npm-init / version / wire-up prompts and accepts sensible defaults.
357364
$SkillProvided = $PSBoundParameters.ContainsKey('Skill')
365+
$VersionProvided = $PSBoundParameters.ContainsKey('Version')
366+
$script:NonInteractive = $SkillProvided -or -not [Environment]::UserInteractive -or [Console]::IsInputRedirected
367+
368+
Confirm-PackageJson
358369

359370
$Skill = Select-Skill $Skill
360-
if (-not $PSBoundParameters.ContainsKey('Version')) {
371+
if (-not $VersionProvided -and -not $script:NonInteractive) {
361372
$Version = Read-WithDefault 'Version (blank for latest)' ''
362373
}
363374

364375
Install-Skill $Skill $Version
365376

366-
# Skip the interactive wire-up step when the user is scripting us (-Skill
367-
# supplied) or when running in a non-interactive host.
368-
if (-not $SkillProvided -and [Environment]::UserInteractive -and -not [Console]::IsInputRedirected) {
377+
# Skip the interactive wire-up step in non-interactive mode (scripted
378+
# invocation or no TTY); reuse the same gate as the prompts above for consistency.
379+
if (-not $script:NonInteractive) {
369380
Invoke-WireUpAgent $Skill
370381
}
371382

scripts/install-skill.sh

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,14 @@ ensure_gh() {
132132
sudo apt-get update
133133
sudo apt-get install -y gh
134134
elif have dnf; then
135-
sudo dnf install -y 'dnf-command(config-manager)'
136-
sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
135+
# Write the .repo file directly so this works on both dnf4 (RHEL 9,
136+
# older Fedora) and dnf5 (Fedora 41+, RHEL 10+). dnf5 removed the
137+
# `config-manager --add-repo` flag; the file-drop form is supported
138+
# on every dnf version and matches the official gh install docs.
139+
info "Detected dnf. Adding GitHub CLI repository (will prompt for sudo)..."
140+
(type -p curl >/dev/null) || sudo dnf install -y curl
141+
sudo curl -fsSL https://cli.github.com/packages/rpm/gh-cli.repo \
142+
-o /etc/yum.repos.d/gh-cli.repo
137143
sudo dnf install -y gh
138144
elif have pacman; then
139145
sudo pacman -S --noconfirm github-cli
@@ -195,6 +201,12 @@ ensure_npmrc() {
195201
# ----- step 5: package.json bootstrap -----
196202
ensure_package_json() {
197203
if [ -f package.json ]; then return; fi
204+
if [ "${NON_INTERACTIVE:-0}" = "1" ]; then
205+
info "No package.json here; running 'npm init -y' (non-interactive)"
206+
npm init -y >/dev/null
207+
ok "Created package.json"
208+
return
209+
fi
198210
if confirm "No package.json here. Run 'npm init -y' first?" "y"; then
199211
npm init -y >/dev/null
200212
ok "Created package.json"
@@ -365,28 +377,36 @@ printf '%sbcgov/agent-skills installer%s\n' "$C_BOLD" "$C_RESET"
365377
printf '%sInstalls a skill from GitHub Packages with auth + registry setup in one pass.%s\n' "$C_DIM" "$C_RESET"
366378
echo
367379

380+
# Capture original positional args BEFORE any function might mutate context.
381+
# When the caller supplied a positional skill arg they want hands-off scripted
382+
# behavior; ditto when there's no controlling terminal (plain CI). NON_INTERACTIVE=1
383+
# silences the npm-init / version / wire-up prompts and accepts sensible defaults.
384+
SKILL_ARG="${1:-}"
385+
VERSION_ARG="${2:-}"
386+
if [ -n "$SKILL_ARG" ] || { [ ! -r /dev/tty ] && [ ! -t 0 ]; }; then
387+
NON_INTERACTIVE=1
388+
else
389+
NON_INTERACTIVE=0
390+
fi
391+
export NON_INTERACTIVE
392+
368393
require_npm
369394
ensure_gh
370395
ensure_gh_auth
371396
ensure_npmrc
372397
ensure_package_json
373398

374-
# Capture original positional args BEFORE choose_skill might mutate context --
375-
# we use this to decide whether to run the interactive wire-up step.
376-
SKILL_ARG="${1:-}"
377-
VERSION_ARG="${2:-}"
378-
379-
SKILL="$(choose_skill "${1:-}")"
380-
VERSION="${2:-}"
381-
if [ -z "$VERSION" ] && [ -z "${2-}" ]; then
399+
SKILL="$(choose_skill "$SKILL_ARG")"
400+
VERSION="$VERSION_ARG"
401+
if [ -z "$VERSION" ] && [ "$NON_INTERACTIVE" != "1" ]; then
382402
VERSION="$(prompt_default "Version (blank for latest)" "")"
383403
fi
384404

385405
install_skill "$SKILL" "$VERSION"
386406

387-
# Skip the interactive wire-up step when the user is scripting us (positional
388-
# skill arg supplied) or when there's no controlling terminal (plain CI).
389-
if [ -z "$SKILL_ARG" ] && { [ -r /dev/tty ] || [ -t 0 ]; }; then
407+
# Skip the interactive wire-up step in non-interactive mode (scripted invocation
408+
# or no TTY); reuse the same gate as the prompts above for consistency.
409+
if [ "$NON_INTERACTIVE" != "1" ]; then
390410
wire_up_agent
391411
fi
392412

0 commit comments

Comments
 (0)