Skip to content

Commit

Permalink
[#45][s]: Implement how-reloading with chokidar.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsalem401 committed Dec 14, 2023
1 parent acb5183 commit 7cc1656
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 16 deletions.
82 changes: 70 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"types": "./dist/src/index.d.ts",
"dependencies": {
"@portaljs/remark-wiki-link": "^1.0.4",
"chokidar": "^3.5.3",
"gray-matter": "^4.0.3",
"knex": "^2.4.2",
"react-markdown": "^9.0.1",
Expand Down
9 changes: 7 additions & 2 deletions src/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { MarkdownDB } from "../lib/markdowndb.js";
// TODO get these from markdowndb.config.js or something
const dbPath = "markdown.db";
const ignorePatterns = [/Excalidraw/, /\.obsidian/, /DS_Store/];
const [contentPath] = process.argv.slice(2);
const [contentPath, watchFlag] = process.argv.slice(2);

if (!contentPath) {
throw new Error("Invalid/Missing path to markdown content folder");
}

const watchEnabled = watchFlag && watchFlag === "--watch";

const client = new MarkdownDB({
client: "sqlite3",
connection: {
Expand All @@ -23,6 +25,9 @@ await client.init();
await client.indexFolder({
folderPath: contentPath,
ignorePatterns: ignorePatterns,
watch: watchEnabled,
});

process.exit();
if (!watchEnabled) {
process.exit();
}
2 changes: 1 addition & 1 deletion src/lib/indexFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function indexFolder(
return files;
}

function shouldIncludeFile(
export function shouldIncludeFile(
filePath: string,
ignorePatterns?: RegExp[]
): boolean {
Expand Down
67 changes: 66 additions & 1 deletion src/lib/markdowndb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from "path";
import knex, { Knex } from "knex";

import { MddbFile, MddbTag, MddbLink, MddbFileTag } from "./schema.js";
import { indexFolder } from "./indexFolder.js";
import { indexFolder, shouldIncludeFile } from "./indexFolder.js";
import {
resetDatabaseTables,
mapFileToInsert,
Expand All @@ -13,6 +13,9 @@ import {
} from "./databaseUtils.js";
import fs from "fs";
import { CustomConfig } from "./CustomConfig.js";
import { FileInfo, processFile } from "./process.js";
import chokidar from "chokidar";
import { recursiveWalkDir } from "./recursiveWalkDir.js";

const defaultFilePathToUrl = (filePath: string) => {
let url = filePath
Expand Down Expand Up @@ -74,11 +77,13 @@ export class MarkdownDB {
ignorePatterns = [],
pathToUrlResolver = defaultFilePathToUrl,
customConfig = {},
watch = false,
}: {
folderPath: string;
ignorePatterns?: RegExp[];
pathToUrlResolver?: (filePath: string) => string;
customConfig?: CustomConfig;
watch?: boolean;
}) {
await resetDatabaseTables(this.db);

Expand All @@ -88,6 +93,66 @@ export class MarkdownDB {
customConfig,
ignorePatterns
);
await this.saveDataToDisk(fileObjects);

if (watch) {
const watcher = chokidar.watch(folderPath, {
ignoreInitial: true,
});

const filePathsToIndex = recursiveWalkDir(folderPath);
const computedFields = customConfig.computedFields || [];

const handleFileEvent = (event: string, filePath: string) => {
if (!shouldIncludeFile(filePath, ignorePatterns)) {
return;
}

if (event === "unlink") {
const index = fileObjects.findIndex(
(obj) => obj.file_path === filePath
);
if (index !== -1) {
fileObjects.splice(index, 1);
}
console.log(`File ${filePath} has been removed`);
return;
}

const fileObject = processFile(
folderPath,
filePath,
pathToUrlResolver,
filePathsToIndex,
computedFields
);
const index = fileObjects.findIndex(
(obj) => obj.file_path === filePath
);

if (index !== -1) {
fileObjects[index] = fileObject;
} else {
fileObjects.push(fileObject);
}

console.log(
`File ${filePath} has been ${event === "add" ? "added" : "updated"}`
);
};

watcher
.on("add", (filePath) => handleFileEvent("add", filePath))
.on("change", (filePath) => handleFileEvent("change", filePath))
.on("unlink", (filePath) => handleFileEvent("unlink", filePath))
.on("all", () => this.saveDataToDisk(fileObjects))
.on("error", (error) => console.error(`Watcher error: ${error}`));
}
}

private async saveDataToDisk(fileObjects: FileInfo[]) {
await resetDatabaseTables(this.db);

const filesToInsert = fileObjects.map(mapFileToInsert);
const uniqueTags = getUniqueValues(
fileObjects.flatMap((file) => file.tags)
Expand Down

0 comments on commit 7cc1656

Please sign in to comment.