|
1 | 1 | import path from 'path' |
2 | 2 |
|
| 3 | +import { Command, Option } from 'commander' |
| 4 | +import signale from 'signale' |
3 | 5 | import { Project } from 'ts-morph' |
4 | 6 |
|
5 | | -import { globalCssToCssModule } from './transforms/globalCssToCssModule/globalCssToCssModule' |
| 7 | +import { transforms, transformNames } from './transforms' |
6 | 8 |
|
7 | | -const TARGET_FILE = path.resolve(__dirname, './transforms/globalCssToCssModule/__tests__/fixtures/Kek.tsx') |
| 9 | +const program = new Command() |
8 | 10 |
|
9 | | -async function main(): Promise<void> { |
10 | | - const project = new Project() |
11 | | - project.addSourceFilesAtPaths(TARGET_FILE) |
12 | | - |
13 | | - const result = await globalCssToCssModule({ project, shouldWriteFiles: true }) |
14 | | - console.log(result) |
| 11 | +interface CodemodCliOptions { |
| 12 | + write: boolean |
| 13 | + format: boolean |
| 14 | + transform: keyof typeof transforms |
15 | 15 | } |
16 | 16 |
|
17 | | -main().catch(error => { |
18 | | - throw error |
19 | | -}) |
| 17 | +program |
| 18 | + .addOption( |
| 19 | + new Option('-t, --transform <transform>', 'Transform name').choices(transformNames).makeOptionMandatory() |
| 20 | + ) |
| 21 | + .option('-w, --write [write]', 'Persist codemod changes to the filesystem', false) |
| 22 | + .argument('<fileGlob>', 'File glob or globs to change files based on') |
| 23 | + .action(async (fileGlob: string, options: CodemodCliOptions) => { |
| 24 | + const { write: shouldWriteFiles, transform } = options |
| 25 | + const projectGlob = path.isAbsolute(fileGlob) ? fileGlob : path.join(process.cwd(), fileGlob) |
| 26 | + |
| 27 | + signale.start(`Starting codemod "${transform}" with project glob "${projectGlob}"`) |
| 28 | + |
| 29 | + const project = new Project() |
| 30 | + project.addSourceFilesAtPaths(projectGlob) |
| 31 | + |
| 32 | + const results = await transforms[transform]({ project, shouldWriteFiles }) |
| 33 | + |
| 34 | + if (results) { |
| 35 | + if (shouldWriteFiles) { |
| 36 | + signale.info('Persisting codemod changes to the filesystem...') |
| 37 | + await Promise.all(results.map(result => result.fsWritePromise)) |
| 38 | + signale.info('Persisting codemod changes completed') |
| 39 | + } else { |
| 40 | + console.log(results) |
| 41 | + } |
| 42 | + |
| 43 | + signale.success('Files are transformed!') |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | +program.parse(process.argv) |
0 commit comments