Skip to content

Commit b436a29

Browse files
author
Schmidely Stéphane
committed
Save file - JSON currently supported
1 parent 6a0b4d6 commit b436a29

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/commands/export.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export default class Export extends Command {
6060

6161
// (FIXME) Step 5 - Aggregate data
6262
// (FIXME) Step 6 - Save data
63+
await tools.utils.saveFile(data, params)
6364
}
6465

6566
/**

src/utils/commands/export/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fs from 'fs'
12
import * as moment from 'moment'
23
import { EOL } from 'os'
34

@@ -41,6 +42,25 @@ export default class ExportUtils {
4142
}
4243
}
4344

45+
/**
46+
* Save file on disk
47+
*/
48+
public saveFile(data: Array<any>, params: any) {
49+
// #neurodecisions$2019-01-12$2019-12-12.csv
50+
const fromDate = params.from.replace(/-/g, '')
51+
const toDate = params.to.replace(/-/g, '')
52+
const project = params.project.replace(/[^a-zA-Z ]/g, '')
53+
const filename = `${project}-${fromDate}-${toDate}.${params.format}`
54+
const writeData = (params.format === 'json') ? JSON.stringify(data) : data
55+
try {
56+
fs.writeFileSync(filename, writeData)
57+
return true
58+
} catch (err) {
59+
// FIXME handle error
60+
console.error(err)
61+
}
62+
}
63+
4464
/**
4565
* Format error message
4666
*/

0 commit comments

Comments
 (0)