|
| 1 | +#!/usr/bin/env node |
| 2 | +import { readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs' |
| 3 | +import { extname, join, relative } from 'node:path' |
| 4 | +import process from 'node:process' |
| 5 | +import { transformDataState } from '../src/data-state.mjs' |
| 6 | + |
| 7 | +const DEFAULT_EXTENSIONS = ['vue', 'css', 'scss', 'ts', 'tsx', 'js', 'jsx', 'html', 'md'] |
| 8 | +const SKIPPED_DIRECTORIES = new Set(['node_modules', 'dist', '.git', '.nuxt', '.output', 'coverage']) |
| 9 | + |
| 10 | +const TRANSFORMS = { |
| 11 | + 'data-state': transformDataState, |
| 12 | +} |
| 13 | + |
| 14 | +const USAGE = `Usage: reka-ui-codemod <transform> [paths...] [--dry-run] [--ext vue,css,scss,ts,tsx,js,jsx,html,md] |
| 15 | +
|
| 16 | +Transforms: |
| 17 | + data-state Rewrite v2 data-state values (on/off, active/inactive, visible/hidden, |
| 18 | + expanded/collapsed, delayed-open/instant-open) to the v3 vocabulary |
| 19 | + in Tailwind variants and CSS attribute selectors. |
| 20 | +
|
| 21 | +Options: |
| 22 | + --dry-run Report what would change without writing any file. |
| 23 | + --ext Comma-separated list of file extensions to process. |
| 24 | + -h, --help Show this message. |
| 25 | +` |
| 26 | + |
| 27 | +function print(message) { |
| 28 | + process.stdout.write(`${message}\n`) |
| 29 | +} |
| 30 | + |
| 31 | +function parseArgs(argv) { |
| 32 | + const options = { transform: undefined, paths: [], dryRun: false, extensions: DEFAULT_EXTENSIONS, help: false } |
| 33 | + for (let i = 0; i < argv.length; i++) { |
| 34 | + const arg = argv[i] |
| 35 | + if (arg === '--dry-run') { |
| 36 | + options.dryRun = true |
| 37 | + } |
| 38 | + else if (arg === '-h' || arg === '--help') { |
| 39 | + options.help = true |
| 40 | + } |
| 41 | + else if (arg === '--ext') { |
| 42 | + options.extensions = parseExtensions(argv[++i]) |
| 43 | + } |
| 44 | + else if (arg.startsWith('--ext=')) { |
| 45 | + options.extensions = parseExtensions(arg.slice('--ext='.length)) |
| 46 | + } |
| 47 | + else if (arg.startsWith('-')) { |
| 48 | + throw new Error(`Unknown option: ${arg}`) |
| 49 | + } |
| 50 | + else if (options.transform === undefined) { |
| 51 | + options.transform = arg |
| 52 | + } |
| 53 | + else { |
| 54 | + options.paths.push(arg) |
| 55 | + } |
| 56 | + } |
| 57 | + if (options.paths.length === 0) |
| 58 | + options.paths.push('.') |
| 59 | + return options |
| 60 | +} |
| 61 | + |
| 62 | +function parseExtensions(value = '') { |
| 63 | + const extensions = value |
| 64 | + .split(',') |
| 65 | + .map(ext => ext.trim().replace(/^\./, '')) |
| 66 | + .filter(Boolean) |
| 67 | + if (extensions.length === 0) |
| 68 | + throw new Error('--ext expects a comma-separated list of extensions') |
| 69 | + return extensions |
| 70 | +} |
| 71 | + |
| 72 | +function* walk(path, extensions) { |
| 73 | + // A supplied root that is, or sits inside, an always-skipped directory is |
| 74 | + // skipped too, so the documented contract holds for explicit paths as well. |
| 75 | + if (path.split(/[\\/]/).some(segment => SKIPPED_DIRECTORIES.has(segment))) |
| 76 | + return |
| 77 | + const stats = statSync(path) |
| 78 | + if (stats.isFile()) { |
| 79 | + if (extensions.has(extname(path).slice(1))) |
| 80 | + yield path |
| 81 | + return |
| 82 | + } |
| 83 | + if (!stats.isDirectory()) |
| 84 | + return |
| 85 | + const entries = readdirSync(path, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name)) |
| 86 | + for (const entry of entries) { |
| 87 | + if (entry.isDirectory()) { |
| 88 | + if (!SKIPPED_DIRECTORIES.has(entry.name)) |
| 89 | + yield* walk(join(path, entry.name), extensions) |
| 90 | + } |
| 91 | + else if (entry.isFile() && extensions.has(extname(entry.name).slice(1))) { |
| 92 | + yield join(path, entry.name) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +function run(argv) { |
| 98 | + const options = parseArgs(argv) |
| 99 | + if (options.help || options.transform === undefined) { |
| 100 | + print(USAGE) |
| 101 | + return options.help ? 0 : 1 |
| 102 | + } |
| 103 | + |
| 104 | + const transform = TRANSFORMS[options.transform] |
| 105 | + if (!transform) { |
| 106 | + print(`Unknown transform "${options.transform}".\n`) |
| 107 | + print(USAGE) |
| 108 | + return 1 |
| 109 | + } |
| 110 | + |
| 111 | + const extensions = new Set(options.extensions) |
| 112 | + let changedFiles = 0 |
| 113 | + let warningCount = 0 |
| 114 | + |
| 115 | + for (const root of options.paths) { |
| 116 | + for (const file of walk(root, extensions)) { |
| 117 | + const source = readFileSync(file, 'utf8') |
| 118 | + const { code, changed, warnings } = transform(source, { filename: file }) |
| 119 | + const displayPath = relative(process.cwd(), file) || file |
| 120 | + |
| 121 | + if (changed) { |
| 122 | + changedFiles++ |
| 123 | + if (!options.dryRun) |
| 124 | + writeFileSync(file, code) |
| 125 | + print(`${options.dryRun ? 'would change' : 'changed'} ${displayPath}`) |
| 126 | + } |
| 127 | + for (const warning of warnings) { |
| 128 | + warningCount++ |
| 129 | + print(`${displayPath}:${warning.line}: ${warning.message}`) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + const suffix = options.dryRun ? ' (dry run, nothing written)' : '' |
| 135 | + print(`${changedFiles} files changed, ${warningCount} warnings${suffix}`) |
| 136 | + return 0 |
| 137 | +} |
| 138 | + |
| 139 | +try { |
| 140 | + process.exitCode = run(process.argv.slice(2)) |
| 141 | +} |
| 142 | +catch (error) { |
| 143 | + process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`) |
| 144 | + process.exitCode = 1 |
| 145 | +} |
0 commit comments