Skip to content

Commit

Permalink
Publish multiple files in one commits
Browse files Browse the repository at this point in the history
  • Loading branch information
oleeskild committed May 7, 2024
1 parent 6d38283 commit 01d12de
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 103 deletions.
71 changes: 13 additions & 58 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,84 +238,39 @@ export default class DigitalGarden extends Plugin {
imagesToDelete.length,
);

let errorFiles = 0;
let errorDeleteFiles = 0;
let errorDeleteImage = 0;

new Notice(
`Publishing ${filesToPublish.length} notes, deleting ${filesToDelete.length} notes and ${imagesToDelete.length} images. See the status bar in lower right corner for progress.`,
8000,
);

for (const file of filesToPublish) {
try {
statusBar.increment();
await publisher.publish(file);
} catch {
errorFiles++;

new Notice(
`Unable to publish note ${file.file.name}, skipping it.`,
);
}
}

for (const filePath of filesToDelete) {
try {
statusBar.increment();

// TODO: include sha from file.remoteHash to make faster!
await publisher.deleteNote(
filePath.path,
filePath.sha,
);
} catch {
errorDeleteFiles++;
await publisher.publishBatch(filesToPublish);
statusBar.incrementMultiple(filesToPublish.length);

new Notice(
`Unable to delete note ${filePath}, skipping it.`,
);
}
}

for (const filePath of imagesToDelete) {
try {
statusBar.increment();

await publisher.deleteImage(
filePath.path,
filePath.sha,
);
} catch {
errorDeleteImage++;
await publisher.deleteBatch(
filesToDelete.map((f) => f.path),
);
statusBar.incrementMultiple(filesToDelete.length);

new Notice(
`Unable to delete image ${filePath}, skipping it.`,
);
}
}
await publisher.deleteBatch(
imagesToDelete.map((f) => f.path),
);
statusBar.incrementMultiple(imagesToDelete.length);

statusBar.finish(8000);

new Notice(
`Successfully published ${
filesToPublish.length - errorFiles
} notes to your garden.`,
`Successfully published ${filesToPublish.length} notes to your garden.`,
);

if (filesToDelete.length > 0) {
new Notice(
`Successfully deleted ${
filesToDelete.length - errorDeleteFiles
} notes from your garden.`,
`Successfully deleted ${filesToDelete.length} notes from your garden.`,
);
}

if (imagesToDelete.length > 0) {
new Notice(
`Successfully deleted ${
imagesToDelete.length - errorDeleteImage
} images from your garden.`,
`Successfully deleted ${imagesToDelete.length} images from your garden.`,
);
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion manifest-beta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "digitalgarden",
"name": "Digital Garden",
"version": "2.56.2",
"version": "2.57.0",
"minAppVersion": "0.12.0",
"description": "Publish your notes to the web for others to enjoy. For free.",
"author": "Ole Eskild Steensen",
Expand Down
60 changes: 54 additions & 6 deletions src/publisher/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class Publisher {
return await this.delete(path, sha);
}
/** If provided with sha, garden connection does not need to get it seperately! */
async delete(path: string, sha?: string): Promise<boolean> {
public async delete(path: string, sha?: string): Promise<boolean> {
this.validateSettings();

const userGardenConnection = new RepositoryConnection({
Expand All @@ -115,7 +115,7 @@ export default class Publisher {
return !!deleted;
}

async publish(file: CompiledPublishFile): Promise<boolean> {
public async publish(file: CompiledPublishFile): Promise<boolean> {
if (!isPublishFrontmatterValid(file.frontmatter)) {
return false;
}
Expand All @@ -133,7 +133,55 @@ export default class Publisher {
}
}

async uploadToGithub(
public async deleteBatch(filePaths: string[]): Promise<boolean> {
if (filePaths.length === 0) {
return true;
}

try {
const userGardenConnection = new RepositoryConnection({
gardenRepository: this.settings.githubRepo,
githubUserName: this.settings.githubUserName,
githubToken: this.settings.githubToken,
});

await userGardenConnection.deleteFiles(filePaths);

return true;
} catch (error) {
console.error(error);

return false;
}
}

public async publishBatch(files: CompiledPublishFile[]): Promise<boolean> {
const filesToPublish = files.filter((f) =>
isPublishFrontmatterValid(f.frontmatter),
);

if (filesToPublish.length === 0) {
return true;
}

try {
const userGardenConnection = new RepositoryConnection({
gardenRepository: this.settings.githubRepo,
githubUserName: this.settings.githubUserName,
githubToken: this.settings.githubToken,
});

await userGardenConnection.updateFiles(filesToPublish);

return true;
} catch (error) {
console.error(error);

return false;
}
}

private async uploadToGithub(
path: string,
content: string,
remoteFileHash?: string,
Expand Down Expand Up @@ -167,18 +215,18 @@ export default class Publisher {
});
}

async uploadText(filePath: string, content: string, sha?: string) {
private async uploadText(filePath: string, content: string, sha?: string) {
content = Base64.encode(content);
const path = `${NOTE_PATH_BASE}${filePath}`;
await this.uploadToGithub(path, content, sha);
}

async uploadImage(filePath: string, content: string, sha?: string) {
private async uploadImage(filePath: string, content: string, sha?: string) {
const path = `src/site${filePath}`;
await this.uploadToGithub(path, content, sha);
}

async uploadAssets(assets: Assets) {
private async uploadAssets(assets: Assets) {
for (let idx = 0; idx < assets.images.length; idx++) {
const image = assets.images[idx];
await this.uploadImage(image.path, image.content, image.remoteHash);
Expand Down
Loading

0 comments on commit 01d12de

Please sign in to comment.