Skip to content

Commit

Permalink
chore: Updates to Compose command
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Feb 20, 2024
1 parent 02889cd commit 6212b05
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
42 changes: 39 additions & 3 deletions src/commands/compose/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from 'commander';
import { projectConfig } from '../../config/project';
import { composeFiles, composeSettings } from '../../utils/compose';
import { compose, composeFiles, composeSettings } from '../../utils/compose';
import { isFile } from '../../utils/file-system';
import log from '../../utils/log';

export const composeCommand = () => {
Expand All @@ -16,27 +17,62 @@ export const composeCommand = () => {
)
.option('-z, --show-compose-string', 'Shows the Composo files string')
.action(async (options) => {
const config = projectConfig();
const args = process.argv.slice(3);

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

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 (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());
}
});

return command;
};

export default composeCommand();

// Execute script only if called directly
if (import.meta.path === Bun.main) {
composeCommand().parse(process.argv);
}
23 changes: 21 additions & 2 deletions src/commands/environment/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command } from 'commander';
import dotEnv from '../../config/env';
import { projectConfig, projectEnvVars } from '../../config/project';
import { copyFile } from '../../utils/file-system';
import log from '../../utils/log';

export const environmentCommand = () => {
Expand All @@ -20,13 +21,31 @@ export const environmentCommand = () => {
'-g, --generate-dot-env',
"Generate the dot env file for current project's config.json",
)
.option(
'-c, --copy-example-files',
"Copy the .env.example file of the current project's repositories",
)
.action(async (options) => {
const config = projectConfig();

if (options.copyExampleFiles) {
for (const repo of config.repositories) {
const repoPath = `${config.paths.repos_root}/${repo}`;
copyFile({
source: `${repoPath}/.env.example`,
target: `${repoPath}/.env`,
});
}
return;
}

if (options.generateDotEnv) {
log.info("Generating project's dot env file...");
dotEnv.generate(config.paths.env_file, config);
} else {
console.log(projectEnvVars());
return;
}

console.log(projectEnvVars());
});

return command;
Expand Down
19 changes: 19 additions & 0 deletions src/utils/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ import { projectConfig } from '../config/project';
import { validateLocalGitRepo } from './git';
import type { ComposeFilesParams } from './interfaces';

export const compose = ({
cmd,
envFile = '.env',
projectName = projectConfig().compose.project_name,
args = '',
}: {
cmd: string;
envFile?: string;
projectName?: string;
args?: string;
}) => {
const command = ['docker', 'compose'];
for (const arg of args.split(' ')) command.push(`${arg.trim()}`);
command.push(cmd);
console.log(command);
const result = Bun.spawnSync(command);
return result;
};

export const composeFiles = ({
filesDir = '.dems',
prefix = 'compose',
Expand Down
4 changes: 3 additions & 1 deletion test/commands/compose.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ describe("Command: 'compose'", () => {

test('Returns error when no arguments', () => {
const command = Bun.spawnSync(['./cli.ts', 'compose']);
expect(command.stdout.toString()).toContain('A Compose command needs to be specified.');
expect(command.stdout.toString()).toContain(
'A Compose command needs to be specified.',
);
expect(command.exitCode).toEqual(1);
});
});

0 comments on commit 6212b05

Please sign in to comment.