|
1 | 1 | import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { dialog } from "electron"; |
2 | 4 |
|
3 | 5 | export function exportFlightLogToCSVs(flightLog) { |
4 | | - const flightLogDir = "flight-log-export" |
5 | 6 | let flightLogSections = ["imu", "baro", "flightInfo", "orientationInfo", "filteredDataInfo", "gnssInfo", "flightStates", "eventInfo", "voltageInfo"]; |
6 | 7 |
|
7 | | - if (!fs.existsSync(flightLogDir)) { |
8 | | - fs.mkdirSync(flightLogDir); |
| 8 | + let paths = dialog.showOpenDialogSync({ properties: ["openDirectory"] }); |
| 9 | + |
| 10 | + if (!paths) { |
| 11 | + console.log("No directory selected for export."); |
| 12 | + // TODO: after snackbar is merged print error message to user |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + const exportFolderPath = path.join(paths[0]); |
| 17 | + |
| 18 | + if (!fs.existsSync(exportFolderPath)) { |
| 19 | + fs.mkdirSync(exportFolderPath); |
9 | 20 | } |
10 | 21 |
|
11 | 22 | for (let flightLogSection of flightLogSections) { |
12 | | - fs.writeFile(`${flightLogDir}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) { |
| 23 | + fs.writeFile(`${exportFolderPath}/${flightLogSection}.csv`, objectArrayToCSV(flightLogSection, flightLog[flightLogSection]), "utf8", function (err) { |
13 | 24 | if (err) { |
14 | 25 | console.log("An error occurred while writing CSV object to file."); |
| 26 | + // TODO: after snackbar is merged print error message to user |
15 | 27 | return console.log(err); |
16 | 28 | } |
17 | | - console.log("CSV file has been saved."); |
18 | 29 | }); |
19 | 30 | } |
20 | 31 | } |
21 | 32 |
|
| 33 | +export function exportFlightLogChartsToHTML(flightLogChartsHTML) { |
| 34 | + let paths = dialog.showOpenDialogSync({ properties: ["openDirectory"] }); |
| 35 | + |
| 36 | + if (!paths) { |
| 37 | + console.log("No directory selected for export."); |
| 38 | + return; |
| 39 | + // TODO: after snackbar is merged print error message to user |
| 40 | + } |
| 41 | + |
| 42 | + const exportFolderPath = path.join(paths[0]); |
| 43 | + |
| 44 | + if (!fs.existsSync(exportFolderPath)) { |
| 45 | + fs.mkdirSync(exportFolderPath); |
| 46 | + } |
| 47 | + |
| 48 | + const flightLogHtmlDocument = ` |
| 49 | + <!DOCTYPE html> |
| 50 | + <html> |
| 51 | + <head> |
| 52 | + <script src="https://cdn.plot.ly/plotly-2.18.2.min.js"></script> |
| 53 | + </head> |
| 54 | + <body> |
| 55 | + ${flightLogChartsHTML} |
| 56 | + </body> |
| 57 | + </html> |
| 58 | + `; |
| 59 | + |
| 60 | + fs.writeFile(`${exportFolderPath}/plots.html`, flightLogHtmlDocument, 'utf8', function (err) { |
| 61 | + if (err) { |
| 62 | + console.log("An error occurred while writing HTML Object to File."); |
| 63 | + return console.log(err); |
| 64 | + // TODO: after snackbar is merged print error message to user |
| 65 | + } |
| 66 | + }); |
| 67 | +} |
| 68 | + |
22 | 69 | export function exportJSON(flightLog) { |
23 | 70 | fs.writeFile("flightlog.json", JSON.stringify(flightLog), "utf8", function (err) { |
24 | 71 | if (err) { |
|
0 commit comments