mirror of
https://github.com/bellingcat/datasheet-server.git
synced 2026-06-12 13:28:32 +03:00
Working file export; need to test out error handling and flows
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import copy from '../copy/en'
|
||||
import { exportToFile } from '../lib/util'
|
||||
|
||||
/**
|
||||
* Controller
|
||||
@@ -40,7 +41,7 @@ class Controller {
|
||||
}
|
||||
|
||||
retrieveAll (fileDest) {
|
||||
if (fileDest === '') throw new Error(copy.errors.export)
|
||||
if (fileDest === '') return Promise.reject(new Error(copy.errors.export.fileMissing))
|
||||
|
||||
const indexedData = {}
|
||||
const urls = []
|
||||
@@ -52,14 +53,19 @@ class Controller {
|
||||
urls.push(bp.urls[0])
|
||||
return this.retrieve(bp.sheet.name, bp.name, resource)
|
||||
})
|
||||
).then(results => {
|
||||
).then(async results => {
|
||||
if (results.every(res => res)) {
|
||||
urls.forEach((item, idx) => {
|
||||
indexedData[item] = results[idx]
|
||||
})
|
||||
return copy.success.export(fileDest)
|
||||
try {
|
||||
await exportToFile(fileDest, indexedData)
|
||||
return copy.success.export(fileDest)
|
||||
} catch (e) {
|
||||
return Promise.reject(e)
|
||||
}
|
||||
} else {
|
||||
throw new Error(copy.errors.export)
|
||||
return Promise.reject(new Error(copy.errors.export.writeFailed))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import R from 'ramda'
|
||||
import fs from 'file-system'
|
||||
import copy from '../copy/en'
|
||||
|
||||
/* eslint-disable */
|
||||
String.prototype.replaceAll = function (search, replacement) {
|
||||
@@ -14,6 +16,15 @@ function camelize (str) {
|
||||
})
|
||||
}
|
||||
|
||||
export function exportToFile(fileDest, data) {
|
||||
const stringifiedData = JSON.stringify(data, null, 2)
|
||||
const filePath = `${fileDest}/export.json`
|
||||
|
||||
fs.writeFile(filePath, stringifiedData, (err) => {
|
||||
if (err) throw new Error(copy.errors.export.writeFailed)
|
||||
})
|
||||
}
|
||||
|
||||
export const fmtObj = R.curry(
|
||||
(
|
||||
columnNames,
|
||||
|
||||
Reference in New Issue
Block a user