Skip to content

Commit

Permalink
[ #53 , JSON output option ] (#73)
Browse files Browse the repository at this point in the history
* [ #53 , JSON output to disk] write file objects to files.json
  • Loading branch information
mohamedsalem401 committed Nov 28, 2023
1 parent 3aa8349 commit aedd641
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-apes-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mddb": minor
---

[ #53 , JSON output option ] Implemented functionality to write file JSON output to disk
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
dist/
markdown.db
*.tgz
.markdowndb/
16 changes: 16 additions & 0 deletions src/lib/markdowndb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
mapFileTagsToInsert,
getUniqueValues,
} from "./databaseUtils.js";
import fs from "fs";

const defaultFilePathToUrl = (filePath: string) => {
let url = filePath
Expand Down Expand Up @@ -76,6 +77,7 @@ export class MarkdownDB {
.filter(isLinkToDefined);
const fileTagsToInsert = fileObjects.flatMap(mapFileTagsToInsert);

writeJsonToFile(".markdowndb/files.json", fileObjects);
await MddbFile.batchInsert(this.db, filesToInsert);
await MddbTag.batchInsert(this.db, tagsToInsert);
await MddbFileTag.batchInsert(this.db, fileTagsToInsert);
Expand Down Expand Up @@ -191,3 +193,17 @@ export class MarkdownDB {
this.db.destroy();
}
}

function writeJsonToFile(filePath: string, jsonData: any[]) {
try {
const directory = path.dirname(filePath);
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}

const jsonString = JSON.stringify(jsonData, null, 2);
fs.writeFileSync(filePath, jsonString);
} catch (error: any) {
console.error(`Error writing data to ${filePath}: ${error}`);
}
}

0 comments on commit aedd641

Please sign in to comment.