@@ -7,6 +7,11 @@ import Promise from 'bluebird'
77import jsonStringifySafe from 'json-stringify-safe'
88import log from 'npmlog'
99import { resolve } from 'path'
10+ import { startCase } from 'lodash'
11+ import Table from 'cli-table2'
12+ import moment from 'moment'
13+
14+ const summary = { }
1015
1116export default function runContentfulExport ( usageParams ) {
1217 const defaultOpts = {
@@ -20,6 +25,8 @@ export default function runContentfulExport (usageParams) {
2025 saveFile : true
2126 }
2227
28+ summary . startTime = moment ( )
29+
2330 const configFile = usageParams . config
2431 ? require ( resolve ( process . cwd ( ) , usageParams . config ) )
2532 : { }
@@ -60,52 +67,83 @@ export default function runContentfulExport (usageParams) {
6067 if ( ! opts . downloadAssets ) {
6168 return response
6269 }
63- let successCount = 0
64- let warningCount = 0
65- let errorCount = 0
70+ summary . assetDownloads = {
71+ successCount : 0 ,
72+ warningCount : 0 ,
73+ errorCount : 0
74+ }
6675
67- log . info ( 'Downloading ' + response . assets . length + ' assets' )
76+ log . info ( 'export' , ' Downloading ' + response . assets . length + ' assets' )
6877
6978 return Promise . map ( response . assets , ( asset ) => {
7079 if ( ! asset . fields . file ) {
71- log . warn ( '-> asset has no file(s)' , jsonStringifySafe ( asset ) )
72- warningCount ++
80+ log . warn ( 'export' , ' -> asset has no file(s)', jsonStringifySafe ( asset ) )
81+ summary . assetDownloads . warningCount ++
7382 return
7483 }
7584 const locales = Object . keys ( asset . fields . file )
7685 return Promise . mapSeries ( locales , ( locale ) => {
7786 const url = asset . fields . file [ locale ] . url || asset . fields . file [ locale ] . upload
87+ if ( ! url ) {
88+ log . warn ( 'export' , '-> asset no file(s) for locale' , locale , jsonStringifySafe ( asset ) )
89+ summary . assetDownloads . warningCount ++
90+ return
91+ }
7892 return downloadAsset ( url , opts . exportDir )
7993 . then ( ( downLoadedFile ) => {
80- log . info ( '-> ' + downLoadedFile )
81- successCount ++
94+ log . info ( 'export' , ' -> ' + downLoadedFile )
95+ summary . assetDownloads . successCount ++
8296 } )
8397 . catch ( ( error ) => {
84- log . error ( '-> error downloading ' + url + ' => ' + error . message )
85- log . error ( JSON . stringify ( error , null , 2 ) )
86- errorCount ++
98+ log . error ( 'export' , ' -> error downloading ' + url + ' => ' + error . message )
99+ log . error ( 'export' , JSON . stringify ( error , null , 2 ) )
100+ summary . assetDownloads . errorCount ++
87101 } )
88102 } )
89103 } , {
90104 concurrency : 6
91105 } )
92106 . then ( ( ) => {
93- log . info ( 'All ' + response . assets . length + ' assets downloaded.' )
94- log . info ( 'Successful file downloads: ' + successCount )
95- log . info ( 'Download warnings: ' + warningCount )
96- log . info ( 'Download errors: ' + errorCount )
107+ log . info ( 'export' , 'Finished loading files for ' + response . assets . length + ' assets.' )
97108 return response
98109 } )
99110 } )
100111 . then ( ( response ) => {
101112 if ( opts . saveFile ) {
102113 fs . existsSync ( opts . exportDir ) || fs . mkdirSync ( opts . exportDir )
103114 const responseFile = `${ opts . exportDir } /contentful-export-${ clients . source . spaceId } -${ Date . now ( ) } .json`
104- log . info ( 'Writing space data to json file at : ' + responseFile )
115+ log . info ( 'export' , ' Writing space data to json file at : ' + responseFile )
105116 fs . writeFileSync ( responseFile , jsonStringifySafe ( response , null , 4 ) )
106117 }
107118 return response
108119 } )
120+ . then ( ( response ) => {
121+ const responseTable = new Table ( )
122+
123+ responseTable . push ( [ { colSpan : 2 , content : 'Exported entities' } ] )
124+
125+ Object . keys ( response ) . forEach ( ( type ) => {
126+ responseTable . push ( [ startCase ( type ) , response [ type ] . length ] )
127+ } )
128+
129+ console . log ( responseTable . toString ( ) )
130+
131+ if ( 'assetDownloads' in summary ) {
132+ const downloadsTable = new Table ( )
133+ downloadsTable . push ( [ { colSpan : 2 , content : 'Asset file download results' } ] )
134+ downloadsTable . push ( [ 'Successful' , summary . assetDownloads . successCount ] )
135+ downloadsTable . push ( [ 'Warnings ' , summary . assetDownloads . warningCount ] )
136+ downloadsTable . push ( [ 'Errors ' , summary . assetDownloads . errorCount ] )
137+ console . log ( downloadsTable . toString ( ) )
138+ }
139+
140+ const durationHuman = summary . startTime . fromNow ( true )
141+ const durationSeconds = moment ( ) . diff ( summary . startTime , 'seconds' )
142+
143+ log . info ( 'export' , `The export took ${ durationHuman } (${ durationSeconds } s)` )
144+
145+ return response
146+ } )
109147 . catch ( ( err ) => {
110148 const { sourceSpace, errorLogFile } = opts
111149 dumpErrorBuffer ( {
0 commit comments