forked from sebastiande/insomnia-plugin-git-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncToLocal.js
More file actions
73 lines (63 loc) · 2.81 KB
/
SyncToLocal.js
File metadata and controls
73 lines (63 loc) · 2.81 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
const Workspace = require('./Workspace');
const Settings = require('./Settings');
const Sync = require('./Sync');
class SyncToLocal {
async pull(context, data) {
const sGit = Sync.getSimpleGit(context, data.workspace);
if (await this.localChangesNotHandled(sGit, context, data)) {
return;
}
try {
await sGit.pull(['--no-rebase'], 'insomnia');
} catch(error) {
console.error(error);
await this.handleError(error, sGit, context, data);
return;
}
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Success!', 'Updated from server.');
Settings.set(context, data.workspace._id, 'SyncToLocal.FirstPullDone', true);
}
async localChangesNotHandled(sGit, context, data) {
if (await Settings.get('SyncToLocal.FirstPullDone', context, data.workspace._id) === false) {
return false;
}
const expFilename = await Workspace.exportProject(context, data);
if (!expFilename) {
return true;
}
const status = await sGit.status();
if (!status.modified || status.modified.length === 0) {
return false;
}
if (confirm('You have local changes, do you want to overwrite your local changes with the server version?')) {
const branchName = await sGit.revparse(['--abbrev-ref', 'HEAD']);
// noinspection JSUnresolvedVariable
await sGit.reset('hard', ['origin/' + branchName], () => {
Workspace.importProject(context, data);
});
return false;
}
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Skipped!', 'Project got not updated, please push your local changes or pull and overwrite them!');
return true;
}
async handleError(error, sGit, context, data) {
if (!error.message.includes('CONFLICT')) {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Error!', 'Could not update the project, more information can be found in console!');
return;
}
if (confirm('There are conflicts, do you want to overwrite your local changes with the server version?')) {
const branchName = await sGit.revparse(['--abbrev-ref', 'HEAD']);
// noinspection JSUnresolvedVariable
await sGit.reset('hard', ['origin/' + branchName], () => {
if (Workspace.importProject(context, data)) {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Finished', 'Project got reset to server version.');
}
});
}
}
}
module.exports = new SyncToLocal();