Skip to content

Commit

Permalink
Change cache clean to manual command
Browse files Browse the repository at this point in the history
Cleaning the cache could take a few seconds, and it prevented other
extension commands from running while it was working.
  • Loading branch information
joelspadin-garmin committed Apr 9, 2020
1 parent 588a7ab commit 8e9c4cc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
5 changes: 5 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
"title": "%command.checkForUpdates.title%",
"category": "%command.category%"
},
{
"command": "privateExtensions.cleanCache",
"title": "%command.cleanCache.title%",
"category": "%command.category%"
},
{
"command": "privateExtensions.configureWorkspaceRegistries",
"title": "%command.configureWorkspaceRegistries.title%",
Expand Down
1 change: 1 addition & 0 deletions extension/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"configuration.updateCheckInterval.description": "Number of seconds between checks for updates to private extensions. Set to 0 to disable automatic update checks.",
"command.category": "Private Extensions",
"command.checkForUpdates.title": "Check for Extension Updates",
"command.cleanCache.title": "Clean NPM Cache",
"command.configureWorkspaceRegistries.title": "Configure Workspace Registries",
"command.configureRecommendedExtensions.title": "Configure Recommended Extensions",
"command.refresh.title": "Refresh",
Expand Down
36 changes: 36 additions & 0 deletions extension/src/commands/cacheCommands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import cacache = require('cacache');
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';

import { Command } from '../commandManager';
import { getLogger } from '../logger';
import { getNpmCacheDir } from '../util';

const localize = nls.loadMessageBundle();

/**
* Opens extensions.private.json to the "registries" element.
*/
export class CleanCacheCommand implements Command {
public readonly id = 'privateExtensions.cleanCache';

public async execute() {
const cache = getNpmCacheDir();

if (!cache) {
vscode.window.showErrorMessage(localize('cache.missing', 'NPM cache is missing.'));
return;
}

vscode.window.withProgress(
{
title: localize('cleaning.npm.cache', 'Cleaning NPM cache.'),
location: vscode.ProgressLocation.Notification,
},
async () => {
const stats = await cacache.verify(cache);
getLogger().log(`Cleaned NPM cache: ${JSON.stringify(stats)}`);
},
);
}
}
1 change: 1 addition & 0 deletions extension/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './cacheCommands';
export * from './configCommands';
export * from './extensionCommands';
export * from './registryCommands';
Expand Down
16 changes: 4 additions & 12 deletions extension/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cacache = require('cacache');
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';

Expand All @@ -9,7 +8,7 @@ import { ExtensionInfoService } from './extensionInfo';
import { ExtensionsFileFeatures } from './extensionsFileFeatures';
import { RegistryProvider } from './RegistryProvider';
import { UpdateChecker } from './UpdateChecker';
import { deleteNpmDownloads, getNpmCacheDir } from './util';
import { deleteNpmDownloads } from './util';
import { RegistryView } from './views/registryView';

// TODO: notify user if extensions.private.json recommends extensions that are
Expand All @@ -36,8 +35,6 @@ export function activate(context: vscode.ExtensionContext) {
registerCommands(registryProvider, registryView, updateChecker, extensionInfo),
registerLanguageFeatures(registryProvider),
);

verifyCache();
}

export async function deactivate() {
Expand Down Expand Up @@ -78,6 +75,9 @@ function registerCommands(
// Configuration commands
new commands.ConfigureWorkspaceRegistries(),
new commands.ConfigureRecommendedExtensions(),

// Other commands
new commands.CleanCacheCommand(),
);

return commandManager;
Expand All @@ -86,11 +86,3 @@ function registerCommands(
function registerLanguageFeatures(registryProvider: RegistryProvider): vscode.Disposable {
return vscode.Disposable.from(new ExtensionsFileFeatures(registryProvider));
}

async function verifyCache() {
const cache = getNpmCacheDir();
if (cache) {
const stats = await cacache.verify(cache);
console.log('Cleaned private extension manager cache:', stats);
}
}

0 comments on commit 8e9c4cc

Please sign in to comment.