-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin_updater.js
184 lines (169 loc) · 6.12 KB
/
plugin_updater.js
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
var fs = require('fs'),
cwd = process.cwd(),
unirest = require('unirest'),
_ = require('lodash'),
inquirer = require('inquirer');
const API_GENERATED_FIELDS = ['last-updated', 'last-modified-by'];
class PluginUpdater {
constructor (config) {
var that = this;
this.config = config;
this.pluginPath = cwd + '/' + config.pluginPath;
this.pluginDataPath = cwd + '/' + config.pluginData;
this.headers = {
'Authorization': 'Token token=' + config.apiToken,
'Content-Type': 'application/json',
'Accept': 'application/json'
};
this.initPluginData();
}
initPluginData () {
try {
this.pluginData = require(this.pluginDataPath);
} catch (e) {
this.pluginData = {};
}
if (this.pluginData._id) {
this.updatePluginData();
} else {
this.selectOrCreatePlugin();
}
}
selectOrCreatePlugin () {
var that = this;
unirest.get(this.config.api + 'plugin?org=' + this.config.org)
.headers(this.headers)
.end(function (response) {
if (response.statusCode === 200) {
var plugins = response.body;
var choices = plugins.map(function (plugin) {
return {
name: plugin.name + ' (id: ' + plugin.id + ')',
value: plugin._id
};
});
// choices.push(new inquirer.Separator());
// choices.push({
// name: 'Create a new plugin (currently not supported)',
// value: 'new'
// });
inquirer.prompt([{
type: 'rawlist',
name: 'pluginChoice',
message: '-- Choose a plugin for local development --',
choices: choices
}]).then(function (data) {
if (data.pluginChoice === 'new') {
console.log('To-do: create a new plugin');
} else {
that.pluginData = {
_id: data.pluginChoice
};
that.updatePluginData();
}
});
} else {
console.warn('Couldn\'t get existing plugins', response.statusCode);
process.exit();
}
});
}
updatePluginData () {
var that = this;
unirest.get(this.config.api + 'plugin/' + this.pluginData._id + '?org=' + this.config.org)
.headers(this.headers)
.end(function (response) {
if (response.statusCode === 200) {
var remotePluginData = response.body;
try {
var localPlugin = fs.readFileSync(that.pluginPath).toString('utf8');
if (remotePluginData.script !== localPlugin) {
var backupPath = cwd + '/backup-';
console.warn('Warning: Local plugin code differs from the remote version!');
inquirer.prompt([{
type: 'rawlist',
name: 'keepFile',
message: '-- Choose which file to keep (other will be backed up) --',
choices: ['Remote', 'Local']
}]).then(function (data) {
if (data.keepFile === 'Local') {
console.log('Overwriting remote with local version...');
backupPath += 'remote-' + Date.now() + '.js';
fs.writeFile(backupPath, remotePluginData.script, 'utf8');
// Overwrite to force a push
fs.writeFile(that.pluginPath, localPlugin, 'utf8');
} else {
backupPath += 'local-' + Date.now() + '.js';
fs.writeFile(backupPath, localPlugin, 'utf8');
fs.writeFile(that.pluginPath, remotePluginData.script, 'utf8');
console.log('Overwrote local with remote version.');
}
that.mergePluginData(remotePluginData);
});
} else {
that.mergePluginData(remotePluginData);
}
} catch (e) {
console.log('Created ' + that.pluginPath + ' from remote.');
fs.writeFile(that.pluginPath, remotePluginData.script, 'utf8');
that.mergePluginData(remotePluginData);
}
} else {
console.warn('Couldn\'t get remote plugin', response.statusCode);
process.exit();
}
});
}
mergePluginData (remotePluginData) {
var mergedPluginData = {},
that = this,
changes = 0;
_.each(remotePluginData, function (remoteVal, key) {
if (API_GENERATED_FIELDS.indexOf(key) !== -1) {
return;
} else if (!that.pluginData[key]) {
mergedPluginData[key] = remoteVal;
} else if (that.pluginData[key] !== remoteVal) {
changes++;
mergedPluginData[key] = remoteVal;
} else {
mergedPluginData[key] = remoteVal;
}
});
this.pluginData = mergedPluginData;
this.writeLocalPluginData();
console.log('Synced local plugin data from remote:');
if (changes === 0) {
console.log('All up-to-date.')
} else {
console.log('Forced updates to ' + this.pluginDataPath);
}
console.log('Waiting for changes...');
}
writeLocalPluginData () {
fs.writeFile(this.pluginDataPath, JSON.stringify(this.pluginData, null, 4), 'utf8');
}
push () {
var that = this;
this.pluginData.script = fs.readFileSync(this.pluginPath).toString('utf8');
this.pluginData.content = 'define("plugin/' + this.pluginData.id + '", [], function(){ return ' + this.pluginData.script + '});';
unirest.put(this.config.api + 'plugin/' + this.pluginData._id + '?org=' + this.config.org)
.headers(this.headers)
.send(this.pluginData)
.end(function (response) {
if (response.statusCode === 200) {
console.log('Successfully updated the plugin.');
that.pluginData._version += 1;
that.writeLocalPluginData();
console.log('Waiting for changes...');
} else if (response.statusCode === 409) {
console.warn('Local version differs from remote version, trying to merge...');
that.updatePluginData();
} else {
console.warn('Couldn\'t update the plugin', response.statusCode);
process.exit();
}
});
}
}
module.exports = PluginUpdater;