Skip to content

Commit

Permalink
fix(cli): allow --logs usage without codemod name, fix recipe runs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
r4zendev authored Aug 15, 2024
1 parent 8980d07 commit e96a4d4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
22 changes: 10 additions & 12 deletions apps/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
execPromise,
getCodemodRc,
} from "@codemod-com/utilities";
import open from "open";
import { version as cliVersion } from "#/../package.json";
import { getDiff, getDiffScreen } from "#dryrun-diff.js";
import { fetchCodemod, populateCodemodArgs } from "#fetch-codemod.js";
Expand All @@ -25,7 +26,7 @@ import { RunnerService } from "#services/runner-service.js";
import type { TelemetryEvent } from "#telemetry.js";
import type { NamedFileCommand } from "#types/commands.js";
import { originalStdoutWrite } from "#utils/constants.js";
import { writeLogs } from "#utils/logs.js";
import { logsPath, writeLogs } from "#utils/logs.js";

const checkFileTreeVersioning = async (target: string) => {
let force = true;
Expand Down Expand Up @@ -98,19 +99,12 @@ export const handleRunCliCommand = async (options: {

const flowSettings = await parseFlowSettings(args, printer);

if (
!flowSettings.dry &&
!args.readme &&
!args.config &&
!args.version &&
args.interactive &&
!args.mode
) {
await checkFileTreeVersioning(flowSettings.target);
}

const nameOrPath = args._.at(0)?.toString() ?? args.source ?? null;
if (nameOrPath === null) {
if (args.logs) {
return open(logsPath);
}

throw new Error("Codemod to run was not specified!");
}

Expand Down Expand Up @@ -206,6 +200,10 @@ export const handleRunCliCommand = async (options: {
runnerService,
});

if (!flowSettings.dry && args.interactive) {
await checkFileTreeVersioning(flowSettings.target);
}

const depsToInstall: Record<
string,
{ deps: string[]; affectedFiles: string[] }
Expand Down
34 changes: 28 additions & 6 deletions apps/cli/src/fetch-codemod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ export const populateCodemodArgs = async (options: {
...codemod,
codemods: await Promise.all(
codemod.codemods.map(async (subCodemod) => {
const codemod = populateCodemodArgs({
const codemod = await populateCodemodArgs({
...options,
codemod: subCodemod,
});

const parsedCodemod = safeParseKnownEnginesCodemod(codemod);

if (!parsedCodemod.success) {
if (safeParseRecipeCodemod(codemod).success) {
throw new Error("Nested recipe codemods are not supported.");
}

throw new Error("Nested codemod is of incorrect structure.");
throw new Error(
`Nested codemod is of incorrect structure: ${flatten(
parsedCodemod.issues,
)}`,
);
}

return parsedCodemod.output;
Expand Down Expand Up @@ -300,6 +300,27 @@ export const fetchCodemod = async (options: FetchOptions): Promise<Codemod> => {
chalk.cyan(`Fetching ${config.names.length} recipe codemods...`),
);

const codemods = await Promise.all(
config.names.map(async (name) => {
const subCodemod = await fetchCodemod({
...options,
disableLogs: true,
nameOrPath: name,
});

if (safeParseRecipeCodemod(subCodemod).success) {
throw new Error("Nested recipe codemods are not supported.");
}

const validatedCodemod = safeParseKnownEnginesCodemod(subCodemod);
if (!validatedCodemod.success) {
throw new Error("Nested codemod is of incorrect structure.");
}

return validatedCodemod.output;
}),
);

subCodemodsSpinner.stopAndPersist({
symbol: oraCheckmark,
text: chalk.cyan("Successfully fetched recipe codemods."),
Expand All @@ -310,6 +331,7 @@ export const fetchCodemod = async (options: FetchOptions): Promise<Codemod> => {
source: "remote",
config,
path,
codemods,
} satisfies CodemodValidationInput);
}

Expand Down
3 changes: 2 additions & 1 deletion apps/cli/src/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { doubleQuotify } from "@codemod-com/utilities";
import { version } from "#/../package.json";
import { codemodDirectoryPath } from "./constants.js";

export const logsPath = join(codemodDirectoryPath, "logs");

export const writeLogs = async (options: {
prefix: string;
content: string;
fatal?: boolean;
}): Promise<string> => {
const { prefix, content, fatal } = options;

const logsPath = join(codemodDirectoryPath, "logs");
const logFilePath = join(
logsPath,
`${fatal ? "FATAL-" : ""}${new Date().toISOString()}-error.log`,
Expand Down

0 comments on commit e96a4d4

Please sign in to comment.