UNPKG

@k1ssh/qbot

Version:

QBot SDK is a set of APIs for creating and managing microservices, and Kubernetes deployments. This is the core package for QBot AI.

104 lines (103 loc) 3.69 kB
import fs from 'fs/promises'; import path from 'path'; import { CommandExecutor } from './CommandExecutor.js'; import { BASE_DIRS, ENV_DIRS, QBOT_DIR } from './QBotProject.js'; import { log } from './Util.js'; export const ProjectUtils = { validateProjectDirectories, gitInit, gitClone, gitFork, }; async function createDirs(projectDir, dir) { await fs .stat(projectDir) .then(async (stats) => { if (!stats.isDirectory()) { await fs.mkdir(projectDir, { recursive: true }); } }) // if the directory does not exist (ENOENT), create it .catch(async (err) => { if (err.code === 'ENOENT') { await fs.mkdir(projectDir, { recursive: true }); } else { log(`Error validating directory ${dir}:`, err); throw err; } }); } /** * Validate that all needed directories for a project exist and are accessible. * If they don't exist, create them. * @param projectName The name of the project to validate directories for * @returns Promise<void> * @throws Error if there is an error validating or creating directories */ async function validateProjectDirectories(projectName) { // Base dirs await Promise.all(BASE_DIRS.map(async (dir) => { const projectDir = path.resolve(QBOT_DIR, projectName, dir); await createDirs(projectDir, dir); })); // Create the envs await Promise.all(ENV_DIRS.map(async (dir) => { const projectDir = path.resolve(QBOT_DIR, projectName, 'envs', dir); await createDirs(projectDir, dir); })); } async function gitInit(projectName) { const projectDir = path.resolve(QBOT_DIR, projectName); try { // cd into the project directory, initialize a git repository, // create a main branch, set it as the default branch, and add an initial commit const executor = new CommandExecutor(projectDir); await executor.executeSequence([ 'git init -q -b main', 'git add .', 'git commit -m "Project initialized"', ]); // @todo add a remote git repository } catch (error) { log(`Error initializing git repository in ${projectDir}:`, error); throw error; } } async function gitClone(projectName, repositoryUrl, cloneDir) { const projectDir = path.resolve(QBOT_DIR, projectName); try { // clone the remote repository into the project directory const executor = new CommandExecutor(projectDir); // Clone the bare repository to the base directory await executor.executeSequence([ // `git clone --bare ${repositoryUrl} ${cloneDir}`, `git clone --depth 1 --single-branch --branch main ${repositoryUrl} ${cloneDir}`, `cd ${cloneDir}`, `git add .`, `git commit -m "Added files from ${repositoryUrl}"`, ]); } catch (error) { log(`Error cloning repository ${repositoryUrl} into ${projectDir}:`, error); throw error; } } async function gitFork(projectName, forkedRepoUrl, newOriginUrl) { const projectDir = path.resolve(QBOT_DIR, projectName); try { const executor = new CommandExecutor(projectDir); // Clone the forked repository await executor.executeSequence([ `git clone ${forkedRepoUrl} .`, `git remote remove origin`, `git remote add upstream ${forkedRepoUrl}`, `git remote add origin ${newOriginUrl}`, ]); } catch (error) { log(`Error forking repository ${forkedRepoUrl} into ${projectDir}:`, error); throw error; } }