-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
61 lines (42 loc) · 1.68 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` });
const fs = require("fs");
const { parse } = require("csv-parse");
const bcrypt = require('bcrypt');
const ObjectsToCsv = require('objects-to-csv');
const readFromFile = process.env.READ_CSV_FILE_LOCATION;
const writeToFile = process.env.WRITE_CSV_FILE_LOCATION;
async function processData() {
var dataArray = [];
var encryPassArray = [];
var finalDataArray = [];
//Read data from CSV File
fs.createReadStream(`${readFromFile}`)
.pipe(parse({ delimiter: ",", from_line: 2 }))
.on("data", function (row) {
// console.log(row);
dataArray.push(row)
})
.on("end", function () {
// console.log(dataArray);
console.log(`\n${dataArray.length} row is fetched from csv file`);
for (var i = 0; i < dataArray.length; i++) {
console.log(`\nPassword Hashing is on progress... 🍺`);
// Hashing plainTextPassword
encryPassArray.push(bcrypt.hashSync(dataArray[i][1], 10));
}
// Inserting User_id and Encrypted Password into array
for (var j = 0; j < dataArray.length; j++) {
finalDataArray.push({ "USER_ID": dataArray[j][0], "PASSWORD": encryPassArray[j] });
}
const csv = new ObjectsToCsv(finalDataArray);
// Save CSV File to Location
csv.toDisk(`${writeToFile}/encryptedPassword.csv`);
console.log("\nCSV File is genereated.");
})
.on("error", function (error) {
console.log(error.message);
});
}
(async () => {
await processData();
})();