Skip to content

Commit

Permalink
chore: Update composeFile utils to validate git repo first
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Feb 19, 2024
1 parent 3e5870c commit 79a8ced
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -18,5 +19,6 @@ cli
.addCommand(cloneCommand())
.addCommand(cleanCommand())
.addCommand(environmentCommand())
.addCommand(composeCommand())

cli.parse();
7 changes: 6 additions & 1 deletion src/commands/compose/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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;
};
Expand Down
4 changes: 3 additions & 1 deletion src/utils/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -13,14 +14,15 @@ export const composeFiles = ({
const composeDirs = [];

for (const dir of repos) {
validateLocalGitRepo(`${reposRoot}/${dir}`);
composeDirs.push(`${reposRoot}/${dir}/${filesDir}`);
}

for (const dir of composeDirs) {
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)} `;
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
};

0 comments on commit 79a8ced

Please sign in to comment.