Skip to content

Commit

Permalink
Add a manual reload command for use in sandboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
pjeby committed Sep 3, 2022
1 parent 92dd77d commit fb35efa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const watchNeeded = window.process.platform !== "darwin" && window.process.platf

module.exports = class HotReload extends Plugin {

statCache = new Map(); // path -> Stat

onload() { this.app.workspace.onLayoutReady( this._onload.bind(this) ); }

async _onload() {
Expand All @@ -14,6 +16,12 @@ module.exports = class HotReload extends Plugin {
this.reindexPlugins = this.debouncedMethod(500, this.getPluginNames);
this.registerEvent( this.app.vault.on("raw", this.onFileChange.bind(this)) );
this.watch(".obsidian/plugins");
await this.checkVersions();
this.addCommand({
id: "scan-for-changes",
name: "Check plugins for changes and reload them",
callback: () => this.checkVersions()
})
}

watch(path) {
Expand All @@ -25,6 +33,22 @@ module.exports = class HotReload extends Plugin {
}
}

async checkVersions() {
const base = this.app.plugins.getPluginFolder();
for (const dir of Object.keys(this.pluginNames)) {
for (const file of ["manifest.json", "main.js", "styles.css", ".hotreload"]) {
const path = `${base}/${dir}/${file}`;
const stat = await app.vault.adapter.stat(path);
if (stat) {
if (this.statCache.has(path) && stat.mtime !== this.statCache.get(path).mtime) {
this.onFileChange(path);
}
this.statCache.set(path, stat);
}
}
}
}

async getPluginNames() {
const plugins = {}, enabled = new Set();
for (const {id, dir} of Object.values(app.plugins.manifests)) {
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "hot-reload",
"name": "Hot Reload",
"version": "0.1.9",
"minAppVersion": "0.11.13",
"version": "0.1.10",
"minAppVersion": "0.15.9",
"description": "Automatically reload in-development plugins when their files are changed",
"isDesktopOnly": true
}

0 comments on commit fb35efa

Please sign in to comment.