Skip to content

Commit 37d519d

Browse files
committed
Cleaned the code
1 parent 329d5c4 commit 37d519d

File tree

4 files changed

+126
-116
lines changed

4 files changed

+126
-116
lines changed

argument/parser.ts

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,59 @@
1-
import { magentaBright, yellow } from 'chalk'
1+
import {magentaBright, yellow} from 'chalk';
22

3-
import { PackageError } from "../error/error";
4-
import { APPLICATION_VERSION } from '../index'
5-
import { Packages } from '../packages/packages';
3+
import {PackageError} from '../error/error';
4+
import {APPLICATION_VERSION} from '../index';
5+
import {Packages} from '../packages/packages';
66

77
export class PackageArgumentParser {
8-
private readonly arguments:Array<string>;
9-
private readonly length:number
8+
private readonly arguments: Array<string>;
9+
private readonly length: number;
1010

11-
constructor(args:Array<string>){
12-
this.arguments = args.slice(2, args.length)
13-
this.length = this.arguments.length
14-
}
11+
/**
12+
* @constructor
13+
* @param args The command line arguments
14+
*/
15+
constructor(args: Array<string>) {
16+
this.arguments = args.slice(2, args.length);
17+
this.length = this.arguments.length;
18+
}
1519

16-
public parseCommandArguments = ():void | null => {
17-
if(this.length == 0){
18-
return null;
19-
} else if([1, 2].includes(this.length)){
20-
const execute:Record<string, any> = {
21-
"-v" : () => {
22-
this.logApplicationVersion(APPLICATION_VERSION)
23-
},
24-
"-p" : () => {
25-
const packages = Packages.packages((data:any) => {
26-
const name = magentaBright(`[${data.data.name}]`)
27-
console.log(
28-
`${name} - ${yellow(data.data.description)}`
29-
)
30-
})
31-
}
32-
}
33-
const validArguments:Array<Array<string>> = [["-v", "-p"],["get", "remove"]]
34-
if(!(validArguments[this.length-1].includes(this.arguments[0]))){
35-
const exception = new PackageError(
36-
"An error occured while parsing your arguments",
37-
"Try again"
38-
).evokePackageException()
39-
} else {
40-
if(Object.keys(execute).includes(this.arguments[0])){
41-
execute[this.arguments[0]]()
42-
}
43-
}
20+
/**
21+
*
22+
* @returns {Void | null}
23+
*/
24+
public parseCommandArguments = (): void | null => {
25+
if (this.length == 0) {
26+
return null;
27+
} else if ([1, 2].includes(this.length)) {
28+
const execute: Record<string, any> = {
29+
'-v': () => {
30+
this.logApplicationVersion(APPLICATION_VERSION);
31+
},
32+
'-p': () => {
33+
const packages = Packages.packages((data: any) => {
34+
const name = magentaBright(`[${data.data.name}]`);
35+
console.log(`${name} - ${yellow(data.data.description)}`);
36+
});
37+
},
38+
};
39+
const validArguments: Array<Array<string>> = [
40+
['-v', '-p'],
41+
['get', 'remove'],
42+
];
43+
if (!validArguments[this.length - 1].includes(this.arguments[0])) {
44+
const exception = new PackageError(
45+
'An error occured while parsing your arguments',
46+
'Try again'
47+
).evokePackageException();
48+
} else {
49+
if (Object.keys(execute).includes(this.arguments[0])) {
50+
execute[this.arguments[0]]();
4451
}
52+
}
4553
}
54+
};
4655

47-
public logApplicationVersion = (version:string) => {
48-
console.log(yellow(APPLICATION_VERSION))
49-
}
56+
public logApplicationVersion = (version: string) => {
57+
console.log(yellow(APPLICATION_VERSION));
58+
};
5059
}

error/error.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { red,yellow } from 'chalk'
1+
import {red, yellow} from 'chalk';
22

33
export class PackageError {
4-
private readonly message:string;
5-
private readonly suggestion:string;
4+
private readonly message: string;
5+
private readonly suggestion: string;
66

7-
constructor(error:string, suggestion:string){
8-
this.message = error
9-
this.suggestion = suggestion
10-
}
7+
constructor(error: string, suggestion: string) {
8+
this.message = error;
9+
this.suggestion = suggestion;
10+
}
1111

12-
public evokePackageException = () => {
13-
const output:Array<string> = [red(this.message), yellow(this.suggestion)]
14-
for(let outputIndex=0; outputIndex<output.length; outputIndex++){
15-
console.log(output[outputIndex])
16-
}
17-
process.exit()
12+
public evokePackageException = () => {
13+
const output: Array<string> = [red(this.message), yellow(this.suggestion)];
14+
for (let outputIndex = 0; outputIndex < output.length; outputIndex++) {
15+
console.log(output[outputIndex]);
1816
}
19-
}
17+
process.exit();
18+
};
19+
}

index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { argv } from 'process'
1+
import {argv} from 'process';
22

3-
import { PackageArgumentParser } from './argument/parser'
3+
import {PackageArgumentParser} from './argument/parser';
44

5-
export const APPLICATION_VERSION:string = "0.0.1"
5+
export const APPLICATION_VERSION: string = '0.0.1';
66

7-
const argumentParser = new PackageArgumentParser(argv)
8-
argumentParser.parseCommandArguments()
7+
const argumentParser = new PackageArgumentParser(argv);
8+
argumentParser.parseCommandArguments();

packages/packages.ts

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,64 @@
1-
import * as axios from 'axios'
2-
import { PackageError } from '../error/error'
1+
import * as axios from 'axios';
2+
import {PackageError} from '../error/error';
33

44
export class Packages {
5-
private readonly organization:string
6-
private readonly url:any = (org:string):string => {
7-
return `https://api.github.com/orgs/${org}/repos`
8-
}
9-
private releaseUrl:any = (name:string) => {
10-
return `https://api.github.com/repos/code-roller/${name}/releases`
11-
}
12-
public releaseList:Array<Record<string, any>> = new Array()
5+
private readonly organization: string;
6+
private readonly url: any = (org: string): string => {
7+
return `https://api.github.com/orgs/${org}/repos`;
8+
};
9+
private releaseUrl: any = (name: string) => {
10+
return `https://api.github.com/repos/code-roller/${name}/releases`;
11+
};
12+
public releaseList: Array<Record<string, any>> = new Array();
1313

14-
constructor(organization:string) {
15-
this.organization = organization
16-
}
14+
constructor(organization: string) {
15+
this.organization = organization;
16+
}
1717

18-
private createPackageIndex = (callback:Function) => {
19-
axios.default.get(this.url(this.organization)).then((value: axios.AxiosResponse<any>) => {
20-
let repositories:Array<Record<string, string>> = new Array()
21-
let data = value.data
18+
private createPackageIndex = (callback: Function) => {
19+
axios.default
20+
.get(this.url(this.organization))
21+
.then((value: axios.AxiosResponse<any>) => {
22+
let repositories: Array<Record<string, string>> = new Array();
23+
let data = value.data;
2224

23-
for(let index=0; index<data.length; index++){
24-
const name = data[index].name
25-
repositories.push({
26-
name : name,
27-
description : data[index].description
28-
})
29-
}
25+
for (let index = 0; index < data.length; index++) {
26+
const name = data[index].name;
27+
repositories.push({
28+
name: name,
29+
description: data[index].description,
30+
});
31+
}
3032

31-
for(let k=0; k<repositories.length; k++){
32-
const repo = repositories[k]
33-
axios.default.get(this.releaseUrl(repo.name)).then((value:axios.AxiosResponse<any>) => {
34-
const releasesData = value.data
35-
if(releasesData.length != 0){
36-
callback({
37-
data : repo,
38-
releases : releasesData[
39-
releasesData.length - 1
40-
]
41-
})
42-
}
43-
}).catch((exception) => {
44-
console.log(exception)
45-
}).then(() => {
33+
for (let k = 0; k < repositories.length; k++) {
34+
const repo = repositories[k];
35+
axios.default
36+
.get(this.releaseUrl(repo.name))
37+
.then((value: axios.AxiosResponse<any>) => {
38+
const releasesData = value.data;
39+
if (releasesData.length != 0) {
40+
callback({
41+
data: repo,
42+
releases: releasesData[releasesData.length - 1],
43+
});
44+
}
45+
})
46+
.catch((exception) => {
47+
console.log(exception);
48+
})
49+
.then(() => {});
50+
}
51+
})
52+
.catch((exception) => {
53+
const error = new PackageError(
54+
exception.toJSON().stack.toString(),
55+
'Try again'
56+
).evokePackageException();
57+
})
58+
.then(() => {});
59+
};
4660

47-
})
48-
}
49-
50-
}).catch((exception) => {
51-
const error = new PackageError(
52-
exception.toJSON().stack.toString(),
53-
"Try again"
54-
).evokePackageException()
55-
}).then(() => {
56-
57-
})
58-
}
59-
60-
public static packages = (callback:Function) => {
61-
const data = new Packages("code-roller").createPackageIndex(callback)
62-
}
63-
}
61+
public static packages = (callback: Function) => {
62+
const data = new Packages('code-roller').createPackageIndex(callback);
63+
};
64+
}

0 commit comments

Comments
 (0)