From 1fc58f4496ee27341024c1ca0e68f94178a8a306 Mon Sep 17 00:00:00 2001 From: Mohamed Marrouchi Date: Tue, 22 Oct 2024 15:47:33 +0100 Subject: [PATCH] fix: remove old way of handling extensions --- api/Dockerfile | 8 -- api/merge-extensions-deps.js | 80 ----------------- api/package.json | 1 - api/src/chat/controllers/block.controller.ts | 2 +- cli/src/index.ts | 90 ++------------------ 5 files changed, 10 insertions(+), 171 deletions(-) delete mode 100644 api/merge-extensions-deps.js diff --git a/api/Dockerfile b/api/Dockerfile index 8f057a9..c35c2d6 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -4,10 +4,6 @@ WORKDIR /app COPY package*.json ./ -COPY merge-extensions-deps.js ./ - -COPY src/extensions ./src/extensions - COPY patches ./patches RUN npm ci @@ -22,8 +18,6 @@ WORKDIR /app COPY package*.json ./ -COPY --from=builder /app/merge-extensions-deps.js ./ - COPY --from=builder /app/src/extensions ./src/extensions COPY --from=builder /app/patches ./patches @@ -44,8 +38,6 @@ WORKDIR /app COPY package*.json ./ -COPY --from=builder /app/merge-extensions-deps.js ./ - COPY --from=builder /app/src/extensions ./src/extensions COPY --from=builder /app/patches ./patches diff --git a/api/merge-extensions-deps.js b/api/merge-extensions-deps.js deleted file mode 100644 index 346cd72..0000000 --- a/api/merge-extensions-deps.js +++ /dev/null @@ -1,80 +0,0 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const fs = require('fs'); -// eslint-disable-next-line @typescript-eslint/no-var-requires -const path = require('path'); - -// Define the paths -const rootPackageJsonPath = path.join(__dirname, 'package.json'); -const pluginsDir = path.join(__dirname, 'src', 'extensions', 'plugins'); -const channelsDir = path.join(__dirname, 'src', 'extensions', 'channels'); -const helpersDir = path.join(__dirname, 'src', 'extensions', 'helpers'); - -// Helper function to merge dependencies -function mergeDependencies(rootDeps, pluginDeps) { - return { - ...rootDeps, - ...Object.entries(pluginDeps).reduce((acc, [key, version]) => { - if (!rootDeps[key]) { - acc[key] = version; - } - return acc; - }, {}), - }; -} - -// Read the root package.json -const rootPackageJson = JSON.parse( - fs.readFileSync(rootPackageJsonPath, 'utf-8'), -); - -// Initialize dependencies if not already present -if (!rootPackageJson.dependencies) { - rootPackageJson.dependencies = {}; -} - -// Iterate over extension directories -[ - ...fs.readdirSync(pluginsDir), - ...fs.readdirSync(helpersDir), - ...fs.readdirSync(channelsDir), -].forEach((pluginFolder) => { - const pluginPackageJsonPath = path.join( - pluginsDir, - pluginFolder, - 'package.json', - ); - - if (fs.existsSync(pluginPackageJsonPath)) { - const pluginPackageJson = JSON.parse( - fs.readFileSync(pluginPackageJsonPath, 'utf-8'), - ); - - // Merge extension dependencies into root dependencies - if (pluginPackageJson.dependencies) { - rootPackageJson.dependencies = mergeDependencies( - rootPackageJson.dependencies, - pluginPackageJson.dependencies, - ); - } - - // Merge extension devDependencies into root devDependencies - if (pluginPackageJson.devDependencies) { - rootPackageJson.devDependencies = mergeDependencies( - rootPackageJson.devDependencies, - pluginPackageJson.devDependencies, - ); - } - } -}); - -// Write the updated root package.json -fs.writeFileSync( - rootPackageJsonPath, - JSON.stringify(rootPackageJson, null, 2), - 'utf-8', -); - -// eslint-disable-next-line no-console -console.log( - 'Dependencies from extensions have been merged into the root package.json', -); diff --git a/api/package.json b/api/package.json index 0ef0749..f992ba1 100644 --- a/api/package.json +++ b/api/package.json @@ -6,7 +6,6 @@ "author": "Hexastack", "license": "AGPL-3.0-only", "scripts": { - "preinstall": "node merge-extensions-deps.js", "postinstall": "patch-package", "build:clean": "rm -rf src/.hexabot", "build:channels": "mkdir -p src/.hexabot/channels && find node_modules/ -name 'hexabot-channel-*' -exec cp -R {} src/.hexabot/channels/ \\;", diff --git a/api/src/chat/controllers/block.controller.ts b/api/src/chat/controllers/block.controller.ts index e3145e5..6621b6f 100644 --- a/api/src/chat/controllers/block.controller.ts +++ b/api/src/chat/controllers/block.controller.ts @@ -94,7 +94,7 @@ export class BlockController extends BaseController< try { if (!pluginName) { throw new BadRequestException( - 'Plugin id must be supplied as a query param', + 'Plugin name must be supplied as a query param', ); } diff --git a/cli/src/index.ts b/cli/src/index.ts index 31620df..c1e86f1 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -1,12 +1,11 @@ #!/usr/bin/env node -import figlet from 'figlet'; -import { Command } from 'commander'; +import chalk from 'chalk'; import { execSync } from 'child_process'; +import { Command } from 'commander'; +import figlet from 'figlet'; import * as fs from 'fs'; import * as path from 'path'; -import chalk from 'chalk'; -import degit from 'degit'; console.log(figlet.textSync('Hexabot')); @@ -95,7 +94,7 @@ const dockerCompose = (args: string): void => { const dockerExec = ( container: string, command: string, - options?: string + options?: string, ): void => { try { execSync(`docker exec -it ${options} ${container} ${command}`, { @@ -179,7 +178,11 @@ program .description('Run database migrations') .action((args) => { const migrateArgs = args.join(' '); - dockerExec('api', `npm run migrate ${migrateArgs}`, '--user $(id -u):$(id -g)'); + dockerExec( + 'api', + `npm run migrate ${migrateArgs}`, + '--user $(id -u):$(id -g)', + ); }); program @@ -222,81 +225,6 @@ program dockerCompose(`${composeArgs} down -v`); }); -// Add install command to install extensions (e.g., channels, plugins) -program - .command('install') - .description('Install an extension for Hexabot') - .argument('', 'The type of extension (e.g., channel, plugin)') - .argument( - '', - 'GitHub repository for the extension (user/repo format)', - ) - .action(async (type, repository) => { - // Define the target folder based on the extension type - let targetFolder = ''; - switch (type) { - case 'channel': - targetFolder = 'api/src/extensions/channels/'; - break; - case 'plugin': - targetFolder = 'api/src/extensions/plugins/'; - break; - default: - console.error(chalk.red(`Unknown extension type: ${type}`)); - process.exit(1); - } - - // Get the last part of the repository name - const repoName = repository.split('/').pop(); - - // If the repo name starts with "hexabot--", remove that prefix - const extensionName = repoName.startsWith(`hexabot-${type}-`) - ? repoName.replace(`hexabot-${type}-`, '') - : repoName; - - const extensionPath = path.resolve( - process.cwd(), - targetFolder, - extensionName, - ); - - // Check if the extension folder already exists - if (fs.existsSync(extensionPath)) { - console.error( - chalk.red(`Error: Extension already exists at ${extensionPath}`), - ); - process.exit(1); - } - - try { - console.log( - chalk.cyan(`Fetching ${repository} into ${extensionPath}...`), - ); - - // Use degit to fetch the repository without .git history - const emitter = degit(repository); - await emitter.clone(extensionPath); - - console.log(chalk.cyan('Running npm install in the api/ folder...')); - // Run npm install in the api folder to install dependencies - execSync('npm run preinstall', { - cwd: path.resolve(process.cwd(), 'api'), - stdio: 'inherit', - }); - execSync('npm install', { - cwd: path.resolve(process.cwd(), 'api'), - stdio: 'inherit', - }); - - console.log( - chalk.green(`Successfully installed ${extensionName} as a ${type}.`), - ); - } catch (error) { - console.error(chalk.red('Error during installation:'), error); - process.exit(1); - } - }); - // Parse arguments program.parse(process.argv);