|
1 |
| -// The module 'vscode' contains the VS Code extensibility API |
2 |
| -// Import the module and reference it with the alias vscode in your code below |
| 1 | +import * as chokidar from 'chokidar'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
3 | 5 | import * as vscode from 'vscode';
|
4 | 6 |
|
5 |
| -// This method is called when your extension is activated |
6 |
| -// Your extension is activated the very first time the command is executed |
| 7 | +const walCachePath = path.join( |
| 8 | + os.homedir(), '.cache', 'hyde', 'landing', 'vscode-wallbash.json'); |
| 9 | +const targetPath = path.join(__dirname, '..', 'themes', 'wallbash.json'); |
| 10 | +let autoUpdateWatcher: chokidar.FSWatcher|null = null; |
| 11 | + |
| 12 | + |
7 | 13 | export function activate(context: vscode.ExtensionContext) {
|
| 14 | + console.log('Initializing!\n'); |
| 15 | + console.log('walCachePath: \n', walCachePath); |
| 16 | + console.log('targetPath: \n', targetPath); |
| 17 | + |
| 18 | + |
| 19 | + // The command has been defined in the package.json file |
| 20 | + // Now provide the implementation of the command with registerCommand |
| 21 | + // The commandId parameter must match the command field in package.json |
| 22 | + let disposable = |
| 23 | + vscode.commands.registerCommand('wallbash.update', populateColorThemes); |
| 24 | + context.subscriptions.push(disposable); |
| 25 | + |
| 26 | + // Start the auto update if enabled |
| 27 | + if (vscode.workspace.getConfiguration().get('wallbash.autoUpdate')) { |
| 28 | + /* |
| 29 | + * Update theme at startup |
| 30 | + * Needed for when wal palette updates while vscode isn't running. |
| 31 | + * The timeout is required to overcome a limitation of vscode which |
| 32 | + * breaks the theme auto-update if updated too early at startup. |
| 33 | + */ |
| 34 | + setTimeout(populateColorThemes, 10000); |
| 35 | + |
| 36 | + autoUpdateWatcher = autoUpdate(); |
| 37 | + } |
8 | 38 |
|
9 |
| - // Use the console to output diagnostic information (console.log) and errors (console.error) |
10 |
| - // This line of code will only be executed once when your extension is activated |
11 |
| - console.log('Congratulations, your extension "wallbash" is now active!'); |
| 39 | + // Toggle the auto update in real time when changing the extension |
| 40 | + // configuration |
| 41 | + vscode.workspace.onDidChangeConfiguration(event => { |
| 42 | + if (event.affectsConfiguration('wallbash.autoUpdate')) { |
| 43 | + if (vscode.workspace.getConfiguration().get('wallbash.autoUpdate')) { |
| 44 | + if (autoUpdateWatcher === null) { |
| 45 | + autoUpdateWatcher = autoUpdate(); |
| 46 | + } |
| 47 | + } else if (autoUpdateWatcher !== null) { |
| 48 | + autoUpdateWatcher.close(); |
| 49 | + autoUpdateWatcher = null; |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +export function deactivate() { |
| 56 | + // Close the watcher if active |
| 57 | + if (autoUpdateWatcher !== null) { |
| 58 | + autoUpdateWatcher.close(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Populates the color themes from the wal color palette |
| 64 | + */ |
| 65 | +function populateColorThemes() { |
| 66 | + // Basically will copy from cache to targetPath |
| 67 | + fs.copyFile(walCachePath, targetPath, (err) => { |
| 68 | + if (err) { |
| 69 | + vscode.window.showErrorMessage( |
| 70 | + `Failed to copy the color palette: ${err}`); |
| 71 | + } else { |
| 72 | + vscode.window.showInformationMessage( |
| 73 | + 'Successfully updated the color palette'); |
| 74 | + } |
| 75 | + }); |
12 | 76 |
|
13 |
| - // The command has been defined in the package.json file |
14 |
| - // Now provide the implementation of the command with registerCommand |
15 |
| - // The commandId parameter must match the command field in package.json |
16 |
| - const disposable = vscode.commands.registerCommand('wallbash.helloWorld', () => { |
17 |
| - // The code you place here will be executed every time your command is executed |
18 |
| - // Display a message box to the user |
19 |
| - vscode.window.showInformationMessage('Hello World from wallbash!'); |
20 |
| - }); |
21 | 77 |
|
22 |
| - context.subscriptions.push(disposable); |
23 | 78 | }
|
24 | 79 |
|
25 |
| -// This method is called when your extension is deactivated |
26 |
| -export function deactivate() {} |
| 80 | + |
| 81 | +/** |
| 82 | + * Automatically updates the theme when the color palette changes |
| 83 | + * @returns The watcher for the color palette |
| 84 | + */ |
| 85 | +function autoUpdate(): chokidar.FSWatcher { |
| 86 | + // Watch for changes in the color palette of wal |
| 87 | + return chokidar.watch(walCachePath).on('change', populateColorThemes); |
| 88 | +} |
0 commit comments