/
schematic.option.ts
37 lines (33 loc) 路 1.14 KB
/
schematic.option.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
import { normalizeToKebabOrSnakeCase } from '../utils/formatting';
export class SchematicOption {
constructor(private name: string, private value: boolean | string) {}
get normalizedName() {
return normalizeToKebabOrSnakeCase(this.name);
}
public toCommandString(): string {
if (typeof this.value === 'string') {
if (this.name === 'name') {
return `--${this.normalizedName}=${this.format()}`;
} else if (this.name === 'version' || this.name === 'path') {
return `--${this.normalizedName}=${this.value}`;
} else {
return `--${this.normalizedName}="${this.value}"`;
}
} else if (typeof this.value === 'boolean') {
const str = this.normalizedName;
return this.value ? `--${str}` : `--no-${str}`;
} else {
return `--${this.normalizedName}=${this.value}`;
}
}
private format() {
return normalizeToKebabOrSnakeCase(this.value as string)
.split('')
.reduce((content, char) => {
if (char === '(' || char === ')' || char === '[' || char === ']') {
return `${content}\\${char}`;
}
return `${content}${char}`;
}, '');
}
}