@turbo/codemod 1.7.1-canary.1 → 1.7.1-canary.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -1
- package/dist/cli.js +857 -0
- package/dist/transforms/add-package-manager.js +284 -102
- package/dist/transforms/create-turbo-config.js +262 -65
- package/dist/transforms/migrate-env-var-dependencies.js +265 -60
- package/dist/transforms/set-default-outputs.js +258 -65
- package/package.json +16 -11
- package/dist/getPackageManagerVersion.js +0 -42
- package/dist/getWorkspaceImplementation.js +0 -73
- package/dist/git.js +0 -56
- package/dist/index.js +0 -300
- package/dist/logger.js +0 -46
- package/dist/runTransform.js +0 -44
- package/dist/types.js +0 -18
package/dist/cli.js
ADDED
@@ -0,0 +1,857 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
"use strict";
|
3
|
+
var __create = Object.create;
|
4
|
+
var __defProp = Object.defineProperty;
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
11
|
+
for (let key of __getOwnPropNames(from))
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
14
|
+
}
|
15
|
+
return to;
|
16
|
+
};
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
18
|
+
|
19
|
+
// src/cli.ts
|
20
|
+
var import_chalk8 = __toESM(require("chalk"));
|
21
|
+
var import_os4 = __toESM(require("os"));
|
22
|
+
var import_commander = require("commander");
|
23
|
+
|
24
|
+
// src/commands/migrate/index.ts
|
25
|
+
var import_chalk5 = __toESM(require("chalk"));
|
26
|
+
var import_os3 = __toESM(require("os"));
|
27
|
+
var import_inquirer = __toESM(require("inquirer"));
|
28
|
+
var import_child_process3 = require("child_process");
|
29
|
+
|
30
|
+
// src/commands/migrate/steps/getCurrentVersion.ts
|
31
|
+
var import_path2 = __toESM(require("path"));
|
32
|
+
var import_fs_extra = require("fs-extra");
|
33
|
+
|
34
|
+
// src/utils/getPackageManager.ts
|
35
|
+
var import_find_up = __toESM(require("find-up"));
|
36
|
+
var import_path = __toESM(require("path"));
|
37
|
+
var cache = {};
|
38
|
+
function getPackageManager({
|
39
|
+
directory
|
40
|
+
}) {
|
41
|
+
const cwd = directory || process.cwd();
|
42
|
+
if (cache[cwd]) {
|
43
|
+
return cache[cwd];
|
44
|
+
}
|
45
|
+
const lockFile = import_find_up.default.sync(["yarn.lock", "pnpm-lock.yaml", "package-lock.json"], {
|
46
|
+
cwd
|
47
|
+
});
|
48
|
+
if (!lockFile) {
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
switch (import_path.default.basename(lockFile)) {
|
52
|
+
case "yarn.lock":
|
53
|
+
cache[cwd] = "yarn";
|
54
|
+
break;
|
55
|
+
case "pnpm-lock.yaml":
|
56
|
+
cache[cwd] = "pnpm";
|
57
|
+
break;
|
58
|
+
case "package-lock.json":
|
59
|
+
cache[cwd] = "npm";
|
60
|
+
break;
|
61
|
+
}
|
62
|
+
return cache[cwd];
|
63
|
+
}
|
64
|
+
|
65
|
+
// src/commands/migrate/utils.ts
|
66
|
+
var import_child_process = require("child_process");
|
67
|
+
function exec(command, opts, fallback) {
|
68
|
+
try {
|
69
|
+
const rawResult = (0, import_child_process.execSync)(command, opts);
|
70
|
+
return rawResult.toString("utf8").trim();
|
71
|
+
} catch (err) {
|
72
|
+
return fallback || void 0;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
// src/commands/migrate/steps/getCurrentVersion.ts
|
77
|
+
function getCurrentVersion(directory, opts) {
|
78
|
+
const { from } = opts;
|
79
|
+
if (from) {
|
80
|
+
return from;
|
81
|
+
}
|
82
|
+
const turboVersionFromGlobal = exec(`turbo --version`, { cwd: directory });
|
83
|
+
if (turboVersionFromGlobal) {
|
84
|
+
return turboVersionFromGlobal;
|
85
|
+
}
|
86
|
+
const packageManager = getPackageManager({ directory });
|
87
|
+
if (packageManager) {
|
88
|
+
if (packageManager === "yarn") {
|
89
|
+
return exec(`yarn turbo --version`, { cwd: directory });
|
90
|
+
}
|
91
|
+
if (packageManager === "pnpm") {
|
92
|
+
return exec(`pnpm turbo --version`, { cwd: directory });
|
93
|
+
} else {
|
94
|
+
const turboBin = import_path2.default.join(directory, "node_modules", ".bin", "turbo");
|
95
|
+
if ((0, import_fs_extra.existsSync)(turboBin)) {
|
96
|
+
return exec(`${turboBin} --version`, { cwd: directory });
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
return void 0;
|
101
|
+
}
|
102
|
+
var getCurrentVersion_default = getCurrentVersion;
|
103
|
+
|
104
|
+
// src/commands/migrate/steps/getLatestVersion.ts
|
105
|
+
var import_axios = __toESM(require("axios"));
|
106
|
+
var REGISTRY = "https://registry.npmjs.org";
|
107
|
+
async function getPackageDetails({ packageName }) {
|
108
|
+
try {
|
109
|
+
const result = await import_axios.default.get(`${REGISTRY}/${packageName}`);
|
110
|
+
return result.data;
|
111
|
+
} catch (err) {
|
112
|
+
throw new Error(`Unable to fetch the latest version of ${packageName}`);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
async function getLatestVersion({
|
116
|
+
to
|
117
|
+
}) {
|
118
|
+
const packageDetails = await getPackageDetails({ packageName: "turbo" });
|
119
|
+
const versions = packageDetails["dist-tags"];
|
120
|
+
if (to) {
|
121
|
+
if (packageDetails[to]) {
|
122
|
+
return to;
|
123
|
+
} else {
|
124
|
+
throw new Error(`turbo@${to} does not exist`);
|
125
|
+
}
|
126
|
+
}
|
127
|
+
return versions.latest;
|
128
|
+
}
|
129
|
+
|
130
|
+
// src/commands/migrate/steps/getTransformsForMigration.ts
|
131
|
+
var import_semver = require("semver");
|
132
|
+
|
133
|
+
// src/utils/loadTransformers.ts
|
134
|
+
var import_path3 = __toESM(require("path"));
|
135
|
+
var import_fs_extra2 = __toESM(require("fs-extra"));
|
136
|
+
var transformerDirectory = process.env.NODE_ENV === "test" ? import_path3.default.join(__dirname, "../transforms") : import_path3.default.join(__dirname, "./transforms");
|
137
|
+
function loadTransformers() {
|
138
|
+
const transformerFiles = import_fs_extra2.default.readdirSync(transformerDirectory);
|
139
|
+
return transformerFiles.map((transformerFilename) => {
|
140
|
+
const transformerPath = import_path3.default.join(transformerDirectory, transformerFilename);
|
141
|
+
try {
|
142
|
+
return require(transformerPath).default;
|
143
|
+
} catch (e) {
|
144
|
+
return void 0;
|
145
|
+
}
|
146
|
+
}).filter(Boolean);
|
147
|
+
}
|
148
|
+
|
149
|
+
// src/commands/migrate/steps/getTransformsForMigration.ts
|
150
|
+
function getTransformsForMigration({
|
151
|
+
fromVersion,
|
152
|
+
toVersion
|
153
|
+
}) {
|
154
|
+
const transforms2 = loadTransformers();
|
155
|
+
return transforms2.filter((transformer) => {
|
156
|
+
return (0, import_semver.gt)(transformer.introducedIn, fromVersion) && (0, import_semver.lte)(transformer.introducedIn, toVersion);
|
157
|
+
});
|
158
|
+
}
|
159
|
+
var getTransformsForMigration_default = getTransformsForMigration;
|
160
|
+
|
161
|
+
// src/utils/checkGitStatus.ts
|
162
|
+
var import_chalk = __toESM(require("chalk"));
|
163
|
+
var import_is_git_clean = __toESM(require("is-git-clean"));
|
164
|
+
function checkGitStatus({
|
165
|
+
directory,
|
166
|
+
force
|
167
|
+
}) {
|
168
|
+
let clean = false;
|
169
|
+
let errorMessage = "Unable to determine if git directory is clean";
|
170
|
+
try {
|
171
|
+
clean = import_is_git_clean.default.sync(directory || process.cwd());
|
172
|
+
errorMessage = "Git directory is not clean";
|
173
|
+
} catch (err) {
|
174
|
+
if (err && err.stderr && err.stderr.indexOf("not a git repository") >= 0) {
|
175
|
+
clean = true;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
if (!clean) {
|
179
|
+
if (force) {
|
180
|
+
console.log(`${import_chalk.default.yellow("WARNING")}: ${errorMessage}. Forcibly continuing...`);
|
181
|
+
} else {
|
182
|
+
console.log("Thank you for using @turbo/codemod!");
|
183
|
+
console.log(import_chalk.default.yellow("\nBut before we continue, please stash or commit your git changes."));
|
184
|
+
console.log("\nYou may use the --force flag to override this safety check.");
|
185
|
+
process.exit(1);
|
186
|
+
}
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
// src/utils/directoryInfo.ts
|
191
|
+
var import_path4 = __toESM(require("path"));
|
192
|
+
var import_fs = __toESM(require("fs"));
|
193
|
+
function directoryInfo({ directory }) {
|
194
|
+
const dir = import_path4.default.isAbsolute(directory) ? directory : import_path4.default.join(process.cwd(), directory);
|
195
|
+
return { exists: import_fs.default.existsSync(dir), absolute: dir };
|
196
|
+
}
|
197
|
+
|
198
|
+
// src/commands/migrate/steps/getTurboUpgradeCommand.ts
|
199
|
+
var import_os = __toESM(require("os"));
|
200
|
+
var import_path5 = __toESM(require("path"));
|
201
|
+
var import_fs_extra3 = __toESM(require("fs-extra"));
|
202
|
+
var import_semver2 = require("semver");
|
203
|
+
|
204
|
+
// src/utils/getPackageManagerVersion.ts
|
205
|
+
var import_child_process2 = require("child_process");
|
206
|
+
function getPackageManagerVersion(packageManager, root) {
|
207
|
+
switch (packageManager) {
|
208
|
+
case "yarn":
|
209
|
+
return (0, import_child_process2.execSync)("yarn --version", { cwd: root }).toString().trim();
|
210
|
+
case "pnpm":
|
211
|
+
return (0, import_child_process2.execSync)("pnpm --version", { cwd: root }).toString().trim();
|
212
|
+
case "npm":
|
213
|
+
return (0, import_child_process2.execSync)("npm --version", { cwd: root }).toString().trim();
|
214
|
+
}
|
215
|
+
}
|
216
|
+
|
217
|
+
// src/commands/migrate/steps/getTurboUpgradeCommand.ts
|
218
|
+
function getGlobalBinaryPaths() {
|
219
|
+
return {
|
220
|
+
yarn: exec(`yarn global bin`, { cwd: import_os.default.homedir() }),
|
221
|
+
npm: exec(`npm bin --global`, { cwd: import_os.default.homedir() }),
|
222
|
+
pnpm: exec(`pnpm bin --global`, { cwd: import_os.default.homedir() })
|
223
|
+
};
|
224
|
+
}
|
225
|
+
function getGlobalUpgradeCommand(packageManager, to = "latest") {
|
226
|
+
switch (packageManager) {
|
227
|
+
case "yarn":
|
228
|
+
return `yarn global add turbo@${to}`;
|
229
|
+
case "npm":
|
230
|
+
return `npm install turbo@${to} --global`;
|
231
|
+
case "pnpm":
|
232
|
+
return `pnpm install turbo@${to} --global`;
|
233
|
+
}
|
234
|
+
}
|
235
|
+
function getLocalUpgradeCommand({
|
236
|
+
packageManager,
|
237
|
+
packageManagerVersion,
|
238
|
+
installType,
|
239
|
+
isUsingWorkspaces,
|
240
|
+
to = "latest"
|
241
|
+
}) {
|
242
|
+
const renderCommand = (command) => command.filter(Boolean).join(" ");
|
243
|
+
switch (packageManager) {
|
244
|
+
case "yarn":
|
245
|
+
if ((0, import_semver2.gte)(packageManagerVersion, "2.0.0")) {
|
246
|
+
return renderCommand([
|
247
|
+
"yarn",
|
248
|
+
"add",
|
249
|
+
`turbo@${to}`,
|
250
|
+
installType === "devDependencies" && "--dev"
|
251
|
+
]);
|
252
|
+
} else {
|
253
|
+
return renderCommand([
|
254
|
+
"yarn",
|
255
|
+
"add",
|
256
|
+
`turbo@${to}`,
|
257
|
+
installType === "devDependencies" && "--dev",
|
258
|
+
isUsingWorkspaces && "-W"
|
259
|
+
]);
|
260
|
+
}
|
261
|
+
case "npm":
|
262
|
+
return renderCommand([
|
263
|
+
"npm",
|
264
|
+
"install",
|
265
|
+
`turbo@${to}`,
|
266
|
+
installType === "devDependencies" && "--save-dev"
|
267
|
+
]);
|
268
|
+
case "pnpm":
|
269
|
+
return renderCommand([
|
270
|
+
"pnpm",
|
271
|
+
"install",
|
272
|
+
`turbo@${to}`,
|
273
|
+
installType === "devDependencies" && "--save-dev",
|
274
|
+
isUsingWorkspaces && "-w"
|
275
|
+
]);
|
276
|
+
}
|
277
|
+
}
|
278
|
+
function getInstallType({ directory }) {
|
279
|
+
const packageJsonPath = import_path5.default.join(directory, "package.json");
|
280
|
+
const pnpmWorkspaceConfig = import_path5.default.join(directory, "pnpm-workspace.yaml");
|
281
|
+
const isPnpmWorkspaces = import_fs_extra3.default.existsSync(pnpmWorkspaceConfig);
|
282
|
+
if (!import_fs_extra3.default.existsSync(packageJsonPath)) {
|
283
|
+
console.error(`Unable to find package.json at ${packageJsonPath}`);
|
284
|
+
return { installType: void 0, isUsingWorkspaces: void 0 };
|
285
|
+
}
|
286
|
+
const packageJson = import_fs_extra3.default.readJsonSync(packageJsonPath);
|
287
|
+
const isDevDependency = packageJson.devDependencies && "turbo" in packageJson.devDependencies;
|
288
|
+
const isDependency = packageJson.dependencies && "turbo" in packageJson.dependencies;
|
289
|
+
let isUsingWorkspaces = "workspaces" in packageJson || isPnpmWorkspaces;
|
290
|
+
if (isDependency || isDevDependency) {
|
291
|
+
return {
|
292
|
+
installType: isDependency ? "dependencies" : "devDependencies",
|
293
|
+
isUsingWorkspaces
|
294
|
+
};
|
295
|
+
}
|
296
|
+
return {
|
297
|
+
installType: void 0,
|
298
|
+
isUsingWorkspaces
|
299
|
+
};
|
300
|
+
}
|
301
|
+
function getTurboUpgradeCommand({
|
302
|
+
directory,
|
303
|
+
to
|
304
|
+
}) {
|
305
|
+
const turboBinaryPathFromGlobal = exec(`turbo bin`, {
|
306
|
+
cwd: directory,
|
307
|
+
stdio: "pipe"
|
308
|
+
});
|
309
|
+
const packageManagerGlobalBinaryPaths = getGlobalBinaryPaths();
|
310
|
+
const globalPackageManager = Object.keys(packageManagerGlobalBinaryPaths).find((packageManager) => {
|
311
|
+
const packageManagerBinPath = packageManagerGlobalBinaryPaths[packageManager];
|
312
|
+
if (packageManagerBinPath && turboBinaryPathFromGlobal) {
|
313
|
+
return turboBinaryPathFromGlobal.includes(packageManagerBinPath);
|
314
|
+
}
|
315
|
+
return false;
|
316
|
+
});
|
317
|
+
if (turboBinaryPathFromGlobal && globalPackageManager) {
|
318
|
+
return getGlobalUpgradeCommand(globalPackageManager, to);
|
319
|
+
} else {
|
320
|
+
const packageManager = getPackageManager({ directory });
|
321
|
+
const { installType, isUsingWorkspaces } = getInstallType({ directory });
|
322
|
+
if (packageManager && installType) {
|
323
|
+
const packageManagerVersion = getPackageManagerVersion(packageManager, directory);
|
324
|
+
return getLocalUpgradeCommand({
|
325
|
+
packageManager,
|
326
|
+
packageManagerVersion,
|
327
|
+
installType,
|
328
|
+
isUsingWorkspaces,
|
329
|
+
to
|
330
|
+
});
|
331
|
+
}
|
332
|
+
}
|
333
|
+
return void 0;
|
334
|
+
}
|
335
|
+
|
336
|
+
// src/runner/Runner.ts
|
337
|
+
var import_chalk4 = __toESM(require("chalk"));
|
338
|
+
|
339
|
+
// src/runner/FileTransform.ts
|
340
|
+
var import_chalk2 = __toESM(require("chalk"));
|
341
|
+
var import_diff = require("diff");
|
342
|
+
var import_fs_extra4 = __toESM(require("fs-extra"));
|
343
|
+
var import_os2 = __toESM(require("os"));
|
344
|
+
var import_path6 = __toESM(require("path"));
|
345
|
+
var FileTransform = class {
|
346
|
+
constructor(args) {
|
347
|
+
this.changes = [];
|
348
|
+
this.filePath = args.filePath;
|
349
|
+
this.rootPath = args.rootPath;
|
350
|
+
this.after = args.after;
|
351
|
+
this.error = args.error;
|
352
|
+
if (args.before === void 0) {
|
353
|
+
try {
|
354
|
+
if (import_path6.default.extname(args.filePath) === ".json") {
|
355
|
+
this.before = import_fs_extra4.default.readJsonSync(args.filePath);
|
356
|
+
} else {
|
357
|
+
this.before = import_fs_extra4.default.readFileSync(args.filePath);
|
358
|
+
}
|
359
|
+
} catch (err) {
|
360
|
+
this.before = "";
|
361
|
+
}
|
362
|
+
} else if (args.before === null) {
|
363
|
+
this.before = "";
|
364
|
+
} else {
|
365
|
+
this.before = args.before;
|
366
|
+
}
|
367
|
+
if (args.after) {
|
368
|
+
if (typeof this.before === "object" || typeof args.after === "object") {
|
369
|
+
this.changes = (0, import_diff.diffJson)(this.before, args.after);
|
370
|
+
} else {
|
371
|
+
this.changes = (0, import_diff.diffLines)(this.before, args.after);
|
372
|
+
}
|
373
|
+
} else {
|
374
|
+
this.changes = [];
|
375
|
+
}
|
376
|
+
}
|
377
|
+
fileName() {
|
378
|
+
return import_path6.default.relative(this.rootPath, this.filePath);
|
379
|
+
}
|
380
|
+
write() {
|
381
|
+
if (this.after) {
|
382
|
+
if (typeof this.after === "object") {
|
383
|
+
import_fs_extra4.default.writeJsonSync(this.filePath, this.after, { spaces: 2 });
|
384
|
+
} else {
|
385
|
+
import_fs_extra4.default.writeFileSync(this.filePath, this.after);
|
386
|
+
}
|
387
|
+
}
|
388
|
+
}
|
389
|
+
additions() {
|
390
|
+
return this.changes.filter((c) => c.added).length;
|
391
|
+
}
|
392
|
+
deletions() {
|
393
|
+
return this.changes.filter((c) => c.removed).length;
|
394
|
+
}
|
395
|
+
hasChanges() {
|
396
|
+
return this.additions() > 0 || this.deletions() > 0;
|
397
|
+
}
|
398
|
+
log(args) {
|
399
|
+
if (args.diff) {
|
400
|
+
this.changes.forEach((part) => {
|
401
|
+
if (part.added) {
|
402
|
+
process.stdout.write(import_chalk2.default.green(part.value));
|
403
|
+
} else if (part.removed) {
|
404
|
+
process.stdout.write(import_chalk2.default.red(part.value));
|
405
|
+
} else {
|
406
|
+
process.stdout.write(import_chalk2.default.dim(part.value));
|
407
|
+
}
|
408
|
+
});
|
409
|
+
console.log(import_os2.default.EOL);
|
410
|
+
} else {
|
411
|
+
console.log(this.after);
|
412
|
+
}
|
413
|
+
}
|
414
|
+
};
|
415
|
+
|
416
|
+
// src/utils/logger.ts
|
417
|
+
var import_chalk3 = __toESM(require("chalk"));
|
418
|
+
var Logger = class {
|
419
|
+
constructor(args) {
|
420
|
+
this.transform = args.transformer;
|
421
|
+
this.dry = args.dry;
|
422
|
+
}
|
423
|
+
modified(...args) {
|
424
|
+
console.log(import_chalk3.default.green(` MODIFIED `), ...args, this.dry ? import_chalk3.default.dim(`(dry run)`) : "");
|
425
|
+
}
|
426
|
+
unchanged(...args) {
|
427
|
+
console.log(import_chalk3.default.gray(` UNCHANGED `), ...args, this.dry ? import_chalk3.default.dim(`(dry run)`) : "");
|
428
|
+
}
|
429
|
+
skipped(...args) {
|
430
|
+
console.log(import_chalk3.default.yellow(` SKIPPED `), ...args, this.dry ? import_chalk3.default.dim(`(dry run)`) : "");
|
431
|
+
}
|
432
|
+
error(...args) {
|
433
|
+
console.log(import_chalk3.default.red(` ERROR `), ...args, this.dry ? import_chalk3.default.dim(`(dry run)`) : "");
|
434
|
+
}
|
435
|
+
info(...args) {
|
436
|
+
console.log(import_chalk3.default.bold(` INFO `), ...args, this.dry ? import_chalk3.default.dim(`(dry run)`) : "");
|
437
|
+
}
|
438
|
+
};
|
439
|
+
|
440
|
+
// src/runner/Runner.ts
|
441
|
+
var Runner = class {
|
442
|
+
constructor(options) {
|
443
|
+
this.modifications = {};
|
444
|
+
this.transform = options.transformer;
|
445
|
+
this.rootPath = options.rootPath;
|
446
|
+
this.dry = options.dry;
|
447
|
+
this.print = options.print;
|
448
|
+
this.logger = new Logger(options);
|
449
|
+
}
|
450
|
+
abortTransform(args) {
|
451
|
+
this.logger.error(args.reason);
|
452
|
+
return {
|
453
|
+
fatalError: new Error(args.reason),
|
454
|
+
changes: args.changes || {}
|
455
|
+
};
|
456
|
+
}
|
457
|
+
modifyFile(args) {
|
458
|
+
this.modifications[args.filePath] = new FileTransform({
|
459
|
+
rootPath: this.rootPath,
|
460
|
+
...args
|
461
|
+
});
|
462
|
+
}
|
463
|
+
finish() {
|
464
|
+
const results = { changes: {} };
|
465
|
+
Object.keys(this.modifications).forEach((filePath) => {
|
466
|
+
const mod = this.modifications[filePath];
|
467
|
+
const result = {
|
468
|
+
action: "unchanged",
|
469
|
+
additions: mod.additions(),
|
470
|
+
deletions: mod.deletions()
|
471
|
+
};
|
472
|
+
if (mod.hasChanges()) {
|
473
|
+
if (this.dry) {
|
474
|
+
result.action = "skipped";
|
475
|
+
this.logger.skipped(import_chalk4.default.dim(mod.fileName()));
|
476
|
+
} else {
|
477
|
+
try {
|
478
|
+
mod.write();
|
479
|
+
result.action = "modified";
|
480
|
+
this.logger.modified(import_chalk4.default.bold(mod.fileName()));
|
481
|
+
} catch (err) {
|
482
|
+
let message = "Unknown error";
|
483
|
+
if (err instanceof Error) {
|
484
|
+
message = err.message;
|
485
|
+
}
|
486
|
+
result.error = new Error(message);
|
487
|
+
result.action = "error";
|
488
|
+
this.logger.error(mod.fileName(), message);
|
489
|
+
}
|
490
|
+
}
|
491
|
+
if (this.print) {
|
492
|
+
mod.log({ diff: true });
|
493
|
+
}
|
494
|
+
} else {
|
495
|
+
this.logger.unchanged(import_chalk4.default.dim(mod.fileName()));
|
496
|
+
}
|
497
|
+
results.changes[mod.fileName()] = result;
|
498
|
+
});
|
499
|
+
const encounteredError = Object.keys(results.changes).some((fileName) => {
|
500
|
+
return results.changes[fileName].action === "error";
|
501
|
+
});
|
502
|
+
if (encounteredError) {
|
503
|
+
return this.abortTransform({
|
504
|
+
reason: "Encountered an error while transforming files",
|
505
|
+
changes: results.changes
|
506
|
+
});
|
507
|
+
}
|
508
|
+
return results;
|
509
|
+
}
|
510
|
+
static logResults(results) {
|
511
|
+
const changedFiles = Object.keys(results.changes);
|
512
|
+
console.log();
|
513
|
+
if (changedFiles.length > 0) {
|
514
|
+
console.log(import_chalk4.default.bold(`Results:`));
|
515
|
+
const table = {};
|
516
|
+
changedFiles.forEach((fileName) => {
|
517
|
+
var _a;
|
518
|
+
const fileChanges = results.changes[fileName];
|
519
|
+
table[fileName] = {
|
520
|
+
action: fileChanges.action,
|
521
|
+
additions: fileChanges.additions,
|
522
|
+
deletions: fileChanges.deletions,
|
523
|
+
error: ((_a = fileChanges.error) == null ? void 0 : _a.message) || "None"
|
524
|
+
};
|
525
|
+
});
|
526
|
+
console.table(table);
|
527
|
+
console.log();
|
528
|
+
}
|
529
|
+
}
|
530
|
+
};
|
531
|
+
var Runner_default = Runner;
|
532
|
+
|
533
|
+
// src/utils/looksLikeRepo.ts
|
534
|
+
var import_path7 = __toESM(require("path"));
|
535
|
+
var import_fs_extra5 = require("fs-extra");
|
536
|
+
var HINTS = ["package.json", "turbo.json", ".git"];
|
537
|
+
function looksLikeRepo({
|
538
|
+
directory
|
539
|
+
}) {
|
540
|
+
return HINTS.some((hint) => (0, import_fs_extra5.existsSync)(import_path7.default.join(directory, hint)));
|
541
|
+
}
|
542
|
+
|
543
|
+
// src/commands/migrate/index.ts
|
544
|
+
function endMigration({
|
545
|
+
message,
|
546
|
+
success
|
547
|
+
}) {
|
548
|
+
if (success) {
|
549
|
+
console.log(import_chalk5.default.bold(import_chalk5.default.green("Migration completed")));
|
550
|
+
if (message) {
|
551
|
+
console.log(message);
|
552
|
+
}
|
553
|
+
return process.exit(0);
|
554
|
+
}
|
555
|
+
console.log(import_chalk5.default.bold(import_chalk5.default.red("Migration failed")));
|
556
|
+
if (message) {
|
557
|
+
console.log(message);
|
558
|
+
}
|
559
|
+
return process.exit(1);
|
560
|
+
}
|
561
|
+
async function migrate(directory, options) {
|
562
|
+
if (!options.dry) {
|
563
|
+
checkGitStatus({ directory, force: options.force });
|
564
|
+
}
|
565
|
+
const answers = await import_inquirer.default.prompt([
|
566
|
+
{
|
567
|
+
type: "input",
|
568
|
+
name: "directoryInput",
|
569
|
+
message: "Where is the root of the repo to migrate?",
|
570
|
+
when: !directory,
|
571
|
+
default: ".",
|
572
|
+
validate: (directory2) => {
|
573
|
+
const { exists: exists2, absolute } = directoryInfo({ directory: directory2 });
|
574
|
+
if (exists2) {
|
575
|
+
return true;
|
576
|
+
} else {
|
577
|
+
return `Directory ${import_chalk5.default.dim(`(${absolute})`)} does not exist`;
|
578
|
+
}
|
579
|
+
},
|
580
|
+
filter: (directory2) => directory2.trim()
|
581
|
+
}
|
582
|
+
]);
|
583
|
+
const { directoryInput: selectedDirectory = directory } = answers;
|
584
|
+
const { exists, absolute: root } = directoryInfo({
|
585
|
+
directory: selectedDirectory
|
586
|
+
});
|
587
|
+
if (!exists) {
|
588
|
+
return endMigration({
|
589
|
+
success: false,
|
590
|
+
message: `Directory ${import_chalk5.default.dim(`(${root})`)} does not exist`
|
591
|
+
});
|
592
|
+
}
|
593
|
+
if (!looksLikeRepo({ directory: root })) {
|
594
|
+
return endMigration({
|
595
|
+
success: false,
|
596
|
+
message: `Directory (${import_chalk5.default.dim(root)}) does not appear to be a repository`
|
597
|
+
});
|
598
|
+
}
|
599
|
+
const fromVersion = getCurrentVersion_default(selectedDirectory, options);
|
600
|
+
if (!fromVersion) {
|
601
|
+
return endMigration({
|
602
|
+
success: false,
|
603
|
+
message: `Unable to infer the version of turbo being used by ${directory}`
|
604
|
+
});
|
605
|
+
}
|
606
|
+
let toVersion = options.to;
|
607
|
+
try {
|
608
|
+
toVersion = await getLatestVersion(options);
|
609
|
+
} catch (err) {
|
610
|
+
let message = "UNKNOWN_ERROR";
|
611
|
+
if (err instanceof Error) {
|
612
|
+
message = err.message;
|
613
|
+
}
|
614
|
+
return endMigration({
|
615
|
+
success: false,
|
616
|
+
message
|
617
|
+
});
|
618
|
+
}
|
619
|
+
if (!toVersion) {
|
620
|
+
return endMigration({
|
621
|
+
success: false,
|
622
|
+
message: `Unable to fetch the latest version of turbo`
|
623
|
+
});
|
624
|
+
}
|
625
|
+
if (fromVersion === toVersion) {
|
626
|
+
return endMigration({
|
627
|
+
success: true,
|
628
|
+
message: `Nothing to do, current version (${import_chalk5.default.bold(fromVersion)}) is the same as the requested version (${import_chalk5.default.bold(toVersion)})`
|
629
|
+
});
|
630
|
+
}
|
631
|
+
const codemods = getTransformsForMigration_default({ fromVersion, toVersion });
|
632
|
+
if (codemods.length === 0) {
|
633
|
+
console.log(`No codemods required to migrate from ${fromVersion} to ${toVersion}`, import_os3.default.EOL);
|
634
|
+
}
|
635
|
+
console.log(`Upgrading turbo from ${import_chalk5.default.bold(fromVersion)} to ${import_chalk5.default.bold(toVersion)} (${codemods.length === 0 ? "no codemods required" : `${codemods.length} required codemod${codemods.length === 1 ? "" : "s"}`})`, import_os3.default.EOL);
|
636
|
+
const results = codemods.map((codemod, idx) => {
|
637
|
+
console.log(`(${idx + 1}/${codemods.length}) ${import_chalk5.default.bold(`Running ${codemod.value}`)}`);
|
638
|
+
const result = codemod.transformer({ root: selectedDirectory, options });
|
639
|
+
Runner_default.logResults(result);
|
640
|
+
return result;
|
641
|
+
});
|
642
|
+
const hasTransformError = results.some((result) => result.fatalError || Object.keys(result.changes).some((key) => result.changes[key].error));
|
643
|
+
if (hasTransformError) {
|
644
|
+
return endMigration({
|
645
|
+
success: false,
|
646
|
+
message: `Could not complete migration due to codemod errors. Please fix the errors and try again.`
|
647
|
+
});
|
648
|
+
}
|
649
|
+
const upgradeCommand = getTurboUpgradeCommand({
|
650
|
+
directory: selectedDirectory,
|
651
|
+
to: options.to
|
652
|
+
});
|
653
|
+
if (!upgradeCommand) {
|
654
|
+
return endMigration({
|
655
|
+
success: false,
|
656
|
+
message: "Unable to determine upgrade command"
|
657
|
+
});
|
658
|
+
}
|
659
|
+
if (options.install) {
|
660
|
+
if (options.dry) {
|
661
|
+
console.log(`Upgrading turbo with ${import_chalk5.default.bold(upgradeCommand)} ${import_chalk5.default.dim("(dry run)")}`, import_os3.default.EOL);
|
662
|
+
} else {
|
663
|
+
console.log(`Upgrading turbo with ${import_chalk5.default.bold(upgradeCommand)}`, import_os3.default.EOL);
|
664
|
+
(0, import_child_process3.execSync)(upgradeCommand, { cwd: selectedDirectory });
|
665
|
+
}
|
666
|
+
} else {
|
667
|
+
console.log(`Upgrade turbo with ${import_chalk5.default.bold(upgradeCommand)}`, import_os3.default.EOL);
|
668
|
+
}
|
669
|
+
endMigration({ success: true });
|
670
|
+
}
|
671
|
+
|
672
|
+
// src/commands/transform/index.ts
|
673
|
+
var import_chalk6 = __toESM(require("chalk"));
|
674
|
+
var import_inquirer2 = __toESM(require("inquirer"));
|
675
|
+
async function transform(transform2, directory, options) {
|
676
|
+
const transforms2 = loadTransformers();
|
677
|
+
if (options.list) {
|
678
|
+
console.log(transforms2.map((transform3) => `- ${import_chalk6.default.cyan(transform3.value)}`).join("\n"));
|
679
|
+
return process.exit(0);
|
680
|
+
}
|
681
|
+
if (!options.dry) {
|
682
|
+
checkGitStatus({ directory, force: options.force });
|
683
|
+
}
|
684
|
+
const answers = await import_inquirer2.default.prompt([
|
685
|
+
{
|
686
|
+
type: "input",
|
687
|
+
name: "directoryInput",
|
688
|
+
message: "Where is the root of the repo where the transform should run?",
|
689
|
+
when: !directory,
|
690
|
+
default: ".",
|
691
|
+
validate: (directory2) => {
|
692
|
+
const { exists: exists2, absolute } = directoryInfo({ directory: directory2 });
|
693
|
+
if (exists2) {
|
694
|
+
return true;
|
695
|
+
} else {
|
696
|
+
return `Directory ${import_chalk6.default.dim(`(${absolute})`)} does not exist`;
|
697
|
+
}
|
698
|
+
},
|
699
|
+
filter: (directory2) => directory2.trim()
|
700
|
+
},
|
701
|
+
{
|
702
|
+
type: "list",
|
703
|
+
name: "transformerInput",
|
704
|
+
message: "Which transform would you like to apply?",
|
705
|
+
when: !transform2,
|
706
|
+
pageSize: transforms2.length,
|
707
|
+
choices: transforms2
|
708
|
+
}
|
709
|
+
]);
|
710
|
+
const {
|
711
|
+
directoryInput: selectedDirectory = directory,
|
712
|
+
transformerInput: selectedTransformer = transform2
|
713
|
+
} = answers;
|
714
|
+
const { exists, absolute: root } = directoryInfo({
|
715
|
+
directory: selectedDirectory
|
716
|
+
});
|
717
|
+
if (!exists) {
|
718
|
+
console.error(`Directory ${import_chalk6.default.dim(`(${root})`)} does not exist`);
|
719
|
+
return process.exit(1);
|
720
|
+
}
|
721
|
+
const transformKeys = transforms2.map((transform3) => transform3.value);
|
722
|
+
const transformData = transforms2.find((transform3) => transform3.value === selectedTransformer);
|
723
|
+
if (!transformData) {
|
724
|
+
console.error(`Invalid transform choice ${import_chalk6.default.dim(`(${transform2})`)}, pick one of:`);
|
725
|
+
console.error(transformKeys.map((key) => `- ${key}`).join("\n"));
|
726
|
+
return process.exit(1);
|
727
|
+
}
|
728
|
+
const result = transformData.transformer({
|
729
|
+
root,
|
730
|
+
options
|
731
|
+
});
|
732
|
+
if (result.fatalError) {
|
733
|
+
return process.exit(1);
|
734
|
+
}
|
735
|
+
Runner_default.logResults(result);
|
736
|
+
}
|
737
|
+
|
738
|
+
// src/utils/notifyUpdate.ts
|
739
|
+
var import_chalk7 = __toESM(require("chalk"));
|
740
|
+
var import_update_check = __toESM(require("update-check"));
|
741
|
+
|
742
|
+
// package.json
|
743
|
+
var package_default = {
|
744
|
+
name: "@turbo/codemod",
|
745
|
+
version: "1.7.1-canary.2",
|
746
|
+
description: "Provides Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.",
|
747
|
+
homepage: "https://turbo.build/repo",
|
748
|
+
license: "MPL-2.0",
|
749
|
+
repository: {
|
750
|
+
type: "git",
|
751
|
+
url: "https://github.com/vercel/turbo",
|
752
|
+
directory: "packages/turbo-codemod"
|
753
|
+
},
|
754
|
+
bugs: {
|
755
|
+
url: "https://github.com/vercel/turbo/issues"
|
756
|
+
},
|
757
|
+
bin: "dist/index.js",
|
758
|
+
scripts: {
|
759
|
+
build: "tsup",
|
760
|
+
test: "jest",
|
761
|
+
lint: "eslint src/**/*.ts",
|
762
|
+
"check-types": "tsc --noEmit",
|
763
|
+
"add-transformer": "plop"
|
764
|
+
},
|
765
|
+
dependencies: {
|
766
|
+
axios: "0.27.2",
|
767
|
+
chalk: "2.4.2",
|
768
|
+
commander: "^9.5.0",
|
769
|
+
diff: "^5.1.0",
|
770
|
+
"find-up": "4.1.0",
|
771
|
+
"fs-extra": "^10.0.0",
|
772
|
+
"gradient-string": "^2.0.0",
|
773
|
+
inquirer: "^8.2.4",
|
774
|
+
"inquirer-file-tree-selection-prompt": "^1.0.19",
|
775
|
+
"is-git-clean": "^1.1.0",
|
776
|
+
ora: "4.1.1",
|
777
|
+
semver: "^7.3.7",
|
778
|
+
"turbo-utils": "workspace:*",
|
779
|
+
"update-check": "^1.5.4"
|
780
|
+
},
|
781
|
+
devDependencies: {
|
782
|
+
"@types/chalk-animation": "^1.6.0",
|
783
|
+
"@types/diff": "^5.0.2",
|
784
|
+
"@types/fs-extra": "^9.0.13",
|
785
|
+
"@types/gradient-string": "^1.1.2",
|
786
|
+
"@types/inquirer": "^8.2.0",
|
787
|
+
"@types/jest": "^27.4.0",
|
788
|
+
"@types/node": "^16.11.12",
|
789
|
+
"@types/semver": "^7.3.9",
|
790
|
+
"@types/uuid": "^9.0.0",
|
791
|
+
deepmerge: "^4.2.2",
|
792
|
+
eslint: "^7.23.0",
|
793
|
+
jest: "^27.4.3",
|
794
|
+
plop: "^3.1.1",
|
795
|
+
semver: "^7.3.5",
|
796
|
+
"ts-jest": "^27.1.1",
|
797
|
+
tsconfig: "workspace:*",
|
798
|
+
tsup: "^5.10.3",
|
799
|
+
"turbo-types": "workspace:*",
|
800
|
+
typescript: "^4.5.5",
|
801
|
+
uuid: "^9.0.0"
|
802
|
+
},
|
803
|
+
files: [
|
804
|
+
"dist"
|
805
|
+
],
|
806
|
+
publishConfig: {
|
807
|
+
access: "public"
|
808
|
+
}
|
809
|
+
};
|
810
|
+
|
811
|
+
// src/utils/notifyUpdate.ts
|
812
|
+
var update = (0, import_update_check.default)(package_default).catch(() => null);
|
813
|
+
async function notifyUpdate() {
|
814
|
+
try {
|
815
|
+
const res = await update;
|
816
|
+
if (res == null ? void 0 : res.latest) {
|
817
|
+
const ws = getPackageManager(process.cwd());
|
818
|
+
console.log();
|
819
|
+
console.log(import_chalk7.default.yellow.bold("A new version of `@turbo/codemod` is available!"));
|
820
|
+
console.log("You can update by running: " + import_chalk7.default.cyan(ws === "yarn" ? "yarn global add @turbo/codemod" : ws === "pnpm" ? "pnpm i -g @turbo/codemod" : "npm i -g @turbo/codemod"));
|
821
|
+
console.log();
|
822
|
+
}
|
823
|
+
process.exit();
|
824
|
+
} catch (_e) {
|
825
|
+
}
|
826
|
+
}
|
827
|
+
|
828
|
+
// src/cli.ts
|
829
|
+
var transforms = loadTransformers();
|
830
|
+
var codemodCli = new import_commander.Command();
|
831
|
+
codemodCli.name("@turbo/codemod").description("Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.").version(package_default.version, "-v, --version", "output the current version");
|
832
|
+
codemodCli.command("migrate").aliases(["update", "upgrade"]).description("Migrate a project to the latest version of Turborepo").argument("[path]", "Directory where the transforms should be applied").option("--from <version>", "Specify the version to migrate from (default: current version)").option("--to <version>", "Specify the version to migrate to (default: latest)").option("--install", "Install new version of turbo after migration", true).option("--force", "Bypass Git safety checks and forcibly run codemods", false).option("--dry", "Dry run (no changes are made to files)", false).option("--print", "Print transformed files to your terminal", false).action(migrate);
|
833
|
+
codemodCli.command("transform").description("Apply a single code transformation to a project").argument("[transform]", "The transformer to run").argument("[path]", "Directory where the transforms should be applied").option("--force", "Bypass Git safety checks and forcibly run codemods", false).option("--list", "List all available transforms", false).option("--dry", "Dry run (no changes are made to files)", false).option("--print", "Print transformed files to your terminal", false).action(transform);
|
834
|
+
codemodCli.showHelpAfterError(true);
|
835
|
+
codemodCli.addHelpText("beforeAll", (context) => {
|
836
|
+
try {
|
837
|
+
const transformKeys = transforms.map((transform2) => transform2.value);
|
838
|
+
if (context.command.args.length >= 1 && transformKeys.includes(context.command.args[0])) {
|
839
|
+
return `Transforms must be run with the "transform" command.${import_os4.default.EOL}Try: ${import_chalk8.default.bold(`transform ${context.command.args[0]}`)}${import_os4.default.EOL}`;
|
840
|
+
}
|
841
|
+
} catch (e) {
|
842
|
+
return "";
|
843
|
+
}
|
844
|
+
return "";
|
845
|
+
});
|
846
|
+
codemodCli.parseAsync().then(notifyUpdate).catch(async (reason) => {
|
847
|
+
console.log();
|
848
|
+
if (reason.command) {
|
849
|
+
console.log(` ${import_chalk8.default.cyan(reason.command)} has failed.`);
|
850
|
+
} else {
|
851
|
+
console.log(import_chalk8.default.red("Unexpected error. Please report it as a bug:"));
|
852
|
+
console.log(reason);
|
853
|
+
}
|
854
|
+
console.log();
|
855
|
+
await notifyUpdate();
|
856
|
+
process.exit(1);
|
857
|
+
});
|