forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
103 lines (89 loc) · 2.76 KB
/
command.ts
File metadata and controls
103 lines (89 loc) · 2.76 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// tslint:disable:no-global-tslint-disable no-any file-header
import { logging, terminal } from '@angular-devkit/core';
export interface CommandConstructor {
new(context: CommandContext, logger: logging.Logger): Command;
aliases: string[];
scope: CommandScope.everywhere;
}
export enum CommandScope {
everywhere,
inProject,
outsideProject,
}
export enum ArgumentStrategy {
MapToOptions,
Nothing,
}
export abstract class Command<T = any> {
protected _rawArgs: string[];
public allowMissingWorkspace = false;
constructor(context: CommandContext, logger: logging.Logger) {
this.logger = logger;
if (context) {
this.project = context.project;
}
}
async initializeRaw(args: string[]): Promise<any> {
this._rawArgs = args;
return args;
}
async initialize(_options: any): Promise<void> {
return;
}
validate(_options: T): boolean | Promise<boolean> {
return true;
}
printHelp(_options: T): void {
this.printHelpUsage(this.name, this.arguments, this.options);
this.printHelpOptions(this.options);
}
protected printHelpUsage(name: string, args: string[], options: Option[]) {
const argDisplay = args && args.length > 0
? ' ' + args.map(a => `<${a}>`).join(' ')
: '';
const optionsDisplay = options && options.length > 0
? ` [options]`
: ``;
this.logger.info(`usage: ng ${name}${argDisplay}${optionsDisplay}`);
}
protected printHelpOptions(options: Option[]) {
if (options && this.options.length > 0) {
this.logger.info(`options:`);
this.options
.filter(o => !o.hidden)
.sort((a, b) => a.name >= b.name ? 1 : -1)
.forEach(o => {
const aliases = o.aliases && o.aliases.length > 0
? '(' + o.aliases.map(a => `-${a}`).join(' ') + ')'
: '';
this.logger.info(` ${terminal.cyan('--' + o.name)} ${aliases}`);
this.logger.info(` ${o.description}`);
});
}
}
abstract run(options: T): number | void | Promise<number | void>;
abstract readonly name: string;
abstract readonly description: string;
abstract readonly arguments: string[];
abstract readonly options: Option[];
public argStrategy = ArgumentStrategy.MapToOptions;
public hidden = false;
public unknown = false;
public scope = CommandScope.everywhere;
protected readonly logger: logging.Logger;
protected readonly project: any;
}
export interface CommandContext {
project: any;
}
export abstract class Option {
abstract readonly name: string;
abstract readonly description: string;
readonly default?: string | number | boolean;
readonly required?: boolean;
abstract readonly aliases?: string[];
abstract readonly type: any;
readonly format?: string;
readonly values?: any[];
readonly hidden?: boolean = false;
}