-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcs.ts
688 lines (645 loc) · 29.3 KB
/
cs.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
'use strict';
import * as vscode from 'vscode';
import * as os from 'os';
import * as path from 'path';
import {StatusBarManager} from './status-bar-manager';
import * as settings from './settings';
import * as helpers from './helpers';
import * as fs from 'fs';
var rimraf = require('rimraf');
import * as chokidar from 'chokidar';
import {FileWatcher, FileWatcherFiles} from './file-watcher';
import {LocalSettings} from './local-settings';
import {Logger} from './logger';
export const EXTENSIONS = 'extensions.json';
export const SETTINGS = 'settings.json';
export const KEYBINDINGS = 'keybindings.json';
export const SNIPPETS = 'snippets';
export const LOCAL_SETTINGS = 'local-settings.json';
export const currentVersion: string = '2.7.3';
export let vsCodeExtensionDir: string = helpers.getExtensionDir();
export let codeSyncExtensionDir: string = path.join(vsCodeExtensionDir, 'golf1052.code-sync-' + currentVersion);
export class CodeSync {
private vsCodeExtensionDir: string;
private codeSyncExtensionDir: string;
private codeSyncDir: string;
private statusBar: StatusBarManager;
private codeSyncSettings: settings.CodeSyncSettings;
private active: boolean;
private fileWatcher: FileWatcher;
private localSettingsManager: LocalSettings;
private canManageExtensions: boolean;
private logger: Logger;
constructor(vsCodeExtensionDir: string, codeSyncExtensionDir: string, codeSyncDir: string) {
this.logger = new Logger('cs');
this.vsCodeExtensionDir = vsCodeExtensionDir;
this.codeSyncExtensionDir = codeSyncExtensionDir;
this.codeSyncDir = codeSyncDir;
this.statusBar = new StatusBarManager();
this.codeSyncSettings = new settings.CodeSyncSettings(path.join(this.codeSyncExtensionDir, SETTINGS), path.join(this.codeSyncDir, EXTENSIONS));
this.active = false;
this.localSettingsManager = new LocalSettings(this.codeSyncExtensionDir);
}
get Active(): boolean {
return this.active;
}
set Active(active: boolean) {
if (active) {
this.statusBar.show();
}
else {
this.statusBar.hide();
}
this.active = active;
}
get Settings(): settings.CodeSyncSettings {
return this.codeSyncSettings;
}
get CanManageExtensions(): boolean {
return this.canManageExtensions;
}
set CanManageExtensions(canManageExtensions: boolean) {
this.canManageExtensions = canManageExtensions;
}
toggleStatusBarIcon(): void {
let settings = this.Settings.Settings;
let visible = this.statusBar.toggle();
settings.showStatusBarIcon = visible;
this.Settings.Settings = settings;
this.Settings.save();
}
setStatusBarIcon(): void {
let settings = this.Settings.Settings;
if (!settings.showStatusBarIcon) {
this.statusBar.hide();
}
}
async checkForSettings() {
this.logger.appendLine('Checking settings');
this.statusBar.StatusBarText = 'Checking settings';
this.checkForOldSettings();
await this.migrateSettings();
let extensionDir = helpers.getDir(this.codeSyncExtensionDir);
// if settings don't already exist
if (!fs.existsSync(path.join(extensionDir, SETTINGS))) {
this.logger.appendLine(`Could not find settings in ${path.join(extensionDir, SETTINGS)}. Creating...`);
// we need to create settings on first launch because when we call this.Settings.Settings
// we're going to try to read settings that don't exist yet
let tmpSettings: settings.Settings = {
$schema: './schema/settings.schema.json',
externalPath: path.join(os.homedir(), 'OneDrive/Apps/code-sync'),
autoImport: true,
autoExport: true,
importSettings: true,
importKeybindings: true,
importSnippets: true,
importExtensions: true,
showStatusBarIcon: true,
excluded: {
installed: [],
external: []
},
executableName: '',
settingsPath: ''
};
this.Settings.Settings = tmpSettings;
this.Settings.save();
await this.setExternalSyncPath();
}
let csSettings: settings.Settings = this.Settings.Settings;
this.codeSyncDir = csSettings.externalPath;
let externalExtensionsPath = path.join(this.codeSyncDir, EXTENSIONS);
this.logger.appendLine(`Setting external extensions path as ${externalExtensionsPath}.`);
this.Settings.ExternalExtensionsPath = externalExtensionsPath;
if (!fs.existsSync(this.codeSyncDir)) {
this.logger.appendLine(`CodeSync external sync directory isn't there?`);
this.logger.appendLine(`Creating external sync path directory: ${this.codeSyncDir}.`);
helpers.getDir(this.codeSyncDir);
}
this.statusBar.reset();
this.logger.appendLine('Done checking settings.');
this.logger.appendLine(`External sync directory: ${csSettings.externalPath}`);
}
async setExternalSyncPath() {
let extPath: string = '';
if (process.env.CODE_SYNC_TESTING) {
// if we're testing don't use showInputBox because it will block testing
extPath = path.join(path.resolve('.'), 'code-sync');
} else {
extPath = await vscode.window.showInputBox({
prompt: 'Enter the full path to where you want CodeSync to sync to',
value: path.join(os.homedir(), 'OneDrive/Apps/code-sync')
});
}
if (extPath == undefined) {
return;
}
else if (extPath == '') {
await vscode.window.showWarningMessage('External sync path was blank');
return;
}
if (!fs.existsSync(extPath)) {
this.logger.appendLine(`Creating external sync path: ${extPath}`);
helpers.getDir(extPath);
}
let csSettings: settings.Settings = this.Settings.Settings;
csSettings.externalPath = extPath;
this.codeSyncDir = extPath;
this.Settings.Settings = csSettings;
this.Settings.save();
this.logger.appendLine(`External sync path is now ${this.Settings.Settings.externalPath}.`);
}
startFileWatcher = () => {
let files: FileWatcherFiles = {};
if (fs.existsSync(helpers.getUserSettingsFilePath(this.Settings.Settings))) {
files[helpers.getUserSettingsFilePath(this.Settings.Settings)] = this.exportSettings.bind(this);
}
if (fs.existsSync(helpers.getKeybindingsFilePath(this.Settings.Settings))) {
files[helpers.getKeybindingsFilePath(this.Settings.Settings)] = this.exportKeybindings.bind(this);
}
if (fs.existsSync(helpers.getSnippetsFolderPath(this.Settings.Settings))) {
files[helpers.getSnippetsFolderPath(this.Settings.Settings)] = this.exportSnippets.bind(this);
}
this.fileWatcher = new FileWatcher(files, this.Settings);
}
async importAll() {
this.startSync('Importing all');
this.importSettings();
await this.importKeybindings();
await this.importSnippets();
if (this.CanManageExtensions) {
this.importExtensions();
}
this.statusBar.reset();
}
async exportAll() {
this.startSync('Exporting all');
this.exportSettings();
await this.exportKeybindings();
await this.exportSnippets();
if (this.CanManageExtensions) {
this.exportExtensions();
}
this.statusBar.reset();
}
importSettings(): void {
if (this.Settings.Settings.importSettings) {
this.logger.appendLine('Importing settings');
this.startSync('Importing settings');
let settingsPath: string = path.join(this.codeSyncDir, SETTINGS);
if (!fs.existsSync(settingsPath)) {
this.logger.appendLine(`Failed to import settings. Could not find settings.json at ${settingsPath}. Giving up.`);
this.statusBar.reset();
return;
}
if (helpers.isFileEmpty(settingsPath) == false &&
helpers.isFileContentEmpty(settingsPath) == false) {
const userSettingsFilePath = helpers.getUserSettingsFilePath(this.Settings.Settings);
this.logger.appendLine(`Importing settings to ${userSettingsFilePath}`);
this.localSettingsManager.import(settingsPath, userSettingsFilePath);
}
this.statusBar.reset();
this.logger.appendLine('Finished importing settings.');
}
}
exportSettings(): void {
if (this.Settings.Settings.importSettings) {
this.logger.appendLine('Exporting settings.');
this.startSync('Exporting settings');
let settingsPath: string = helpers.getUserSettingsFilePath(this.Settings.Settings);
if (!fs.existsSync(settingsPath)) {
this.logger.appendLine(`Could not find settings path at ${settingsPath}. Giving up.`);
this.statusBar.reset();
return;
}
this.localSettingsManager.export(settingsPath, path.join(this.codeSyncDir, SETTINGS));
this.statusBar.reset();
this.logger.appendLine('Finished exporting settings.');
}
}
async importKeybindings() {
if (this.Settings.Settings.importKeybindings) {
this.logger.appendLine('Importing keybindings');
this.startSync('Importing keybindings');
let keybindingsPath: string = path.join(this.codeSyncDir, KEYBINDINGS);
if (!fs.existsSync(keybindingsPath)) {
this.logger.appendLine(`Failed to import keybindings. Could not find keybindings.json at ${keybindingsPath}. Giving up.`);
this.statusBar.reset();
return;
}
if (helpers.isFileEmpty(keybindingsPath) == false &&
helpers.isFileContentEmpty(keybindingsPath) == false) {
const keybindingsFilePath = helpers.getKeybindingsFilePath(this.Settings.Settings)
this.logger.appendLine(`Importing keybindings to ${keybindingsFilePath}`);
await helpers.copy(keybindingsPath, keybindingsFilePath);
}
this.statusBar.reset();
this.logger.appendLine('Finished importing keybindings.');
}
}
async exportKeybindings() {
if (this.Settings.Settings.importKeybindings) {
this.startSync('Exporting keybindings');
let keybindingsPath = helpers.getKeybindingsFilePath(this.Settings.Settings);
if (!fs.existsSync(keybindingsPath)) {
this.logger.appendLine(`Could not find keybindings path at ${keybindingsPath}. Giving up.`);
this.statusBar.reset();
return;
}
await helpers.copy(keybindingsPath, path.join(this.codeSyncDir, KEYBINDINGS));
this.statusBar.reset();
}
}
async importSnippets() {
if (this.Settings.Settings.importSnippets) {
this.logger.appendLine('Importing snippets');
this.startSync('Importing snippets');
let snippetsDirectory = path.join(this.codeSyncDir, SNIPPETS);
if (!fs.existsSync(snippetsDirectory)) {
this.logger.appendLine(`Failed to import snippets. Could not find snippets directory at ${snippetsDirectory}. Giving up.`);
this.statusBar.reset();
return;
}
let snippetFiles: string[] = fs.readdirSync(snippetsDirectory);
for (let i = 0; i < snippetFiles.length; i++) {
let s = snippetFiles[i];if (fs.lstatSync(path.join(snippetsDirectory, s)).isFile()) {
if (helpers.isFileEmpty(path.join(snippetsDirectory, s)) == false &&
helpers.isFileContentEmpty(path.join(snippetsDirectory, s)) == false) {
await helpers.copy(path.join(snippetsDirectory, s), path.join(helpers.getSnippetsFolderPath(this.Settings.Settings), s));
}
}
}
this.statusBar.reset();
this.logger.appendLine('Finished importing snippets.');
}
}
async exportSnippets() {
if (this.Settings.Settings.importSnippets) {
this.startSync('Exporting snippets');
const snippetsFolderPath = helpers.getSnippetsFolderPath(this.Settings.Settings);
if (!fs.existsSync(snippetsFolderPath)) {
this.logger.appendLine(`Could not find snippets path at ${snippetsFolderPath}. Giving up.`);
this.statusBar.reset();
return;
}
await helpers.copy(snippetsFolderPath, path.join(this.codeSyncDir, SNIPPETS));
this.statusBar.reset();
}
}
importExtensions() {
if (this.Settings.Settings.importExtensions) {
this.logger.appendLine('Importing extensions');
this.startSync('Importing extensions');
let excluded: string[] = this.Settings.ExcludedExternalPackages;
let extensions: string[] = this.Settings.Extensions;
let installedExtensions = helpers.getInstalledExtensions();
let installedAny: boolean = false;
extensions.forEach(e => {
if (installedExtensions.filter(i => i.id == e).length == 0) {
let val = helpers.installExtension(e, this.Settings.Settings);
if (val) {
installedAny = true;
}
}
});
if (installedAny) {
this.statusBar.StatusBarText = 'Restart required';
this.statusBar.setStop();
}
else {
this.statusBar.reset();
}
this.logger.appendLine('Finished importing extensions.');
}
}
exportExtensions() {
if (this.Settings.Settings.importExtensions) {
this.logger.appendLine('Exporting extensions');
this.startSync('Exporting extensions');
let excluded: string[] = this.Settings.ExcludedInstalledPackages;
let extensions: string[] = [];
let e = helpers.getInstalledExtensions();
helpers.getInstalledExtensions().forEach(e => {
if (excluded.indexOf(e.id) == -1) {
extensions.push(e.id);
}
});
this.Settings.Extensions = extensions;
this.Settings.saveExtensions();
this.statusBar.reset();
this.logger.appendLine('Finished exporting extensions');
}
}
async addExcludedInstalledPackage() {
let excluded: string[] = this.Settings.Settings.excluded.installed;
let items: vscode.QuickPickItem[] = [];
let installed = helpers.getInstalledExtensions();
if (installed.length == 0) {
vscode.window.showInformationMessage('There are no installed extensions.');
return;
}
installed.forEach(e => {
if (excluded.indexOf(e.id) == -1) {
items.push({
label: e.id,
description: e.packageJSON.description
});
}
});
if (items.length == 0) {
vscode.window.showInformationMessage('All installed extensions excluded.');
return;
}
let result = await vscode.window.showQuickPick(items, {matchOnDescription: true});
if (result) {
excluded.push(result.label);
let settings = this.Settings.Settings;
settings.excluded.installed = excluded;
this.Settings.Settings = settings;
this.Settings.save();
vscode.window.showInformationMessage('Successfully excluded package ' + result.label);
}
}
async removeExcludedInstalledPackage() {
let excluded: string[] = this.Settings.Settings.excluded.installed;
let items: vscode.QuickPickItem[] = [];
excluded.forEach(str => {
items.push({
label: str,
description: ''
});
});
if (items.length == 0) {
vscode.window.showInformationMessage('No installed extensions excluded.');
return;
}
let result = await vscode.window.showQuickPick(items);
if (result) {
excluded.splice(excluded.indexOf(result.label, 1));
let settings = this.Settings.Settings;
settings.excluded.installed = excluded;
this.Settings.Settings = settings;
this.Settings.save();
vscode.window.showInformationMessage('Successfully included package ' + result.label);
}
}
async addExcludedExternalPackage() {
let excluded: string[] = this.Settings.Settings.excluded.external;
let external: string[] = this.Settings.Extensions;
let items: vscode.QuickPickItem[] = [];
if (external.length == 0) {
vscode.window.showInformationMessage('There are no external extensions.');
return;
}
external.forEach(e => {
if (excluded.indexOf(e) == -1) {
items.push({
label: e,
description: ''
});
}
});
if (items.length == 0) {
vscode.window.showInformationMessage('All external extensions excluded.');
return;
}
let result = await vscode.window.showQuickPick(items);
if (result) {
excluded.push(result.label);
let settings = this.Settings.Settings;
settings.excluded.external = excluded;
this.Settings.Settings = settings;
this.Settings.save();
vscode.window.showInformationMessage('Successfully excluded package ' + result.label);
}
}
async removeExcludedExternalPackage() {
let excluded: string[] = this.Settings.Settings.excluded.external;
let items: vscode.QuickPickItem[] = [];
excluded.forEach(str => {
items.push({
label: str,
description: ''
});
});
if (items.length == 0) {
vscode.window.showInformationMessage('No external extensions excluded.');
return;
}
let result = await vscode.window.showQuickPick(items);
if (result) {
excluded.splice(excluded.indexOf(result.label, 1));
let settings = this.Settings.Settings;
settings.excluded.external = excluded;
this.Settings.Settings = settings;
this.Settings.save();
vscode.window.showInformationMessage('Successfully included package ' + result.label);
}
}
displayExcludedInstalledPackages() {
let excluded: string[] = this.Settings.Settings.excluded.installed;
if (excluded.length == 0) {
vscode.window.showInformationMessage('No excluded installed packages.');
return;
}
vscode.window.showInformationMessage('Excluded installed packages:');
excluded.forEach(e => {
vscode.window.showInformationMessage(e);
});
}
displayExcludedExternalPackages() {
let excluded: string[] = this.Settings.Settings.excluded.external;
if (excluded.length == 0) {
vscode.window.showInformationMessage('No excluded external packages.');
return;
}
vscode.window.showInformationMessage('Excluded external packages:');
excluded.forEach(e => {
vscode.window.showInformationMessage(e);
});
}
async toggleSetting(setting: string, value: boolean) {
let items: vscode.QuickPickItem[] = [];
items.push({label: 'On', description: ''});
items.push({label: 'Off', description: ''});
let options: vscode.QuickPickOptions = {};
if (value) {
items[0].description = 'Current setting';
}
else {
items[1].description = 'Current setting';
}
let settings = this.Settings.Settings;
let result = await vscode.window.showQuickPick(items, options);
if (result) {
if (result.label == 'On') {
settings[setting] = true;
}
else {
settings[setting] = false;
if (setting == 'importSettings') {
if (fs.existsSync(path.join(this.Settings.Settings.externalPath, SETTINGS))) {
rimraf.sync(path.join(this.Settings.Settings.externalPath, SETTINGS));
}
}
else if (setting == 'importKeybindings') {
if (fs.existsSync(path.join(this.Settings.Settings.externalPath, KEYBINDINGS))) {
rimraf.sync(path.join(this.Settings.Settings.externalPath, KEYBINDINGS));
}
}
else if (setting == 'importSnippets') {
if (fs.existsSync(path.join(this.Settings.Settings.externalPath, SNIPPETS))) {
rimraf.sync(path.join(this.Settings.Settings.externalPath, SNIPPETS));
}
}
else if (setting == 'importExtensions') {
if (fs.existsSync(path.join(this.Settings.Settings.externalPath, EXTENSIONS))) {
rimraf.sync(path.join(this.Settings.Settings.externalPath, EXTENSIONS));
}
}
}
this.Settings.Settings = settings;
this.Settings.save();
}
}
async setCodeExecutableName(): Promise<void> {
let executableName: string = '';
executableName = await vscode.window.showInputBox({
prompt: 'Enter the VSCode executable name. NOTE: Do not include the file extension (.exe, .sh, etc.)',
placeHolder: 'code'
});
if (executableName == undefined) {
return;
} else {
vscode.window.showInformationMessage('Testing user defined VSCode executable name...');
let oldExecutableName;
if (this.Settings.Settings.executableName) {
oldExecutableName = this.Settings.Settings.executableName;
} else {
oldExecutableName = '';
}
let csSettings = this.Settings.Settings;
csSettings.executableName = executableName;
this.Settings.Settings = csSettings;
this.Settings.save();
if (helpers.isCodeOnPath(this.Settings.Settings)) {
vscode.window.showInformationMessage(`Test succeeded. Will now use ${executableName} as VSCode executable name.`);
} else {
csSettings.executableName = oldExecutableName;
this.Settings.Settings = csSettings;
this.Settings.save();
vscode.window.showInformationMessage('Test failed. Reverting back to old VSCode executable name.');
}
}
}
async setCodeSettingsPath(): Promise<void> {
let settingsPath: string = '';
settingsPath = await vscode.window.showInputBox({
prompt: 'Enter the VSCode user settings path. This must be an absolute path.',
placeHolder: helpers.getDefaultCodeSettingsFolderPath()
});
if (settingsPath == undefined) {
return;
} else {
vscode.window.showInformationMessage('Testing user defined VSCode user settings path...');
const oldSettingsPath = this.Settings.Settings.settingsPath;
this.Settings.Settings.settingsPath = settingsPath;
this.Settings.save();
if (!settingsPath) {
settingsPath = helpers.getDefaultCodeSettingsFolderPath();
}
if (fs.existsSync(settingsPath)) {
vscode.window.showInformationMessage(`Test succeeded. Will now use ${settingsPath} as VSCode user settings path.`);
} else {
this.Settings.Settings.settingsPath = oldSettingsPath;
this.Settings.save();
vscode.window.showInformationMessage('Test failed. Reverting back to old VSCode user settings path.');
}
}
}
private startSync(text: string) {
this.statusBar.StatusBarText = text;
this.statusBar.setSync();
}
private checkForOldSettings() {
let folders: string[] = fs.readdirSync(this.vsCodeExtensionDir);
folders.forEach(f => {
let tmpExtension = helpers.getFolderExtensionInfo(f);
if (tmpExtension.id == 'golf1052.code-sync') {
if (tmpExtension.version != '') {
let splitVersion = tmpExtension.version.split('.');
if (splitVersion.length > 0) {
let major = parseInt(splitVersion[0]);
if (!isNaN(major) && major < 2) {
this.logger.appendLine(`Found version less than 2. Removing: ${tmpExtension.version}.`);
this.emptySyncDir(path.join(this.vsCodeExtensionDir, f, SETTINGS));
rimraf.sync(path.join(this.vsCodeExtensionDir, f));
}
}
else {
this.logger.appendLine(`Could not split version. Removing: ${tmpExtension.version}.`);
this.emptySyncDir(path.join(this.vsCodeExtensionDir, f, SETTINGS));
rimraf.sync(path.join(this.vsCodeExtensionDir, f));
}
}
else {
this.logger.appendLine(`Could not determine version. Removing ${path.join(this.vsCodeExtensionDir, f)}.`);
this.emptySyncDir(path.join(this.vsCodeExtensionDir, f, SETTINGS));
rimraf.sync(path.join(this.vsCodeExtensionDir, f));
}
}
});
}
private async migrateSettings(): Promise<void> {
let folders: string[] = fs.readdirSync(this.vsCodeExtensionDir);
for (let i = 0; i < folders.length; i++) {
let f = folders[i];
let tmpExtension = helpers.getFolderExtensionInfo(f);
if (tmpExtension.id == 'golf1052.code-sync') {
if (tmpExtension.id == 'golf1052.code-sync' && helpers.isVersionGreaterThan(currentVersion, tmpExtension.version) == 1) {
// When the extension is updated we migrate settings to the new extension folder before trying to create settings.
// So if the settings files already exist we've migrated them already and we don't need to migrate them again.
// We don't migrate them again so we don't overwrite any new changes in the new settings files.
let settingsExists: boolean = fs.existsSync(path.join(codeSyncExtensionDir, SETTINGS));
let localSettingsExists: boolean = fs.existsSync(path.join(codeSyncExtensionDir, LOCAL_SETTINGS));
if (!settingsExists || !localSettingsExists) {
this.logger.appendLine(`Migrating stuff. Previous version: ${tmpExtension.version}. Current version: ${currentVersion}.`);
} else {
this.logger.appendLine(`All settings files already exist. Not migrating. Previous version: ${tmpExtension.version}. Current version: ${currentVersion}.`);
}
if (fs.existsSync(path.join(this.vsCodeExtensionDir, f, SETTINGS)) && !settingsExists) {
this.logger.appendLine(`Migrating settings.`);
let oldSettings = path.join(this.vsCodeExtensionDir, f, SETTINGS);
let newSettings = path.join(codeSyncExtensionDir, SETTINGS);
this.logger.appendLine(`Previous file: ${oldSettings}. New file: ${newSettings}.`)
await helpers.copy(oldSettings, newSettings);
}
if (fs.existsSync(path.join(this.vsCodeExtensionDir, f, LOCAL_SETTINGS)) && !localSettingsExists) {
this.logger.appendLine(`Migrating local settings.`);
let oldLocalSettings = path.join(this.vsCodeExtensionDir, f, LOCAL_SETTINGS);
let newLocalSettings = path.join(codeSyncExtensionDir, LOCAL_SETTINGS);
this.logger.appendLine(`Previous file: ${oldLocalSettings}. New file: ${newLocalSettings}.`);
await helpers.copy(oldLocalSettings, newLocalSettings);
}
}
}
}
}
private emptySyncDir(settingsFilePath: string) {
let settings: any = null;
if (fs.existsSync(settingsFilePath)) {
settings = helpers.parseJson(fs.readFileSync(settingsFilePath, 'utf8'));
}
try {
if (fs.existsSync(settings.externalPath)) {
let stuff = fs.readdirSync(settings.externalPath);
stuff.forEach(f => {
rimraf.sync(path.join(settings.externalPath, f));
});
}
}
catch (ex) {
return;
}
}
}