Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/force.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import args from './cli/args';
import { error, info } from './cli/messages';
import { del } from './delete';

export const getSuffixFromUrl = (urlStr: string): string => {
const url = new URL(urlStr);
return url.pathname.replace(/^\//, '').split('/').join('-');
};

export const force = async (api: APIInterface): Promise<string> => {
info('Trying to deploy forcefully!');

const suffix = args['addrepo']
? args['addrepo']?.split('com/')[1].split('/').join('-')
: args['projectName'].toLowerCase();
let suffix: string;
if (args['addrepo']) {
try {
suffix = getSuffixFromUrl(args['addrepo']);
} catch (e) {
return error('Invalid repository URL');
}
} else {
suffix = args['projectName'].toLowerCase();
}

let res = '';

Expand Down
29 changes: 29 additions & 0 deletions src/test/force.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { strictEqual, throws } from 'assert';
import { getSuffixFromUrl } from '../force';

describe('force URL parsing logic', () => {
it('should parse GitHub URLs correctly', () => {
strictEqual(
getSuffixFromUrl('https://github.com/mygroup/myrepo'),
'mygroup-myrepo'
);
});

it('should parse GitLab URLs correctly', () => {
strictEqual(
getSuffixFromUrl('https://gitlab.com/mygroup/myrepo'),
'mygroup-myrepo'
);
});

it('should parse arbitrary URLs correctly', () => {
strictEqual(
getSuffixFromUrl('http://git.mycompany.local/group/subgroup/repo'),
'group-subgroup-repo'
);
});

it('should throw on invalid URLs', () => {
throws(() => getSuffixFromUrl('not-a-valid-url'));
});
});