-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hot-Reloading Feature Implementation
- Loading branch information
1 parent
abaed38
commit a7fa720
Showing
15 changed files
with
1,197 additions
and
231 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import chokidar from "chokidar"; | ||
import { generateFileIdFromPath } from "../lib/markdowndb.js"; | ||
import { markdownToJson } from "../lib/markdownToJson.js"; | ||
import { Knex } from "knex"; | ||
import { FolderMdJsonData } from "../lib/FolderMdJsonData.js"; | ||
|
||
class MddbFolderWatcher { | ||
folderPath: any; | ||
watcher: chokidar.FSWatcher | null = null; | ||
folderMdJsonData: FolderMdJsonData; | ||
db: Knex; | ||
ready = false; | ||
|
||
constructor( | ||
db: Knex, | ||
folderMdJsonData: FolderMdJsonData, | ||
folderPath: string | ||
) { | ||
this.folderPath = folderPath; | ||
this.folderMdJsonData = folderMdJsonData; | ||
this.db = db; | ||
} | ||
|
||
createWatcher() { | ||
this.watcher = chokidar.watch(this.folderPath, { | ||
ignoreInitial: true, | ||
}); | ||
|
||
this.watcher.on("add", this.handleFileAdd.bind(this)); | ||
this.watcher.on("unlink", this.handleFileDelete.bind(this)); | ||
this.watcher.on("change", this.handleFileChange.bind(this)); | ||
this.watcher.on("ready", () => { | ||
this.ready = true; | ||
console.log("Initial scan complete. Chokidar is ready."); | ||
}); | ||
} | ||
|
||
async handleFileAdd(filePath: string, filePathsToIndex: string[]) { | ||
const newJson = markdownToJson(this.folderPath, filePath, filePathsToIndex); | ||
await this.folderMdJsonData.addFileJson(newJson); | ||
} | ||
|
||
handleFileDelete(filePath: string) { | ||
const fileId = generateFileIdFromPath(filePath); | ||
this.folderMdJsonData.deleteFile(fileId); | ||
} | ||
|
||
handleFileChange(filePath: string) { | ||
const newFileJson = markdownToJson(this.folderPath, filePath, []); | ||
const fileId = generateFileIdFromPath(filePath); | ||
this.folderMdJsonData.updateFileJson(fileId, newFileJson); | ||
} | ||
|
||
start() { | ||
if (this.watcher === null) { | ||
this.createWatcher(); | ||
} | ||
this.watcher!.add(this.folderPath); | ||
} | ||
|
||
stop() { | ||
if (this.watcher !== null) { | ||
this.watcher.close(); | ||
} | ||
} | ||
|
||
setDatabase(db: Knex<any, any[]>) { | ||
this.db = db; | ||
} | ||
} | ||
|
||
export default MddbFolderWatcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Knex } from "knex"; | ||
import { jsonDiff } from "./utils.js"; | ||
import { MddbFile } from "./schema.js"; | ||
import { FileJson } from "./FolderMdJsonData.js"; | ||
|
||
export class FileJsonDataManager { | ||
private db: Knex; | ||
private _files: FileJson[]; | ||
|
||
constructor(db: Knex, files: FileJson[]) { | ||
this.db = db; | ||
this._files = files; | ||
} | ||
|
||
async addFileJson(newFileJson: FileJson) { | ||
this._files.push(newFileJson); | ||
await MddbFile.insert(this.db, newFileJson.file); | ||
} | ||
|
||
async updateFileJson(fileId: string, newFileJson: FileJson) { | ||
const index = this.getFileIndex(fileId); | ||
if (index === -1) { | ||
throw new Error(`Failed to update non-sxising file ${fileId}`); | ||
} | ||
|
||
const oldJson = this._files[index]; | ||
this._files[index] = newFileJson; | ||
|
||
const fileDiff = jsonDiff(oldJson.file, newFileJson.file); | ||
if (Object.keys(fileDiff).length === 0) { | ||
return; | ||
} | ||
await MddbFile.updateFileProperties(this.db, fileId, fileDiff); | ||
} | ||
|
||
async deleteFile(fileId: string) { | ||
const index = this.getFileIndex(fileId); | ||
if (index === -1) { | ||
throw new Error("Failed to delete file"); | ||
} | ||
this._files.splice(index, 1); | ||
this._files.push(); | ||
await MddbFile.deleteById(this.db, fileId); | ||
} | ||
|
||
getFileIndex(fileId: string) { | ||
return this._files.findIndex((fileJson) => fileJson.file._id === fileId); | ||
} | ||
|
||
getFileJsonById(fileId: string) { | ||
return this._files.find((fileJson) => fileJson.file._id === fileId); | ||
} | ||
|
||
getFileJsonByUrlPath(urlPath: string) { | ||
return this._files.find((fileJson) => fileJson.file.url_path === urlPath); | ||
} | ||
|
||
getFileTags(fileId: string) { | ||
const fileJson = this.getFileJsonById(fileId); | ||
|
||
if (!fileJson) { | ||
throw new Error(`Unable to get non-existing file ${fileId}`); | ||
} | ||
|
||
return fileJson.tags; | ||
} | ||
|
||
getLinks(fileId: string) { | ||
const fileJson = this.getFileJsonById(fileId); | ||
|
||
if (!fileJson) { | ||
throw new Error(`Unable to get non-existing file ${fileId}`); | ||
} | ||
|
||
return fileJson.links; | ||
} | ||
|
||
get files() { | ||
return this._files; | ||
} | ||
} |
Oops, something went wrong.