Skip to content

Commit baf570c

Browse files
Benedikt Rötschaxe312ger
authored andcommitted
feat(summary): adds summary report to the end of the export
closes #11
1 parent 70cb79d commit baf570c

File tree

4 files changed

+75
-18
lines changed

4 files changed

+75
-18
lines changed

bin/contentful-export

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ runContentfulExport(usageParams)
1212
process.exit(0)
1313
})
1414
.catch((err) => {
15-
log.error('', 'Failed with\n', err)
15+
log.error('export', 'Failed with\n', err)
1616
process.exit(1)
1717
})

lib/index.js

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import Promise from 'bluebird'
77
import jsonStringifySafe from 'json-stringify-safe'
88
import log from 'npmlog'
99
import { resolve } from 'path'
10+
import { startCase } from 'lodash'
11+
import Table from 'cli-table2'
12+
import moment from 'moment'
13+
14+
const summary = {}
1015

1116
export 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({

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
},
5050
"dependencies": {
5151
"bluebird": "^3.3.3",
52+
"cli-table2": "^0.2.0",
5253
"contentful-batch-libs": "^5.3.0",
5354
"fs-extra": "^2.1.2",
5455
"json-stringify-safe": "^5.0.1",
5556
"lodash": "^4.0.0",
57+
"moment": "^2.18.1",
5658
"npmlog": "^4.0.0",
5759
"request": "^2.81.0",
5860
"yargs": "^7.0.2"

yarn.lock

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,15 @@ cli-cursor@^1.0.1, cli-cursor@^1.0.2:
909909
dependencies:
910910
restore-cursor "^1.0.1"
911911

912+
cli-table2@^0.2.0:
913+
version "0.2.0"
914+
resolved "https://registry.yarnpkg.com/cli-table2/-/cli-table2-0.2.0.tgz#2d1ef7f218a0e786e214540562d4bd177fe32d97"
915+
dependencies:
916+
lodash "^3.10.1"
917+
string-width "^1.0.1"
918+
optionalDependencies:
919+
colors "^1.1.2"
920+
912921
cli-width@^2.0.0:
913922
version "2.1.0"
914923
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.1.0.tgz#b234ca209b29ef66fc518d9b98d5847b00edf00a"
@@ -937,6 +946,10 @@ code-point-at@^1.0.0:
937946
version "1.1.0"
938947
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
939948

949+
colors@^1.1.2:
950+
version "1.1.2"
951+
resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"
952+
940953
combined-stream@^1.0.5, combined-stream@~1.0.5:
941954
version "1.0.5"
942955
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.5.tgz#938370a57b4a51dea2c77c15d5c5fdf895164009"
@@ -2429,7 +2442,7 @@ lodash.restparam@^3.0.0:
24292442
version "3.6.1"
24302443
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
24312444

2432-
lodash@^3.6.0:
2445+
lodash@^3.10.1, lodash@^3.6.0:
24332446
version "3.10.1"
24342447
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"
24352448

@@ -2563,6 +2576,10 @@ [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.1:
25632576
dependencies:
25642577
minimist "0.0.8"
25652578

2579+
moment@^2.18.1:
2580+
version "2.18.1"
2581+
resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"
2582+
25662583
25672584
version "0.7.1"
25682585
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"

0 commit comments

Comments
 (0)