diff --git a/cli.ts b/cli.ts index 3bba3be..f0b68d3 100755 --- a/cli.ts +++ b/cli.ts @@ -6,6 +6,7 @@ import { setupCommand } from './src/commands/setup'; import { cloneCommand } from './src/commands/clone'; import { cleanCommand } from './src/commands/clean'; import { environmentCommand } from './src/commands/environment'; +import { composeCommand } from './src/commands/compose'; const cli = new Command(); @@ -18,5 +19,6 @@ cli .addCommand(cloneCommand()) .addCommand(cleanCommand()) .addCommand(environmentCommand()) + .addCommand(composeCommand()) cli.parse(); diff --git a/src/commands/compose/index.ts b/src/commands/compose/index.ts index e5d0047..b3e464c 100644 --- a/src/commands/compose/index.ts +++ b/src/commands/compose/index.ts @@ -1,5 +1,6 @@ import { Command } from 'commander'; import { projectConfig } from '../../config/project'; +import { composeFiles } from '../../utils/compose'; export const composeCommand = () => { const command = new Command(); @@ -12,7 +13,11 @@ export const composeCommand = () => { 'Aids in container orchestration for services in DEMS.\n' + 'Uses Compose under the hood.', ) - .action(async (options) => {}); + .option('-z, --show-compose-string', 'Shows the Composo files string') + .action(async (options) => { + if (options.showComposeString) + console.log(composeFiles({ prefix: 'compose', filesDir: '.dems' })); + }); return command; }; diff --git a/src/utils/compose.ts b/src/utils/compose.ts index bad3b7f..ee96d1b 100644 --- a/src/utils/compose.ts +++ b/src/utils/compose.ts @@ -2,6 +2,7 @@ import fs from 'node:fs'; import path from 'path'; import { projectConfig } from '../config/project'; import type { ComposeFilesParams } from './interfaces'; +import { validateLocalGitRepo } from './git'; export const composeFiles = ({ filesDir = '.dems', @@ -13,6 +14,7 @@ export const composeFiles = ({ const composeDirs = []; for (const dir of repos) { + validateLocalGitRepo(`${reposRoot}/${dir}`); composeDirs.push(`${reposRoot}/${dir}/${filesDir}`); } @@ -20,7 +22,7 @@ export const composeFiles = ({ const files = fs.readdirSync(dir); for (const file of files) { if (file.match(`${prefix}*.yml`)) { - composeFileString += ` -f ${path.join(dir, file)}`; + composeFileString += `-f ${path.join(dir, file)} `; } } } diff --git a/src/utils/git.ts b/src/utils/git.ts index 33f5281..f5e14d8 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -53,3 +53,15 @@ export default class Git { } } } + +export const validateLocalGitRepo = (path: string) => { + const gitStatus = Bun.spawnSync(['git', 'status'], { + cwd: path, + }); + + if (gitStatus.exitCode === 0) { + return; + } + + throw new Error(`Local path ${path} is not a valid git repository.`); +};