Regex-based on purpose: converts simple top-level CJS patterns to ESM, remaps package specifiers to their @choojs homes, points retired nano* packages at built-in replacements (choo-lazy-route → lazy()), and reports everything it refuses to guess at instead of guessing. Validated against choo's own v7 example app: migrated output runs on v8 unmodified. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014NgfSjHE11oFpoSnLVKLXd
53 lines
1.7 KiB
JavaScript
Executable File
53 lines
1.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
// choo-migrate [--dry] <files or directories…>
|
|
// Rewrites choo v7 sources toward v8: CJS → ESM for simple top-level
|
|
// patterns, package specifiers remapped, and a per-file report of what
|
|
// needs human hands. --dry prints the report without writing.
|
|
|
|
import { readFile, writeFile, readdir, stat } from 'node:fs/promises'
|
|
import { join, extname } from 'node:path'
|
|
|
|
import transform from './lib/transform.js'
|
|
|
|
const args = process.argv.slice(2)
|
|
const dry = args.includes('--dry')
|
|
const targets = args.filter((a) => a !== '--dry')
|
|
|
|
if (targets.length === 0) {
|
|
console.log('usage: choo-migrate [--dry] <files or directories…>')
|
|
process.exit(1)
|
|
}
|
|
|
|
const SKIP = new Set(['node_modules', '.git', 'dist', 'coverage'])
|
|
|
|
async function * walk (path) {
|
|
const info = await stat(path)
|
|
if (info.isFile()) {
|
|
if (extname(path) === '.js' || extname(path) === '.mjs') yield path
|
|
return
|
|
}
|
|
for (const entry of await readdir(path)) {
|
|
if (SKIP.has(entry)) continue
|
|
yield * walk(join(path, entry))
|
|
}
|
|
}
|
|
|
|
let changedCount = 0
|
|
for (const target of targets) {
|
|
for await (const file of walk(target)) {
|
|
const source = await readFile(file, 'utf8')
|
|
const { code, changed, notes } = transform(source)
|
|
if (!changed && notes.length === 0) continue
|
|
|
|
console.log(`\n${file}${changed ? (dry ? ' (would change)' : ' (rewritten)') : ''}`)
|
|
for (const note of notes) console.log(` ⚠ ${note}`)
|
|
|
|
if (changed && !dry) {
|
|
await writeFile(file, code)
|
|
changedCount++
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`\n${dry ? 'dry run — no files written' : changedCount + ' file(s) rewritten'}. Remember: package.json needs "type": "module" and deps on @choojs/*.`)
|