/
theme-plugins-manager.js
393 lines (382 loc) · 10.9 KB
/
theme-plugins-manager.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
const {app} = require('electron');
const path = require('path');
const log = require('electron-log');
const fs = require('fs-extra');
const {ThemeInfo} = require('../../../main/models/theme-info');
const {PluginManager} = require('live-plugin-manager');
const semver = require('semver');
/**
* This is the main process interface.
*
* Manages themes packages. Installs, uninstalls and updates themes from it's
* repository or npm registry.
*/
class ThemePluginsManager {
/**
* Creates, if needed, the PluginManager and returns it.
* @return {PluginManager}
*/
get pluginManager() {
if (!this.__pluginManager) {
this.__pluginManager = new PluginManager({
cwd: process.env.ARC_THEMES,
pluginsPath: process.env.ARC_THEMES
});
}
return this.__pluginManager;
}
/**
* Creates a model for theme info file.
* @return {ThemeInfo}
*/
get themeInfo() {
return new ThemeInfo();
}
/**
* Resolves file path to correct path if it's starts with `~`.
*
* @param {String} file Settings file path
* @return {String} Path to the file.
*/
resolvePath(file) {
if (file && file[0] === '~') {
file = app.getPath('home') + file.substr(1);
}
return file;
}
/**
* Installs a theme package to the themes directory.
* @param {String} name NPM name or Github repo. Local paths are symlink
* to target location.
* @param {String} version Theme version to install.
* @return {Promise}
*/
install(name, version) {
let message = 'Installing theme: ' + name;
if (version) {
message += ', version ' + version;
}
log.info(message);
return this._installPackage(name, version)
.then((info) => this._createThemeInfo(name, info))
.then((info) => this._addThemeEntry(info));
}
/**
* Implementation of installation process.
* If the `name` argument represents a pato to a directory then
* local installation is performed. PluginManager is used otherwise.
* @param {String} name NPM name or Github repo. Local paths are symlink
* to target location.
* @param {String} version Theme version to install.
* @return {Promise<Object>} Promise resolved to theme info object
*/
_installPackage(name, version) {
try {
fs.accessSync(name, fs.constants.R_OK | fs.constants.X_OK);
return this._installLocalPackage(name);
} catch (_) {
return this._installRemotePackage(name, version);
}
}
/**
* Creates a symlink from a local package to themes directory.
* @param {String} name Package location.
* @return {Promise<Object>} Promise resolved to theme info object
*/
_installLocalPackage(name) {
log.info('Installing theme from local sources...');
const info = {
isSymlink: true
};
return fs.readJson(path.join(name, 'package.json'))
.catch(() => {
throw new Error('Unable to read package.json file of the theme.');
})
.then((pkg) => {
info.name = pkg.name;
info.version = pkg.version;
info.location = path.join(process.env.ARC_THEMES, pkg.name);
if (pkg.main) {
info.mainFile = path.join(info.location, pkg.main);
} else {
info.mainFile = path.join(info.location, path.basename(name));
}
return this._ensureSymlinkPath(info.location);
})
.then(() => fs.symlink(path.resolve(name), path.resolve(info.location), 'dir'))
.then(() => {
log.info('Installation complete.');
return info;
});
}
/**
* Ensures the path to create a symlint is created.
* @param {String} location Symlink location
* @return {Promise}
*/
_ensureSymlinkPath(location) {
location = path.dirname(location);
return fs.ensureDir(location);
}
/**
* Uses PluginManager to install package from npm or GitHub.
* @param {String} name NPM name or Github repo. Local paths are symlink
* to target location.
* @param {String} version Theme version to install.
* @return {Promise<Object>} Promise resolved to theme info object
*/
_installRemotePackage(name, version) {
log.info('Installing theme from remote sources...');
return this.pluginManager.install(
...this._prepareSourceAndVersion(name, version));
}
/**
* Uninstalls package and removes it from the registry file.
* @param {String} name Name of the package.
* @return {Promise}
*/
uninstall(name) {
log.info('Uninstalling theme ' + name);
return this._uninstallPackage(name)
.then(() => this._removeThemeEntry(name));
}
/**
* Implementation of removing process.
* @param {String} name NPM name or Github repo. Local paths are symlink
* to target location.
* @return {Promise<Object>} Promise resolved to theme info object
*/
_uninstallPackage(name) {
return this.getInfo(name)
.then((info) => {
if (!info) {
throw new Error(`Package ${name} is not installed.`);
}
if (info.isSymlink) {
return fs.remove(info.location);
}
return this.pluginManager.uninstall(info.name);
});
}
/**
* Updates themes from passed list.
* @param {Object} info A map of theme name and version.
* Version is optional and if not set it installs latest version.
* @return {Promise}
*/
update(info) {
const data = Object.keys(info).map((name) => {
return {
name,
version: info[name]
};
});
return this._updatesQueue(data);
}
_updatesQueue(queue, result) {
if (!result) {
result = {};
}
const item = queue.shift();
if (!item) {
return Promise.resolve(result);
}
return this.install(item.name, item.version)
.then(() => {
result[item.name] = {
error: false
};
})
.catch((cause) => {
result[item.name] = {
error: true,
message: cause.message
};
})
.then(() => this._updatesQueue(queue, result));
}
/**
* Reads information from the theme registry abiut the theme.
* @param {String} name Name of the package (_id in info object).
* @return {Promise<Object>} Resolves to theme info object.
*/
getInfo(name) {
return this.themeInfo.load()
.then((info) => {
const {themes} = info;
for (let i = 0, len = themes.length; i < len; i++) {
if (themes[i]._id === name) {
return themes[i];
}
}
});
}
/**
* Checks for updates to all installed packages.
* @return {Promise<Object>} Promise resolved to a map of package names and
* info objects downloaded from the remote server. Only packages with
* update available are returned.
*/
checkForUpdates() {
return this._getUpdateCandidates()
.then((names) => this._updatesInfoQueue(names));
}
_getUpdateCandidates() {
const now = Date.now();
return this.themeInfo.load()
.then((info) => {
const {themes} = info;
const names = [];
themes.forEach((item) => {
if (item.isSymlink) {
return;
}
if (item.updateCheck) {
const delta = now - item.updateCheck;
if (delta < 7.2e+6) {
// 2 hours
return;
}
}
names[names.length] = item._id;
});
return names;
});
}
_updatesInfoQueue(queue, result) {
if (!result) {
result = {};
}
const name = queue.shift();
if (!name) {
return Promise.resolve(result);
}
return this.checkUpdateAvailable(name)
.then((info) => {
if (info) {
result[name] = info;
}
return this._updateUpdateTime(name);
})
.catch(() => {})
.then(() => this._updatesInfoQueue(queue, result));
}
_updateUpdateTime(name) {
const store = this.themeInfo;
return store.load()
.then((info) => {
const {themes} = info;
for (let i = 0, len = themes.length; i < len; i++) {
if (themes[i]._id === name) {
themes[i].updateCheck = Date.now();
return store.store();
}
}
});
}
checkUpdateAvailable(name, version) {
const names = this._prepareSourceAndVersion(name, version);
const qPromise = this.pluginManager.queryPackage(...names);
const iPromise = this.getInfo(name);
return Promise.all([iPromise, qPromise])
.then((data) => {
const localInfo = data[0];
const remoteInfo = data[1];
if (!this._compareVersions(remoteInfo, localInfo)) {
return;
}
if (this._compareEngines(remoteInfo)) {
return remoteInfo;
}
});
}
_compareVersions(remoteInfo, localInfo) {
return semver.gt(remoteInfo.version, localInfo.version);
}
_compareEngines(remoteInfo) {
const engines = remoteInfo.engines;
if (!engines || !engines.arc) {
return true;
}
const engine = engines.arc;
const version = process.env.npm_package_version;
if (semver.validRange(engine)) {
return semver.satisfies(version, engine);
}
return true;
}
/**
* Processes source and version properties to produce right input for
* `live-plugin-manager`
* @param {String} source NPM name or Github repo
* @param {String} version Theme version to install.
* @return {Array<String>} First item is the source and version the other.
*/
_prepareSourceAndVersion(source, version) {
if (source.indexOf('/') !== -1 && source[0] !== '@') {
if (!version) {
version = 'master';
}
version = source + '#' + version;
}
return [source, version];
}
/**
* Creates a theme info object from both `live-plugin-manager` install response
* and installed theme package file.
* @param {String} id
* @param {Object} info
* @return {Object} An object to be stored in theme info file.
*/
_createThemeInfo(id, info) {
const result = {
_id: id,
name: info.name,
version: info.version,
location: info.location,
mainFile: info.mainFile,
title: info.name,
description: '',
isSymlink: info.isSymlink
};
return fs.readJson(path.join(info.location, 'package.json'))
.then((pkg) => {
if (pkg.themeTitle) {
result.title = pkg.themeTitle;
}
if (pkg.description) {
result.description = pkg.description;
}
return result;
});
}
/**
* Adds theme info object to themes registry file.
* @param {Object} info An object to add.
* @return {Promise<Object>} Promise resolve to the info object.
*/
_addThemeEntry(info) {
const store = this.themeInfo;
return store.load()
.then((data) => {
const {themes} = data;
themes.push(info);
return store.store();
})
.then(() => info);
}
_removeThemeEntry(name) {
const store = this.themeInfo;
return store.load()
.then((info) => {
const {themes} = info;
for (let i = 0, len = themes.length; i < len; i++) {
if (themes[i]._id === name) {
themes.splice(i, 1);
return store.store();
}
}
});
}
}
module.exports.ThemePluginsManager = ThemePluginsManager;