-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathinit.ts
457 lines (423 loc) · 13.7 KB
/
init.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import { CliUx } from "@oclif/core";
import chalk from "chalk";
import figures from "figures";
import { orderBy } from "lodash";
import { normalize } from "path";
import terminalLink from "terminal-link";
import { fetchTrackingPlans, SegmentAPI, validateToken } from "../api";
import { BaseCommand } from "../base-command";
import {
saveGlobalToken,
saveWorkspaceConfig,
tokenMethodToUserString,
TrackingPlanConfig,
} from "../config";
import { LanguageGenerator, supportedLanguages } from "../languages";
import { CommandInit, toCommandConfig } from "../telemetry";
/**
* Intro message that explains the features of the CLI
*/
export const MESSAGE_INTRO =
`Typewriter is a tool for generating strongly-typed ${terminalLink(
"Segment",
"https://segment.com"
)} analytics libraries from a ${terminalLink(
"Tracking Plan",
"https://segment.com/docs/protocols/tracking-plan/"
)}.` +
"\n" +
chalk.gray(
`Learn more from ${terminalLink(
"Typewriter's documentation here",
"https://segment.com/docs/protocols/typewriter"
)}.`
) +
"\n" +
chalk.gray(
`To get started you'll need a ${chalk.yellow(
"typewriter.yml"
)} file. The quickstart below will walk you through creating one.`
);
export default class Init extends BaseCommand {
static aliases: string[] = ["initialize", "quickstart"];
static description = "";
static examples = ["<%= config.bin %> <%= command.id %>"];
/**
* Fetches tracking plans from the Segment API and returns a list of options for the user to choose from.
* @returns Tracking plan options
*/
private getTrackingPlanChoices = async (apiToken: string) => {
// We load this from Segment using the API token supplied above so let's load i
const trackingPlans = await fetchTrackingPlans(apiToken);
return orderBy(
trackingPlans.map((plan) => ({
name: plan.name,
value: plan,
})),
"name",
"asc"
);
};
/**
* Formats a question with tips that show underneath it.
* e.g.
*
* ? Enter a Segment Public API token
* → A Public API token is used to download Tracking Plans from Segment.
* → Documentation on generating an API token can be found here
* @param question Question string
* @param tips list of tips string to show below the question
* @returns a formatted question string
*/
private formatQuestion(question: string, tips?: string[]): string {
let tipsText = "";
if (tips !== undefined) {
tipsText = tips
.map((tip) => chalk.gray(`${figures.arrowRight} ${tip}`))
.join("\n");
}
return [question, tipsText, chalk.white(figures.pointer)].join("\n");
}
/**
* Validates a user API token. Returns a user friendly error message if the token is invalid.
* @param token Segment PAPI token
* @returns true if the token is valid, a user friendly string if there's an error
*/
private async validateToken(token: string): Promise<boolean | string> {
if (!token) {
return "You must enter an API token.";
}
try {
const result = await validateToken(token);
if (!result.isValid) {
return `Input is not a valid Segment Public API token.`;
}
return result.isValid;
} catch (error) {
return (
"Unable to validate token\n" +
`Failed due to an ${
(error as Record<string, unknown>).code
} error (${JSON.stringify(error)}).`
);
}
}
public async run(): Promise<void> {
const startTime = process.hrtime();
if (this.workspaceConfig !== undefined) {
this.log(
`Found a workspace config file in your current directory ${this.configPath}. We will use this values by default if you don't change them.`
);
}
// Show intro message
this.log(MESSAGE_INTRO);
// Ask user for input to continue
const { confirmation } = await this.prompt({
type: "input",
name: "confirmation",
message: `Ready?`,
});
if (confirmation === false) {
this.exit();
}
// If we have a token already check if the user wants to reuse it
let useCurrentToken = this.tokenMetadata !== undefined;
let token: string | undefined;
if (this.tokenMetadata !== undefined) {
const { useToken } = await this.prompt({
type: "list",
name: "useToken",
default: true,
message: this.formatQuestion(
`Found a Segment Token for workspace ${
this.tokenMetadata.workspace?.name
} on ${tokenMethodToUserString(
this.tokenMetadata.method,
this.configPath
)}`
),
choices: [
{
name: "Use this token",
value: true,
},
{
name: "Enter a new token",
value: false,
},
],
});
useCurrentToken = useToken;
if (useCurrentToken) {
token = this.tokenMetadata.token;
}
}
// If we don't have a token or the user wants to enter a new one, ask for one and validate before continue
if (this.tokenMetadata === undefined || useCurrentToken === false) {
const { apiToken } = await this.prompt({
type: "password",
name: "apiToken",
message: this.formatQuestion("Enter a Segment Public API token", [
`A Public API token is used to download Tracking Plans from Segment.`,
`Documentation on generating an API token can be found ${terminalLink(
"here",
"https://segment.com/docs/protocols/typewriter/#api-token-configuration"
)}`,
]),
mask: "*",
validate: this.validateToken,
});
token = apiToken;
}
// Ask if the user wants to store this token in the global ~/.typewriter
// We don't do this automatically cause the user can have multiple workspaces, or supply the token as an input/pipe it
const { shouldStoreToken } = await this.prompt({
type: "confirm",
name: "shouldStoreToken",
message: this.formatQuestion(
"Would you like to store this token for future use?",
[
`This token will be stored in ~/.typewriter`,
`If you choose not to store this token, you will need to enter it each time you run typewriter.`,
`Do not store the token if you plan to use a script to retrieve the token before running typewriter.`,
]
),
default: true,
});
// Load the tracking plans for the user workspace
CliUx.ux.action.start("Loading Tracking Plans");
let planChoices: Awaited<ReturnType<typeof this.getTrackingPlanChoices>>;
try {
planChoices = await this.getTrackingPlanChoices(token!);
if (planChoices.length === 0) {
this.error("No tracking plans found. Create a tracking plan first.");
}
} catch (error) {
this.debug("Error loading tracking plans", error);
this.error(
"Unable to load tracking plans. Check your token and try again."
);
} finally {
CliUx.ux.action.stop();
}
const trackingPlanConfigs: TrackingPlanConfig[] = [];
// Ask the user to select a tracking plan for the workspace
const { trackingPlans } = await this.prompt<{
trackingPlans: SegmentAPI.TrackingPlan[];
}>({
type: "checkbox",
name: "trackingPlans",
message: this.formatQuestion("Tracking Plan:", [
"Typewriter will generate a client from this Tracking Plan",
"This Tracking Plan is saved locally in a plan.json file",
]),
choices: planChoices,
default: this.workspaceConfig?.trackingPlans.map((t) => t.id),
validate: (selection: SegmentAPI.TrackingPlan[]) => {
if (selection === undefined || selection.length === 0) {
return "You must select at least one tracking plan";
}
return true;
},
});
// Ask the user where to save the generated client for each tracking plan
for (const trackingPlan of trackingPlans) {
const { path } = await this.prompt({
type: "input",
name: "path",
message: this.formatQuestion(
`Enter a directory for the Tracking Plan ${chalk.green(
trackingPlan.name
)} output:`,
[
"The generated client will be stored in this directory.",
"Directories will be automatically created, if needed.",
]
),
filter: normalize,
default: this.workspaceConfig?.trackingPlans.find(
(t) => t.id === trackingPlan.id
)?.path,
});
trackingPlanConfigs.push({
id: trackingPlan.id,
name: trackingPlan.name,
path: path,
});
}
// Ask the user to select a language and SDK
const { language } = await this.prompt<{ language: LanguageGenerator }>([
{
type: "list",
name: "language",
message: `Choose a Language:`,
choices: supportedLanguages.map((lang) => ({
name: lang.name,
value: lang,
})),
default: supportedLanguages.find(
(lang) => lang.id === this.workspaceConfig?.client.language
),
},
]);
const { sdk } = await this.prompt<{ sdk: string }>([
{
type: "list",
name: "sdk",
message: `Choose an SDK:`,
choices: Object.entries(language.supportedSDKs).map(([key, value]) => ({
name: key,
value,
})),
default: this.workspaceConfig?.client.sdk,
},
]);
// Languages can have additional prompts as per quicktype: https://github.com/quicktype/quicktype
// Some of them are advanced options, others are required
let languagePrompts: Record<string, unknown> = {};
if (language.requiredOptions !== undefined) {
languagePrompts = await this.prompt(language.requiredOptions);
}
if (language.advancedOptions !== undefined) {
const { languageFineTune } = await this.prompt<{
languageFineTune: boolean;
}>({
type: "confirm",
name: "languageFineTune",
message: this.formatQuestion(
`Do you want to review the advanced options for ${language.name}?`,
[
`Typewriter uses ${terminalLink(
"quicktype",
"https://app.quicktype.io/"
)} to generate the classes.`,
`You can fine tune the generated classes by using any of the quicktype options.`,
]
),
default: false,
});
if (languageFineTune === true) {
languagePrompts = {
...languagePrompts,
...(await this.prompt(language.advancedOptions)),
// Prefixes and Suffixes
...(await this.prompt<{
typePrefix?: string;
typeSuffix?: string;
functionPrefix?: string;
functionSuffix: string;
}>([
{
type: "input",
name: "typePrefix",
message: `Event Class Type Prefix:`,
default: undefined,
},
{
type: "input",
name: "typeSuffix",
message: `Event Class Type Suffix:`,
default: undefined,
},
{
type: "input",
name: "functionPrefix",
message: `Functions Prefix:`,
default: undefined,
},
{
type: "input",
name: "functionSuffix",
message: `Functions Suffix:`,
default: undefined,
},
]).then((results) => ({
// Format the results into a valid configuration object
prefixes: {
functionName: results.functionPrefix,
typeName: results.typePrefix,
},
suffixes: {
functionName: results.functionSuffix,
typeName: results.typeSuffix,
},
}))),
};
}
}
this.log("\n");
this.log("Configuration Summary:");
CliUx.ux.table(
[
{
name: "Tracking Plans",
value: trackingPlanConfigs.map((t) => t.name).join(","),
},
{
name: "Paths",
value: trackingPlanConfigs.map((t) => t.path).join(","),
},
{
name: "Language",
value: language.name,
},
{
name: "SDK",
value: sdk,
},
...Object.entries(languagePrompts).map(([key, value]) => ({
name: key,
value: value,
})),
],
{
name: {
header: "Name",
},
value: {
header: "Value",
},
}
);
this.log("\n");
const { showSummary } = await this.prompt<{ showSummary: boolean }>({
type: "confirm",
name: "showSummary",
message: "Save these settings?",
});
if (!showSummary) {
this.exit(0);
}
if (shouldStoreToken === true) {
try {
await saveGlobalToken(token!);
} catch (error) {
this.error(
"Unable to write token to ~/.typewriter\n" +
`Failed due to an ${
(error as Record<string, unknown>).code
} error (${(error as Record<string, unknown>).errno}).`
);
}
}
const mergedConfig = {
...this.workspaceConfig,
client: {
...this.workspaceConfig?.client,
language: language.id,
sdk: sdk,
languageOptions: { ...languagePrompts },
},
trackingPlans: trackingPlanConfigs,
};
await saveWorkspaceConfig(mergedConfig, this.configPath);
this.segmentClient.initCommand({
properties: {
config: toCommandConfig(mergedConfig, this.tokenMetadata!.method),
hasConfig: this.workspaceConfig !== undefined,
rawCommand: this.rawCommand,
duration: process.hrtime(startTime)[1],
} as CommandInit,
});
}
}