forked from david-barbion/psono-to-csv
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.js
More file actions
59 lines (53 loc) · 1.62 KB
/
Copy pathparse.js
File metadata and controls
59 lines (53 loc) · 1.62 KB
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
const fs = require('fs');
const args = process.argv;
const inputFile = args[2] || 'psono.json';
const outputFile = args[3] || 'passbolt.csv';
console.log('\nREADING: ' + inputFile + '\n');
function parseFolder(items, csv, fieldMapping) {
items.forEach(folder => {
const rowData = {
grouping: '"' + folder.name + '"',
title: '',
username: '',
password: '',
url: '',
notes: '',
};
if (folder.items) {
folder.items.forEach(item => {
console.log("add item " + item.name + " for folder " + folder.name);
Object.keys(fieldMapping).forEach(type => {
console.log("field " + type + " ==> " + fieldMapping[type]);
rowData[fieldMapping[type]] = '"' + item[type] + '"';
});
csv.push(
Object.keys(rowData)
.map(key => rowData[key])
.join(','),
);
});
}
if (folder.folders) {
console.log("parsing subfolder " + folder.name);
csv.push(parseFolder(folder.folders, csv, fieldMapping));
}
});
return (csv);
}
try {
const contents = fs.readFileSync(inputFile);
const vault = JSON.parse(contents);
const csvOutput = ['"Group","Title","Username","Password","URL","Notes"'];
const fieldMapping = {
name: 'title',
website_password_username: 'username',
website_password_password: 'password',
website_password_url: 'url',
website_password_notes: 'notes',
};
csvOutput.push(parseFolder(vault.folders, csvOutput, fieldMapping));
console.log('WRITING: ' + outputFile + '\n');
fs.writeFileSync(outputFile, csvOutput.join('\n'));
} catch (err) {
throw err;
}