@@ -44,6 +44,13 @@ import { fileURLToPath } from 'node:url';
4444
4545const ROOT = resolve ( dirname ( fileURLToPath ( import . meta. url ) ) , '..' ) ;
4646const DRY_RUN = process . argv . includes ( '--dry-run' ) ;
47+ // --revoke-and-recreate <pkg> — delete all existing trust entries for ONE
48+ // package and add a fresh one. Use this when a pre-existing trust entry
49+ // may have hidden constraints (e.g. an `environment` field) that cause the
50+ // OIDC exchange to fail in CI.
51+ // Example: node scripts/setup-npm-trust.mjs --revoke-and-recreate @cfxdevkit/compiler
52+ const REVOKE_IDX = process . argv . indexOf ( '--revoke-and-recreate' ) ;
53+ const REVOKE_PKG = REVOKE_IDX !== - 1 ? process . argv [ REVOKE_IDX + 1 ] : null ;
4754
4855// ── Config ────────────────────────────────────────────────────────────────────
4956const GITHUB_REPO = 'cfxdevkit/devkit' ; // owner/repo
@@ -109,6 +116,47 @@ function packageExists(name) {
109116 }
110117}
111118
119+ // ── --revoke-and-recreate: delete old entry then add fresh one ────────────────
120+
121+ if ( REVOKE_PKG ) {
122+ console . log ( `\n=== Revoke & recreate trust for ${ REVOKE_PKG } ===\n` ) ;
123+
124+ // Get existing trust IDs from text output (one block per entry, "id: <uuid>")
125+ let trustText = '' ;
126+ try {
127+ trustText = execSync ( `npm trust list "${ REVOKE_PKG } "` , { stdio : 'pipe' , encoding : 'utf8' } ) ;
128+ } catch { /* no entries */ }
129+
130+ const ids = [ ...trustText . matchAll ( / ^ i d : \s * ( .+ ) $ / gm) ] . map ( m => m [ 1 ] . trim ( ) ) ;
131+
132+ if ( ids . length === 0 ) {
133+ console . log ( ` No existing trust entries found for ${ REVOKE_PKG } — nothing to revoke.` ) ;
134+ } else {
135+ for ( const id of ids ) {
136+ console . log ( ` − Revoking entry ${ id } …` ) ;
137+ console . log ( `\n npm will open a 2FA URL — open it in your browser to confirm.\n` ) ;
138+ const code = runInteractive ( `npm trust revoke "${ REVOKE_PKG } " "${ id } " --yes` ) ;
139+ if ( code !== 0 ) {
140+ console . error ( ` ✗ Revoke failed (exit ${ code } ). Aborting.` ) ;
141+ process . exit ( 1 ) ;
142+ }
143+ console . log ( ` revoked ✓` ) ;
144+ }
145+ }
146+
147+ console . log ( `\n + Adding fresh trust for ${ REVOKE_PKG } …` ) ;
148+ console . log ( `\n npm will open a 2FA URL — open it in your browser to confirm.\n` ) ;
149+ const addCode = runInteractive (
150+ `npm trust github "${ REVOKE_PKG } " --repository ${ GITHUB_REPO } --file ${ WORKFLOW_FILE } --yes` ,
151+ ) ;
152+ if ( addCode !== 0 ) {
153+ console . error ( ` ✗ Trust add failed (exit ${ addCode } ).` ) ;
154+ process . exit ( 1 ) ;
155+ }
156+ console . log ( ` ✓ Trust recreated for ${ REVOKE_PKG } \n` ) ;
157+ process . exit ( 0 ) ;
158+ }
159+
112160// ── Step 1: Initial publish for packages that don't exist yet ─────────────────
113161
114162console . log ( '\n=== Step 1: Initial publish (new packages only) ===\n' ) ;
0 commit comments