-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathcommandBuilder.ts
78 lines (65 loc) · 1.77 KB
/
commandBuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
export class Command {
public readonly command: string;
public readonly description?: string;
public readonly args: string[];
public readonly logName?: string;
public constructor(builder: CommandBuilder) {
this.command = builder.command;
this.description = builder.description;
this.args = builder.args;
this.logName = builder.logName;
}
public toString(): string {
return this.description ? this.description : `${this.command} ${this.args.join(' ')}`;
}
public toCommand(): string {
return `${this.command} ${this.args.join(' ')}`;
}
}
export class CommandBuilder {
public readonly command: string;
public description?: string;
public args: string[] = [];
public logName?: string;
public constructor(command: string) {
this.command = command;
}
public withDescription(description: string): CommandBuilder {
this.description = description;
return this;
}
public withArg(arg: string): CommandBuilder {
if (arg === '--json') {
this.withJson();
} else {
this.args.push(arg);
}
return this;
}
public withFlag(name: string, value: string): CommandBuilder {
this.args.push(name, value);
return this;
}
public withJson(): CommandBuilder {
this.args.push('--json');
return this;
}
public withLogName(logName: string): CommandBuilder {
this.logName = logName;
return this;
}
public build(): Command {
return new Command(this);
}
}
export class SfCommandBuilder extends CommandBuilder {
public constructor() {
super('sf');
}
}