Skip to content

Commit e7e61ee

Browse files
committed
fix(lib): handle the case where opts is undefined
when using contentful-export as a library opts object will be undefined and will cause a crash at the content-batch-libs when creating the client also the promise was not resolving to the reponse when it is used as library closes #3
1 parent 91d30b9 commit e7e61ee

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ coverage
55
node_modules
66
config.json
77
dist
8+
*.swp

lib/run-contentful-export.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@ var log = require('npmlog')
88

99
Promise.promisifyAll(fs)
1010
export default function runContentfulExport (usageParams) {
11-
const {opts, errorLogFile} = usageParams
11+
let {opts, errorLogFile} = usageParams
12+
let exportToFile = true
13+
if (!opts) {
14+
exportToFile = false
15+
opts = {}
16+
opts.sourceSpace = opts.sourceSpace || usageParams.spaceId
17+
opts.sourceManagementToken = opts.sourceManagementToken || usageParams.managementToken
18+
}
1219
const clients = createClients(opts)
1320
return getFullSourceSpace({
1421
managementClient: clients.source.management,
1522
spaceId: clients.source.spaceId
1623
})
17-
.then((response) => {
18-
const responseFile = `${opts.exportDir}/contentful-export-${clients.source.spaceId}-${Date.now()}.json`
19-
log.info('Writing the data to a json to file at : ' + responseFile)
20-
return fs.writeFile(responseFile, jsonStringifySafe(response))
21-
})
22-
.catch((err) => {
23-
dumpErrorBuffer({
24-
sourceSpace: opts.sourceSpace,
25-
errorLogFile: errorLogFile
24+
.then((response) => {
25+
if (exportToFile) {
26+
const responseFile = `${opts.exportDir}/contentful-export-${clients.source.spaceId}-${Date.now()}.json`
27+
log.info('Writing the data to a json to file at : ' + responseFile)
28+
return fs.writeFile(responseFile, jsonStringifySafe(response))
29+
}
30+
return response
31+
})
32+
.catch((err) => {
33+
dumpErrorBuffer({
34+
sourceSpace: opts.sourceSpace,
35+
errorLogFile: errorLogFile
36+
})
37+
throw err
2638
})
27-
throw err
28-
})
2939
}

0 commit comments

Comments
 (0)