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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
import chalk from "chalk";
import os from "os";
import inquirer from "inquirer";
import { execSync } from "child_process";
import getCurrentVersion from "./steps/getCurrentVersion";
import getLatestVersion from "./steps/getLatestVersion";
import getCodemodsForMigration from "./steps/getTransformsForMigration";
import checkGitStatus from "../../utils/checkGitStatus";
import directoryInfo from "../../utils/directoryInfo";
import getTurboUpgradeCommand from "./steps/getTurboUpgradeCommand";
import Runner from "../../runner/Runner";
import type { MigrateCommandArgument, MigrateCommandOptions } from "./types";
import looksLikeRepo from "../../utils/looksLikeRepo";
function endMigration({
message,
success,
}: {
message?: string;
success: boolean;
}) {
if (success) {
console.log(chalk.bold(chalk.green("Migration completed")));
if (message) {
console.log(message);
}
return process.exit(0);
}
console.log(chalk.bold(chalk.red("Migration failed")));
if (message) {
console.log(message);
}
return process.exit(1);
}
/**
Migration is done in 4 steps:
-- gather information
1. find the version (x) of turbo to migrate from (if not specified)
2. find the version (y) of turbo to migrate to (if not specified)
3. determine which codemods need to be run to move from version x to version y
-- action
4. execute the codemods (serially, and in order)
5. update the turbo version (optionally)
**/
export default async function migrate(
directory: MigrateCommandArgument,
options: MigrateCommandOptions
) {
// check git status
if (!options.dry) {
checkGitStatus({ directory, force: options.force });
}
const answers = await inquirer.prompt<{
directoryInput?: string;
}>([
{
type: "input",
name: "directoryInput",
message: "Where is the root of the repo to migrate?",
when: !directory,
default: ".",
validate: (directory: string) => {
const { exists, absolute } = directoryInfo({ directory });
if (exists) {
return true;
} else {
return `Directory ${chalk.dim(`(${absolute})`)} does not exist`;
}
},
filter: (directory: string) => directory.trim(),
},
]);
const { directoryInput: selectedDirectory = directory as string } = answers;
const { exists, absolute: root } = directoryInfo({
directory: selectedDirectory,
});
if (!exists) {
return endMigration({
success: false,
message: `Directory ${chalk.dim(`(${root})`)} does not exist`,
});
}
if (!looksLikeRepo({ directory: root })) {
return endMigration({
success: false,
message: `Directory (${chalk.dim(
root
)}) does not appear to be a repository`,
});
}
// step 1
const fromVersion = getCurrentVersion(selectedDirectory, options);
if (!fromVersion) {
return endMigration({
success: false,
message: `Unable to infer the version of turbo being used by ${directory}`,
});
}
// step 2
let toVersion = options.to;
try {
toVersion = await getLatestVersion(options);
} catch (err) {
let message = "UNKNOWN_ERROR";
if (err instanceof Error) {
message = err.message;
}
return endMigration({
success: false,
message,
});
}
if (!toVersion) {
return endMigration({
success: false,
message: `Unable to fetch the latest version of turbo`,
});
}
if (fromVersion === toVersion) {
return endMigration({
success: true,
message: `Nothing to do, current version (${chalk.bold(
fromVersion
)}) is the same as the requested version (${chalk.bold(toVersion)})`,
});
}
// step 3
const codemods = getCodemodsForMigration({ fromVersion, toVersion });
if (codemods.length === 0) {
console.log(
`No codemods required to migrate from ${fromVersion} to ${toVersion}`,
os.EOL
);
}
// step 4
console.log(
`Upgrading turbo from ${chalk.bold(fromVersion)} to ${chalk.bold(
toVersion
)} (${
codemods.length === 0
? "no codemods required"
: `${codemods.length} required codemod${
codemods.length === 1 ? "" : "s"
}`
})`,
os.EOL
);
const results = codemods.map((codemod, idx) => {
console.log(
`(${idx + 1}/${codemods.length}) ${chalk.bold(
`Running ${codemod.value}`
)}`
);
const result = codemod.transformer({ root: selectedDirectory, options });
Runner.logResults(result);
return result;
});
const hasTransformError = results.some(
(result) =>
result.fatalError ||
Object.keys(result.changes).some((key) => result.changes[key].error)
);
if (hasTransformError) {
return endMigration({
success: false,
message: `Could not complete migration due to codemod errors. Please fix the errors and try again.`,
});
}
// step 5
const upgradeCommand = getTurboUpgradeCommand({
directory: selectedDirectory,
to: options.to,
});
if (!upgradeCommand) {
return endMigration({
success: false,
message: "Unable to determine upgrade command",
});
}
if (options.install) {
if (options.dry) {
console.log(
`Upgrading turbo with ${chalk.bold(upgradeCommand)} ${chalk.dim(
"(dry run)"
)}`,
os.EOL
);
} else {
console.log(`Upgrading turbo with ${chalk.bold(upgradeCommand)}`, os.EOL);
execSync(upgradeCommand, { cwd: selectedDirectory });
}
} else {
console.log(`Upgrade turbo with ${chalk.bold(upgradeCommand)}`, os.EOL);
}
endMigration({ success: true });
}
|