Export command

master
sundowndev 2019-02-27 17:02:17 +01:00
parent 95ee2e75ed
commit fa1e98ce4d
1 changed files with 39 additions and 0 deletions

39
commands/export/index.js Normal file
View File

@ -0,0 +1,39 @@
const fs = require('fs');
export const command = 'export <type>';
export const description = 'Export data to json or csv';
export const action = async ({ args, jsonToCsv, logger }) => {
const [type] = args._
const dataDir = global.rootPath + "/data";
// if data directory doesn't exists, create it
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
switch (type) {
case 'json':
fs.writeFile(dataDir + "/db.json", JSON.stringify(global.data), function (err) {
if (err) {
return logger.error(err);
}
});
logger.info('Successfully exported data.');
break;
case 'csv':
const csv = await jsonToCsv.parse(global.data.students);
fs.writeFile(dataDir + "/students.csv", csv, function (err) {
if (err) {
return logger.error(err);
}
});
logger.info('Successfully exported data.');
break;
default:
logger.error('You need to specify a valid type (json or csv).');
}
};