From 19aa3afc96ae47308d55bedc200a30d760e50729 Mon Sep 17 00:00:00 2001 From: Nemanja Stojoski Date: Sun, 15 Oct 2023 13:31:00 +0200 Subject: [PATCH] Fix empty CSV generation. --- package.json | 2 +- src/modules/flightlog.js | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 10ed955..e110b94 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cats-configurator", - "version": "0.3.3", + "version": "0.3.4", "private": true, "scripts": { "start": "vue-cli-service electron:serve", diff --git a/src/modules/flightlog.js b/src/modules/flightlog.js index 4e499ff..3308e32 100644 --- a/src/modules/flightlog.js +++ b/src/modules/flightlog.js @@ -4,12 +4,12 @@ export function exportFlightLogToCSVs(flightLog) { const flightLogDir = "flight-log-export" let flightLogSections = ["imu", "baro", "flightInfo", "orientationInfo", "filteredDataInfo", "gnssInfo", "flightStates", "eventInfo", "voltageInfo"]; - if (!fs.existsSync(flightLogDir)){ + if (!fs.existsSync(flightLogDir)) { fs.mkdirSync(flightLogDir); } for (let flightLogSection of flightLogSections) { - fs.writeFile(`${flightLogDir}/${flightLogSection}.csv`, objectArrayToCSV(flightLog[flightLogSection]), "utf8", function (err) { + fs.writeFile(`${flightLogDir}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) { if (err) { console.log("An error occurred while writing CSV object to file."); return console.log(err); @@ -29,20 +29,26 @@ export function exportJSON(flightLog) { }); } -function objectArrayToCSV(arr, separator = ",") { +function objectArrayToCSV(section, arr, separator = ",") { if (!Array.isArray(arr)) { console.log("objectArrayToCSV first argument must be an array."); return; } - let CSVColumnNames = getCSVColumnNames(arr[0]); + if (arr.length > 0) { + console.log("Section " + section + " :" + arr[0]) + let CSVColumnNames = getCSVColumnNames(arr[0]); - let CSVHeader = CSVColumnNames.join(separator); - let CSVBody = arr.map(obj => - CSVColumnNames.map(header => getObjectValue(obj, header)).join(separator) - ).join("\n"); + let CSVHeader = CSVColumnNames.join(separator); + let CSVBody = arr.map(obj => + CSVColumnNames.map(header => getObjectValue(obj, header)).join(separator) + ).join("\n"); - return CSVHeader + "\n" + CSVBody; + return CSVHeader + "\n" + CSVBody; + } else { + console.log("Array length of " + section + " is 0!"); + return "No data recorded" + } } function getCSVColumnNames(obj) {