forked from shanalikhan/code-settings-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginService.ts
545 lines (497 loc) · 15.1 KB
/
pluginService.ts
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
"use strict";
import * as fs from "fs-extra";
import * as path from "path";
import * as vscode from "vscode";
import { OsType } from "../enums";
import * as util from "../util";
const apiPath =
"https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery";
const extensionDir: string = ".vscode";
const extensionDirPortable: string = "/data/extensions/";
export class ExtensionInformation {
public static fromJSON(text: string) {
// TODO: JSON.parse may throw error
// Throw custom error should be more friendly
const obj = JSON.parse(text);
const meta = new ExtensionMetadata(
obj.meta.galleryApiUrl,
obj.meta.id,
obj.meta.downloadUrl,
obj.meta.publisherId,
obj.meta.publisherDisplayName,
obj.meta.date
);
const item = new ExtensionInformation();
item.metadata = meta;
item.name = obj.name;
item.publisher = obj.publisher;
item.version = obj.version;
return item;
}
public static fromJSONList(text: string) {
const extList: ExtensionInformation[] = [];
try {
// TODO: JSON.parse may throw error
// Throw custom error should be more friendly
const list = JSON.parse(text);
list.forEach(obj => {
const meta = new ExtensionMetadata(
obj.metadata.galleryApiUrl,
obj.metadata.id,
obj.metadata.downloadUrl,
obj.metadata.publisherId,
obj.metadata.publisherDisplayName,
obj.metadata.date
);
const item = new ExtensionInformation();
item.metadata = meta;
item.name = obj.name;
item.publisher = obj.publisher;
item.version = obj.version;
if (item.name !== "code-settings-sync") {
extList.push(item);
}
});
} catch (err) {
console.error("Sync : Unable to Parse extensions list", err);
}
return extList;
}
public metadata: ExtensionMetadata;
public name: string;
public version: string;
public publisher: string;
}
export class ExtensionMetadata {
constructor(
public galleryApiUrl: string,
public id: string,
public downloadUrl: string,
public publisherId: string,
public publisherDisplayName: string,
public date: string
) {}
}
export class PluginService {
public static GetMissingExtensions(
remoteExt: string,
ignoredExtensions: string[]
) {
const hashset = {};
const remoteList = ExtensionInformation.fromJSONList(remoteExt);
const localList = this.CreateExtensionList();
const missingList: ExtensionInformation[] = [];
for (const ext of localList) {
if (hashset[ext.name] == null) {
hashset[ext.name] = ext;
}
}
for (const ext of remoteList) {
if (
hashset[ext.name] == null &&
ignoredExtensions.includes(ext.name) === false
) {
missingList.push(ext);
}
}
return missingList;
}
public static GetDeletedExtensions(
remoteList: ExtensionInformation[],
ignoredExtensions: string[]
) {
const localList = this.CreateExtensionList();
const deletedList: ExtensionInformation[] = [];
// for (var i = 0; i < remoteList.length; i++) {
// var ext = remoteList[i];
// var found: boolean = false;
// for (var j = 0; j < localList.length; j++) {
// var localExt = localList[j];
// if (ext.name == localExt.name) {
// found = true;
// break;
// }
// }
// if (!found) {
// deletedList.push(localExt);
// }
// }
for (const ext of localList) {
let found: boolean = false;
if (ext.name !== "code-settings-sync") {
for (const localExt of remoteList) {
if (
ext.name === localExt.name ||
ignoredExtensions.includes(ext.name)
) {
found = true;
break;
}
}
if (!found) {
deletedList.push(ext);
}
}
}
return deletedList;
}
public static CreateExtensionList() {
const list: ExtensionInformation[] = [];
for (const ext of vscode.extensions.all) {
console.log(ext.extensionPath);
if (
(ext.extensionPath.includes(extensionDir) ||
ext.extensionPath.includes(extensionDirPortable)) && // skip if not install from gallery
ext.packageJSON.isBuiltin === false
) {
const meta = ext.packageJSON.__metadata || {
id: ext.packageJSON.uuid,
publisherId: ext.id,
publisherDisplayName: ext.packageJSON.publisher
};
const data = new ExtensionMetadata(
meta.galleryApiUrl,
meta.id,
meta.downloadUrl,
meta.publisherId,
meta.publisherDisplayName,
meta.date
);
const info = new ExtensionInformation();
info.metadata = data;
info.name = ext.packageJSON.name;
info.publisher = ext.packageJSON.publisher;
info.version = ext.packageJSON.version;
list.push(info);
}
}
return list;
}
public static async DeleteExtension(
item: ExtensionInformation,
ExtensionFolder: string
): Promise<boolean> {
const destination = path.join(
ExtensionFolder,
item.publisher + "." + item.name + "-" + item.version
);
try {
await fs.remove(destination);
return true;
} catch (err) {
console.log("Sync : " + "Error in uninstalling Extension.");
console.log(err);
throw err;
}
}
public static async DeleteExtensions(
extensionsJson: string,
extensionFolder: string,
ignoredExtensions: string[]
): Promise<ExtensionInformation[]> {
const remoteList = ExtensionInformation.fromJSONList(extensionsJson);
const deletedList = PluginService.GetDeletedExtensions(
remoteList,
ignoredExtensions
);
const deletedExt: ExtensionInformation[] = [];
if (deletedList.length === 0) {
return deletedExt;
}
for (const selectedExtension of deletedList) {
try {
await PluginService.DeleteExtension(selectedExtension, extensionFolder);
deletedExt.push(selectedExtension);
} catch (err) {
console.error(
"Sync : Unable to delete extension " +
selectedExtension.name +
" " +
selectedExtension.version
);
console.error(err);
throw deletedExt;
}
}
return deletedExt;
}
public static async InstallExtensions(
extensions: string,
extFolder: string,
useCli: boolean,
ignoredExtensions: string[],
osType: OsType,
insiders: boolean,
notificationCallBack: (...data: any[]) => void
): Promise<ExtensionInformation[]> {
let actionList: Array<Promise<void>> = [];
let addedExtensions: ExtensionInformation[] = [];
const missingList = PluginService.GetMissingExtensions(
extensions,
ignoredExtensions
);
if (missingList.length === 0) {
notificationCallBack("Sync : No Extensions needs to be installed.");
return [];
}
if (useCli) {
addedExtensions = await PluginService.ProcessInstallationCLI(
missingList,
osType,
insiders,
notificationCallBack
);
return addedExtensions;
} else {
actionList = await this.ProcessInstallation(
extFolder,
notificationCallBack,
missingList
);
try {
await Promise.all(actionList);
return addedExtensions;
} catch (err) {
// always return extension list
return addedExtensions;
}
}
}
public static async ProcessInstallationCLI(
missingList: ExtensionInformation[],
osType: OsType,
isInsiders: boolean,
notificationCallBack: (...data: any[]) => void
): Promise<ExtensionInformation[]> {
const addedExtensions: ExtensionInformation[] = [];
const exec = require("child_process").exec;
notificationCallBack("TOTAL EXTENSIONS : " + missingList.length);
notificationCallBack("");
notificationCallBack("");
let myExt: string = process.argv0;
console.log(myExt);
let codeLastFolder = "";
let codeCliPath = "";
if (osType === OsType.Windows) {
if (isInsiders) {
codeLastFolder = "Code - Insiders";
codeCliPath = "bin/code-insiders";
} else {
codeLastFolder = "Code";
codeCliPath = "bin/code";
}
} else if (osType === OsType.Linux) {
if (isInsiders) {
codeLastFolder = "code-insiders";
codeCliPath = "bin/code-insiders";
} else {
codeLastFolder = "code";
codeCliPath = "bin/code";
}
} else if (osType === OsType.Mac) {
codeLastFolder = "Frameworks";
codeCliPath = "Resources/app/bin/code";
}
myExt =
'"' +
myExt.substr(0, myExt.lastIndexOf(codeLastFolder)) +
codeCliPath +
'"';
for (let i = 0; i < missingList.length; i++) {
const missExt = missingList[i];
const name = missExt.publisher + "." + missExt.name;
const extensionCli = myExt + " --install-extension " + name;
notificationCallBack(extensionCli);
try {
const installed = await new Promise<boolean>(res => {
exec(extensionCli, (err, stdout, stderr) => {
if (!stdout && (err || stderr)) {
notificationCallBack(err || stderr);
res(false);
}
notificationCallBack(stdout);
res(true);
});
});
if (installed) {
notificationCallBack("");
notificationCallBack(
"EXTENSION : " +
(i + 1) +
" / " +
missingList.length.toString() +
" INSTALLED.",
true
);
notificationCallBack("");
notificationCallBack("");
addedExtensions.push(missExt);
}
} catch (err) {
console.log(err);
}
}
return addedExtensions;
}
public static async ProcessInstallation(
extFolder: string,
notificationCallBack: (...data: any[]) => void,
missingList: ExtensionInformation[]
) {
const actionList: Array<Promise<void>> = [];
const addedExtensions: ExtensionInformation[] = [];
let totalInstalled: number = 0;
for (const element of missingList) {
actionList.push(
PluginService.InstallExtension(element, extFolder).then(
() => {
totalInstalled = totalInstalled + 1;
notificationCallBack(
"Sync : Extension " +
totalInstalled +
" of " +
missingList.length.toString() +
" installed.",
false
);
addedExtensions.push(element);
},
(err: any) => {
console.error(err);
notificationCallBack(
"Sync : " + element.name + " Download Failed.",
true
);
}
)
);
}
return actionList;
}
public static async InstallExtension(
item: ExtensionInformation,
ExtensionFolder: string
) {
const header = {
Accept: "application/json;api-version=3.0-preview.1"
};
let extractPath: string = null;
const data = {
filters: [
{
criteria: [
{
filterType: 4,
value: item.metadata.id
}
]
}
],
flags: 133
};
try {
const res = await util.Util.HttpPostJson(apiPath, data, header);
let downloadUrl: string;
try {
let targetVersion = null;
const content = JSON.parse(res);
// Find correct version
for (const result of content.results) {
for (const extension of result.extensions) {
for (const version of extension.versions) {
if (version.version === item.version) {
targetVersion = version;
break;
}
}
if (targetVersion !== null) {
break;
}
}
if (targetVersion !== null) {
break;
}
}
if (
targetVersion === null ||
!targetVersion ||
!targetVersion.assetUri
) {
// unable to find one
throw new Error("NA");
}
// Proceed to install
downloadUrl =
targetVersion.assetUri +
"/Microsoft.VisualStudio.Services.VSIXPackage?install=true";
console.log("Installing from Url :" + downloadUrl);
} catch (error) {
if (error === "NA" || error.message === "NA") {
console.error(
"Sync : Extension : '" +
item.name +
"' - Version : '" +
item.version +
"' Not Found in marketplace. Remove the extension and upload the settings to fix this."
);
}
console.error(error);
throw error;
}
const filePath = await util.Util.HttpGetFile(downloadUrl);
const dir = await util.Util.Extract(filePath);
extractPath = dir;
const packageJson = await PluginService.GetPackageJson(dir, item);
Object.assign(packageJson, {
__metadata: item.metadata
});
const text = JSON.stringify(packageJson, null, " ");
await PluginService.WritePackageJson(extractPath, text);
// Move the folder to correct path
const destination = path.join(
ExtensionFolder,
item.publisher + "." + item.name + "-" + item.version
);
const source = path.join(extractPath, "extension");
await PluginService.CopyExtension(destination, source);
} catch (err) {
console.error(
`Sync : Extension : '${item.name}' - Version : '${item.version}'` + err
);
throw err;
}
}
private static async CopyExtension(
destination: string,
source: string
): Promise<void> {
await fs.copy(source, destination, { overwrite: true });
}
private static async WritePackageJson(dirName: string, packageJson: string) {
await fs.writeFile(
dirName + "/extension/package.json",
packageJson,
"utf-8"
);
}
private static async GetPackageJson(
dirName: string,
item: ExtensionInformation
) {
const text = await fs.readFile(
dirName + "/extension/package.json",
"utf-8"
);
const config = JSON.parse(text);
if (config.name !== item.name) {
throw new Error("name not equal");
}
if (config.publisher !== item.publisher) {
throw new Error("publisher not equal");
}
if (config.version !== item.version) {
throw new Error("version not equal");
}
return config;
}
}