-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.ts
281 lines (240 loc) · 7.82 KB
/
main.ts
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, Vault } from 'obsidian';
import { simpleGit, SimpleGit, CleanOptions, SimpleGitOptions } from 'simple-git';
import { setIntervalAsync, clearIntervalAsync } from 'set-interval-async';
let simpleGitOptions: Partial<SimpleGitOptions>;
let git: SimpleGit;
interface GHSyncSettings {
remoteURL: string;
gitLocation: string;
syncinterval: number;
isSyncOnLoad: boolean;
}
const DEFAULT_SETTINGS: GHSyncSettings = {
remoteURL: '',
gitLocation: '',
syncinterval: 0,
isSyncOnLoad: false,
}
export default class GHSyncPlugin extends Plugin {
settings: GHSyncSettings;
async SyncNotes()
{
new Notice("Syncing to GitHub remote")
const remote = this.settings.remoteURL;
simpleGitOptions = {
//@ts-ignore
baseDir: this.app.vault.adapter.getBasePath(),
binary: this.settings.gitLocation + "git",
maxConcurrentProcesses: 6,
trimmed: false,
};
git = simpleGit(simpleGitOptions);
let os = require("os");
let hostname = os.hostname();
let statusResult = await git.status().catch((e) => {
new Notice("Vault is not a Git repo or git binary cannot be found.", 10000);
return; })
//@ts-ignore
let clean = statusResult.isClean();
let date = new Date();
let msg = hostname + " " + date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + ":" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
// git add .
// git commit -m hostname-date-time
if (!clean) {
try {
await git
.add("./*")
.commit(msg);
} catch (e) {
new Notice(e);
return;
}
} else {
new Notice("Working branch clean");
}
// configure remote
try {
await git.removeRemote('origin').catch((e) => { new Notice(e); });
await git.addRemote('origin', remote).catch((e) => { new Notice(e); });
}
catch (e) {
new Notice(e);
return;
}
// check if remote url valid by fetching
try {
await git.fetch();
} catch (e) {
new Notice(e + "\nGitHub Sync: Invalid remote URL.", 10000);
return;
}
new Notice("GitHub Sync: Successfully set remote origin url");
// git pull origin main
try {
//@ts-ignore
await git.pull('origin', 'main', { '--no-rebase': null }, (err, update) => {
if (update) {
new Notice("GitHub Sync: Pulled " + update.summary.changes + " changes");
}
})
} catch (e) {
let conflictStatus = await git.status().catch((e) => { new Notice(e, 10000); return; });
let conflictMsg = "Merge conflicts in:";
//@ts-ignore
for (let c of conflictStatus.conflicted)
{
conflictMsg += "\n\t"+c;
}
conflictMsg += "\nResolve them or click sync button again to push with unresolved conflicts."
new Notice(conflictMsg)
//@ts-ignore
for (let c of conflictStatus.conflicted)
{
this.app.workspace.openLinkText("", c, true);
}
return;
}
// resolve merge conflicts
// git push origin main
if (!clean) {
try {
git.push('origin', 'main', ['-u']);
new Notice("GitHub Sync: Pushed on " + msg);
} catch (e) {
new Notice(e, 10000);
}
}
}
async onload() {
await this.loadSettings();
const ribbonIconEl = this.addRibbonIcon('github', 'Sync with Remote', (evt: MouseEvent) => {
this.SyncNotes();
});
ribbonIconEl.addClass('gh-sync-ribbon');
this.addCommand({
id: 'github-sync-command',
name: 'Sync with Remote',
callback: () => {
this.SyncNotes();
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new GHSyncSettingTab(this.app, this));
if (!isNaN(this.settings.syncinterval))
{
let interval: number = this.settings.syncinterval;
if (interval >= 1)
{
try {
setIntervalAsync(async () => {
await this.SyncNotes();
}, interval * 60 * 1000);
//this.registerInterval(setInterval(this.SyncNotes, interval * 6 * 1000));
new Notice("Auto sync enabled");
} catch (e) {
}
}
}
// check status
try {
simpleGitOptions = {
//@ts-ignore
baseDir: this.app.vault.adapter.getBasePath(),
binary: this.settings.gitLocation + "git",
maxConcurrentProcesses: 6,
trimmed: false,
};
git = simpleGit(simpleGitOptions);
//check for remote changes
// git branch --set-upstream-to=origin/main main
await git.branch({'--set-upstream-to': 'origin/main'});
let statusUponOpening = await git.fetch().status();
if (statusUponOpening.behind > 0)
{
// Automatically sync if needed
if (this.settings.isSyncOnLoad == true)
{
this.SyncNotes();
}
else
{
new Notice("GitHub Sync: " + statusUponOpening.behind + " commits behind remote.\nClick the GitHub ribbon icon to sync.")
}
}
else
{
new Notice("GitHub Sync: up to date with remote.")
}
} catch (e) {
// don't care
// based
}
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class GHSyncSettingTab extends PluginSettingTab {
plugin: GHSyncPlugin;
constructor(app: App, plugin: GHSyncPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
const howto = containerEl.createEl("div", { cls: "howto" });
howto.createEl("div", { text: "How to use this plugin", cls: "howto_title" });
howto.createEl("small", { text: "Grab your GitHub repository's HTTPS or SSH url and paste it into the settings here. If you're not authenticated, the first sync with this plugin should prompt you to authenticate. If you've already setup SSH on your device with GitHub, you won't need to authenticate - just paste your repo's SSH url into the settings here.", cls: "howto_text" });
howto.createEl("br");
const linkEl = howto.createEl('p');
linkEl.createEl('span', { text: 'See the ' });
linkEl.createEl('a', { href: 'https://github.com/kevinmkchin/Obsidian-GitHub-Sync/blob/main/README.md', text: 'README' });
linkEl.createEl('span', { text: ' for more information and troubleshooting.' });
new Setting(containerEl)
.setName('Remote URL')
.setDesc('')
.addText(text => text
.setPlaceholder('')
.setValue(this.plugin.settings.remoteURL)
.onChange(async (value) => {
this.plugin.settings.remoteURL = value;
await this.plugin.saveSettings();
})
.inputEl.addClass('my-plugin-setting-text'));
new Setting(containerEl)
.setName('[OPTIONAL] git binary location')
.setDesc('If git is not findable via your system PATH, then provide its location here')
.addText(text => text
.setPlaceholder('')
.setValue(this.plugin.settings.gitLocation)
.onChange(async (value) => {
this.plugin.settings.gitLocation = value;
await this.plugin.saveSettings();
})
.inputEl.addClass('my-plugin-setting-text2'));
new Setting(containerEl)
.setName('[OPTIONAL] Auto sync on startup')
.setDesc('Automatically sync when you start obsidian if there are unsynced changes')
.addToggle((toggle) => toggle
.setValue(this.plugin.settings.isSyncOnLoad)
.onChange(async (value) => {
this.plugin.settings.isSyncOnLoad = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('[OPTIONAL] Auto sync at interval')
.setDesc('Set a positive integer minute interval after which your vault is synced automatically. Auto sync is disabled if this field is left empty or not a positive integer. Restart Obsidan to take effect.')
.addText(text => text
.setValue(String(this.plugin.settings.syncinterval))
.onChange(async (value) => {
this.plugin.settings.syncinterval = Number(value);
await this.plugin.saveSettings();
}));
}
}