Skip to content

Commit

Permalink
publishMultiple: stop if nothing to publish (#433)
Browse files Browse the repository at this point in the history
* publishMultiple: stop if nothing to publish

* commands: validate github settings before commands
  • Loading branch information
julesvirallinen authored Sep 30, 2023
1 parent 8fe967b commit 9dac3cb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 41 deletions.
14 changes: 14 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export default class DigitalGarden extends Plugin {
metadataCache,
this.settings,
);
publisher.validateSettings();

const siteManager = new DigitalGardenSiteManager(
metadataCache,
Expand All @@ -191,6 +192,18 @@ export default class DigitalGarden extends Plugin {
const filesToDelete = publishStatus.deletedNotePaths;
const imagesToDelete = publishStatus.deletedImagePaths;

const totalItems =
filesToPublish.length +
filesToDelete.length +
imagesToDelete.length;

if (totalItems === 0) {
new Notice("Garden is already fully synced!");
statusBarItem.remove();

return;
}

const statusBar = new PublishStatusBar(
statusBarItem,
filesToPublish.length +
Expand Down Expand Up @@ -387,6 +400,7 @@ export default class DigitalGarden extends Plugin {
metadataCache,
this.settings,
);
publisher.validateSettings();
const publishSuccessful = await publisher.publish(activeFile);

if (publishSuccessful) {
Expand Down
66 changes: 25 additions & 41 deletions src/publisher/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,7 @@ export default class Publisher {
}

async delete(path: string): Promise<boolean> {
if (!this.settings.githubRepo) {
new Notice(
"Config error: You need to define a GitHub repo in the plugin settings",
);
throw {};
}

if (!this.settings.githubUserName) {
new Notice(
"Config error: You need to define a GitHub Username in the plugin settings",
);
throw {};
}

if (!this.settings.githubToken) {
new Notice(
"Config error: You need to define a GitHub Token in the plugin settings",
);
throw {};
}

this.validateSettings();
const octokit = new Octokit({ auth: this.settings.githubToken });

const payload = {
Expand Down Expand Up @@ -173,26 +153,7 @@ export default class Publisher {
}

async uploadToGithub(path: string, content: string) {
if (!this.settings.githubRepo) {
new Notice(
"Config error: You need to define a GitHub repo in the plugin settings",
);
throw {};
}

if (!this.settings.githubUserName) {
new Notice(
"Config error: You need to define a GitHub Username in the plugin settings",
);
throw {};
}

if (!this.settings.githubToken) {
new Notice(
"Config error: You need to define a GitHub Token in the plugin settings",
);
throw {};
}
this.validateSettings();

const octokit = new Octokit({ auth: this.settings.githubToken });

Expand Down Expand Up @@ -249,4 +210,27 @@ export default class Publisher {
await this.uploadImage(image.path, image.content);
}
}

validateSettings() {
if (!this.settings.githubRepo) {
new Notice(
"Config error: You need to define a GitHub repo in the plugin settings",
);
throw {};
}

if (!this.settings.githubUserName) {
new Notice(
"Config error: You need to define a GitHub Username in the plugin settings",
);
throw {};
}

if (!this.settings.githubToken) {
new Notice(
"Config error: You need to define a GitHub Token in the plugin settings",
);
throw {};
}
}
}

0 comments on commit 9dac3cb

Please sign in to comment.