diff --git a/src/api/index.js b/src/api/index.js index b2d48a3..32961ba 100755 --- a/src/api/index.js +++ b/src/api/index.js @@ -1,6 +1,5 @@ import { version } from '../../package.json' import { Router } from 'express' -import { exportToFile } from '../utilities' import copy from '../copy/en' export default ({ config, controller }) => { diff --git a/src/copy/en.js b/src/copy/en.js index 2795462..b4ccf21 100644 --- a/src/copy/en.js +++ b/src/copy/en.js @@ -1,7 +1,11 @@ export default { errors: { update: 'The server could not update. Check your API credentials and internet connection and try again.', - export: 'The server could not export. Check that you provided a file path to export to and try again.', + export: { + fileMissing: 'The server could not export. Check that you provided a file path to export to and try again.', + writeFailed: 'The server could not export the data to the file. There is an issue with the data format' + }, + exportFileMissing: 'The server could not export. Check that you provided a file path to export to and try again.', onlySheet: 'You cannot query a sheet directly. The URL needs to be in the format /:sheet/:tab/:resource.', onlyTab: 'You cannot query a tab directly. The URL needs to be in the format /:sheet/:tab/:resource.', noSheet: sheet => `The sheet ${sheet} is not available in this server.`, diff --git a/src/lib/Controller.js b/src/lib/Controller.js index e3a5970..2150e98 100644 --- a/src/lib/Controller.js +++ b/src/lib/Controller.js @@ -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)) } }) } diff --git a/src/lib/util.js b/src/lib/util.js index e213df1..76ab1db 100755 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -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, diff --git a/src/utilities.js b/src/utilities.js deleted file mode 100644 index e2dfeda..0000000 --- a/src/utilities.js +++ /dev/null @@ -1,11 +0,0 @@ -import fs from 'file-system' - -function exportToFile(url, data) { - console.info(url, data) - return 1 -} - -export const utilities = { - default: exportToFile(), - exportToFile -} \ No newline at end of file