-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-command.ts
More file actions
199 lines (181 loc) · 5.38 KB
/
base-command.ts
File metadata and controls
199 lines (181 loc) · 5.38 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import omit from "lodash/omit";
import merge from "lodash/merge";
import { existsSync, readFileSync } from "fs";
import { Command } from "@contentstack/cli-command";
import {
Flags,
FlagInput,
Interfaces,
ContentstackClient,
managementSDKClient,
managementSDKInitiator,
InquirePayload,
cliux,
isAuthenticated,
ContentstackMarketplaceClient,
marketplaceSDKInitiator,
marketplaceSDKClient,
} from "@contentstack/cli-utilities";
import config from "./config";
import { ConfigType, LogFn } from "./types";
import { Logger, getDeveloperHubUrl } from "./util";
import messages, { $t, commonMsg } from "./messages";
export type Flags<T extends typeof Command> = Interfaces.InferredFlags<
(typeof BaseCommand)["baseFlags"] & T["flags"]
>;
export type Args<T extends typeof Command> = Interfaces.InferredArgs<T["args"]>;
export abstract class BaseCommand<T extends typeof Command> extends Command {
public log!: LogFn;
public logger!: Logger;
public readonly $t = $t;
public developerHubBaseUrl!: string;
protected sharedConfig: ConfigType = {
...config,
projectBasePath: process.cwd(),
};
readonly messages: typeof messages = {
...messages,
};
protected managementSdk!: ContentstackClient;
protected managementAppSdk!: ContentstackClient;
protected marketplaceAppSdk!: ContentstackMarketplaceClient;
protected flags!: Flags<T>;
protected args!: Args<T>;
// NOTE define flags that can be inherited by any command that extends BaseCommand
static baseFlags: FlagInput = {
org: Flags.string({
description: commonMsg.PROVIDE_ORG_UID,
}),
yes: Flags.boolean({
char: "y",
hidden: true,
description: commonMsg.SKIP_CONFIRMATION,
}),
};
public async init(): Promise<void> {
await super.init();
try {
const { args, flags } = await this.parse({
flags: this.ctor.flags,
baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
args: this.ctor.args,
strict: this.ctor.strict,
});
this.flags = flags as Flags<T>;
this.args = args as Args<T>;
} catch (error) {
throw error;
}
cliux.registerSearchPlugin();
this.registerConfig();
// Init logger
const logger = new Logger(this.sharedConfig);
this.log = logger.log.bind(logger);
this.validateRegionAndAuth();
this.developerHubBaseUrl = this.sharedConfig.developerHubBaseUrl;
if (this.developerHubUrl?.startsWith("https")) {
this.developerHubBaseUrl = this.developerHubUrl?.split("//")[1];
}
if (!this.developerHubBaseUrl)
this.developerHubBaseUrl = getDeveloperHubUrl();
await this.initCmaSDK();
await this.initMarketplaceSDK();
}
protected async catch(err: Error & { exitCode?: number }): Promise<any> {
// add any custom logic to handle errors from the command
// or simply return the parent class error handling
// Handle NonExistentFlagsError specifically
if (err.message && err.message.includes("Nonexistent flag")) {
console.error(err.message);
process.exit(2);
}
return super.catch(err);
}
protected async finally(_: Error | undefined): Promise<any> {
// called after run and catch regardless of whether or not the command errored
return super.finally(_);
}
/**
* @method registerConfig
*
* @memberof BaseCommand
*/
registerConfig() {
if (this.flags.config && existsSync(this.flags.config)) {
try {
const config = JSON.parse(
readFileSync(this.flags.config, { encoding: "utf-8" })
);
const omitKeys = [
"manifestPath",
"boilerplateName",
"developerHubUrls",
];
this.sharedConfig = merge(this.sharedConfig, omit(config, omitKeys));
} catch (error) {
throw error;
}
}
}
/**
* @method initCmaSDK
*
* @memberof BaseCommand
*/
async initCmaSDK() {
managementSDKInitiator.init(this.context);
this.managementSdk = await managementSDKClient({
host: this.cmaHost,
});
this.managementAppSdk = await managementSDKClient({
host: this.developerHubBaseUrl,
});
}
async initMarketplaceSDK() {
marketplaceSDKInitiator.init(this.context);
this.marketplaceAppSdk = await marketplaceSDKClient({
host: this.developerHubBaseUrl,
});
}
/**
* @method getValPrompt
*
* @param {(Partial<InquirePayload> & Record<string, any>)} [options={
* validate: (val) => {
* if (!val) return this.messages.NOT_EMPTY;
* return true;
* },
* }]
* @return {*} {(Promise<string | boolean>)}
* @memberof BaseCommand
*/
getValPrompt(
options: Partial<InquirePayload> & Record<string, any> = {
message: "Enter value",
validate: (val) => {
if (!val) return this.$t(this.messages.NOT_EMPTY, { value: "Value" });
return true;
},
}
): Promise<string | boolean> {
const { name = "getVal", message, validate } = options;
return cliux.inquire({
validate,
name,
type: "input",
default: options?.default,
message: message as string,
});
}
/**
* The `validateRegionAndAuth` function verify whether region is set and user is logged in or not
*/
validateRegionAndAuth() {
if (this.region) {
if (!isAuthenticated()) {
this.log(this.messages.CLI_APP_CLI_LOGIN_FAILED, "error");
this.exit(1);
}
}
}
}