Skip to content

Commit

Permalink
refactor: make async
Browse files Browse the repository at this point in the history
  • Loading branch information
talkor committed Sep 11, 2024
1 parent e850149 commit 47e2056
Showing 1 changed file with 64 additions and 47 deletions.
111 changes: 64 additions & 47 deletions packages/codemod/bin/vibe-codemod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import { join, resolve } from "path";
import { spawnSync } from "child_process";
import { spawn, spawnSync } from "child_process";
import * as globby from "globby";
import * as fs from "fs";
import * as path from "path";
Expand Down Expand Up @@ -138,49 +138,61 @@ async function main() {

const spinner = ora(`Running transformations...`).start();

async function runSingleTransformation(transform: string): Promise<boolean> {
const result = spawnSync(
"node",
[
require.resolve("jscodeshift/bin/jscodeshift"),
`--extensions=${extensions.join(",")}`,
"--ignore-pattern=node_modules",
"--ignore-pattern=**/*.d.ts",
`--verbose=${verbosityLevel}`,
"--max-workers=8",
"--no-babel",
"-t",
transform,
targetDir,
outputTarget
],
{
stdio: verbose ? "inherit" : ["inherit", "pipe", "pipe"],
shell: true,
env: { ...process.env, FORCE_COLOR: "1" }
async function runSingleTransformation(transform: string) {
return new Promise(resolve => {
const child = spawn(
"node",
[
require.resolve("jscodeshift/bin/jscodeshift"),
`--extensions=${extensions.join(",")}`,
"--ignore-pattern=node_modules",
"--ignore-pattern=**/*.d.ts",
`--verbose=${verbosityLevel}`,
"--max-workers=8",
"--no-babel",
"-t",
transform,
targetDir,
outputTarget
],
{
stdio: verbose ? "inherit" : ["inherit", "pipe", "pipe"],
shell: true,
env: { ...process.env, FORCE_COLOR: "1" }
}
);

let hasError = false;

if (child.stderr) {
child.stderr.on("data", data => {
const errorOutput = data.toString();
process.stderr.write(errorOutput);
if (errorOutput.includes("ERROR")) {
hasError = true;
errorsCount++;
}
});
}
);

let hasError = false;

if (result.stderr) {
const errorOutput = result.stderr.toString();
process.stderr.write(errorOutput);
if (errorOutput.includes("ERROR")) {
hasError = true;
errorsCount++;
}
}

if (result.stdout) {
const output = result.stdout.toString();
if (output.includes("ERROR")) {
hasError = true;
errorsCount++;
if (child.stdout) {
child.stdout.on("data", data => {
const output = data.toString();
if (output.includes("ERROR")) {
hasError = true;
errorsCount++;
}
});
}
}

return result.status === 0 && !hasError;
child.on("exit", code => {
if (code !== 0 || hasError) {
resolve(false);
} else {
resolve(true);
}
});
});
}

const transformationFiles: string[] = globby.sync(`${transformationsDir}/*.js`, {
Expand All @@ -207,14 +219,19 @@ async function main() {

spinner.text = `Processing transformation (${index + 1}/${orderedTransformationFiles.length}): ${transformName}`;

const result = await runSingleTransformation(transform);

if (result) {
successCount++;
spinner.succeed(chalk.green(`Transformation completed: ${transformName}`));
} else {
try {
const result = await runSingleTransformation(transform);

if (result) {
successCount++;
spinner.succeed(chalk.green(`Transformation completed: ${transformName}`));
} else {
failureCount++;
spinner.fail(chalk.red(`Transformation finished with errors: ${transformName}`));
}
} catch (error) {
failureCount++;
spinner.fail(chalk.red(`Transformation finished with errors: ${transformName}`));
spinner.fail(chalk.red(`Transformation failed: ${transformName}`));
}

if (index < orderedTransformationFiles.length - 1) {
Expand Down

0 comments on commit 47e2056

Please sign in to comment.