forked from sebastiande/insomnia-plugin-git-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspace.js
More file actions
95 lines (85 loc) · 3.3 KB
/
Workspace.js
File metadata and controls
95 lines (85 loc) · 3.3 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
const fs = require('fs');
class Workspace {
importProject(context, data) {
const impFilename = this.getWorkspaceFile(data);
if (!fs.existsSync(impFilename)) {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.app.alert('Error importing',
'Seems there is no configuration within your repository that can be read! Push first!');
return false;
}
fs.readFile(impFilename, "utf8", function (err, fileContent) {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures
context.data.import.raw(fileContent, {
workspaceId: data.workspace._id,
});
});
return true;
}
async exportProject(context, data) {
// noinspection JSUnresolvedVariable,JSCheckFunctionSignatures,JSUnresolvedFunction
let exp = await context.data.export.insomnia({
includePrivate: false,
format: 'json',
workspace: data.workspace,
});
// modify data to not have that much conflicts and fix environment imports
exp = exp.replaceAll(data.workspace._id, '__WORKSPACE_ID__');
const expObj = JSON.parse(exp);
expObj.__export_date = '2021-10-03T17:27:43.046Z';
expObj.__export_source = 'insomnia.desktop.app:v2021.6.0';
let tmpArr = [];
for (let i = 0; i < expObj.resources.length; i++) {
if (expObj.resources[i]._type === 'api_spec'
|| expObj.resources[i]._type === 'cookie_jar') {
// remove this as it makes many merge conflicts
continue;
}
expObj.resources[i].modified = '1637671845661';
if (expObj.resources[i]._type !== 'environment') {
tmpArr.push(expObj.resources[i]);
continue;
}
if (expObj.resources[i].parentId === '__WORKSPACE_ID__') {
expObj.resources[i]._id = '__BASE_ENVIRONMENT_ID__';
tmpArr.push(expObj.resources[i]);
continue;
}
if (expObj.resources[i].parentId.startsWith('env_')) {
expObj.resources[i].parentId = '__BASE_ENVIRONMENT_ID__';
}
tmpArr.push(expObj.resources[i]);
}
expObj.resources = tmpArr;
const expFilename = this.getWorkspaceFile(data);
fs.writeFileSync(expFilename, JSON.stringify(expObj, null, 2));
return expFilename;
}
getWorkingDir(workspace) {
if (!workspace || !workspace._id) {
return false;
}
let folder = __dirname + '/git';
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
folder = __dirname + '/git/' + workspace._id;
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder);
}
return folder;
}
/* helper methods */
sanitizeString(str) {
str = str.replace(/[^a-z0-9\._-]/gim, "_");
return str.trim();
}
getWorkspaceFile(data) {
const filename = data.workspace.name
.toLowerCase()
.replace(/[^\w\s-]/g, '-')
.replace(/[-\s]+/g, '-');
return this.getWorkingDir(data.workspace) + '/' + filename + '-insomnia.json';
}
}
module.exports = new Workspace();