Working file export; need to test out error handling and flows

This commit is contained in:
efarooqui
2021-02-09 15:38:54 -08:00
parent fb77e1c365
commit 7c963eb1d0
5 changed files with 26 additions and 17 deletions

View File

@@ -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))
}
})
}

View File

@@ -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,