forked from lugenx/keep2log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (122 loc) · 3.98 KB
/
Copy pathindex.js
File metadata and controls
143 lines (122 loc) · 3.98 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const fs = require("node:fs");
const path = require("node:path");
// const readline = require("node:readline");
const rls = require("readline-sync");
// import psp from "prompt-sync-plus";
// const prompt = psp();
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 >. CAUTION: If using Google Keep in other languages, make sure to rename the directory inside Takeout to \'Keep\'!";
const SECOND_QUESTION =
" \n\x1b[1m\x1b[33mEnter location of your 'Notes' folder (CAUTION: The directory should exist!) or press 'ENTER' if you want to create new 'notes' 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;
function checkSourcePath(srcpath) {
const askedFolderLocation = srcpath.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 1;
}
else {
return 0;
}
}
function checkDestPath(destpath) {
const askedDestinationLocation = destpath
.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 1;
}
else {
return 0;
}
}
function getKeepData() {
try {
const files = fs.readdirSync(sourceDirectory);
jsonFiles = files.filter(
(file) => path.extname(file).toLowerCase() === ".json"
);
} catch (error) {
console.log("Error getting directory info");
}
}
function convertData() {
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}/notes/`
);
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 'notes' folder in '${
destinationDirectory === "." ? __dirname : destinationDirectory
}' directory. \x1b[0m\n`
);
copyAssets(sourceDirectory, destinationDirectory);
}
function main() {
console.log(`Starting! Arguments: ${process.argv[2]}, ${process.argv[3]}`)
if (typeof process.argv[2] !== 'undefined') {
sourceDirectory = process.argv[2];
}
else {
/* rl.question(FIRST_QUESTION, (firstAnswer) => {
sourceDirectory = firstAnswer;
rl.close();
}); */
sourceDirectory = rls.question(FIRST_QUESTION);
}
if (checkSourcePath(sourceDirectory) == 0) {
getKeepData();
}
else {
return;
}
if (typeof process.argv[3] !== 'undefined') {
destinationDirectory = process.argv[3];
}
else {
/* rl.question(SECOND_QUESTION, (secondAnswer) => {
destinationDirectory = secondAnswer;
rl.close();
}); */
destinationDirectory = rls.question(SECOND_QUESTION);
}
if (checkDestPath(destinationDirectory) == 0) {
convertData();
}
else {
return;
}
}
main();