* Checks a file for changes * @param {EditorFile} file File to check * @returns {Promise }
(file)
| 51 | * @returns {Promise<void>} |
| 52 | */ |
| 53 | async function checkFile(file) { |
| 54 | if (file === undefined || !file.loaded || file.loading) return; |
| 55 | |
| 56 | if (file.uri) { |
| 57 | const fs = fsOperation(file.uri); |
| 58 | const exists = await fs.exists(); |
| 59 | |
| 60 | if (!exists && !file.readOnly) { |
| 61 | file.isUnsaved = true; |
| 62 | file.uri = null; |
| 63 | editorManager.onupdate("file-changed"); |
| 64 | editorManager.emit("update", "file-changed"); |
| 65 | await new Promise((resolve) => { |
| 66 | alert( |
| 67 | strings.info, |
| 68 | strings["file has been deleted"].replace("{file}", file.filename), |
| 69 | resolve, |
| 70 | ); |
| 71 | }); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | let mtime = null; |
| 76 | if (file.hasVersionMetadata && file.savedMtime != null) { |
| 77 | const stat = await fs.stat().catch(() => null); |
| 78 | mtime = helpers.getStatMtime(stat); |
| 79 | if (mtime != null) { |
| 80 | if (mtime === file.savedMtime) return; |
| 81 | const alreadyWarnedConflict = |
| 82 | file.hasDiskConflict && file.diskMtime === mtime; |
| 83 | file.markDiskChanged({ mtime }); |
| 84 | if (file.hasDiskConflict) { |
| 85 | editorManager.onupdate("file-changed"); |
| 86 | editorManager.emit("update", "file-changed"); |
| 87 | console.warn( |
| 88 | `File changed on disk while unsaved: ${file.filename}`, |
| 89 | ); |
| 90 | if (!alreadyWarnedConflict) { |
| 91 | await new Promise((resolve) => { |
| 92 | alert( |
| 93 | strings.warning.toUpperCase(), |
| 94 | `${file.filename} changed on disk while you have unsaved edits. Saving now may overwrite the external changes.`, |
| 95 | resolve, |
| 96 | ); |
| 97 | }); |
| 98 | } |
| 99 | return; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (file.isUnsaved) return; |
| 105 | |
| 106 | const text = await fs.readFile(file.encoding); |
| 107 | const diskDoc = Text.of(String(text ?? "").split("\n")); |
| 108 | const currentDoc = file.session?.doc; |
| 109 | |
| 110 | if (!currentDoc?.eq?.(diskDoc)) { |
no test coverage detected