Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: fix automigrate filtering #20329

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions code/lib/cli/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import dedent from 'ts-dedent';
import { JsPackageManagerFactory, type PackageManagerName } from '../js-package-manager';

import type { Fix } from './fixes';
import { fixes } from './fixes';
import { fixes as allFixes } from './fixes';

const logger = console;

type FixId = string;

interface FixOptions {
fixId?: FixId;
list?: boolean;
yes?: boolean;
dryRun?: boolean;
useNpm?: boolean;
Expand All @@ -37,15 +38,32 @@ type FixSummary = {
failed: Record<FixId, string>;
};

export const automigrate = async ({ fixId, dryRun, yes, useNpm, force }: FixOptions = {}) => {
const logAvailableMigrations = () => {
const availableFixes = allFixes.map((f) => chalk.yellow(f.id)).join(', ');
logger.info(`\nThe following migrations are available: ${availableFixes}`);
};

export const automigrate = async ({ fixId, dryRun, yes, useNpm, force, list }: FixOptions = {}) => {
if (list) {
logAvailableMigrations();
return null;
}

const fixes = fixId ? allFixes.filter((f) => f.id === fixId) : allFixes;

if (fixId && fixes.length === 0) {
logger.info(`📭 No migrations found for ${chalk.magenta(fixId)}.`);
logAvailableMigrations();
return null;
}

const packageManager = JsPackageManagerFactory.getPackageManager({ useNpm, force });
const filtered = fixId ? fixes.filter((f) => f.id === fixId) : fixes;

logger.info('🔎 checking possible migrations..');
const fixResults = {} as Record<FixId, FixStatus>;
const fixSummary: FixSummary = { succeeded: [], failed: {}, manual: [], skipped: [] };

for (let i = 0; i < filtered.length; i += 1) {
for (let i = 0; i < fixes.length; i += 1) {
const f = fixes[i] as Fix;
let result;

Expand Down
1 change: 1 addition & 0 deletions code/lib/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ program
.option('-n --dry-run', 'Only check for fixes, do not actually run them')
.option('--package-manager <npm|pnpm|yarn1|yarn2>', 'Force package manager')
.option('-N --use-npm', 'Use npm as package manager (deprecated)')
.option('-l --list', 'List available migrations')
.action(async (fixId, options) => {
await automigrate({ fixId, ...options }).catch((e) => {
logger.error(e);
Expand Down