|
| 1 | +import { httpGet } from "./api.mjs"; |
| 2 | +import { areAutoUpdateChecksDisabled, storeAutoUpdateChecksDisabled } from "./session.mjs"; |
| 3 | + |
| 4 | +// Conflict with the DOM File type. |
| 5 | +const TiledFile = /** @type {any} */ (File); |
| 6 | + |
| 7 | +function isGit() { |
| 8 | + const p = new Process(); |
| 9 | + p.workingDirectory = tiled.project.extensionsPath; |
| 10 | + const status = p.exec('git', ['status'], false); |
| 11 | + return status === 0; |
| 12 | +} |
| 13 | + |
| 14 | +function pullGit() { |
| 15 | + const p = new Process(); |
| 16 | + p.workingDirectory = tiled.project.extensionsPath; |
| 17 | + const status = p.exec('git', ['pull'], false); |
| 18 | + if (status !== 0) { |
| 19 | + tiled.alert(`Failed to pull updates from Git: ${p.readStdErr()}`); |
| 20 | + } else { |
| 21 | + tiled.log(p.readStdOut()); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Recursively retrieves all files in a directory and its subdirectories that |
| 27 | + * are relevant to the update process. |
| 28 | + * @param {string} dir Directory to inspect for files |
| 29 | + * @param {string[]} prefix Base directory for relative paths |
| 30 | + * @returns {Record<string, string>} |
| 31 | + */ |
| 32 | +function getAllUpdaterFiles(dir, prefix = []) { |
| 33 | + const /** @type {Record<string, string>} */ files = {}; |
| 34 | + for (const fileName of TiledFile.directoryEntries(dir, 2, 0)) { |
| 35 | + if (!fileName.endsWith('.mjs')) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + const file = new TextFile(`${dir}/${fileName}`, TextFile.ReadOnly); |
| 39 | + files[[...prefix, fileName].join('/')] = file.readAll(); |
| 40 | + file.close(); |
| 41 | + } |
| 42 | + for (const directory of TiledFile.directoryEntries(dir, 24577, 0)) { |
| 43 | + Object.assign(files, getAllUpdaterFiles(directory, [...prefix, directory])); |
| 44 | + } |
| 45 | + return files; |
| 46 | +} |
| 47 | + |
| 48 | +function update() { |
| 49 | + if (areAutoUpdateChecksDisabled()) { |
| 50 | + return; |
| 51 | + } |
| 52 | + const scriptPath = FileInfo.joinPaths(tiled.project.extensionsPath, 'includes'); |
| 53 | + const localFiles = getAllUpdaterFiles(scriptPath); |
| 54 | + httpGet('https://maps.undertale.wiki/bundle.json').then(bundle => { |
| 55 | + const onlyLocalFiles = Object.keys(localFiles).filter(path => !(path in bundle)); |
| 56 | + const diffFiles = Object.entries(bundle).filter(([path, content]) => { |
| 57 | + const filePath = FileInfo.joinPaths(scriptPath, path); |
| 58 | + if (!TiledFile.exists(filePath)) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + const file = new TextFile(filePath, TextFile.ReadOnly); |
| 62 | + const localContent = file.readAll(); |
| 63 | + file.close(); |
| 64 | + return localContent !== content; |
| 65 | + }); |
| 66 | + if (onlyLocalFiles.length === 0 && diffFiles.length === 0) { |
| 67 | + tiled.log('Extension is up to date.'); |
| 68 | + return; |
| 69 | + } |
| 70 | + if (isGit()) { |
| 71 | + if (tiled.confirm('Updates are available for the wiki extension, but you are using Git. Do you want to run git pull?')) { |
| 72 | + pullGit(); |
| 73 | + } |
| 74 | + return; |
| 75 | + } |
| 76 | + if (!tiled.confirm(`Updates are available for the wiki extension! Do you want to update now?`)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + for (const path of onlyLocalFiles) { |
| 80 | + TiledFile.remove(FileInfo.joinPaths(scriptPath, path)); |
| 81 | + } |
| 82 | + for (const [path, content] of diffFiles) { |
| 83 | + const filePath = FileInfo.joinPaths(scriptPath, path); |
| 84 | + const file = new TextFile(filePath, TextFile.WriteOnly); |
| 85 | + file.write(content); |
| 86 | + file.commit(); |
| 87 | + } |
| 88 | + tiled.alert('Extension updated successfully! Please restart Tiled to apply the updates.'); |
| 89 | + }).catch(error => { |
| 90 | + tiled.alert(`Failed to check for updates: ${error.message}. Check the console for more details.`); |
| 91 | + tiled.log(`Error details: ${error.stack}`); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +const enablePopup = tiled.registerAction('WikiDisableUpdateChecks', () => { |
| 96 | + storeAutoUpdateChecksDisabled(enablePopup.checked); |
| 97 | +}); |
| 98 | +enablePopup.checkable = true; |
| 99 | +enablePopup.checked = areAutoUpdateChecksDisabled(); |
| 100 | +enablePopup.iconVisibleInMenu = false; |
| 101 | +enablePopup.text = 'Wiki: Disable update checks'; |
| 102 | + |
| 103 | +tiled.extendMenu('Help', [ |
| 104 | + { |
| 105 | + action: 'WikiDisableUpdateChecks' |
| 106 | + } |
| 107 | +]); |
| 108 | + |
| 109 | +update(); |
0 commit comments