This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
export-data.ts
73 lines (65 loc) · 2.09 KB
/
export-data.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {createDirectory, getFileName, BaseCommand} from '../lib'
import {cli} from 'cli-ux'
import {createWriteStream} from 'fs'
import {join} from 'path'
import fetch from 'node-fetch'
const QUERY = `
mutation {
export {
signedUrls
}
}`
export default class ExportData extends BaseCommand {
static description = 'Export data from your backend'
static examples = [
'$ slash-graphql export-data -e https://frozen-mango.cloud.dgraph.io/graphql -t <apiToken> ./output-directory',
]
static flags = {
...BaseCommand.commonFlags,
...BaseCommand.endpointFlags,
}
static args = [{name: 'outputdir', description: 'Output Directory', required: true}]
// FIXME: The progress bar can get a lot better here
async run() {
const opts = this.parse(ExportData)
const backend = await this.backendFromOpts(opts)
const outputDir = opts.args.outputdir as string
await createDirectory(outputDir)
if (!opts.flags.quiet) {
this.log('Exporting Data, this might take a few minutes')
}
const progressBar = opts.flags.quiet ?
null :
cli.progress({
format: '{step} [{bar}] {percentage}%',
})
progressBar && progressBar.start()
progressBar && progressBar.update(10, {
step: 'Exporting',
})
const {errors, data} = await backend.slashAdminQuery<{ export: { signedUrls: string[] } }>(QUERY)
progressBar && progressBar.update(33, {
step: 'Downloading',
})
if (errors) {
for (const {message} of errors) {
this.error(message)
}
return
}
const urls = data.export.signedUrls
const stepIncrement = 67 / (urls.length || 1)
for (const url of urls) {
// eslint-disable-next-line no-await-in-loop
const res = await fetch(url)
if (res.status !== 200 || !res.body) {
this.error('Unable to download file')
return
}
res.body.pipe(createWriteStream(join(outputDir, getFileName(url))))
progressBar && progressBar.increment(stepIncrement)
}
progressBar && progressBar.update(100, {step: 'Success'})
progressBar && progressBar.stop()
}
}