-
Notifications
You must be signed in to change notification settings - Fork 813
/
plugins.js
230 lines (195 loc) · 6.39 KB
/
plugins.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
const path = require('path');
const fs = require('fs');
const electron = require('electron');
const got = require('got');
const execa = require('execa');
const makeDir = require('make-dir');
const {ipcMain: ipc} = require('electron-better-ipc');
const packageJson = require('package-json');
const {satisfies} = require('semver');
const {app, Notification} = electron;
const Plugin = require('../plugin');
const {openConfigWindow} = require('../config');
const {openPrefsWindow} = require('../preferences');
const {notify} = require('./notifications');
const {track} = require('./analytics');
class Plugins {
constructor() {
this.npmBin = path.join(__dirname, '../../node_modules/npm/bin/npm-cli.js');
this.yarnBin = path.join(__dirname, '../../node_modules/yarn/bin/yarn.js');
this._makePluginsDir();
this.appVersion = app.getVersion();
}
setUpdateExportOptions(updateExportOptions) {
this.updateExportOptions = updateExportOptions;
}
_makePluginsDir() {
const cwd = path.join(app.getPath('userData'), 'plugins');
const fp = path.join(cwd, 'package.json');
if (!fs.existsSync(fp)) {
makeDir.sync(cwd);
fs.writeFileSync(fp, '{"dependencies":{}}');
}
this.cwd = cwd;
this.pkgPath = fp;
}
_modifyMainPackageJson(modifier) {
const pkg = JSON.parse(fs.readFileSync(this.pkgPath, 'utf8'));
modifier(pkg);
fs.writeFileSync(this.pkgPath, JSON.stringify(pkg));
}
async _runNpm(...commands) {
await execa(process.execPath, [this.npmBin, ...commands], {
cwd: this.cwd,
env: {
ELECTRON_RUN_AS_NODE: 1
}
});
}
async _runYarn(...commands) {
await execa(process.execPath, [this.yarnBin, ...commands], {
cwd: this.cwd,
env: {
ELECTRON_RUN_AS_NODE: 1
}
});
}
_getPrettyName(name) {
return name.replace(/^kap-/, '');
}
_pluginPath(name, subPath = '') {
return path.join(this.cwd, 'node_modules', name, subPath);
}
_pluginNames() {
const pkg = fs.readFileSync(path.join(this.cwd, 'package.json'), 'utf8');
return Object.keys(JSON.parse(pkg).dependencies);
}
async _yarnInstall() {
await this._runYarn('install', '--no-lockfile', '--registry', 'https://registry.npmjs.org');
}
async install(name) {
const prettyName = this._getPrettyName(name);
track(`plugin/installed/${name}`);
// We manually add it to the package.json here so we're able to set the version to `latest`
this._modifyMainPackageJson(pkg => {
pkg.dependencies[name] = 'latest';
});
try {
await this._yarnInstall();
const plugin = new Plugin(name);
const isValid = plugin.isConfigValid();
const hasConfig = this.getServices(name).some(({config = {}}) => Object.keys(config).length > 0);
const options = isValid ? {
title: 'Plugin installed',
body: `"${prettyName}" is ready for use`
} : {
title: 'Configure plugin',
body: `"${prettyName}" requires configuration`,
actions: [{type: 'button', text: 'Configure'}]
};
const notification = new Notification(options);
if (!isValid) {
const openConfig = async () => {
const prefsWindow = await openPrefsWindow();
ipc.callRenderer(prefsWindow, 'open-plugin-config', name);
};
notification.on('click', openConfig);
notification.on('action', (_, index) => {
if (index === 0) {
openConfig();
} else {
notification.close();
}
});
}
notification.show();
this.updateExportOptions();
return {hasConfig, isValid};
} catch (error) {
notify(`Something went wrong while installing ${prettyName}`);
this._modifyMainPackageJson(pkg => {
delete pkg.dependencies[name];
});
console.log(error);
}
}
async upgrade() {
await this._yarnInstall();
}
uninstall(name) {
track(`plugin/uninstalled/${name}`);
this._modifyMainPackageJson(pkg => {
delete pkg.dependencies[name];
});
const plugin = new Plugin(name);
plugin.config.clear();
this.updateExportOptions();
}
async prune() {
await this._runNpm('prune');
}
getServices(pluginName) {
return require(path.join(this.cwd, 'node_modules', pluginName)).shareServices;
}
getInstalled() {
return this._pluginNames().map(name => {
const pluginPath = this._pluginPath(name, 'package.json');
const json = JSON.parse(fs.readFileSync(pluginPath, 'utf8'));
const plugin = new Plugin(name);
return {
...json,
pluginPath: this._pluginPath(name),
prettyName: this._getPrettyName(name),
hasConfig: this.getServices(name).some(({config = {}}) => Object.keys(config).length > 0),
isValid: plugin.isConfigValid(),
kapVersion: json.kapVersion || '*',
isCompatible: satisfies(this.appVersion, json.kapVersion || '*'),
isInstalled: true,
isSymlink: fs.lstatSync(this._pluginPath(name)).isSymbolicLink()
};
});
}
getBuiltIn() {
return [{
pluginPath: './plugins/copy-to-clipboard-plugin',
isCompatible: true,
name: '_copyToClipboard'
}, {
pluginPath: './plugins/save-file-plugin',
isCompatible: true,
name: '_saveToDisk'
}, {
pluginPath: './plugins/open-with-plugin',
isCompatible: true,
name: '_openWith'
}];
}
async getFromNpm() {
const url = 'https://api.npms.io/v2/search?q=keywords:kap-plugin+not:deprecated';
const response = await got(url, {json: true});
const installed = this._pluginNames();
return Promise.all(response.body.results
.map(x => x.package)
.filter(x => x.name.startsWith('kap-'))
.filter(x => !installed.includes(x.name)) // Filter out installed plugins
.map(async x => {
const {kapVersion = '*'} = await packageJson(x.name, {fullMetadata: true});
return {
...x,
kapVersion,
prettyName: this._getPrettyName(x.name),
isCompatible: satisfies(this.appVersion, kapVersion)
};
}));
}
getPluginService(pluginName, serviceTitle) {
return this.getServices(pluginName).find(shareService => shareService.title === serviceTitle);
}
async openPluginConfig(name) {
await openConfigWindow(name);
const plugin = new Plugin(name);
return plugin.isConfigValid();
}
}
const plugins = new Plugins();
module.exports = plugins;