Skip to content

Commit

Permalink
fix(cli): post-refactoring logic fixes (codemod-com#1142)
Browse files Browse the repository at this point in the history
  • Loading branch information
r4zendev authored Jul 24, 2024
1 parent bc9995f commit 531b1cb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 33 deletions.
4 changes: 3 additions & 1 deletion apps/backend/src/publishHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ export const publishHandler: RouteHandler<{
});
}
}
} else {
}

if (codemodRc.engine !== "recipe") {
const { path } = await getEntryPath({
source: unpackPath,
throwOnNotFound: false,
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"imports": {
"#*": "./src/*"
},
"version": "0.12.0",
"version": "0.12.1",
"description": "A codemod engine for Node.js libraries (jscodeshift, ts-morph, etc.)",
"type": "module",
"exports": null,
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import type {
VerifyTokenResponse,
} from "@codemod-com/api-types";

export const extractCLIApiError = (err: unknown): string => {
export const extractPrintableApiError = (err: unknown): string => {
if (!(err instanceof Error)) {
return "An unknown error occurred.";
}

return err instanceof AxiosError ? err.response?.data : err.message;
return err instanceof AxiosError ? err.response?.data.errorText : err.message;
};

export const getCLIAccessToken = async (
Expand Down
36 changes: 20 additions & 16 deletions apps/cli/src/commands/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
getEntryPath,
} from "@codemod-com/utilities";
import { version as cliVersion } from "#/../package.json";
import { extractCLIApiError, getCodemod, publish } from "#api.js";
import { extractPrintableApiError, getCodemod, publish } from "#api.js";
import { getCurrentUserOrLogin } from "#auth-utils.js";
import { handleInitCliCommand } from "#commands/init.js";
import type { TelemetryEvent } from "#telemetry.js";
Expand Down Expand Up @@ -244,21 +244,25 @@ export const handlePublishCliCommand = async (options: {
})),
);

const builtExecutable = await getCodemodExecutable(source).catch(() => null);

if (builtExecutable === null) {
throw new Error(
chalk(
"Failed to build the codemod executable.",
"Please ensure that the node_modules are installed and the codemod is correctly configured.",
),
if (codemodRc.engine !== "recipe") {
const builtExecutable = await getCodemodExecutable(source).catch(
() => null,
);
}

codemodFileBuffers.push({
name: BUILT_SOURCE_PATH,
data: Buffer.from(builtExecutable),
});
if (builtExecutable === null) {
throw new Error(
chalk(
"Failed to build the codemod executable.",
"Please ensure that the node_modules are installed and the codemod is correctly configured.",
),
);
}

codemodFileBuffers.push({
name: BUILT_SOURCE_PATH,
data: Buffer.from(builtExecutable),
});
}

const codemodZip = await tarService.pack(codemodFileBuffers);

Expand All @@ -282,7 +286,7 @@ export const handlePublishCliCommand = async (options: {
} catch (error) {
publishSpinner.fail();

const message = extractCLIApiError(error);
const message = extractPrintableApiError(error);
const errorMessage = `${chalk.bold(
`Could not publish the "${codemodRc.name}" codemod`,
)}:\n${message}`;
Expand All @@ -292,7 +296,7 @@ export const handlePublishCliCommand = async (options: {
message: errorMessage,
});
} finally {
if (source.includes("temp")) {
if (source.includes(join(codemodDirectoryPath, "temp"))) {
await fs.promises.rm(source, { recursive: true, force: true });
}
}
Expand Down
1 change: 0 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
"formatWithErrors": true,
"indentStyle": "space",
"ignore": [
"./packages",
"*.d.ts",
"input.js",
"output.js",
Expand Down
32 changes: 20 additions & 12 deletions scripts/batchPublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { readdir } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";

// This script requires commenting out questions about namespaces, git url and tags because
// we not ask them unconditionally.

const execPromise = promisify(exec);

// Function to recursively walk through directory tree
Expand All @@ -21,6 +24,8 @@ const walkDirectory = async (dir: string, accumulator: string[]) => {
}
};

const oraCheckmark = "✔";

// Main function to execute the script
(async () => {
const args = process.argv.slice(2);
Expand All @@ -37,24 +42,27 @@ const walkDirectory = async (dir: string, accumulator: string[]) => {
for (let i = 0; i < accumulator.length; i++) {
const dir = accumulator[i].replace(/(\s+)/g, "\\$1");

console.log(`Publishing ${dir}`);

const { stderr, stdout } = await execPromise(
`codemod publish --source ${dir}`,
`apps/cli/dist/index.cjs publish --source ${dir}`,
);

const output = stdout.trim();
if (output.length) {
console.log(output);
}

const errors = stderr.trim();
if (!errors.length) {
console.log(`Successfully published ${dir}`);
console.log(`Published ${i + 1} of ${accumulator.length} directories`);
console.log("=====================================");
continue;

if (
!output.includes("Codemod was successfully published to the registry")
) {
console.error(`Failed to publish ${dir}`);
console.error(errors);
break;
}

console.error(`Failed to publish ${dir}`);
console.error(errors);
console.log("OUTPUT:", output);
console.log("ERRORS: ", errors);
console.log(`\nSuccessfully published ${dir}?`);
console.log(`Published ${i + 1} of ${accumulator.length} directories`);
console.log("=====================================\n");
}
})();
1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"cache": false
},
"@codemod-com/database#build": {
"dependsOn": ["^build"],
"cache": false
},
"build": {
Expand Down

0 comments on commit 531b1cb

Please sign in to comment.