@@ -18,7 +18,7 @@ import FileUtils from './utils/file-utils.js';
18
18
import MdToHtmlTransform from './content-transforms/md-to-html-transform.js' ;
19
19
import ContentTransform from './content-transforms/content-transform.js' ;
20
20
21
- import { LogManager , Logger } from '@bluecadet/launchpad-utils' ;
21
+ import { LogManager , Logger , onExit } from '@bluecadet/launchpad-utils' ;
22
22
import ContentResult from './content-sources/content-result.js' ;
23
23
import SanityToPlainTransform from './content-transforms/sanity-to-plain.js' ;
24
24
import SanityToHtmlTransform from './content-transforms/sanity-to-html.js' ;
@@ -59,19 +59,19 @@ export class LaunchpadContent {
59
59
return Promise . reject ( error ) ;
60
60
}
61
61
}
62
-
62
+
63
63
/** @type {import('./content-options.js').ResolvedContentOptions } */
64
64
_config ;
65
65
66
66
/** @type {Logger } */
67
67
_logger ;
68
-
68
+
69
69
/** @type {Array<ContentSource> } */
70
70
_sources = [ ] ;
71
-
71
+
72
72
/** @type {MediaDownloader } */
73
73
_mediaDownloader ;
74
-
74
+
75
75
/** @type {Map<string, ContentTransform> } */
76
76
_contentTransforms = new Map ( ) ;
77
77
@@ -91,7 +91,7 @@ export class LaunchpadContent {
91
91
this . _contentTransforms . set ( 'sanityToHtml' , new SanityToHtmlTransform ( ) ) ;
92
92
this . _contentTransforms . set ( 'sanityToMd' , new SanityToMarkdownTransform ( ) ) ;
93
93
this . _contentTransforms . set ( 'sanityToMarkdown' , new SanityToMarkdownTransform ( ) ) ;
94
-
94
+
95
95
if ( this . _config . credentialsPath ) {
96
96
try {
97
97
Credentials . init ( this . _config . credentialsPath , this . _logger ) ;
@@ -101,11 +101,12 @@ export class LaunchpadContent {
101
101
}
102
102
}
103
103
}
104
-
104
+
105
105
this . sources = this . _createSources ( this . _config . sources ) ;
106
- // onExit(() => {
107
- // // TODO: Abort media downloader and wait for remaining downloads to finish
108
- // });
106
+
107
+ onExit ( async ( ) => {
108
+ this . _mediaDownloader . abort ( ) ;
109
+ } ) ;
109
110
}
110
111
111
112
/**
@@ -118,17 +119,17 @@ export class LaunchpadContent {
118
119
this . _logger . warn ( chalk . yellow ( 'No sources found to download' ) ) ;
119
120
return Promise . resolve ( ) ;
120
121
}
121
-
122
+
122
123
try {
123
124
this . _logger . info ( `Downloading ${ chalk . cyan ( sources . length ) } sources` ) ;
124
-
125
+
125
126
if ( this . _config . backupAndRestore ) {
126
127
this . _logger . info ( `Backing up ${ chalk . cyan ( sources . length ) } sources` ) ;
127
128
await this . backup ( sources ) ;
128
129
}
129
-
130
+
130
131
let sourcesComplete = 0 ;
131
-
132
+
132
133
for ( const source of sources ) {
133
134
const progress = ( sourcesComplete + 1 ) + '/' + sources . length ;
134
135
this . _logger . info ( `Downloading source ${ chalk . cyan ( progress ) } : ${ chalk . yellow ( source ) } ` ) ;
@@ -140,7 +141,7 @@ export class LaunchpadContent {
140
141
141
142
sourcesComplete ++ ;
142
143
}
143
-
144
+
144
145
this . _logger . info (
145
146
chalk . green ( `Finished downloading ${ sources . length } sources` )
146
147
) ;
@@ -151,7 +152,7 @@ export class LaunchpadContent {
151
152
await this . restore ( sources ) ;
152
153
}
153
154
}
154
-
155
+
155
156
try {
156
157
this . _logger . debug ( 'Cleaning up temp and backup files' ) ;
157
158
await this . clear ( sources , {
@@ -162,10 +163,10 @@ export class LaunchpadContent {
162
163
} catch ( err ) {
163
164
this . _logger . error ( 'Could not clean up temp and backup files' , err ) ;
164
165
}
165
-
166
+
166
167
return Promise . resolve ( ) ;
167
168
}
168
-
169
+
169
170
/**
170
171
* Alias for start(source)
171
172
* @param {Array<ContentSource> } sources
@@ -174,7 +175,7 @@ export class LaunchpadContent {
174
175
async download ( sources = [ ] ) {
175
176
return this . start ( sources ) ;
176
177
}
177
-
178
+
178
179
/**
179
180
* Clears all cached content except for files that match config.keep.
180
181
* @param {Array<ContentSource> } sources The sources you want to clear. If left undefined, this will clear all known sources. If no sources are passed, the entire downloads/temp/backup dirs are removed.
@@ -207,7 +208,7 @@ export class LaunchpadContent {
207
208
await this . _clearDir ( this . getDownloadPath ( source ) , { removeIfEmpty } ) ;
208
209
}
209
210
}
210
-
211
+
211
212
if ( removeIfEmpty && temp ) {
212
213
await FileUtils . removeDirIfEmpty ( this . getTempPath ( ) ) ;
213
214
}
@@ -217,10 +218,10 @@ export class LaunchpadContent {
217
218
if ( removeIfEmpty && downloads ) {
218
219
await FileUtils . removeDirIfEmpty ( this . getDownloadPath ( ) ) ;
219
220
}
220
-
221
+
221
222
return Promise . resolve ( ) ;
222
223
}
223
-
224
+
224
225
/**
225
226
* Backs up all downloads of source to a separate backup dir.
226
227
* @param {Array<ContentSource> } sources
@@ -240,7 +241,7 @@ export class LaunchpadContent {
240
241
}
241
242
}
242
243
}
243
-
244
+
244
245
/**
245
246
* Restores all downloads of source from its backup dir if it exists.
246
247
* @param {Array<ContentSource> } sources
@@ -265,7 +266,7 @@ export class LaunchpadContent {
265
266
}
266
267
}
267
268
}
268
-
269
+
269
270
/**
270
271
* @param {ContentSource } [source]
271
272
* @returns {string }
@@ -277,7 +278,7 @@ export class LaunchpadContent {
277
278
return path . resolve ( this . _config . downloadPath ) ;
278
279
}
279
280
}
280
-
281
+
281
282
/**
282
283
* @param {ContentSource } [source]
283
284
* @returns {string }
@@ -292,7 +293,7 @@ export class LaunchpadContent {
292
293
return detokenizedPath ;
293
294
}
294
295
}
295
-
296
+
296
297
/**
297
298
* @param {ContentSource } [source]
298
299
* @returns {string }
@@ -307,7 +308,7 @@ export class LaunchpadContent {
307
308
return detokenizedPath ;
308
309
}
309
310
}
310
-
311
+
311
312
/**
312
313
* @param {import('./content-options.js').ContentOptions['sources'] } sourceConfigs
313
314
* @returns {Array<ContentSource> }
@@ -317,14 +318,14 @@ export class LaunchpadContent {
317
318
this . _logger . warn ( 'No content sources found in config.' ) ;
318
319
return [ ] ;
319
320
}
320
-
321
+
321
322
const sources = [ ] ;
322
323
323
324
/**
324
325
* @type {(import('./content-options.js').AllSourceOptions)[] }
325
326
*/
326
327
let sourceConfigArray = [ ] ;
327
-
328
+
328
329
if ( ! Array . isArray ( sourceConfigs ) ) {
329
330
// Backwards compatibility for key/value-based
330
331
// configs where the key is the source ID
@@ -380,7 +381,7 @@ export class LaunchpadContent {
380
381
this . _logger . error ( 'Could not create content source:' , err ) ;
381
382
}
382
383
}
383
-
384
+
384
385
return sources ;
385
386
}
386
387
@@ -447,12 +448,12 @@ export class LaunchpadContent {
447
448
this . _logger . error ( `Unsupported content transform: '${ transformId } '` ) ;
448
449
continue ;
449
450
}
450
-
451
+
451
452
const transformIdStr = chalk . yellow ( transformId ) ;
452
453
const pathStr = chalk . yellow ( path ) ;
453
454
const localPathStr = chalk . yellow ( resultData . localPath ) ;
454
455
const transformer = this . _contentTransforms . get ( transformId ) ;
455
-
456
+
456
457
try {
457
458
if ( ! transformer ) {
458
459
throw new Error ( `Could not find content transform '${ transformId } '` ) ;
@@ -473,7 +474,7 @@ export class LaunchpadContent {
473
474
474
475
return Promise . resolve ( result ) ;
475
476
}
476
-
477
+
477
478
/**
478
479
* @param {string } dirPath
479
480
*/
0 commit comments