import { cp, readFile, readdir } from 'node:fs/promises'; import { createRequire } from 'node:module'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import semver from 'semver'; import type { ArgumentsCamelCase, Argv, CommandModule } from 'yargs'; interface UpgradeArgs { from?: number; to?: number; export?: string; } const runtimePackages = new Set([ 'crawlee', ...[ 'basic', 'browser', 'browser-pool', 'cheerio', 'core', 'fs-storage', 'http', 'http-client', 'impit-client', 'got-scraping-client', 'playwright', 'puppeteer', 'stagehand', 'types', 'utils', 'otel', ].map((name) => `@crawlee/${name}`), ]); async function detectProjectMajor(): Promise { let directory = process.cwd(); let manifestPath: string; let manifest: Record>; while (true) { manifestPath = join(directory, 'package.json'); try { manifest = JSON.parse(await readFile(manifestPath, 'utf8')); break; } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; const parent = dirname(directory); if (parent === directory) { throw new Error('No project package.json found. Run inside the project or specify --from .'); } directory = parent; } } const require = createRequire(manifestPath); const majors = new Set(); for (const section of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) { for (const [name, specifier] of Object.entries(manifest[section] ?? {})) { if (!runtimePackages.has(name)) continue; const range = typeof specifier === 'string' ? semver.validRange(specifier.replace(/^workspace:/, '')) : null; let installedVersion: string | null = null; try { const installed = JSON.parse(await readFile(require.resolve(`${name}/package.json`), 'utf8')); installedVersion = semver.valid(installed.version); } catch (error) { if ( !['MODULE_NOT_FOUND', 'ERR_PACKAGE_PATH_NOT_EXPORTED', 'ENOENT'].includes( (error as NodeJS.ErrnoException).code ?? '', ) ) { throw error; } } if ( installedVersion && (!range || semver.satisfies(installedVersion, range, { includePrerelease: true })) ) { majors.add(semver.major(installedVersion)); continue; } const minimum = range ? semver.minVersion(range) : null; if ( !minimum || !semver.subset(range!, `>=${minimum.major}.0.0-0 <${minimum.major + 1}.0.0-0`, { includePrerelease: true, }) ) { throw new Error( `Cannot determine the Crawlee major from ${name}: ${specifier} in ${manifestPath}. Install project dependencies or specify --from .`, ); } majors.add(minimum.major); } } if (majors.size !== 1) { const reason = majors.size ? `Crawlee dependencies use different majors (${[...majors].sort().join(', ')})` : 'No Crawlee runtime dependency found'; throw new Error( `${reason} in ${manifestPath}. Run in the relevant workspace package or specify --from .`, ); } return [...majors][0]; } function body(markdown: string): string { return markdown.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n+/, '').trim(); } export class UpgradeCommand implements CommandModule { command = 'upgrade'; describe = 'Prints an AI migration prompt, upgrading one Crawlee major at a time'; builder = (args: Argv) => args .option('from', { type: 'number', describe: 'Starting major version; defaults to the project version' }) .option('to', { type: 'number', describe: 'Target major version; defaults to the newest bundled migration', }) .option('export', { type: 'string', describe: 'Copy the migration guides into this directory and link the prompt to that copy', }); async handler(args: ArgumentsCamelCase) { for (const [name, value] of Object.entries({ from: args.from, to: args.to })) { if (value !== undefined && (!Number.isSafeInteger(value) || value < 1)) { throw new Error(`--${name} must be a positive major version number.`); } } const skillsUrl = new URL('../skills/', import.meta.url); const migrations = new Map(); for (const entry of await readdir(skillsUrl, { withFileTypes: true })) { const match = /^crawlee-upgrade-v(\d+)$/.exec(entry.name); if (entry.isDirectory() && match) migrations.set(Number(match[1]), new URL(`${entry.name}/`, skillsUrl)); } if (!migrations.size) throw new Error('No migration guides are bundled with this CLI.'); const from = args.from ?? (await detectProjectMajor()); const to = args.to ?? Math.max(...migrations.keys()); if (from >= to) { throw new Error( `The project is already on Crawlee v${from}; the target must be newer. Use --from to explicitly review an earlier migration.`, ); } const steps: string[] = []; const guides: string[] = []; for (let major = from + 1; major <= to; major++) { const skillUrl = migrations.get(major); if (!skillUrl) throw new Error( `No bundled migration from v${major - 1} to v${major}. Choose a supported --to version or use a CLI with that guide.`, ); let skillPath = fileURLToPath(skillUrl); if (args.export !== undefined) { const target = join(resolve(args.export), `crawlee-upgrade-v${major}`); await cp(skillPath, target, { recursive: true }); skillPath = target; } const skillRoot = skillPath.replace(/\\/g, '/').replace(/\/$/, ''); const prompt = await readFile(new URL('SKILL.md', skillUrl), 'utf8'); steps.push(`${steps.length + 1}. Upgrade v${major - 1} to v${major}, then verify before continuing.`); guides.push(body(prompt).replaceAll('{SKILL_ROOT}', () => skillRoot)); } const template = await readFile(new URL('crawlee-upgrade/SKILL.md', skillsUrl), 'utf8'); const replacements: Record = { FROM_MAJOR: String(from), TO_MAJOR: String(to), MIGRATION_PLAN: steps.join('\n'), MIGRATION_GUIDES: guides.join('\n\n---\n\n'), }; const prompt = body(template).replace( /\{(FROM_MAJOR|TO_MAJOR|MIGRATION_PLAN|MIGRATION_GUIDES)\}/g, (_, key: string) => replacements[key], ); process.stdout.write(`${prompt}\n`); } }