Skip to content

Commit

Permalink
feat: --json, save results to .json file
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Aug 18, 2020
1 parent 15597de commit 6347757
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@popstas/xlsx-style": "^0.8.20",
"chrome-launcher": "^0.13.4",
"commander": "^5.0.0",
"csvtojson": "^2.0.10",
"googleapis": "^59.0.0",
"lighthouse": "^6.2.0",
"xlsx": "^0.15.6"
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { program } = require('commander');
const packageJson = require('../package.json');
const scrapSite = require('./scrap-site');
const saveAsXlsx = require('./save-as-xlsx');
const saveAsJson = require('./save-as-json');
const publishGoogleSheets = require('./publish-google-sheets');
const { exec } = require('child_process');
const os = require('os');
Expand Down Expand Up @@ -53,6 +54,7 @@ program
.option('--out-dir <dir>', `Output directory`, '.')
.option('--csv <path>', `Skip scan, only convert csv to xlsx`)
.option('--web', `Publish sheet to google docs`)
.option('--json', `Output results in JSON`)
.option('--no-color', `No console colors`)
.option('--open-file', `Open file after scan (default: yes on Windows and MacOS)`)
.option('--no-open-file', `Don't open file after scan`)
Expand All @@ -70,8 +72,10 @@ async function start() {
if(program.csv) {
const csvPath = program.csv
const xlsxPath = csvPath.replace(/\.csv$/, '.xlsx');
const jsonPath = csvPath.replace(/\.csv$/, '.json');
try {
saveAsXlsx(csvPath, xlsxPath);
if (program.json) await saveAsJson(csvPath, jsonPath);
if (program.web) await publishGoogleSheets(xlsxPath);
console.log(`${xlsxPath} saved`);
if(program.openFile) exec(`"${xlsxPath}"`);
Expand Down
13 changes: 13 additions & 0 deletions src/save-as-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fs = require('fs');
const csv = require('csvtojson');

module.exports = async (csvPath, jsonPath) => {
// read csv to workbook
const data = {};
data.items = await csv({delimiter: ';'}).fromFile(csvPath);
// console.log(data);
data.fields = [];
const raw = JSON.stringify(data);
fs.writeFileSync(jsonPath, raw);
console.log('Saved to ' + jsonPath);
}

0 comments on commit 6347757

Please sign in to comment.