|
| 1 | +// Used for parsing a transform module. |
| 2 | +import 'ts-node/register/transpile-only' |
| 3 | +import path from 'path' |
| 4 | + |
| 5 | +import { Command } from 'commander' |
| 6 | +import signale from 'signale' |
| 7 | +import { dedent } from 'ts-dedent' |
| 8 | +// TODO: replace project with generic interface that can be used for CSS codemods. |
| 9 | +import { Project } from 'ts-morph' |
| 10 | + |
| 11 | +import { logRequiredManualChanges } from '@sourcegraph/codemod-common' |
| 12 | + |
| 13 | +import { Codemod, CodemodContext, TransformOptions } from './types' |
| 14 | + |
| 15 | +const program = new Command() |
| 16 | + |
| 17 | +interface CodemodCliOptions extends TransformOptions { |
| 18 | + write: boolean |
| 19 | + format: boolean |
| 20 | + transform: string |
| 21 | +} |
| 22 | + |
| 23 | +const PROJECT_ROOT = path.resolve(__dirname, '../../../') |
| 24 | + |
| 25 | +program |
| 26 | + // TODO: make it `true` by default after switching to fast bulk format. |
| 27 | + .option('-f, --format [format]', 'Format Typescript source files with ESLint', false) |
| 28 | + .option('-w, --write [write]', 'Persist codemod changes to the filesystem', false) |
| 29 | + .option('-t, --transform <transform>', 'Absolute or relative to project root path to a transform module') |
| 30 | + .argument('<fileGlob>', 'Absolute or relative to project root file glob to change files based on') |
| 31 | + .allowUnknownOption(true) |
| 32 | + .enablePositionalOptions(true) |
| 33 | + .addHelpText( |
| 34 | + 'after', |
| 35 | + dedent` |
| 36 | +
|
| 37 | + Transform-specific options can be passed via using '--option=value' syntax: |
| 38 | + yarn transform --write --tagToConvert=Link -t ./transformPath.ts 'globPath/**/*.{ts,tsx}' |
| 39 | + ` |
| 40 | + ) |
| 41 | + .action(async (commandArgument: string, options: CodemodCliOptions) => { |
| 42 | + const { fileGlob, transformOptions } = parseOptions(commandArgument) |
| 43 | + const { write: shouldWriteFiles, format: shouldFormat, transform } = options |
| 44 | + |
| 45 | + const projectGlob = path.isAbsolute(fileGlob) ? fileGlob : path.join(PROJECT_ROOT, fileGlob) |
| 46 | + const transformPath = path.isAbsolute(transform) ? transform : path.join(PROJECT_ROOT, transform) |
| 47 | + |
| 48 | + signale.start(`Starting codemod "${transformPath}" with the project glob "${projectGlob}".`) |
| 49 | + |
| 50 | + const project = new Project() |
| 51 | + project.addSourceFilesAtPaths(projectGlob) |
| 52 | + |
| 53 | + const transformExports = Object.values(await import(transformPath)) |
| 54 | + if (transformExports.length !== 1) { |
| 55 | + throw new Error('Transform file should have one named export with the transform function.') |
| 56 | + } |
| 57 | + |
| 58 | + const [codemod] = transformExports |
| 59 | + const codemodContext: CodemodContext = { |
| 60 | + project, |
| 61 | + shouldWriteFiles, |
| 62 | + shouldFormat, |
| 63 | + transformOptions, |
| 64 | + } |
| 65 | + |
| 66 | + const results = await (codemod as Codemod)(codemodContext) |
| 67 | + |
| 68 | + results.map(result => { |
| 69 | + logRequiredManualChanges(result.manualChangesReported) |
| 70 | + }) |
| 71 | + |
| 72 | + if (shouldWriteFiles) { |
| 73 | + signale.await('Persisting codemod changes to the filesystem...') |
| 74 | + await Promise.all( |
| 75 | + results.map(result => { |
| 76 | + return result.fsWritePromise |
| 77 | + }) |
| 78 | + ) |
| 79 | + signale.complete('Persisting codemod changes completed.') |
| 80 | + } else { |
| 81 | + for (const file of results.flatMap(result => { |
| 82 | + return result.files |
| 83 | + })) { |
| 84 | + signale.log(file?.source) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + signale.success('Codemod is applied!') |
| 89 | + }) |
| 90 | + |
| 91 | +program.parse(process.argv) |
| 92 | + |
| 93 | +interface ParseOptionsResult { |
| 94 | + fileGlob: string |
| 95 | + transformOptions: TransformOptions |
| 96 | +} |
| 97 | + |
| 98 | +function parseOptions(commandArgument: string): ParseOptionsResult { |
| 99 | + const { unknown } = program.parseOptions(process.argv) |
| 100 | + |
| 101 | + // TODO: find a better way to process and validate `transformOptions`. |
| 102 | + const fileGlob = unknown.pop() || commandArgument |
| 103 | + |
| 104 | + const transformOptions = unknown.reduce<Record<string, unknown>>((result, key) => { |
| 105 | + const [name, value = true] = key.split('=') |
| 106 | + result[name.replace('--', '')] = value |
| 107 | + |
| 108 | + return result |
| 109 | + }, {}) |
| 110 | + |
| 111 | + return { fileGlob, transformOptions } |
| 112 | +} |
0 commit comments