From f238579ec16b8480e487d0b0bfbf9e820fcfe42e Mon Sep 17 00:00:00 2001 From: Ben <43026681+bwp91@users.noreply.github.com> Date: Sat, 8 Feb 2025 08:57:32 +0000 Subject: [PATCH] clean up `npm` cache before any install/uninstall --- CHANGELOG.md | 1 + src/modules/plugins/plugins.service.ts | 35 +++++++++++--------------- src/modules/server/server.service.ts | 2 +- test/e2e/plugins.gateway.e2e-spec.ts | 2 ++ 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c025b8f50..b7089a7cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ All notable changes to `homebridge-config-ui-x` will be documented in this file. ### Other Changes - append `--omit=dev` to `npm i` commands +- clean up `npm` cache before any install/uninstall ### Homebridge Dependencies diff --git a/src/modules/plugins/plugins.service.ts b/src/modules/plugins/plugins.service.ts index 9ea3861de..f39490e93 100755 --- a/src/modules/plugins/plugins.service.ts +++ b/src/modules/plugins/plugins.service.ts @@ -546,27 +546,22 @@ export class PluginsService { npmPluginLabel = `${pluginAction.name}@${pluginAction.version}` } - try { - await this.runNpmCommand( - [...this.npm, action, ...installOptions, npmPluginLabel], - installPath, - client, - pluginAction.termCols, - pluginAction.termRows, - ) + // Clean up the npm cache before any install or uninstall + await this.cleanNpmCache() + + // Run the npm command + await this.runNpmCommand( + [...this.npm, action, ...installOptions, npmPluginLabel], + installPath, + client, + pluginAction.termCols, + pluginAction.termRows, + ) - // Ensure the custom plugin dir was not deleted - await this.ensureCustomPluginDirExists() + // Ensure the custom plugin dir was not deleted + await this.ensureCustomPluginDirExists() - return true - } catch (e) { - if (pluginAction.name === this.configService.name) { - client.emit('stdout', yellow('\r\nCleaning up npm cache, please wait...\r\n')) - await this.cleanNpmCache() - client.emit('stdout', yellow(`npm cache cleared, please try updating ${this.configService.name} again.\r\n`)) - } - throw e - } + return true } /** @@ -1593,7 +1588,7 @@ export class PluginsService { } return new Promise((res) => { - const child = spawn(command.shift(), command) + const child = spawn(command.shift(), command, { shell: true }) child.on('exit', (code) => { this.logger.log(`Executed npm cache clear command with exit code ${code}.`) diff --git a/src/modules/server/server.service.ts b/src/modules/server/server.service.ts index eb669c36d..406730f19 100644 --- a/src/modules/server/server.service.ts +++ b/src/modules/server/server.service.ts @@ -688,7 +688,7 @@ export class ServerService { return new Promise((res) => { let result = false - const child = spawn(process.execPath, ['-v']) + const child = spawn(process.execPath, ['-v'], { shell: true }) child.stdout.once('data', (data) => { result = data.toString().trim() !== process.version diff --git a/test/e2e/plugins.gateway.e2e-spec.ts b/test/e2e/plugins.gateway.e2e-spec.ts index 48c354615..c05e287f2 100644 --- a/test/e2e/plugins.gateway.e2e-spec.ts +++ b/test/e2e/plugins.gateway.e2e-spec.ts @@ -1,6 +1,7 @@ import type { NestFastifyApplication } from '@nestjs/platform-fastify' import type { TestingModule } from '@nestjs/testing' +import child_process from 'node:child_process' import { EventEmitter } from 'node:events' import { platform } from 'node:os' import { resolve } from 'node:path' @@ -79,6 +80,7 @@ describe('PluginsGateway (e2e)', () => { vi.spyOn(client, 'emit') vi.spyOn(client, 'on') + vi.spyOn(child_process, 'spawn') // Ensure config is correct configService.ui.sudo = false