|
1 | | -import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 1 | +import { |
| 2 | + existsSync, |
| 3 | + mkdirSync, |
| 4 | + readFileSync, |
| 5 | + renameSync, |
| 6 | + rmSync, |
| 7 | + writeFileSync, |
| 8 | +} from "node:fs"; |
2 | 9 | import path from "node:path"; |
3 | 10 | import JSZip from "jszip"; |
4 | 11 | import { requestUrl } from "obsidian"; |
@@ -32,27 +39,71 @@ export class SlidesExtendedDistribution { |
32 | 39 | async update() { |
33 | 40 | const version = this.plugin.manifest.version; |
34 | 41 | const downloadUrl = `https://github.com/ebullient/obsidian-slides-extended/releases/download/${version}/slides-extended.zip`; |
35 | | - const response = await requestUrl(downloadUrl); |
36 | | - if (response.status !== 200) { |
37 | | - console.error(`Failed to download ${downloadUrl}`); |
38 | | - return; |
| 42 | + |
| 43 | + // Backup existing dist directory before attempting update |
| 44 | + // Use dist-backup instead of dist/.backup to avoid issues with trailing slashes |
| 45 | + const backupDir = path.join(this.pluginDirectory, "dist-backup"); |
| 46 | + let didBackup = false; |
| 47 | + |
| 48 | + if (existsSync(this.distDirectory)) { |
| 49 | + console.debug( |
| 50 | + "Backing up existing distribution files before update", |
| 51 | + ); |
| 52 | + // Remove any existing backup first |
| 53 | + if (existsSync(backupDir)) { |
| 54 | + rmSync(backupDir, { recursive: true, force: true }); |
| 55 | + } |
| 56 | + renameSync(this.distDirectory, backupDir); |
| 57 | + didBackup = true; |
39 | 58 | } |
40 | 59 |
|
41 | | - const zip = new JSZip(); |
42 | | - const contents = await zip.loadAsync(response.arrayBuffer); |
43 | | - const pluginDirectory = this.pluginDirectory; |
44 | | - |
45 | | - for (const filename of Object.keys(contents.files)) { |
46 | | - if (!contents.files[filename].dir) { |
47 | | - zip.file(filename) |
48 | | - .async("nodebuffer") |
49 | | - .then((content) => { |
50 | | - const dest = path.join(pluginDirectory, filename); |
51 | | - const dir = path.dirname(dest); |
52 | | - mkdirSync(dir, { recursive: true }); |
53 | | - writeFileSync(dest, content); |
| 60 | + try { |
| 61 | + const response = await requestUrl(downloadUrl); |
| 62 | + if (response.status !== 200) { |
| 63 | + throw new Error( |
| 64 | + `Failed to download ${downloadUrl}: HTTP ${response.status}`, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + const zip = new JSZip(); |
| 69 | + const contents = await zip.loadAsync(response.arrayBuffer); |
| 70 | + const pluginDirectory = this.pluginDirectory; |
| 71 | + |
| 72 | + for (const filename of Object.keys(contents.files)) { |
| 73 | + if (!contents.files[filename].dir) { |
| 74 | + zip.file(filename) |
| 75 | + .async("nodebuffer") |
| 76 | + .then((content) => { |
| 77 | + const dest = path.join(pluginDirectory, filename); |
| 78 | + const dir = path.dirname(dest); |
| 79 | + mkdirSync(dir, { recursive: true }); |
| 80 | + writeFileSync(dest, content); |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Update successful, remove backup |
| 86 | + if (didBackup && existsSync(backupDir)) { |
| 87 | + console.debug("Update successful, removing backup"); |
| 88 | + rmSync(backupDir, { recursive: true, force: true }); |
| 89 | + } |
| 90 | + } catch (error) { |
| 91 | + console.error("Failed to update distribution files:", error); |
| 92 | + |
| 93 | + // Restore backup on failure |
| 94 | + if (didBackup && existsSync(backupDir)) { |
| 95 | + console.debug("Restoring backup due to update failure"); |
| 96 | + // Remove partial update if it exists |
| 97 | + if (existsSync(this.distDirectory)) { |
| 98 | + rmSync(this.distDirectory, { |
| 99 | + recursive: true, |
| 100 | + force: true, |
54 | 101 | }); |
| 102 | + } |
| 103 | + renameSync(backupDir, this.distDirectory); |
55 | 104 | } |
| 105 | + |
| 106 | + throw error; |
56 | 107 | } |
57 | 108 | } |
58 | 109 | } |
0 commit comments