Skip to content

Commit

Permalink
chore: Add more clone command/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Feb 14, 2024
1 parent d359f0a commit dabbd25
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,4 @@ dist
# Build files
dist/**
./dems
**/repos/
16 changes: 8 additions & 8 deletions src/commands/clone/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { Command } from 'commander';
import { projectConfig, projectEnvVars } from '../../config/project';
import Git from '../../utils/git';
import log from '../../utils/log';
import sharedOptions, {
exitCommandIfInfoOnly,
} from '../../utils/shared-options';
import sharedOptions from '../../utils/shared-options';

export const cloneCommand = () => {
const command = new Command();
Expand All @@ -21,14 +19,16 @@ export const cloneCommand = () => {
.addOption(sharedOptions.gitRef().default(config.git.default_ref))
.addOption(sharedOptions.reposRoot().default(config.paths.repos_root))
.addOption(sharedOptions.gitOrg().default(config.git.org_url))
.addOption(sharedOptions.repos())
.addOption(sharedOptions.info())
.action((options) => {
console.log(`Git org > ${chalk.bold(options.gitOrg)}`);
console.log(`Git ref > ${chalk.bold(options.gitRef)}`);
console.log(`Repos path > ${chalk.bold(options.reposRoot)}`);
exitCommandIfInfoOnly(options.info);
console.log(`Git org > ${options.gitOrg}`);
console.log(`Git ref > ${options.gitRef}`);
console.log(`Repos path > ${options.reposRoot}`);

for (const repo of config.repositories) {
if (options.info) return;

for (const repo of options.repos) {
const repoUrl = `${options.gitOrg}/${repo}`;
const git = new Git({
workingDir: options.reposRoot,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const setupCommand = () => {
)
.option('-p, --project-name [project-name]', 'Set project name')
.option('-o, --repos-root-path [root-path]', 'Repositories root path')
.option('-r, --repo [repo...]', 'Set project repositories')
.option('-e, --dot-env [path]', 'Project config dot env file')
.option('-d, --dockerfile [dockerfile]', 'Dockerfile needed for dev')
.addOption(sharedOptions.repos())
.addOption(sharedOptions.gitOrg())
.addOption(sharedOptions.reposRoot())
.addOption(sharedOptions.gitRef())
Expand Down Expand Up @@ -82,7 +82,7 @@ export const setupCommand = () => {
const repos = await input({
message:
'What are the repositories for this project? (comma-sperated list)',
default: demsEnvVars.repos || options.repo || 'demo-api,demo-webapp',
default: demsEnvVars.repos || options.repos || 'demo-api,demo-webapp',
});
for (const repo of repos.split(',')) {
config.repositories.push(repo);
Expand Down
9 changes: 6 additions & 3 deletions src/utils/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ export const createPath = (path: string, verbose = true): void => {
export const deletePath = async ({
path,
force = false,
verbose = true,
}: {
path: string;
force?: boolean;
verbose?: boolean;
}): Promise<void> => {
if (!isDirectory(path)) {
log.warning(`⏩ Path: ${path} is not a valid directory. Not removing.`);
if (verbose)
log.warning(`⏩ Path: ${path} is not a valid directory. Not removing.`);
return;
}

Expand All @@ -84,8 +87,8 @@ export const deletePath = async ({
(await confirm({ message: `Delete path ${path} recursively?` }))
) {
fs.rmSync(path, { recursive: true, force: true });
log.success(`🗑️ Path: ${path} recursively deleted.`);
if (verbose) log.success(`🗑️ Path: ${path} recursively deleted.`);
} else {
log.info('⏩ Skipping...');
if (verbose) log.info('⏩ Skipping...');
}
};
3 changes: 2 additions & 1 deletion src/utils/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs';
import { createPath } from './file-system.js';
import log from './log.js';

type GitParams = {
Expand All @@ -20,7 +21,7 @@ export default class Git {

clone({ workingDir, repo, ref }: GitParams) {
this.remoteRepoExists(repo);

createPath(workingDir);
if (this.localRepoExists(this.repoPath)) {
log.warning(`Repo ${repo} already cloned.`);
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/utils/shared-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ export const sharedOptions = {
'Git organization URL for repositories',
);
},
};

export const exitCommandIfInfoOnly = (info = false) => {
if (info) process.exit(0);
repos() {
return new Option('-r, --repos [repos...]', 'Git repositories to clone');
},
};

export default sharedOptions;
25 changes: 22 additions & 3 deletions test/commands/clone.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { describe, expect, test } from 'bun:test';
import { spawnSync } from 'bun';
import { projectConfig } from '../../src/config/project';
import { deletePath } from '../../src/utils/file-system';
import { removeExtraSpaces } from '../../src/utils/string';

describe("Command: 'clone'", () => {
const config = projectConfig();

test('Flags (repos-root, git-org, git-ref) parsed correctly', () => {
const reposPath = './repos';
const gitRef = 'custom-branch';
const gitOrgUrl = 'git@github.com/example';
const reposRoot = './repos';
const result = spawnSync([
'./cli.ts',
'clone',
'--repos-root',
reposPath,
reposRoot,
'--git-ref',
gitRef,
'--git-org',
Expand All @@ -23,8 +24,26 @@ describe("Command: 'clone'", () => {
]);
const output = removeExtraSpaces(result.stdout.toString());

expect(output).toContain(`Repos path > ${reposPath}`);
expect(output).toContain(`Repos path > ${reposRoot}`);
expect(output).toContain(`Git ref > ${gitRef}`);
expect(output).toContain(`Git org > ${gitOrgUrl}`);
});

test('Clones a repository', () => {
const reposRoot = './repos';
const result = spawnSync([
'./cli.ts',
'clone',
'--repos',
'demo-api',
'--repos',
'demo-webapp',
'--repos-root',
reposRoot,
]);

const out = result.stdout.toString();
const err = result.stderr.toString();
deletePath({ path: reposRoot, force: true, verbose: false });
});
});

0 comments on commit dabbd25

Please sign in to comment.