Skip to content

Commit

Permalink
Add command to clear local npm cache
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Aug 20, 2024
1 parent 24ef473 commit 7bb3902
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3085,6 +3085,12 @@
"title": "Open SceneGraph Inspector In New Window",
"category": "BrighterScript",
"icon": "$(link-external)"
},
{
"command": "extension.brightscript.clearNpmPackageCache",
"title": "Clear the extension's local node_modules cache",
"category": "BrighterScript",
"icon": "$(link-external)"
}
],
"keybindings": [
Expand Down
2 changes: 1 addition & 1 deletion src/BrightScriptCommands.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('BrightScriptFileUtils ', () => {
let languagesMock;

beforeEach(() => {
commands = new BrightScriptCommands({} as any, {} as any, {} as any, {} as any, {} as any);
commands = new BrightScriptCommands({} as any, {} as any, {} as any, {} as any, {} as any, {} as any);
commandsMock = sinon.mock(commands);
languagesMock = sinon.mock(vscode.languages);
});
Expand Down
6 changes: 5 additions & 1 deletion src/BrightScriptCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type { ActiveDeviceManager } from './ActiveDeviceManager';
import * as xml2js from 'xml2js';
import { firstBy } from 'thenby';
import type { UserInputManager } from './managers/UserInputManager';
import { clearNpmPackageCacheCommand } from './commands/ClearNpmPackageCacheCommand';
import type { LanguageServerManager } from './LanguageServerManager';

export class BrightScriptCommands {

Expand All @@ -22,7 +24,8 @@ export class BrightScriptCommands {
private whatsNewManager: WhatsNewManager,
private context: vscode.ExtensionContext,
private activeDeviceManager: ActiveDeviceManager,
private userInputManager: UserInputManager
private userInputManager: UserInputManager,
private languageServerManager: LanguageServerManager
) {
this.fileUtils = new BrightScriptFileUtils();
}
Expand All @@ -39,6 +42,7 @@ export class BrightScriptCommands {
languageServerInfoCommand.register(this.context);
captureScreenshotCommand.register(this.context, this);
rekeyAndPackageCommand.register(this.context, this, this.userInputManager);
clearNpmPackageCacheCommand.register(this.context, this.languageServerManager);

this.registerGeneralCommands();

Expand Down
23 changes: 17 additions & 6 deletions src/LanguageServerManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Module.prototype.require = function hijacked(file) {
const tempDir = s`${process.cwd()}/.tmp`;

describe('LanguageServerManager', () => {
const storageDir = s`${tempDir}/brighterscript-storage`;

let languageServerManager: LanguageServerManager;

beforeEach(() => {
Expand All @@ -49,6 +51,10 @@ describe('LanguageServerManager', () => {
update: () => { }
}
} as unknown as ExtensionContext;

fsExtra.removeSync(storageDir);
(languageServerManager['context'] as any).globalStorageUri = URI.file(storageDir);

});

function stubConstructClient(processor?: (LanguageClient) => void) {
Expand Down Expand Up @@ -332,12 +338,6 @@ describe('LanguageServerManager', () => {
//these tests take a long time (due to running `npm install`)
this.timeout(20_000);

const storageDir = s`${tempDir}/brighterscript-storage`;
beforeEach(() => {
fsExtra.removeSync(storageDir);
(languageServerManager['context'] as any).globalStorageUri = URI.file(storageDir);
});

it('installs a bsc version when not present', async () => {
expect(
await languageServerManager['ensureBscVersionInstalled']('0.65.0')
Expand Down Expand Up @@ -398,4 +398,15 @@ describe('LanguageServerManager', () => {
).to.eql(s`${storageDir}/packages/brighterscript-0.65.1`);
});
});

describe('clearNpmPackageCache', () => {
it('clears the cache', async () => {
fsExtra.ensureFileSync(`${storageDir}/test.txt`);
expect(fsExtra.pathExistsSync(`${storageDir}/packages/test.txt`)).to.be.true;

await languageServerManager.clearNpmPackageCache();

expect(fsExtra.pathExistsSync(`${storageDir}/packages/test.txt`)).to.be.false;
});
});
});
7 changes: 7 additions & 0 deletions src/LanguageServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ export class LanguageServerManager {

return bscPath;
}

/**
* Clear all packages stored in the npm cache for this extension
*/
public async clearNpmPackageCache() {
await fsExtra.emptyDir(s`${this.context.globalStorageUri.fsPath}/packages`);
}
}

export const languageServerManager = new LanguageServerManager();
Expand Down
14 changes: 14 additions & 0 deletions src/commands/ClearNpmPackageCacheCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as vscode from 'vscode';
import type { LanguageServerManager } from '../LanguageServerManager';

export class ClearNpmPackageCacheCommand {
public static commandName = 'extension.brightscript.clearNpmPackageCache';

public register(context: vscode.ExtensionContext, languageServerManager: LanguageServerManager) {
context.subscriptions.push(vscode.commands.registerCommand(ClearNpmPackageCacheCommand.commandName, async () => {
await languageServerManager.clearNpmPackageCache();
}));
}
}

export const clearNpmPackageCacheCommand = new ClearNpmPackageCacheCommand();
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class Extension {
this.whatsNewManager,
context,
activeDeviceManager,
userInputManager
userInputManager,
languageServerManager
);

this.rtaManager = new RtaManager(context);
Expand Down
2 changes: 1 addition & 1 deletion src/managers/WebviewViewProviderManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('WebviewViewProviderManager', () => {
const config = {} as BrightScriptLaunchConfiguration;
let webviewViewProviderManager: WebviewViewProviderManager;
let rtaManager: RtaManager;
const brightScriptCommands = new BrightScriptCommands({} as any, {} as any, {} as any, {} as any, {} as any);
const brightScriptCommands = new BrightScriptCommands({} as any, {} as any, {} as any, {} as any, {} as any, {} as any);

before(() => {
context = {
Expand Down

0 comments on commit 7bb3902

Please sign in to comment.