@@ -4,6 +4,7 @@ import type { SdkHttpOptions } from '../aws-auth';
44import type { Context } from '../context' ;
55import type { IIoHost } from '../io' ;
66import { CachedDataSource } from './cached-data-source' ;
7+ import type { FilteredNotice } from './filter' ;
78import { NoticesFilter } from './filter' ;
89import type { BootstrappedEnvironment , Notice , NoticeDataSource } from './types' ;
910import { WebsiteNoticeDataSource } from './web-data-source' ;
@@ -18,13 +19,6 @@ export interface NoticesProps {
1819 */
1920 readonly context : Context ;
2021
21- /**
22- * Include notices that have already been acknowledged.
23- *
24- * @default false
25- */
26- readonly includeAcknowledged ?: boolean ;
27-
2822 /**
2923 * Global CLI option for output directory for synthesized cloud assembly
3024 *
@@ -48,8 +42,16 @@ export interface NoticesProps {
4842 readonly cliVersion : string ;
4943}
5044
51- export interface NoticesPrintOptions {
45+ export interface NoticesFilterOptions {
46+ /**
47+ * Include notices that have already been acknowledged.
48+ *
49+ * @default false
50+ */
51+ readonly includeAcknowledged ?: boolean ;
52+ }
5253
54+ export interface NoticesDisplayOptions extends NoticesFilterOptions {
5355 /**
5456 * Whether to append the total number of unacknowledged notices to the display.
5557 *
@@ -98,7 +100,6 @@ export class Notices {
98100 private readonly context : Context ;
99101 private readonly output : string ;
100102 private readonly acknowledgedIssueNumbers : Set < Number > ;
101- private readonly includeAcknowlegded : boolean ;
102103 private readonly httpOptions : SdkHttpOptions ;
103104 private readonly ioHelper : IoHelper ;
104105 private readonly ioMessages : IoDefaultMessages ;
@@ -112,7 +113,6 @@ export class Notices {
112113 private constructor ( props : NoticesProps ) {
113114 this . context = props . context ;
114115 this . acknowledgedIssueNumbers = new Set ( this . context . get ( 'acknowledged-issue-numbers' ) ?? [ ] ) ;
115- this . includeAcknowlegded = props . includeAcknowledged ?? false ;
116116 this . output = props . output ?? 'cdk.out' ;
117117 this . httpOptions = props . httpOptions ?? { } ;
118118 this . ioHelper = asIoHelper ( props . ioHost , 'notices' as any /* forcing a CliAction to a ToolkitAction */ ) ;
@@ -136,35 +136,39 @@ export class Notices {
136136
137137 /**
138138 * Refresh the list of notices this instance is aware of.
139- * To make sure this never crashes the CLI process, all failures are caught and
140- * silently logged.
141139 *
142- * If context is configured to not display notices, this will no-op.
140+ * This method throws an error if the data source fails to fetch notices.
141+ * When using, consider if execution should halt immdiately or if catching the rror and continuing is more appropriate
142+ *
143+ * @throws on failure to refresh the data source
143144 */
144145 public async refresh ( options : NoticesRefreshOptions = { } ) {
145- try {
146- const underlyingDataSource = options . dataSource ?? new WebsiteNoticeDataSource ( this . ioHelper , this . httpOptions ) ;
147- const dataSource = new CachedDataSource ( this . ioMessages , CACHE_FILE_PATH , underlyingDataSource , options . force ?? false ) ;
148- const notices = await dataSource . fetch ( ) ;
149- this . data = new Set ( this . includeAcknowlegded ? notices : notices . filter ( n => ! this . acknowledgedIssueNumbers . has ( n . issueNumber ) ) ) ;
150- } catch ( e : any ) {
151- this . ioMessages . debug ( `Could not refresh notices: ${ e } ` ) ;
152- }
146+ const innerDataSource = options . dataSource ?? new WebsiteNoticeDataSource ( this . ioHelper , this . httpOptions ) ;
147+ const dataSource = new CachedDataSource ( this . ioMessages , CACHE_FILE_PATH , innerDataSource , options . force ?? false ) ;
148+ const notices = await dataSource . fetch ( ) ;
149+ this . data = new Set ( notices ) ;
153150 }
154151
155152 /**
156- * Display the relevant notices (unless context dictates we shouldn't).
153+ * Filter the data sourece for relevant notices
157154 */
158- public display ( options : NoticesPrintOptions = { } ) {
159- const filteredNotices = new NoticesFilter ( this . ioMessages ) . filter ( {
160- data : Array . from ( this . data ) ,
155+ public filter ( options : NoticesDisplayOptions = { } ) : FilteredNotice [ ] {
156+ return new NoticesFilter ( this . ioMessages ) . filter ( {
157+ data : this . noticesFromData ( options . includeAcknowledged ?? false ) ,
161158 cliVersion : this . cliVersion ,
162159 outDir : this . output ,
163160 bootstrappedEnvironments : Array . from ( this . bootstrappedEnvironments . values ( ) ) ,
164161 } ) ;
162+ }
163+
164+ /**
165+ * Display the relevant notices (unless context dictates we shouldn't).
166+ */
167+ public async display ( options : NoticesDisplayOptions = { } ) : Promise < void > {
168+ const filteredNotices = this . filter ( options ) ;
165169
166170 if ( filteredNotices . length > 0 ) {
167- void this . ioMessages . notify ( IO . CDK_TOOLKIT_I0100 . msg ( [
171+ await this . ioHelper . notify ( IO . CDK_TOOLKIT_I0100 . msg ( [
168172 '' ,
169173 'NOTICES (What\'s this? https://github.com/aws/aws-cdk/wiki/CLI-Notices)' ,
170174 '' ,
@@ -173,25 +177,41 @@ export class Notices {
173177 const formatted = filtered . format ( ) + '\n' ;
174178 switch ( filtered . notice . severity ) {
175179 case 'warning' :
176- void this . ioMessages . notify ( IO . CDK_TOOLKIT_W0101 . msg ( formatted ) ) ;
180+ await this . ioHelper . notify ( IO . CDK_TOOLKIT_W0101 . msg ( formatted ) ) ;
177181 break ;
178182 case 'error' :
179- void this . ioMessages . notify ( IO . CDK_TOOLKIT_E0101 . msg ( formatted ) ) ;
183+ await this . ioHelper . notify ( IO . CDK_TOOLKIT_E0101 . msg ( formatted ) ) ;
180184 break ;
181185 default :
182- void this . ioMessages . notify ( IO . CDK_TOOLKIT_I0101 . msg ( formatted ) ) ;
186+ await this . ioHelper . notify ( IO . CDK_TOOLKIT_I0101 . msg ( formatted ) ) ;
183187 break ;
184188 }
185189 }
186- void this . ioMessages . notify ( IO . CDK_TOOLKIT_I0100 . msg (
190+ await this . ioHelper . notify ( IO . CDK_TOOLKIT_I0100 . msg (
187191 `If you don’t want to see a notice anymore, use "cdk acknowledge <id>". For example, "cdk acknowledge ${ filteredNotices [ 0 ] . notice . issueNumber } ".` ,
188192 ) ) ;
189193 }
190194
191195 if ( options . showTotal ?? false ) {
192- void this . ioMessages . notify ( IO . CDK_TOOLKIT_I0100 . msg (
196+ await this . ioHelper . notify ( IO . CDK_TOOLKIT_I0100 . msg (
193197 `\nThere are ${ filteredNotices . length } unacknowledged notice(s).` ,
194198 ) ) ;
195199 }
196200 }
201+
202+ /**
203+ * List all notices available in the data source.
204+ *
205+ * @param includeAcknowlegded Whether to include acknowledged notices.
206+ */
207+ private noticesFromData ( includeAcknowlegded = false ) : Notice [ ] {
208+ const data = Array . from ( this . data ) ;
209+
210+ if ( includeAcknowlegded ) {
211+ return data ;
212+ }
213+
214+ return data . filter ( n => ! this . acknowledgedIssueNumbers . has ( n . issueNumber ) ) ;
215+ }
197216}
217+
0 commit comments