Skip to content

Commit

Permalink
fix(codemod): output updates (vercel#3075)
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman authored Dec 19, 2022
1 parent 8faea8e commit 6c90175
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/turbo-codemod/__tests__/set-default-outputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ describe("add-default-outputs", () => {
expect(output.pipeline["build-one"].outputs).toStrictEqual(["foo"]);
expect(output.pipeline["build-two"].outputs).toStrictEqual(undefined);
expect(output.pipeline["build-three"].outputs).toStrictEqual([
"dist/**/*",
"build/**/*",
"dist/**",
"build/**",
]);

// @ts-ignore-next-line
Expand Down
2 changes: 1 addition & 1 deletion packages/turbo-codemod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function listTransforms(): void {
"\n"
)
);
process.exit(1);
process.exit(0);
}

function expandFilePathsIfNeeded(filesBeforeExpansion: string[]) {
Expand Down
34 changes: 21 additions & 13 deletions packages/turbo-codemod/src/transforms/set-default-outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from "fs-extra";
import { error, ok, skip } from "../logger";
import chalk from "chalk";

const DEFAULT_OUTPUTS = ["dist/**/*", "build/**/*"];
const DEFAULT_OUTPUTS = ["dist/**", "build/**"];

interface TaskDefinition {
outputs: [];
Expand Down Expand Up @@ -52,36 +52,44 @@ export default function addDefaultOutputs(files: string[], flags: Flags) {

let skippedCount = 0;
let modifiedCount = 0;
let deletedEmptyOutputs = 0;

let unmodifiedCount = 0;
for (const [taskName, taskDef] of Object.entries(rootTurboJson.pipeline)) {
if (!taskDef.outputs) {
ok(`Updating outputs for ${taskName}`);
taskDef.outputs = DEFAULT_OUTPUTS;
modifiedCount++;
if (flags.dry) {
skippedCount++;
} else {
taskDef.outputs = DEFAULT_OUTPUTS;
modifiedCount++;
}
} else if (Array.isArray(taskDef.outputs) && taskDef.outputs.length === 0) {
ok(
`Removing outputs: [] from ${taskName} as that is now the default behavior`
);
deletedEmptyOutputs++;
delete taskDef.outputs;
if (flags.dry) {
skippedCount++;
} else {
delete taskDef.outputs;
modifiedCount++;
}
} else {
skippedCount++;
unmodifiedCount++;
skip(`Skipping "${taskName}", it already has an outputs key defined`);
}
}

if (!flags.dry) {
if (flags.dry) {
console.log(JSON.stringify(rootTurboJson, null, 2));
} else {
fs.writeJsonSync(turboConfigPath, rootTurboJson, {
spaces: 2,
});
} else {
console.log(JSON.stringify(rootTurboJson, null, 2));
}

console.log("All done.");
console.log("Results:");
console.log(chalk.green(`${modifiedCount} modified`));
console.log(chalk.red(`${deletedEmptyOutputs} unmodified`));
console.log(chalk.red(`0 errors`));
console.log(chalk.yellow(`${skippedCount} skipped`));
console.log(chalk.yellow(`${unmodifiedCount} unmodified`));
console.log(chalk.green(`${modifiedCount} modified`));
}

0 comments on commit 6c90175

Please sign in to comment.