Skip to content

Commit fcb2ebd

Browse files
committed
Added progress
1 parent 6439e02 commit fcb2ebd

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

src/db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ export async function addImportStatus({ noteId, title, filePath, imported, error
1212
const text = 'INSERT INTO imported_notes(note_id, title, file_path, imported, error) VALUES($1, $2, $3, $4, $5)'
1313
const values = [noteId, title, filePath, imported, error]
1414
const res = await pool.query(text, values)
15-
console.log(res)
15+
//console.log(res)
1616
}
1717

src/index.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ async function importNotes() {
1313
const files = readdirSync(notesDir, { recursive: true })
1414
.filter(it => lstatSync(`${notesDir}/${it}`).isFile())
1515

16+
let fileIndex = 0
1617
for (const relFilePath of files) {
17-
createParentNotes(relFilePath)
18+
const progress = Math.round(++fileIndex / files.length * 100)
19+
console.log(`\n[${progress}%][${fileIndex}/${files.length}] ${relFilePath}`)
20+
console.log('-'.repeat(30) + '\n')
21+
22+
const parentId = await createParentNotes(relFilePath)
1823

1924
let data = ''
2025
try {
21-
const filePath = `${notesDir}/${relFilePath}`
2226
data = readFileSync(`${notesDir}/${relFilePath}`, 'utf8')
2327
} catch (e) {
2428
console.error('error in file: ', relFilePath, '\n', e)
@@ -28,52 +32,55 @@ async function importNotes() {
2832
const noteInfo = {
2933
title: relFilePath.split('/').pop().split('.')[0],
3034
filePath: relFilePath,
35+
parentId,
3136
data
3237
}
33-
console.log(noteInfo)
38+
//console.log(noteInfo)
3439
await saveNoteToNotion(noteInfo)
3540
}
3641
}
3742

38-
function createParentNotes(relFilePath) {
39-
let dirPath = notesDir
40-
for (const dir of relFilePath.split('/').slice(0, -1)) {
41-
dirPath += '/' + dir
42-
if (importedNotes.hasOwnProperty(dirPath)) {
43-
console.info(`dir ${dirPath} already exists`)
44-
continue
43+
async function createParentNotes(relFilePath) {
44+
let dirPath = ''
45+
let parentId = ''
46+
for (const dirName of relFilePath.split('/').slice(0, -1)) {
47+
if (dirPath) {
48+
dirPath += '/'
4549
}
50+
dirPath += dirName
4651

47-
console.log('dir', dir)
52+
parentId = (await saveNoteToNotion({
53+
title: dirName,
54+
filePath: dirPath,
55+
parentId
56+
})).noteId
4857
}
58+
59+
return parentId
4960
}
5061

5162
async function saveNoteToNotion(noteInfo) {
5263
if (importedNotes.hasOwnProperty(noteInfo.filePath)) {
53-
console.info(`Note ${noteInfo.filePath} already exists`)
54-
return
64+
console.info(`Note "${noteInfo.filePath}" already exists`)
65+
return importedNotes[noteInfo.filePath]
5566
}
5667

57-
const page = await createPage({ title: noteInfo.title })
68+
const page = await createPage({ title: noteInfo.title, pageId: noteInfo.parentId })
5869
noteInfo.noteId = page.id
70+
noteInfo.imported = true
5971
importedNotes[noteInfo.filePath] = noteInfo
6072

61-
console.log(`\n${noteInfo.filePath}`)
62-
console.log('-'.repeat(30) + '\n')
63-
64-
if (!noteInfo.data) {
65-
return
66-
}
67-
68-
const blocks = markdownToBlocks(noteInfo.data)
69-
try {
70-
await addBlocks({ parentId: page.id, blocks })
71-
noteInfo.imported = true
72-
} catch (e) {
73-
noteInfo.imported = false
74-
noteInfo.error = JSON.stringify(e)
75-
console.error('ERROR')
73+
if (noteInfo.data) {
74+
const blocks = markdownToBlocks(noteInfo.data)
75+
try {
76+
await addBlocks({parentId: page.id, blocks})
77+
} catch (e) {
78+
noteInfo.imported = false
79+
noteInfo.error = e
80+
console.error('ERROR')
81+
}
7682
}
7783

7884
await addImportStatus(noteInfo)
85+
return noteInfo
7986
}

0 commit comments

Comments
 (0)