|
| 1 | +import { runTransform } from 'codemod-cli'; |
| 2 | +import Debug from 'debug'; |
| 3 | +import { |
| 4 | + analyzeEmberObject, |
| 5 | + gatherTelemetryForUrl, |
| 6 | + getTelemetry, |
| 7 | +} from 'ember-codemods-telemetry-helpers'; |
| 8 | +import fs from 'node:fs'; |
| 9 | +import http from 'node:http'; |
| 10 | +import yargs from 'yargs'; |
| 11 | +import { assert, isRecord } from '../../../helpers/types'; |
| 12 | + |
| 13 | +const debug = Debug('ember-no-implicit-this-codemod'); |
| 14 | + |
| 15 | +export interface Options { |
| 16 | + config: string | undefined; |
| 17 | + telemetry: 'auto' | 'embroider' | 'runtime'; |
| 18 | + url: string; |
| 19 | +} |
| 20 | + |
| 21 | +export const Parser = yargs |
| 22 | + .option('config', { |
| 23 | + describe: 'Path to config file FIXME', |
| 24 | + normalize: true, |
| 25 | + }) |
| 26 | + .option('telemetry', { |
| 27 | + describe: 'Telemetry source FIXME', |
| 28 | + choices: ['auto', 'embroider', 'runtime'] as const, |
| 29 | + default: 'auto' as const, |
| 30 | + }) |
| 31 | + .option('url', { |
| 32 | + describe: 'URL source FIXME', |
| 33 | + default: 'http://localhost:4200', |
| 34 | + }); |
| 35 | + |
| 36 | +export function parseOptions(args: string[]): Options { |
| 37 | + return Parser.parseSync(args); |
| 38 | +} |
| 39 | + |
| 40 | +export function validateOptions(options: unknown): asserts options is Options { |
| 41 | + assert('options must be an object', isRecord(options)); |
| 42 | + |
| 43 | + const { config, telemetry, url } = options; |
| 44 | + |
| 45 | + assert('config must be a string', config === undefined || typeof config === 'string'); |
| 46 | + assert( |
| 47 | + 'telemetry must be one of auto, embroider, or runtime', |
| 48 | + typeof telemetry === 'string' && ['auto', 'embroider', 'runtime'].includes(telemetry) |
| 49 | + ); |
| 50 | + assert('url must be a string', typeof url === 'string'); |
| 51 | +} |
| 52 | + |
| 53 | +export interface DetectResult { |
| 54 | + isServerRunning: boolean; |
| 55 | + hasStage2Output: boolean; |
| 56 | +} |
| 57 | + |
| 58 | +type MaybePromise<T> = T | Promise<T>; |
| 59 | + |
| 60 | +export default class Runner { |
| 61 | + static withArgs(args: string[]): Runner { |
| 62 | + return new Runner(parseOptions(args)); |
| 63 | + } |
| 64 | + |
| 65 | + static withOptions(options: Partial<Options> = {}): Runner { |
| 66 | + return new Runner({ |
| 67 | + config: undefined, |
| 68 | + telemetry: 'auto', |
| 69 | + url: 'http://localhost:4200', |
| 70 | + ...options, |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + constructor(private readonly options: Options) { |
| 75 | + validateOptions(options); |
| 76 | + } |
| 77 | + |
| 78 | + async detectTelemetryType( |
| 79 | + detect: MaybePromise<DetectResult> = this.detect() |
| 80 | + ): Promise<'embroider' | 'runtime'> { |
| 81 | + const result = await detect; |
| 82 | + const type = this.options.telemetry; |
| 83 | + switch (type) { |
| 84 | + case 'embroider': { |
| 85 | + if (result.hasStage2Output) { |
| 86 | + return 'embroider'; |
| 87 | + } else { |
| 88 | + throw new Error('Please run the thing first FIXME'); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + case 'runtime': { |
| 93 | + if (result.hasStage2Output) { |
| 94 | + console.warn('Are you sure you want to use runtime telemetry? FIXME'); |
| 95 | + } |
| 96 | + |
| 97 | + if (result.isServerRunning) { |
| 98 | + return 'runtime'; |
| 99 | + } else { |
| 100 | + throw new Error('Please run the server or pass correct URL FIXME'); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + case 'auto': { |
| 105 | + if (result.hasStage2Output) { |
| 106 | + return 'embroider'; |
| 107 | + } else if (result.isServerRunning) { |
| 108 | + return 'runtime'; |
| 109 | + } else { |
| 110 | + throw new Error('Please RTFM FIXME'); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + async run(): Promise<void> { |
| 117 | + const telemetryType = await this.detectTelemetryType(); |
| 118 | + |
| 119 | + if (telemetryType === 'runtime') { |
| 120 | + debug('Gathering telemetry data from %s ...', this.options.url); |
| 121 | + await gatherTelemetryForUrl(this.options.url, analyzeEmberObject); |
| 122 | + |
| 123 | + const telemetry = getTelemetry() as Record<string, unknown>; // FIXME |
| 124 | + debug('Gathered telemetry on %d modules', Object.keys(telemetry).length); |
| 125 | + } |
| 126 | + |
| 127 | + runTransform(__dirname, 'no-implicit-this', process.argv, 'hbs'); |
| 128 | + } |
| 129 | + |
| 130 | + private async detect(): Promise<DetectResult> { |
| 131 | + const isServerRunning = await new Promise<boolean>((resolve) => { |
| 132 | + http.get(this.options.url, () => resolve(true)).on('error', () => resolve(false)); |
| 133 | + }); |
| 134 | + |
| 135 | + const hasStage2Output = fs.existsSync('dist/.stage2-output'); |
| 136 | + |
| 137 | + return { isServerRunning, hasStage2Output }; |
| 138 | + } |
| 139 | +} |
0 commit comments