UNPKG

4.46 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { logging } from '@angular-devkit/core';
9import { ArgumentsCamelCase, Argv, CamelCaseKey, CommandModule as YargsCommandModule } from 'yargs';
10import { AnalyticsCollector } from '../analytics/analytics-collector';
11import { EventCustomDimension, EventCustomMetric } from '../analytics/analytics-parameters';
12import { AngularWorkspace } from '../utilities/config';
13import { PackageManagerUtils } from '../utilities/package-manager';
14import { Option } from './utilities/json-schema';
15export type Options<T> = {
16 [key in keyof T as CamelCaseKey<key>]: T[key];
17};
18export declare enum CommandScope {
19 /** Command can only run inside an Angular workspace. */
20 In = 0,
21 /** Command can only run outside an Angular workspace. */
22 Out = 1,
23 /** Command can run inside and outside an Angular workspace. */
24 Both = 2
25}
26export interface CommandContext {
27 currentDirectory: string;
28 root: string;
29 workspace?: AngularWorkspace;
30 globalConfiguration: AngularWorkspace;
31 logger: logging.Logger;
32 packageManager: PackageManagerUtils;
33 /** Arguments parsed in free-from without parser configuration. */
34 args: {
35 positional: string[];
36 options: {
37 help: boolean;
38 jsonHelp: boolean;
39 getYargsCompletions: boolean;
40 } & Record<string, unknown>;
41 };
42}
43export type OtherOptions = Record<string, unknown>;
44export interface CommandModuleImplementation<T extends {} = {}> extends Omit<YargsCommandModule<{}, T>, 'builder' | 'handler'> {
45 /** Scope in which the command can be executed in. */
46 scope: CommandScope;
47 /** Path used to load the long description for the command in JSON help text. */
48 longDescriptionPath?: string;
49 /** Object declaring the options the command accepts, or a function accepting and returning a yargs instance. */
50 builder(argv: Argv): Promise<Argv<T>> | Argv<T>;
51 /** A function which will be passed the parsed argv. */
52 run(options: Options<T> & OtherOptions): Promise<number | void> | number | void;
53}
54export interface FullDescribe {
55 describe?: string;
56 longDescription?: string;
57 longDescriptionRelativePath?: string;
58}
59export declare abstract class CommandModule<T extends {} = {}> implements CommandModuleImplementation<T> {
60 protected readonly context: CommandContext;
61 abstract readonly command: string;
62 abstract readonly describe: string | false;
63 abstract readonly longDescriptionPath?: string;
64 protected readonly shouldReportAnalytics: boolean;
65 readonly scope: CommandScope;
66 private readonly optionsWithAnalytics;
67 constructor(context: CommandContext);
68 /**
69 * Description object which contains the long command descroption.
70 * This is used to generate JSON help wich is used in AIO.
71 *
72 * `false` will result in a hidden command.
73 */
74 get fullDescribe(): FullDescribe | false;
75 protected get commandName(): string;
76 abstract builder(argv: Argv): Promise<Argv<T>> | Argv<T>;
77 abstract run(options: Options<T> & OtherOptions): Promise<number | void> | number | void;
78 handler(args: ArgumentsCamelCase<T> & OtherOptions): Promise<void>;
79 protected getAnalytics(): Promise<AnalyticsCollector | undefined>;
80 /**
81 * Adds schema options to a command also this keeps track of options that are required for analytics.
82 * **Note:** This method should be called from the command bundler method.
83 */
84 protected addSchemaOptionsToCommand<T>(localYargs: Argv<T>, options: Option[]): Argv<T>;
85 protected getWorkspaceOrThrow(): AngularWorkspace;
86 /**
87 * Flush on an interval (if the event loop is waiting).
88 *
89 * @returns a method that when called will terminate the periodic
90 * flush and call flush one last time.
91 */
92 protected getAnalyticsParameters(options: (Options<T> & OtherOptions) | OtherOptions): Partial<Record<EventCustomDimension | EventCustomMetric, string | boolean | number>>;
93 private reportCommandRunAnalytics;
94 private reportWorkspaceInfoAnalytics;
95}
96/**
97 * Creates an known command module error.
98 * This is used so during executation we can filter between known validation error and real non handled errors.
99 */
100export declare class CommandModuleError extends Error {
101}