Skip to content

Commit

Permalink
chore: Add show-args sub-command to Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Feb 20, 2024
1 parent 6212b05 commit 6255476
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 85 deletions.
63 changes: 14 additions & 49 deletions src/commands/compose/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Command } from 'commander';
import { projectConfig } from '../../config/project';
import { compose, composeFiles, composeSettings } from '../../utils/compose';
import { isFile } from '../../utils/file-system';
import log from '../../utils/log';
import { composeShowArgsCommand } from './show-args';
import { composeExec } from '../../utils/compose';

export const composeCommand = () => {
const command = new Command();
Expand All @@ -15,56 +14,22 @@ export const composeCommand = () => {
'Aids in container orchestration for services in DEMS.\n' +
'Uses Compose under the hood.',
)
.option('-z, --show-compose-string', 'Shows the Composo files string')
.addCommand(composeShowArgsCommand())
.action(async (options) => {
const config = projectConfig();
const args = process.argv.slice(3);

const composeFilesString = composeFiles({
prefix: 'compose',
filesDir: '.dems',
});

let composeEnvFilesString = '';
for (const repo of config.repositories) {
const envFile = `${config.paths.repos_root}/${repo}/.env`;
if (isFile(envFile)) {
composeEnvFilesString += ` --env-file ${envFile}`;
}
}

const composeString = composeFilesString
.concat(composeSettings())
.concat(composeEnvFilesString.trimEnd());
// if (args.length === 0) {
// log.error('A Compose command needs to be specified.');
// process.exit(1);
// }

if (options.showComposeString) {
console.log(composeString);
return;
}

if (args.length === 0) {
log.error('A Compose command needs to be specified.');
process.exit(1);
}

console.log(args);
if (args[0] === 'build') {
const result = compose({
args: composeString,
cmd: 'build',
});
console.log(result.exitCode);
console.log(result.stderr.toString());
}
if (args[0] === 'config') {
const result = compose({
args: composeString,
cmd: 'config',
});
console.log(result.exitCode);
console.log(result.stderr.toString());
console.log(result.stdout.toString());
}
const result = composeExec({
cmd: ['build'],
});
console.log(result.exitCode);
console.log(result.stdout.toString());
console.log(result.stderr.toString());
});

return command;
Expand All @@ -74,5 +39,5 @@ export default composeCommand();

// Execute script only if called directly
if (import.meta.path === Bun.main) {
composeCommand().parse(process.argv);
composeCommand().parse();
}
32 changes: 32 additions & 0 deletions src/commands/compose/show-args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Command } from 'commander';
import { projectConfig } from '../../config/project';
import { composeExecParams, composeFiles } from '../../utils/compose';
import log from '../../utils/log';

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

command
.name('show-args')
.summary('Show args for DEMS-custom Compose command')
.description(
'Prints to console all the flags and arguments used for the custom\n' +
'docker compose command for DEMS.',
)
.action(() => {
log.info('Compose command params:')
console.log(JSON.stringify(composeExecParams(), null, 2));
log.info('Compose files params:')
console.log(JSON.stringify(composeFiles({}), null, 2));
});

return command;
};

export default composeShowArgsCommand();

// Execute script only if called directly
if (import.meta.path === Bun.main) {
composeShowArgsCommand().parse();
}
74 changes: 41 additions & 33 deletions src/utils/compose.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import fs from 'node:fs';
import path from 'path';
import { projectConfig } from '../config/project';
import { isFile } from './file-system';
import { validateLocalGitRepo } from './git';
import type { ComposeFilesParams } from './interfaces';
import type { ComposeExecParams, ComposeFilesParams } from './interfaces';

export const compose = ({
export const composeExec = ({
envFiles = composeExecParams(),
files = composeFiles({}),
cmd,
envFile = '.env',
projectName = projectConfig().compose.project_name,
args = '',
}: {
cmd: string;
envFile?: string;
projectName?: string;
args?: string;
}) => {
}: ComposeExecParams) => {
const command = ['docker', 'compose'];
for (const arg of args.split(' ')) command.push(`${arg.trim()}`);
command.push(cmd);
command.concat(envFiles).concat(files).concat(cmd);
console.log(command);
const result = Bun.spawnSync(command);
return result;
Expand All @@ -26,40 +20,54 @@ export const compose = ({
export const composeFiles = ({
filesDir = '.dems',
prefix = 'compose',
repos = projectConfig().repositories,
reposRoot = projectConfig().paths.repos_root,
}: ComposeFilesParams): string => {
let composeFileString = '';
const composeDirs = [];
}: ComposeFilesParams) => {
const config = projectConfig();

for (const dir of repos) {
validateLocalGitRepo(`${reposRoot}/${dir}`);
composeDirs.push(`${reposRoot}/${dir}/${filesDir}`);
const composeFiles: string[] = [];
const dirs = [];

for (const dir of config.repositories) {
validateLocalGitRepo(`${config.paths.repos_root}/${dir}`);
dirs.push(`${config.paths.repos_root}/${dir}/${filesDir}`);
}

for (const dir of composeDirs) {
for (const dir of dirs) {
const files = fs.readdirSync(dir);
for (const file of files) {
if (file.match(`${prefix}*.yml`)) {
composeFileString += `-f ${path.join(dir, file)} `;
composeFiles.push(`--file ${path.join(dir, file)}`);
}
}
}

return composeFileString;
return composeFiles;
};

export const composeSettings = (
projectName: string = projectConfig().compose.project_name,
envFile: string = projectConfig().paths.env_file,
) => {
let composeSettingString = '';
composeSettingString += `-p ${projectName} `;
composeSettingString += `--env-file ${envFile}`;
return composeSettingString;
export const composeExecParams = () => {
const config = projectConfig();
const params = [];

params.push(`--project-name ${config.compose.project_name}`);
params.push(`--env-file ${config.paths.env_file}`);

for (const repo of config.repositories) {
const envFile = `${config.paths.repos_root}/${repo}/.env`;
if (isFile(envFile)) {
params.push(`--env-file ${envFile}`);
}
}

return params;
};

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

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

export interface ComposeExecParams {
envFiles?: Array<string>;
files?: Array<string>;
cmd: Array<string>;
}

0 comments on commit 6255476

Please sign in to comment.