Skip to content

Commit

Permalink
chore: Update ComposeFiles function
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Feb 19, 2024
1 parent 1979f77 commit 3e5870c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 18 deletions.
15 changes: 12 additions & 3 deletions src/commands/clean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ export const cleanCommand = () => {
log.info('User interactivity disabled due to --force flag.');
}

await deletePath({ path: config.paths.repos_root, force: options.force });
await deletePath({ path: config.paths.data, force: options.force });
await deletePath({ path: config.paths.env_file, force: options.force });
await deletePath({
path: config.paths.repos_root,
force: options.force,
});
await deletePath({
path: config.paths.data,
force: options.force,
});
await deletePath({
path: config.paths.env_file,
force: options.force,
});

log.success('Clean completed for current project.');
});
Expand Down
20 changes: 20 additions & 0 deletions src/commands/compose/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Command } from 'commander';
import { projectConfig } from '../../config/project';

export const composeCommand = () => {
const command = new Command();
const config = projectConfig();

command
.name('compose')
.summary('Container orchestration command for DEMS')
.description(
'Aids in container orchestration for services in DEMS.\n' +
'Uses Compose under the hood.',
)
.action(async (options) => {});

return command;
};

export default composeCommand();
2 changes: 1 addition & 1 deletion src/commands/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const configCommand = () => {
.addCommand(projectConfigCommand())
.action((options) => {
if (options.generate) {
createPath(cliConfig.root);
createPath({ path: cliConfig.root });
createFile({
file: cliConfig.file,
content: JSON.stringify(cliConfig, null, 2),
Expand Down
13 changes: 13 additions & 0 deletions src/utils/compose-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const COMPOSE_SPEC_JSON_FILE_RULE =
'https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json';

const downloadComposeSpecJSON = (
url: URL = new URL(COMPOSE_SPEC_JSON_FILE_RULE),
) => {
console.log(url.toString());
};

// Execute script only if called directly
if (import.meta.path === Bun.main) {
downloadComposeSpecJSON();
}
32 changes: 18 additions & 14 deletions src/utils/compose.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import fs from 'node:fs';
import path from 'path';
import { projectConfig } from '../config/project';
import type { ComposeFilesParams } from './interfaces';

export const composeFiles = ({
filesDir = '.dems',
prefix = 'compose',
filesDir = 'src/compose',
dockerDir = '.docker',
repos = projectConfig().repositories,
reposRoot = projectConfig().paths.repos_root,
}: ComposeFilesParams): string => {
let composeFileString = '';
const composeDirs = [];

const readFilesRecursively = (currentDir: string) => {
const files = fs.readdirSync(currentDir);
for (const dir of repos) {
composeDirs.push(`${reposRoot}/${dir}/${filesDir}`);
}

for (const dir of composeDirs) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(currentDir, file);
const isDirectory = fs.statSync(filePath).isDirectory();

if (isDirectory && file === dockerDir) {
readFilesRecursively(filePath);
} else if (file.match(`${prefix}.*.yml`)) {
composeFileString += ` -f ${filePath}`;
if (file.match(`${prefix}*.yml`)) {
composeFileString += ` -f ${path.join(dir, file)}`;
}
}
};

readFilesRecursively(filesDir);
}

return composeFileString;
};

// Execute script only if called directly
if (import.meta.path === Bun.main) {
console.log(composeFiles({ prefix: 'compose', filesDir: '.dems' }));
}
2 changes: 2 additions & 0 deletions src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface GitParams {
}

export interface ComposeFilesParams {
reposRoot?: string;
repos?: Array<string>;
prefix?: string;
filesDir?: string;
dockerDir?: string;
Expand Down

0 comments on commit 3e5870c

Please sign in to comment.