-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (66 loc) · 1.8 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { writeFile, readFile } from 'fs';
import { v4 as uuidv4 } from 'uuid';
import { transformItem } from './src/transformers.js';
import { parseJsonLocation } from './src/commands.js';
let sourceFile;
try {
sourceFile = parseJsonLocation(process.argv);
} catch (err) {
console.error(`please provide -config=/file/path.json`, err);
process.exit(1);
}
// Store the translated structures
const translatedFolders = [];
const translatedItems = [];
// Handles the folder structures
const handleFolder = (folder, prevFolderName) => {
let name = folder.name;
const items = folder.items;
const uuid = uuidv4();
if (prevFolderName) {
name = `${prevFolderName}/${name}`;
}
translatedFolders.push({
name,
id: uuid,
});
items.forEach((item) => {
const transformed = transformItem(item);
transformed.folderId = uuid;
translatedItems.push(transformed);
});
if (!folder.folders) {
return;
}
folder.folders.forEach((folder2) => {
handleFolder(folder2, name);
});
};
const handleWrite = (err) => {
if (err) {
console.error('There was an error writing the file.', err);
return;
}
console.log('Write completed successfully');
};
const handleRead = (err, data) => {
if (err) {
console.error('There was an error reading the psono file', err);
return;
}
const parsed = JSON.parse(data);
parsed.items.forEach((item) => {
translatedItems.push(transformItem(item));
});
parsed.folders.forEach((folder) => {
handleFolder(folder);
});
const translatedJSON = JSON.stringify({
items: translatedItems,
folders: translatedFolders,
});
console.log('starting write');
writeFile('./bitwarden-import.json', translatedJSON, handleWrite);
};
console.log('Reading exported psono JSON');
readFile(sourceFile, 'utf8', handleRead);