@@ -9,7 +9,7 @@ import type {
99} from '@mongodb-js/dl-center/dist/download-center-config' ;
1010import {
1111 ARTIFACTS_BUCKET ,
12- ARTIFACTS_FOLDER ,
12+ JSON_FEED_ARTIFACT_KEY ,
1313 ARTIFACTS_URL_PUBLIC_BASE ,
1414 CONFIGURATION_KEY ,
1515 CONFIGURATIONS_BUCKET ,
@@ -32,6 +32,24 @@ import path from 'path';
3232import semver from 'semver' ;
3333import { hashListFiles } from '../run-download-and-list-artifacts' ;
3434
35+ async function getCurrentJsonFeed (
36+ dlcenterArtifacts : DownloadCenterCls
37+ ) : Promise < JsonFeed | undefined > {
38+ let existingJsonFeedText ;
39+ try {
40+ existingJsonFeedText = await dlcenterArtifacts . downloadAsset (
41+ JSON_FEED_ARTIFACT_KEY
42+ ) ;
43+ } catch ( err : any ) {
44+ console . warn ( 'Failed to get existing JSON feed text' , err ) ;
45+ if ( err ?. code !== 'NoSuchKey' ) throw err ;
46+ }
47+
48+ return existingJsonFeedText
49+ ? JSON . parse ( existingJsonFeedText . toString ( ) )
50+ : undefined ;
51+ }
52+
3553export async function createAndPublishDownloadCenterConfig (
3654 outputDir : string ,
3755 packageInformation : PackageInformationProvider ,
@@ -80,20 +98,8 @@ export async function createAndPublishDownloadCenterConfig(
8098 accessKeyId : awsAccessKeyId ,
8199 secretAccessKey : awsSecretAccessKey ,
82100 } ) ;
83- const jsonFeedArtifactkey = `${ ARTIFACTS_FOLDER } /mongosh.json` ;
84- let existingJsonFeedText ;
85- try {
86- existingJsonFeedText = await dlcenterArtifacts . downloadAsset (
87- jsonFeedArtifactkey
88- ) ;
89- } catch ( err : any ) {
90- console . warn ( 'Failed to get existing JSON feed text' , err ) ;
91- if ( err ?. code !== 'NoSuchKey' ) throw err ;
92- }
93101
94- const existingJsonFeed : JsonFeed | undefined = existingJsonFeedText
95- ? JSON . parse ( existingJsonFeedText . toString ( ) )
96- : undefined ;
102+ const existingJsonFeed = await getCurrentJsonFeed ( dlcenterArtifacts ) ;
97103 const injectedJsonFeed : JsonFeed | undefined = injectedJsonFeedFile
98104 ? JSON . parse ( await fs . readFile ( injectedJsonFeedFile , 'utf8' ) )
99105 : undefined ;
@@ -122,12 +128,42 @@ export async function createAndPublishDownloadCenterConfig(
122128 await Promise . all ( [
123129 dlcenter . uploadConfig ( CONFIGURATION_KEY , config ) ,
124130 dlcenterArtifacts . uploadAsset (
125- jsonFeedArtifactkey ,
131+ JSON_FEED_ARTIFACT_KEY ,
126132 JSON . stringify ( newJsonFeed , null , 2 )
127133 ) ,
128134 ] ) ;
129135}
130136
137+ export async function updateJsonFeedCTA (
138+ config : UpdateCTAConfig ,
139+ DownloadCenter : typeof DownloadCenterCls = DownloadCenterCls
140+ ) {
141+ const dlcenterArtifacts = new DownloadCenter ( {
142+ bucket : ARTIFACTS_BUCKET ,
143+ accessKeyId : config . awsAccessKeyId ,
144+ secretAccessKey : config . awsSecretAccessKey ,
145+ } ) ;
146+
147+ const jsonFeed = await getCurrentJsonFeed ( dlcenterArtifacts ) ;
148+ if ( ! jsonFeed ) {
149+ throw new Error ( 'No existing JSON feed found' ) ;
150+ }
151+
152+ jsonFeed . cta = config . ctas [ '*' ] ;
153+ for ( const version of jsonFeed . versions ) {
154+ version . cta = config . ctas [ version . version ] ;
155+ }
156+
157+ const patchedJsonFeed = JSON . stringify ( jsonFeed , null , 2 ) ;
158+ if ( config . isDryRun ) {
159+ console . warn ( 'Not uploading JSON feed in dry-run mode' ) ;
160+ console . warn ( `Patched JSON feed: ${ patchedJsonFeed } ` ) ;
161+ return ;
162+ }
163+
164+ await dlcenterArtifacts . uploadAsset ( JSON_FEED_ARTIFACT_KEY , patchedJsonFeed ) ;
165+ }
166+
131167export function getUpdatedDownloadCenterConfig (
132168 downloadedConfig : DownloadCenterConfig ,
133169 getVersionConfig : ( ) => ReturnType < typeof createVersionConfig >
@@ -201,13 +237,32 @@ export function createVersionConfig(
201237 } ;
202238}
203239
240+ // TODO: this is duplicated in update-notification-manager.ts
241+ interface GreetingCTADetails {
242+ chunks : {
243+ text : string ;
244+ style : string ; // TODO: this is actually clr.ts/StyleDefinition
245+ } [ ] ;
246+ }
247+
248+ export interface UpdateCTAConfig {
249+ ctas : {
250+ [ version : string ] : GreetingCTADetails ;
251+ } ;
252+ awsAccessKeyId : string ;
253+ awsSecretAccessKey : string ;
254+ isDryRun : boolean ;
255+ }
256+
204257interface JsonFeed {
205258 versions : JsonFeedVersionEntry [ ] ;
259+ cta ?: GreetingCTADetails ;
206260}
207261
208262interface JsonFeedVersionEntry {
209263 version : string ;
210264 downloads : JsonFeedDownloadEntry [ ] ;
265+ cta ?: GreetingCTADetails ;
211266}
212267
213268interface JsonFeedDownloadEntry {
@@ -275,6 +330,8 @@ function mergeFeeds(...args: (JsonFeed | undefined)[]): JsonFeed {
275330 if ( index === - 1 ) newFeed . versions . unshift ( version ) ;
276331 else newFeed . versions . splice ( index , 1 , version ) ;
277332 }
333+
334+ newFeed . cta = feed ?. cta ?? newFeed . cta ;
278335 }
279336 newFeed . versions . sort ( ( a , b ) => semver . rcompare ( a . version , b . version ) ) ;
280337 return newFeed ;
0 commit comments