11#!/usr/bin/env node
22/**
3- * Fails the build when the site's fallback release drifts from the release the
4- * package repository actually mirrors.
3+ * Fails the build when either of the site's fallback releases drifts from the
4+ * release its package channel actually mirrors.
55 *
66 * The /packages page used to hardcode its versions, and they went stale: the
77 * page still advertised 0.114-0 (and 0.113-0 for the repository examples) after
8- * v0.116-0 had been published and mirrored. The page now reads
9- * out/packages/ release-info.json at runtime, but it still needs a compiled-in
10- * fallback for the first paint and for the case where that fetch fails - so the
11- * fallback can drift in exactly the same way, just less visibly.
8+ * v0.116-0 had been published and mirrored. The page now reads each channel's
9+ * release feed at runtime, but it still needs a compiled-in fallback for the
10+ * first paint and for the case where that fetch fails - so the fallback can
11+ * drift in exactly the same way, just less visibly.
1212 *
13- * This check closes that loop: whenever the deployment mirrors a release, the
14- * fallback baked into the bundle must name the same one .
13+ * This check closes that loop: whenever the deployment mirrors the releases,
14+ * each fallback baked into the bundle must name its channel's release .
1515 *
16- * Skipped when release-info.json is absent , which is the normal case for a
17- * site-only build (BUILD_PACKAGES=false, or any fork without the packaging
18- * secrets). There is nothing to compare against then.
16+ * Skipped when neither feed exists , which is the normal case for a site-only
17+ * build (BUILD_PACKAGES=false, or any fork without the packaging secrets).
18+ * There is nothing to compare against then.
1919 */
2020
2121const fs = require ( "node:fs" ) ;
2222const path = require ( "node:path" ) ;
2323
24- const releaseInfoPath = path . join ( process . cwd ( ) , "out" , "packages" , "release-info.json" ) ;
25- const fallbackSourcePath = path . join ( process . cwd ( ) , "app" , "lib" , "releaseInfo.ts" ) ;
24+ // Mirrors releaseInfoPaths in app/lib/releaseInfo.ts.
25+ const channels = [
26+ { channel : "latest" , feed : path . join ( "out" , "packages" , "release-info-latest.json" ) } ,
27+ { channel : "stable" , feed : path . join ( "out" , "packages" , "release-info.json" ) } ,
28+ ] ;
29+ const fallbackSourcePath = path . join ( "app" , "lib" , "releaseInfo.ts" ) ;
2630
27- if ( ! fs . existsSync ( releaseInfoPath ) ) {
28- console . log ( `No ${ path . relative ( process . cwd ( ) , releaseInfoPath ) } ; skipping release drift check.` ) ;
31+ const present = channels . filter ( ( { feed } ) => fs . existsSync ( feed ) ) ;
32+ if ( present . length === 0 ) {
33+ console . log ( "No release feeds in out/packages; skipping release drift check." ) ;
2934 process . exit ( 0 ) ;
3035}
36+ // A deploy mirrors both channels, so a lone feed means the other channel was not built.
37+ if ( present . length !== channels . length ) {
38+ const missing = channels . filter ( ( entry ) => ! present . includes ( entry ) ) . map ( ( { feed } ) => feed ) ;
39+ console . error ( `Release drift: ${ missing . join ( ", " ) } is missing; cannot verify every fallback release.` ) ;
40+ process . exit ( 1 ) ;
41+ }
3142
3243const source = fs . readFileSync ( fallbackSourcePath , "utf8" ) ;
44+ const fallbacks = source . slice ( source . indexOf ( "export const FALLBACK_RELEASES" ) ) ;
3345
34- /** Reads a string field out of the FALLBACK_RELEASE object literal. */
35- function fallbackField ( field ) {
36- const match = new RegExp ( `${ field } :\\s*"([^"]+)"` ) . exec ( source ) ;
46+ /** Reads a string field out of FALLBACK_RELEASES[channel]. */
47+ function fallbackField ( channel , field ) {
48+ const entry = new RegExp ( `\\b${ channel } :\\s*\\{([^}]*)\\}` ) . exec ( fallbacks ) ;
49+ const match = entry && new RegExp ( `\\b${ field } :\\s*"([^"]+)"` ) . exec ( entry [ 1 ] ) ;
3750 return match ? match [ 1 ] : null ;
3851}
3952
40- const mirrored = JSON . parse ( fs . readFileSync ( releaseInfoPath , "utf8" ) ) ;
41- const mirroredTag = mirrored . tag_name ;
42- if ( typeof mirroredTag !== "string" || mirroredTag . length === 0 ) {
43- console . error ( "release-info.json has no tag_name; cannot verify the site's fallback release." ) ;
44- process . exit ( 1 ) ;
45- }
46-
47- const fallbackTag = fallbackField ( "tagName" ) ;
48- if ( fallbackTag !== mirroredTag ) {
49- console . error (
50- [
51- "Release drift: the site's fallback release does not match the mirrored release." ,
52- "" ,
53- ` mirrored (out/packages/release-info.json): ${ mirroredTag } ` ,
54- ` fallback (app/lib/releaseInfo.ts): ${ fallbackTag ?? "<not found>" } ` ,
55- "" ,
56- "Update FALLBACK_RELEASE in app/lib/releaseInfo.ts to the mirrored release." ,
57- "It is what visitors see before the release feed loads, and permanently if" ,
58- "that fetch fails." ,
59- ] . join ( "\n" ) ,
60- ) ;
61- process . exit ( 1 ) ;
62- }
63-
6453// The version strings are derived from real asset filenames, so a mismatch here
6554// means the fallback would render install commands for packages the release
6655// does not contain.
67- const assetNames = Array . isArray ( mirrored . assets )
68- ? mirrored . assets . map ( ( asset ) => asset && asset . name ) . filter ( ( name ) => typeof name === "string" )
69- : [ ] ;
70-
7156const derived = [
7257 {
7358 field : "aptVersion" ,
@@ -84,26 +69,60 @@ const derived = [
8469] ;
8570
8671let failed = false ;
87- for ( const { field, pattern } of derived ) {
88- const actual = assetNames . map ( ( name ) => pattern . exec ( name ) ) . find ( ( m ) => m && m [ 1 ] ) ;
89- if ( ! actual ) {
90- // The release simply does not ship that package shape; the fallback keeps
91- // whatever it had, which is not drift.
72+ for ( const { channel, feed } of channels ) {
73+ const mirrored = JSON . parse ( fs . readFileSync ( feed , "utf8" ) ) ;
74+ const mirroredTag = mirrored . tag_name ;
75+ if ( typeof mirroredTag !== "string" || mirroredTag . length === 0 ) {
76+ console . error ( `${ feed } has no tag_name; cannot verify the ${ channel } fallback release.` ) ;
77+ failed = true ;
9278 continue ;
9379 }
94- const expected = actual [ 1 ] ;
95- const declared = fallbackField ( field ) ;
96- if ( declared !== expected ) {
80+
81+ const fallbackTag = fallbackField ( channel , "tagName" ) ;
82+ if ( fallbackTag !== mirroredTag ) {
9783 console . error (
98- `Release drift: FALLBACK_RELEASE.${ field } is "${ declared } " but ${ mirroredTag } ships "${ expected } ".` ,
84+ [
85+ `Release drift: the site's ${ channel } fallback release does not match the mirrored release.` ,
86+ "" ,
87+ ` mirrored (${ feed } ): ${ mirroredTag } ` ,
88+ ` fallback (FALLBACK_RELEASES.${ channel } ): ${ fallbackTag ?? "<not found>" } ` ,
89+ "" ,
90+ ] . join ( "\n" ) ,
9991 ) ;
10092 failed = true ;
93+ continue ;
94+ }
95+
96+ const assetNames = Array . isArray ( mirrored . assets )
97+ ? mirrored . assets . map ( ( asset ) => asset && asset . name ) . filter ( ( name ) => typeof name === "string" )
98+ : [ ] ;
99+ let channelFailed = false ;
100+ for ( const { field, pattern } of derived ) {
101+ const actual = assetNames . map ( ( name ) => pattern . exec ( name ) ) . find ( ( m ) => m && m [ 1 ] ) ;
102+ if ( ! actual ) {
103+ // The release simply does not ship that package shape; the fallback keeps
104+ // whatever it had, which is not drift.
105+ continue ;
106+ }
107+ const expected = actual [ 1 ] ;
108+ const declared = fallbackField ( channel , field ) ;
109+ if ( declared !== expected ) {
110+ console . error (
111+ `Release drift: FALLBACK_RELEASES.${ channel } .${ field } is "${ declared } " but ${ mirroredTag } ships "${ expected } ".` ,
112+ ) ;
113+ channelFailed = true ;
114+ }
115+ }
116+ failed ||= channelFailed ;
117+ if ( ! channelFailed ) {
118+ console . log ( `The ${ channel } fallback matches the mirrored release (${ mirroredTag } ).` ) ;
101119 }
102120}
103121
104122if ( failed ) {
105- console . error ( "\nUpdate FALLBACK_RELEASE in app/lib/releaseInfo.ts." ) ;
123+ console . error (
124+ "\nUpdate FALLBACK_RELEASES in app/lib/releaseInfo.ts. It is what visitors see before the\n" +
125+ "release feeds load, and permanently if a fetch fails." ,
126+ ) ;
106127 process . exit ( 1 ) ;
107128}
108-
109- console . log ( `Release fallback matches the mirrored release (${ mirroredTag } ).` ) ;
0 commit comments