Skip to content

Commit 824bf9f

Browse files
committed
Added ability to show status and clean db
1 parent 3d064e8 commit 824bf9f

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { importNotes } from './src/notes_importer.js'
1+
import { getImportNotesStatus, importNotes } from './src/notes_importer.js'
2+
import { cleanDb } from './src/db.js'
23

34
await main()
45

@@ -13,6 +14,12 @@ async function main() {
1314
case '--import':
1415
await importNotes(args[1])
1516
return
17+
case '--status':
18+
await printStatus(args[1])
19+
return
20+
case '--clean-db':
21+
await cleanDb()
22+
return
1623
default:
1724
printHelp()
1825
}
@@ -21,6 +28,13 @@ async function main() {
2128
function printHelp() {
2229
console.info("--help - Print help")
2330
console.info("--import \"/data/notes\" - Import markdown notes from directory")
24-
console.info("--status - Show count of imported notes")
31+
console.info("--status \"/data/notes\" - Show count of imported notes")
2532
console.info("--clean-db - Clean local cache db with imported notes")
2633
}
34+
35+
async function printStatus(notesDir) {
36+
const { importedCount, notesCount, importedWithErrorsCount, importedWithoutErrorsCount, progress } = await getImportNotesStatus(notesDir)
37+
console.info(`Imported: ${importedCount}/${notesCount} [${progress}%]`)
38+
console.info(`Imported with errors: ${importedWithErrorsCount}`)
39+
console.info(`Imported without errors: ${importedWithoutErrorsCount}`)
40+
}

src/db.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ export async function addImportStatus({ noteId, title, filePath, imported, error
2020
})
2121
}
2222

23+
export async function cleanDb() {
24+
// noinspection SqlWithoutWhere
25+
await pool.query({
26+
text: 'DELETE FROM imported_notes'
27+
})
28+
console.info('The database has been cleared.')
29+
}

src/notes_importer.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ export async function importNotes(notesDir) {
1212
console.info(`\nLoaded already imported ${importedNoteList.length} notes`)
1313

1414
// noinspection JSValidateTypes
15-
const files = readdirSync(notesDir, { recursive: true })
16-
.filter(it => lstatSync(`${notesDir}/${it}`).isFile())
15+
const files = getNotesFiles(notesDir)
1716

1817
let fileIndex = 0
1918
for (const relFilePath of files) {
@@ -41,7 +40,7 @@ export async function importNotes(notesDir) {
4140
await saveNoteToNotion(noteInfo)
4241
}
4342

44-
console.info('\nDONE')
43+
console.info('\nDONE.')
4544
}
4645

4746
async function createParentNotes(relFilePath) {
@@ -63,6 +62,12 @@ async function createParentNotes(relFilePath) {
6362
return parentId
6463
}
6564

65+
function getNotesFiles(notesDir) {
66+
// noinspection JSValidateTypes
67+
return readdirSync(notesDir, { recursive: true })
68+
.filter(it => lstatSync(`${notesDir}/${it}`).isFile())
69+
}
70+
6671
async function saveNoteToNotion(noteInfo) {
6772
if (importedNotes.hasOwnProperty(noteInfo.filePath)) {
6873
console.info(` - Note "${noteInfo.filePath}" already exists`)
@@ -88,3 +93,19 @@ async function saveNoteToNotion(noteInfo) {
8893
await addImportStatus(noteInfo)
8994
return noteInfo
9095
}
96+
97+
export async function getImportNotesStatus(notesDir) {
98+
const notesCount = getNotesFiles(notesDir).length
99+
const importedNotes = await loadImportStatuses()
100+
const importedCount = importedNotes.length
101+
const importedWithErrorsCount = importedNotes.filter(it => !it.imported).length
102+
const importedWithoutErrorsCount = importedCount - importedWithErrorsCount
103+
104+
return {
105+
notesCount: notesCount,
106+
importedCount,
107+
importedWithErrorsCount,
108+
importedWithoutErrorsCount,
109+
progress: Math.round(importedCount / notesCount * 100)
110+
}
111+
}

0 commit comments

Comments
 (0)