forked from sebastiande/insomnia-plugin-git-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSync.js
More file actions
107 lines (96 loc) · 3.88 KB
/
Sync.js
File metadata and controls
107 lines (96 loc) · 3.88 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
const simpleGit = require('simple-git');
const commandExistsSync = require('command-exists').sync;
const Workspace = require('./Workspace');
const Settings = require('./Settings');
class Sync {
async setupRepoUrl(context, data) {
if (!data || !data.workspace || !data.workspace._id) {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Error reading workspace',
'Could not get workspace config');
return;
}
let repoUrl = false;
try {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
repoUrl = await context.app.prompt(
'Insert the GIT Repository URL for Workspace ' + data.workspace.name,
{
defaultValue: await this.getRepoUrl(context, data.workspace._id),
placeholder: 'git@github.com:sebastiande/insomnia-plugin-git-sync.git',
submitName: 'Save',
cancelable: true
}
);
} catch (error) {
// ignore
console.error(error);
}
if (repoUrl === false) {
return;
}
Settings.set(context, data.workspace._id, 'repo', repoUrl);
Settings.set(context, 'global', 'last_repo', repoUrl);
}
async getRepoUrl(context, workspaceId, fallback) {
let repo = await Settings.get('repo', context, workspaceId) || false;
if (repo === false && fallback === true) {
repo = await Settings.get('last_repo', context, workspaceId) || false;
}
if (repo === false) {
return '';
}
return repo;
}
async isSetup(context, data) {
if (!context || !data || !data.workspace || !data.workspace._id) {
return false;
}
const repoUrl = await this.getRepoUrl(context, data.workspace._id, false);
if (repoUrl === '') {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Error doing action',
'Please setup at least one GIT repository url for this or any workspace first!');
return false;
}
try {
const sGit = this.getSimpleGit(context, data.workspace);
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
const isGit = await sGit.checkIsRepo('root'); // using the enum does not work on linux
if (!isGit) {
await this.initialiseRepo(sGit, context, data.workspace);
}
} catch(error) {
console.error(error);
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Error doing action',
'Initializing GIT repository failed. Please delete the project folder: '
+ Workspace.getWorkingDir(data.workspace));
return false;
}
return true;
}
/* helper methods */
getSimpleGit(context, scope) {
const folder = Workspace.getWorkingDir(scope);
if (folder === false) {
return false;
}
if (!commandExistsSync('git')) {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Error doing action',
'Seems git is not installed. ' +
'Git needs to be installed and reachable via command git on the console!');
return false;
}
// noinspection JSCheckFunctionSignatures
return simpleGit(folder);
}
async initialiseRepo(sGit, context, workspace) {
const workDirectory = Workspace.getWorkingDir(workspace);
const repoUrl = await this.getRepoUrl(context, workspace._id, false);
await sGit.clone(repoUrl, workDirectory);
await sGit.checkout('insomnia');
}
}
module.exports = new Sync();