forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckFiles.js
More file actions
143 lines (126 loc) · 3.51 KB
/
Copy pathcheckFiles.js
File metadata and controls
143 lines (126 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import fsOperation from "fileSystem";
import { Text } from "@codemirror/state";
import alert from "dialogs/alert";
import confirm from "dialogs/confirm";
import helpers from "utils/helpers";
let checkFileEnabled = true;
Object.defineProperty(checkFiles, "check", {
set(value) {
checkFileEnabled = value;
},
get() {
return checkFileEnabled;
},
});
export default async function checkFiles() {
if (!editorManager) return;
if (checkFileEnabled === false) {
checkFileEnabled = true;
return;
}
const files = editorManager.files;
// @ts-check
/** @type {{ editor: import('@codemirror/view').EditorView }} */
const { editor } = editorManager;
recursiveFileCheck([...files]);
/**
* Checks if the file has been changed
* @param {EditorFile[]} files List of files to check
*/
async function recursiveFileCheck(files) {
const file = files.pop();
await checkFile(file);
if (files.length) {
recursiveFileCheck(files);
}
return;
}
/**
* @typedef {import('./editorFile').default} EditorFile
*/
/**
* Checks a file for changes
* @param {EditorFile} file File to check
* @returns {Promise<void>}
*/
async function checkFile(file) {
if (file === undefined || !file.loaded || file.loading) return;
if (file.uri) {
const fs = fsOperation(file.uri);
const exists = await fs.exists();
if (!exists && !file.readOnly) {
file.isUnsaved = true;
file.uri = null;
editorManager.onupdate("file-changed");
editorManager.emit("update", "file-changed");
await new Promise((resolve) => {
alert(
strings.info,
strings["file has been deleted"].replace("{file}", file.filename),
resolve,
);
});
return;
}
let mtime = null;
if (file.hasVersionMetadata && file.savedMtime != null) {
const stat = await fs.stat().catch(() => null);
mtime = helpers.getStatMtime(stat);
if (mtime != null) {
if (mtime === file.savedMtime) return;
const alreadyWarnedConflict =
file.hasDiskConflict && file.diskMtime === mtime;
file.markDiskChanged({ mtime });
if (file.hasDiskConflict) {
editorManager.onupdate("file-changed");
editorManager.emit("update", "file-changed");
console.warn(
`File changed on disk while unsaved: ${file.filename}`,
);
if (!alreadyWarnedConflict) {
await new Promise((resolve) => {
alert(
strings.warning.toUpperCase(),
`${file.filename} changed on disk while you have unsaved edits. Saving now may overwrite the external changes.`,
resolve,
);
});
}
return;
}
}
}
if (file.isUnsaved) return;
const text = await fs.readFile(file.encoding);
const diskDoc = Text.of(String(text ?? "").split("\n"));
const currentDoc = file.session?.doc;
if (!currentDoc?.eq?.(diskDoc)) {
try {
const confirmation = await confirm(
strings.warning.toUpperCase(),
file.filename + strings["file changed"],
);
if (!confirmation) return;
const cursorPos = editor.getCursorPosition();
editorManager.getFile(file.id, "id")?.makeActive();
file.markChanged = false;
try {
file.session.setValue(text);
file.markLoaded({ mtime });
} finally {
file.markChanged = true;
}
await file.writeToCache();
editor.gotoLine(cursorPos.row, cursorPos.column);
} catch (error) {
// ignore
}
} else if (mtime != null && file.hasVersionMetadata) {
file.markLoaded({ mtime });
}
}
}
if (!editorManager.activeFile) {
app.focus();
}
}