|
1 |
| -import { getOptions as getCLIOptions } from 'codemod-cli'; |
| 1 | +import { getOptions as getCodemodOptions, runTransform } from 'codemod-cli'; |
| 2 | +import Debug from 'debug'; |
| 3 | +import { |
| 4 | + analyzeEmberObject, |
| 5 | + gatherTelemetryForUrl, |
| 6 | + getTelemetry, |
| 7 | +} from 'ember-codemods-telemetry-helpers'; |
2 | 8 | import fs from 'node:fs';
|
| 9 | +import http from 'node:http'; |
3 | 10 | import path from 'node:path';
|
| 11 | +import yargs from 'yargs'; |
4 | 12 | import { ZodError, ZodType, z } from 'zod';
|
| 13 | +import { assert } from '../../../helpers/types'; |
5 | 14 | import Resolver, { EmbroiderResolver, MockResolver, RuntimeResolver } from './resolver';
|
6 | 15 |
|
7 |
| -export interface Options { |
8 |
| - customHelpers: string[]; |
9 |
| - resolver: Resolver; |
| 16 | +const debug = Debug('ember-no-implicit-this-codemod'); |
| 17 | + |
| 18 | +const DEFAULT_ROOT = 'app'; |
| 19 | +const DEFAULT_URL = 'http://localhost:4200'; |
| 20 | + |
| 21 | +export const Parser = yargs |
| 22 | + .option('root', { |
| 23 | + describe: 'app root FIXME', |
| 24 | + default: DEFAULT_ROOT, |
| 25 | + normalize: true, |
| 26 | + }) |
| 27 | + .option('config', { |
| 28 | + describe: 'Path to config file FIXME', |
| 29 | + normalize: true, |
| 30 | + }) |
| 31 | + .option('telemetry', { |
| 32 | + describe: 'Telemetry source FIXME', |
| 33 | + choices: ['auto', 'embroider', 'runtime'] as const, |
| 34 | + default: 'auto' as const, |
| 35 | + }) |
| 36 | + .option('url', { |
| 37 | + describe: 'URL source FIXME', |
| 38 | + default: DEFAULT_URL, |
| 39 | + }); |
| 40 | + |
| 41 | +export function parseOptions(args: string[]): Options { |
| 42 | + return Parser.parseSync(args); |
10 | 43 | }
|
11 | 44 |
|
12 |
| -const CLIOptions = z.object({ |
| 45 | +const Options = z.object({ |
| 46 | + root: z.string().default(DEFAULT_ROOT), |
13 | 47 | config: z.string().optional(),
|
14 |
| - // FIXME: Defaulting to 'runtime' isn't quite correct |
15 |
| - telemetry: z.union([z.literal('runtime'), z.literal('embroider')]).default('runtime'), |
16 |
| - // FIXME: Optionally allow angle-bracket conversion for components |
17 |
| - // FIXME: Optionally add parens for helpers |
| 48 | + telemetry: z |
| 49 | + .union([z.literal('auto'), z.literal('embroider'), z.literal('runtime')]) |
| 50 | + .default('auto'), |
| 51 | + url: z.string().url().default(DEFAULT_URL), |
18 | 52 | });
|
19 | 53 |
|
20 |
| -type CLIOptions = z.infer<typeof CLIOptions>; |
21 |
| - |
22 |
| -const FileOptions = z.object({ |
23 |
| - helpers: z.array(z.string()), |
24 |
| -}); |
| 54 | +type Options = z.infer<typeof Options>; |
25 | 55 |
|
26 |
| -type FileOptions = z.infer<typeof FileOptions>; |
| 56 | +const STAGE2_OUTPUT_FILE = 'dist/.stage2-output'; |
27 | 57 |
|
28 |
| -/** |
29 |
| - * Returns custom options object to support the custom helpers config path passed |
30 |
| - * by the user. |
31 |
| - */ |
32 |
| -export function getOptions(): Options { |
33 |
| - const cliOptions = parse(getCLIOptions(), CLIOptions); |
34 |
| - return { |
35 |
| - customHelpers: getCustomHelpersFromConfig(cliOptions.config), |
36 |
| - resolver: buildResolver(cliOptions), |
37 |
| - }; |
| 58 | +export interface DetectResult { |
| 59 | + isServerRunning: boolean; |
| 60 | + hasTelemetryOutput: boolean; |
| 61 | + hasStage2Output: boolean; |
38 | 62 | }
|
39 | 63 |
|
40 |
| -function buildResolver(cliOptions: CLIOptions): Resolver { |
41 |
| - if (process.env['TESTING']) { |
42 |
| - return MockResolver.build(); |
43 |
| - } else { |
44 |
| - return cliOptions.telemetry === 'runtime' ? RuntimeResolver.build() : EmbroiderResolver.build(); |
| 64 | +type MaybePromise<T> = T | Promise<T>; |
| 65 | + |
| 66 | +export class Runner { |
| 67 | + static withArgs(args: string[]): Runner { |
| 68 | + return new Runner(parseOptions(args)); |
| 69 | + } |
| 70 | + |
| 71 | + static withOptions(options: Partial<Options> = {}): Runner { |
| 72 | + return new Runner({ |
| 73 | + root: 'app', |
| 74 | + config: undefined, |
| 75 | + telemetry: 'auto', |
| 76 | + url: 'http://localhost:4200', |
| 77 | + ...options, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + static fromCodemodOptions(): Runner { |
| 82 | + const options = parse(getCodemodOptions(), Options); |
| 83 | + return Runner.withOptions(options); |
| 84 | + } |
| 85 | + |
| 86 | + constructor(private readonly options: Options) {} |
| 87 | + |
| 88 | + async detectTelemetryType( |
| 89 | + detect: MaybePromise<DetectResult> = this.detect() |
| 90 | + ): Promise<'embroider' | 'runtime'> { |
| 91 | + const result = await detect; |
| 92 | + const type = this.options.telemetry; |
| 93 | + switch (type) { |
| 94 | + case 'embroider': { |
| 95 | + if (result.hasStage2Output) { |
| 96 | + return 'embroider'; |
| 97 | + } else { |
| 98 | + throw new Error('Please run the thing first FIXME'); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + case 'runtime': { |
| 103 | + if (result.hasStage2Output) { |
| 104 | + console.warn('Are you sure you want to use runtime telemetry? FIXME'); |
| 105 | + } |
| 106 | + |
| 107 | + if (result.isServerRunning) { |
| 108 | + return 'runtime'; |
| 109 | + } else { |
| 110 | + throw new Error('Please run the server or pass correct URL FIXME'); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + case 'auto': { |
| 115 | + if (result.hasStage2Output) { |
| 116 | + return 'embroider'; |
| 117 | + } else if (result.isServerRunning) { |
| 118 | + return 'runtime'; |
| 119 | + } else { |
| 120 | + throw new Error('Please RTFM FIXME'); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + async gatherTelemetry(): Promise<void> { |
| 127 | + const telemetryType = await this.detectTelemetryType(); |
| 128 | + |
| 129 | + if (telemetryType === 'runtime') { |
| 130 | + debug('Gathering telemetry data from %s ...', this.options.url); |
| 131 | + await gatherTelemetryForUrl(this.options.url, analyzeEmberObject); |
| 132 | + |
| 133 | + const telemetry = getTelemetry() as Record<string, unknown>; // FIXME |
| 134 | + debug('Gathered telemetry on %d modules', Object.keys(telemetry).length); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + async run(root: string, args: string[]): Promise<void> { |
| 139 | + await this.gatherTelemetry(); |
| 140 | + runTransform(root, 'no-implicit-this', [this.options.root, ...args], 'hbs'); |
| 141 | + } |
| 142 | + |
| 143 | + buildResolver(): Resolver { |
| 144 | + const customHelpers = getCustomHelpersFromConfig(this.options.config); |
| 145 | + if (process.env['TESTING']) { |
| 146 | + return MockResolver.build(customHelpers); |
| 147 | + } else if (this.detectTelemetryTypeSync() === 'embroider') { |
| 148 | + return EmbroiderResolver.build(customHelpers); |
| 149 | + } else { |
| 150 | + return RuntimeResolver.build(customHelpers); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private detectTelemetryTypeSync(): 'embroider' | 'runtime' { |
| 155 | + const result = this.detectSync(); |
| 156 | + |
| 157 | + if (result.hasStage2Output) { |
| 158 | + return 'embroider'; |
| 159 | + } else if (result.hasTelemetryOutput) { |
| 160 | + return 'runtime'; |
| 161 | + } else { |
| 162 | + assert('gatherTelemetry must be called first'); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private async detect(): Promise<DetectResult> { |
| 167 | + const isServerRunning = await new Promise<boolean>((resolve) => { |
| 168 | + http.get(this.options.url, () => resolve(true)).on('error', () => resolve(false)); |
| 169 | + }); |
| 170 | + |
| 171 | + return { ...this.detectSync(), isServerRunning }; |
| 172 | + } |
| 173 | + |
| 174 | + private detectSync(): DetectResult { |
| 175 | + const isServerRunning = false; |
| 176 | + const hasTelemetryOutput = Object.keys(getTelemetry() as Record<string, unknown>).length > 0; |
| 177 | + const hasStage2Output = fs.existsSync(STAGE2_OUTPUT_FILE); |
| 178 | + return { isServerRunning, hasTelemetryOutput, hasStage2Output }; |
45 | 179 | }
|
46 | 180 | }
|
47 | 181 |
|
48 |
| -// FIXME: Document |
| 182 | +const FileOptions = z.object({ |
| 183 | + helpers: z.array(z.string()), |
| 184 | +}); |
| 185 | + |
| 186 | +type FileOptions = z.infer<typeof FileOptions>; |
| 187 | + |
| 188 | +// FIXME: Document option |
49 | 189 | /**
|
50 | 190 | * Accepts the config path for custom helpers and returns the array of helpers
|
51 | 191 | * if the file path is resolved.
|
@@ -74,7 +214,7 @@ function parse<Z extends ZodType>(raw: unknown, type: Z): z.infer<Z> {
|
74 | 214 | }
|
75 | 215 | }
|
76 | 216 |
|
77 |
| -function makeConfigError(source: string, error: ZodError<CLIOptions>): ConfigError { |
| 217 | +function makeConfigError(source: string, error: ZodError<ZodType>): ConfigError { |
78 | 218 | const flattened = error.flatten();
|
79 | 219 | const errors = flattened.formErrors;
|
80 | 220 | for (const [key, value] of Object.entries(flattened.fieldErrors)) {
|
|
0 commit comments