1
1
import { lstatSync , readdirSync , readFileSync } from 'node:fs'
2
2
import { markdownToBlocks } from '@tryfabric/martian'
3
3
import { addBlocks , createPage } from './notion_api.js'
4
- import { addImportStatus } from './db.js'
4
+ import { addImportStatus , loadImportStatuses } from './db.js'
5
5
6
6
7
7
const notesDir = '/home/phpusr/tmp/notes/NOTES/car'
8
8
const importedNotes = { }
9
9
await importNotes ( )
10
10
11
11
async function importNotes ( ) {
12
+ const importedNoteList = await loadImportStatuses ( )
13
+ importedNoteList . forEach ( noteInfo => importedNotes [ noteInfo . filePath ] = noteInfo )
14
+ console . info ( `\nLoaded already imported ${ importedNoteList . length } notes` )
15
+
12
16
// noinspection JSValidateTypes
13
17
const files = readdirSync ( notesDir , { recursive : true } )
14
18
. filter ( it => lstatSync ( `${ notesDir } /${ it } ` ) . isFile ( ) )
@@ -17,7 +21,7 @@ async function importNotes() {
17
21
for ( const relFilePath of files ) {
18
22
const progress = Math . round ( ++ fileIndex / files . length * 100 )
19
23
console . log ( `\n[${ progress } %][${ fileIndex } /${ files . length } ] ${ relFilePath } ` )
20
- console . log ( '-' . repeat ( 30 ) + '\n' )
24
+ console . log ( '-' . repeat ( 100 ) + '\n' )
21
25
22
26
const parentId = await createParentNotes ( relFilePath )
23
27
@@ -38,6 +42,8 @@ async function importNotes() {
38
42
//console.log(noteInfo)
39
43
await saveNoteToNotion ( noteInfo )
40
44
}
45
+
46
+ console . info ( '\nDONE' )
41
47
}
42
48
43
49
async function createParentNotes ( relFilePath ) {
@@ -61,7 +67,7 @@ async function createParentNotes(relFilePath) {
61
67
62
68
async function saveNoteToNotion ( noteInfo ) {
63
69
if ( importedNotes . hasOwnProperty ( noteInfo . filePath ) ) {
64
- console . info ( `Note "${ noteInfo . filePath } " already exists` )
70
+ console . info ( ` - Note "${ noteInfo . filePath } " already exists` )
65
71
return importedNotes [ noteInfo . filePath ]
66
72
}
67
73
@@ -76,7 +82,7 @@ async function saveNoteToNotion(noteInfo) {
76
82
await addBlocks ( { parentId : page . id , blocks} )
77
83
} catch ( e ) {
78
84
noteInfo . imported = false
79
- noteInfo . error = e
85
+ noteInfo . error = e . body || e
80
86
console . error ( 'ERROR' )
81
87
}
82
88
}
0 commit comments