1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
import path from "path";
import fs from "fs-extra";
import { getTurboConfigs } from "@turbo/utils";
import type { Schema as TurboJsonSchema } from "@turbo/types";
import type { TransformerArgs } from "../types";
import getTransformerHelpers from "../utils/getTransformerHelpers";
import { TransformerResults } from "../runner";
const DEFAULT_OUTPUTS = ["dist/**", "build/**"];
// transformer details
const TRANSFORMER = "set-default-outputs";
const DESCRIPTION =
'Add the "outputs" key with defaults where it is missing in `turbo.json`';
const INTRODUCED_IN = "1.7.0";
function migrateConfig(config: TurboJsonSchema) {
for (const [_, taskDef] of Object.entries(config.pipeline)) {
if (taskDef.cache !== false) {
if (!taskDef.outputs) {
taskDef.outputs = DEFAULT_OUTPUTS;
} else if (
Array.isArray(taskDef.outputs) &&
taskDef.outputs.length === 0
) {
delete taskDef.outputs;
}
}
}
return config;
}
export function transformer({
root,
options,
}: TransformerArgs): TransformerResults {
const { log, runner } = getTransformerHelpers({
transformer: TRANSFORMER,
rootPath: root,
options,
});
// If `turbo` key is detected in package.json, require user to run the other codemod first.
const packageJsonPath = path.join(root, "package.json");
// package.json should always exist, but if it doesn't, it would be a silly place to blow up this codemod
let packageJSON = {};
try {
packageJSON = fs.readJSONSync(packageJsonPath);
} catch (e) {
// readJSONSync probably failed because the file doesn't exist
}
if ("turbo" in packageJSON) {
return runner.abortTransform({
reason:
'"turbo" key detected in package.json. Run `npx @turbo/codemod transform create-turbo-config` first',
});
}
log.info(`Adding default \`outputs\` key into tasks if it doesn't exist`);
const turboConfigPath = path.join(root, "turbo.json");
if (!fs.existsSync(turboConfigPath)) {
return runner.abortTransform({
reason: `No turbo.json found at ${root}. Is the path correct?`,
});
}
const turboJson: TurboJsonSchema = fs.readJsonSync(turboConfigPath);
runner.modifyFile({
filePath: turboConfigPath,
after: migrateConfig(turboJson),
});
// find and migrate any workspace configs
const workspaceConfigs = getTurboConfigs(root);
workspaceConfigs.forEach((workspaceConfig) => {
const { config, turboConfigPath } = workspaceConfig;
runner.modifyFile({
filePath: turboConfigPath,
after: migrateConfig(config),
});
});
return runner.finish();
}
const transformerMeta = {
name: `${TRANSFORMER}: ${DESCRIPTION}`,
value: TRANSFORMER,
introducedIn: INTRODUCED_IN,
transformer,
};
export default transformerMeta;
|