-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
93 lines (80 loc) · 3.11 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
93
const fs = require("node:fs");
const path = require("node:path");
const readline = require("node:readline");
const { stdin: input, stdout: output } = require("node:process");
const convertFile = require("./convertFile.js");
const createOrReturnDirectory = require("./createOrReturnDirectory.js");
const copyAssets = require("./copyAssets.js");
const rl = readline.createInterface({ input, output });
const FIRST_QUESTION =
" \n\x1b[1m\x1b[33mEnter the location of downloaded and unzipped 'Google Keep' Takeout folder?\x1b[0m \n\x1b[33m e.g. /Users/<your username>/desktop/\x1b[0m\n\n >";
const SECOND_QUESTION =
" \n\x1b[1m\x1b[33mEnter location of your 'Journals' folder or press 'ENTER' if you want to create new 'journals' folder in current location\x1b[0m \n\x1b[33m e.g. /Users/<your username>/Documents/<Logseq Graph name>/\x1b[0m\n\n >";
let sourceDirectory;
let destinationDirectory;
let jsonFiles;
const runFirstQuestion = () => {
rl.question(FIRST_QUESTION, (firstAnswer) => {
const askedFolderLocation = firstAnswer.trim().replace(/^['"]|['"]$/g, "");
sourceDirectory = path.normalize(`${askedFolderLocation}/Takeout/Keep/`);
if (
!fs.existsSync(sourceDirectory) ||
!fs.statSync(sourceDirectory).isDirectory()
) {
console.log(
"\n\x1b[1m\x1b[31mTakeout folder not found! Please try again!\x1b[0m"
);
return runFirstQuestion();
}
try {
const files = fs.readdirSync(sourceDirectory);
jsonFiles = files.filter(
(file) => path.extname(file).toLowerCase() === ".json"
);
runSecondQuestion();
} catch (error) {
console.log("Error getting directory info");
}
});
};
const runSecondQuestion = () => {
rl.question(SECOND_QUESTION, (secondAnswer) => {
const askedDestinationLocation = secondAnswer
.trim()
.replace(/^['"]|['"]$/g, "");
destinationDirectory = path.normalize(askedDestinationLocation);
if (
!fs.existsSync(destinationDirectory) ||
!fs.statSync(destinationDirectory).isDirectory()
) {
console.log(
"\n\x1b[1m\x1b[31mDestination location is not valid! Please try again!\x1b[0m"
);
return runSecondQuestion();
}
let processedFilesCount = 0;
for (let file of jsonFiles) {
try {
const fileContent = fs.readFileSync(`${sourceDirectory}/${file}`);
const jsonData = JSON.parse(fileContent);
if (jsonData.isTrashed) continue;
const { mdFileName, content } = convertFile(jsonData);
const pathToAppend = createOrReturnDirectory(
`${destinationDirectory}/journals/`
);
fs.appendFileSync(`${pathToAppend}/${mdFileName}`, content);
processedFilesCount++;
} catch (error) {
console.error(error);
}
}
console.log(
`\n\x1b[92m All notes processed. Total converted notes: ${processedFilesCount}. \n Please look for newly created 'journals' folder in '${
destinationDirectory === "." ? __dirname : destinationDirectory
}' directory. \x1b[0m\n`
);
copyAssets(sourceDirectory, destinationDirectory);
rl.close();
});
};
runFirstQuestion();