Skip to content

Commit 5a31ebc

Browse files
committed
write data to json file
1 parent 5a332f0 commit 5a31ebc

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var async = require('async');
44

55
var openProductsPage = require('./lib/openProductsPage');
66
var openCardPage = require('./lib/openCardPage');
7+
var writeCardsToFile = require('./lib/writeCardsToFile');
78

89
/** @type Card[] */
910
var userCards = [];
@@ -63,6 +64,8 @@ function allProductsGotten() {
6364
console.log(userCards[i].trend);
6465
console.log('---');
6566
}
67+
68+
writeCardsToFile(userCards);
6669

6770
phantom.exit();
6871
});

lib/writeCardsToFile.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* global phantom */
2+
3+
var fs = require('fs');
4+
5+
/**
6+
* JSON encode the provided array and write it on the "/data" folder of the project
7+
*
8+
* @param {Card[]} userCards
9+
*/
10+
function writeCardsToFile(userCards) {
11+
var file = fs.workingDirectory + '/data/' + Date.now() + '.json';
12+
13+
try {
14+
fs.write(file, JSON.stringify(userCards), 'w');
15+
} catch (e) {
16+
console.error('Error writing data:', e);
17+
18+
phantom.exit();
19+
}
20+
}
21+
22+
module.exports = writeCardsToFile;

0 commit comments

Comments
 (0)