From bd1034eff8dbe1aa4a44795351f16190b2e26dbf Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 9 Nov 2022 11:28:31 +0800 Subject: [PATCH] core: add `hydrooj uninstall` command --- packages/hydrooj/bin/commands.ts | 17 +++++++++++++++++ packages/utils/lib/common.ts | 30 ++++++++++++++++++++++++++++++ packages/utils/lib/utils.ts | 29 ----------------------------- 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/packages/hydrooj/bin/commands.ts b/packages/hydrooj/bin/commands.ts index cfe12e6b..2378a8f3 100644 --- a/packages/hydrooj/bin/commands.ts +++ b/packages/hydrooj/bin/commands.ts @@ -175,6 +175,23 @@ if (!argv.args[0] || argv.args[0] === 'cli') { lastUpdate: Date.now(), })); }); + cli.command('uninstall [package]').action(async (name) => { + if (!name) { + cli.outputHelp(); + return; + } + if (yarnVersion !== 1) throw new Error('Yarn 1 is required.'); + const addonDir = path.join(hydroPath, 'addons'); + fs.ensureDirSync(addonDir); + const plugins = fs.readdirSync(addonDir); + if (!plugins.includes(name)) { + throw new Error(`Plugin ${name} not found or not installed with \`hydrooj install\`.`); + } + const newAddonPath = path.join(addonDir, name); + child.execSync(`hydrooj addon remove '${newAddonPath}'`, { stdio: 'inherit' }); + fs.removeSync(newAddonPath); + console.log(`Successfully uninstalled ${name}.`); + }); cli.help(); cli.parse(); if (!cli.matchedCommand) { diff --git a/packages/utils/lib/common.ts b/packages/utils/lib/common.ts index be8dfc79..5c71e068 100644 --- a/packages/utils/lib/common.ts +++ b/packages/utils/lib/common.ts @@ -126,6 +126,36 @@ Set.intersection = function Intersection(A: Set | Array = [], B: Set return intersection; }; +export function sleep(timeout: number) { + return new Promise((resolve) => { + setTimeout(() => resolve(true), timeout); + }); +} + +function deepen(modifyString: (source: string) => string) { + function modifyObject(source: T): T { + if (typeof source !== 'object' || !source) return source; + if (Array.isArray(source)) return source.map(modifyObject) as any; + const result = {} as T; + for (const key in source) { + result[modifyString(key)] = modifyObject(source[key]); + } + return result; + } + + return function t(source: T): T { + if (typeof source === 'string') return modifyString(source) as any; + return modifyObject(source); + }; +} + +export function noop() { } + +export const camelCase = deepen((source) => source.replace(/[_-][a-z]/g, (str) => str.slice(1).toUpperCase())); +export const paramCase = deepen((source) => source.replace(/_/g, '-').replace(/(? `-${str.toLowerCase()}`)); +export const snakeCase = deepen((source) => source.replace(/-/g, '_').replace(/(? `_${str.toLowerCase()}`)); + + const TIME_RE = /^([0-9]+(?:\.[0-9]*)?)([mu]?)s?$/i; const TIME_UNITS = { '': 1000, m: 1, u: 0.001 }; const MEMORY_RE = /^([0-9]+(?:\.[0-9]*)?)([kmg])b?$/i; diff --git a/packages/utils/lib/utils.ts b/packages/utils/lib/utils.ts index 83cd47d4..8f399d49 100644 --- a/packages/utils/lib/utils.ts +++ b/packages/utils/lib/utils.ts @@ -105,35 +105,6 @@ export function bufferToStream(buffer: Buffer): NodeJS.ReadableStream { return stream; } -export function sleep(timeout: number) { - return new Promise((resolve) => { - setTimeout(() => resolve(true), timeout); - }); -} - -function deepen(modifyString: (source: string) => string) { - function modifyObject(source: T): T { - if (typeof source !== 'object' || !source) return source; - if (Array.isArray(source)) return source.map(modifyObject) as any; - const result = {} as T; - for (const key in source) { - result[modifyString(key)] = modifyObject(source[key]); - } - return result; - } - - return function t(source: T): T { - if (typeof source === 'string') return modifyString(source) as any; - return modifyObject(source); - }; -} - -export function noop() { } - -export const camelCase = deepen((source) => source.replace(/[_-][a-z]/g, (str) => str.slice(1).toUpperCase())); -export const paramCase = deepen((source) => source.replace(/_/g, '-').replace(/(? `-${str.toLowerCase()}`)); -export const snakeCase = deepen((source) => source.replace(/-/g, '_').replace(/(? `_${str.toLowerCase()}`)); - export namespace Time { export const second = 1000; export const minute = second * 60;