@turbo/codemod 1.7.1-canary.0 → 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.
@@ -23,31 +23,235 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
23
23
  // src/transforms/migrate-env-var-dependencies.ts
24
24
  var migrate_env_var_dependencies_exports = {};
25
25
  __export(migrate_env_var_dependencies_exports, {
26
- default: () => migrateEnvVarDependencies,
26
+ default: () => migrate_env_var_dependencies_default,
27
27
  hasLegacyEnvVarDependencies: () => hasLegacyEnvVarDependencies,
28
28
  migrateConfig: () => migrateConfig,
29
29
  migrateDependencies: () => migrateDependencies,
30
30
  migrateGlobal: () => migrateGlobal,
31
- migratePipeline: () => migratePipeline
31
+ migratePipeline: () => migratePipeline,
32
+ transformer: () => transformer
32
33
  });
33
34
  module.exports = __toCommonJS(migrate_env_var_dependencies_exports);
35
+ var import_fs_extra2 = __toESM(require("fs-extra"));
36
+ var import_path2 = __toESM(require("path"));
37
+
38
+ // src/runner/Runner.ts
39
+ var import_chalk3 = __toESM(require("chalk"));
40
+
41
+ // src/runner/FileTransform.ts
42
+ var import_chalk = __toESM(require("chalk"));
43
+ var import_diff = require("diff");
34
44
  var import_fs_extra = __toESM(require("fs-extra"));
45
+ var import_os = __toESM(require("os"));
35
46
  var import_path = __toESM(require("path"));
47
+ var FileTransform = class {
48
+ constructor(args) {
49
+ this.changes = [];
50
+ this.filePath = args.filePath;
51
+ this.rootPath = args.rootPath;
52
+ this.after = args.after;
53
+ this.error = args.error;
54
+ if (args.before === void 0) {
55
+ try {
56
+ if (import_path.default.extname(args.filePath) === ".json") {
57
+ this.before = import_fs_extra.default.readJsonSync(args.filePath);
58
+ } else {
59
+ this.before = import_fs_extra.default.readFileSync(args.filePath);
60
+ }
61
+ } catch (err) {
62
+ this.before = "";
63
+ }
64
+ } else if (args.before === null) {
65
+ this.before = "";
66
+ } else {
67
+ this.before = args.before;
68
+ }
69
+ if (args.after) {
70
+ if (typeof this.before === "object" || typeof args.after === "object") {
71
+ this.changes = (0, import_diff.diffJson)(this.before, args.after);
72
+ } else {
73
+ this.changes = (0, import_diff.diffLines)(this.before, args.after);
74
+ }
75
+ } else {
76
+ this.changes = [];
77
+ }
78
+ }
79
+ fileName() {
80
+ return import_path.default.relative(this.rootPath, this.filePath);
81
+ }
82
+ write() {
83
+ if (this.after) {
84
+ if (typeof this.after === "object") {
85
+ import_fs_extra.default.writeJsonSync(this.filePath, this.after, { spaces: 2 });
86
+ } else {
87
+ import_fs_extra.default.writeFileSync(this.filePath, this.after);
88
+ }
89
+ }
90
+ }
91
+ additions() {
92
+ return this.changes.filter((c) => c.added).length;
93
+ }
94
+ deletions() {
95
+ return this.changes.filter((c) => c.removed).length;
96
+ }
97
+ hasChanges() {
98
+ return this.additions() > 0 || this.deletions() > 0;
99
+ }
100
+ log(args) {
101
+ if (args.diff) {
102
+ this.changes.forEach((part) => {
103
+ if (part.added) {
104
+ process.stdout.write(import_chalk.default.green(part.value));
105
+ } else if (part.removed) {
106
+ process.stdout.write(import_chalk.default.red(part.value));
107
+ } else {
108
+ process.stdout.write(import_chalk.default.dim(part.value));
109
+ }
110
+ });
111
+ console.log(import_os.default.EOL);
112
+ } else {
113
+ console.log(this.after);
114
+ }
115
+ }
116
+ };
117
+
118
+ // src/utils/logger.ts
36
119
  var import_chalk2 = __toESM(require("chalk"));
120
+ var Logger = class {
121
+ constructor(args) {
122
+ this.transform = args.transformer;
123
+ this.dry = args.dry;
124
+ }
125
+ modified(...args) {
126
+ console.log(import_chalk2.default.green(` MODIFIED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
127
+ }
128
+ unchanged(...args) {
129
+ console.log(import_chalk2.default.gray(` UNCHANGED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
130
+ }
131
+ skipped(...args) {
132
+ console.log(import_chalk2.default.yellow(` SKIPPED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
133
+ }
134
+ error(...args) {
135
+ console.log(import_chalk2.default.red(` ERROR `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
136
+ }
137
+ info(...args) {
138
+ console.log(import_chalk2.default.bold(` INFO `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
139
+ }
140
+ };
37
141
 
38
- // src/logger.ts
39
- var import_chalk = __toESM(require("chalk"));
40
- function skip(...args) {
41
- console.log(import_chalk.default.yellow.inverse(` SKIP `), ...args);
42
- }
43
- function error(...args) {
44
- console.log(import_chalk.default.red.inverse(` ERROR `), ...args);
45
- }
46
- function ok(...args) {
47
- console.log(import_chalk.default.green.inverse(` OK `), ...args);
142
+ // src/runner/Runner.ts
143
+ var Runner = class {
144
+ constructor(options) {
145
+ this.modifications = {};
146
+ this.transform = options.transformer;
147
+ this.rootPath = options.rootPath;
148
+ this.dry = options.dry;
149
+ this.print = options.print;
150
+ this.logger = new Logger(options);
151
+ }
152
+ abortTransform(args) {
153
+ this.logger.error(args.reason);
154
+ return {
155
+ fatalError: new Error(args.reason),
156
+ changes: args.changes || {}
157
+ };
158
+ }
159
+ modifyFile(args) {
160
+ this.modifications[args.filePath] = new FileTransform({
161
+ rootPath: this.rootPath,
162
+ ...args
163
+ });
164
+ }
165
+ finish() {
166
+ const results = { changes: {} };
167
+ Object.keys(this.modifications).forEach((filePath) => {
168
+ const mod = this.modifications[filePath];
169
+ const result = {
170
+ action: "unchanged",
171
+ additions: mod.additions(),
172
+ deletions: mod.deletions()
173
+ };
174
+ if (mod.hasChanges()) {
175
+ if (this.dry) {
176
+ result.action = "skipped";
177
+ this.logger.skipped(import_chalk3.default.dim(mod.fileName()));
178
+ } else {
179
+ try {
180
+ mod.write();
181
+ result.action = "modified";
182
+ this.logger.modified(import_chalk3.default.bold(mod.fileName()));
183
+ } catch (err) {
184
+ let message = "Unknown error";
185
+ if (err instanceof Error) {
186
+ message = err.message;
187
+ }
188
+ result.error = new Error(message);
189
+ result.action = "error";
190
+ this.logger.error(mod.fileName(), message);
191
+ }
192
+ }
193
+ if (this.print) {
194
+ mod.log({ diff: true });
195
+ }
196
+ } else {
197
+ this.logger.unchanged(import_chalk3.default.dim(mod.fileName()));
198
+ }
199
+ results.changes[mod.fileName()] = result;
200
+ });
201
+ const encounteredError = Object.keys(results.changes).some((fileName) => {
202
+ return results.changes[fileName].action === "error";
203
+ });
204
+ if (encounteredError) {
205
+ return this.abortTransform({
206
+ reason: "Encountered an error while transforming files",
207
+ changes: results.changes
208
+ });
209
+ }
210
+ return results;
211
+ }
212
+ static logResults(results) {
213
+ const changedFiles = Object.keys(results.changes);
214
+ console.log();
215
+ if (changedFiles.length > 0) {
216
+ console.log(import_chalk3.default.bold(`Results:`));
217
+ const table = {};
218
+ changedFiles.forEach((fileName) => {
219
+ var _a;
220
+ const fileChanges = results.changes[fileName];
221
+ table[fileName] = {
222
+ action: fileChanges.action,
223
+ additions: fileChanges.additions,
224
+ deletions: fileChanges.deletions,
225
+ error: ((_a = fileChanges.error) == null ? void 0 : _a.message) || "None"
226
+ };
227
+ });
228
+ console.table(table);
229
+ console.log();
230
+ }
231
+ }
232
+ };
233
+ var Runner_default = Runner;
234
+
235
+ // src/utils/getTransformerHelpers.ts
236
+ function getTransformerHelpers({
237
+ transformer: transformer2,
238
+ rootPath,
239
+ options
240
+ }) {
241
+ const utilArgs = {
242
+ transformer: transformer2,
243
+ rootPath,
244
+ ...options
245
+ };
246
+ const log = new Logger(utilArgs);
247
+ const runner = new Runner_default(utilArgs);
248
+ return { log, runner };
48
249
  }
49
250
 
50
251
  // src/transforms/migrate-env-var-dependencies.ts
252
+ var TRANSFORMER = "migrate-env-var-dependencies";
253
+ var DESCRIPTION = 'Migrate environment variable dependencies from "dependsOn" to "env" in `turbo.json`';
254
+ var INTRODUCED_IN = "1.5.0";
51
255
  function hasLegacyEnvVarDependencies(config) {
52
256
  const dependsOn = [
53
257
  config.globalDependencies,
@@ -66,7 +270,7 @@ function migrateDependencies({
66
270
  const envDeps = new Set(env);
67
271
  const otherDeps = [];
68
272
  deps == null ? void 0 : deps.forEach((dep) => {
69
- if (dep == null ? void 0 : dep.startsWith("$")) {
273
+ if (dep.startsWith("$")) {
70
274
  envDeps.add(dep.slice(1));
71
275
  } else {
72
276
  otherDeps.push(dep);
@@ -105,7 +309,7 @@ function migrateGlobal(config) {
105
309
  deps: config.globalDependencies
106
310
  });
107
311
  const migratedConfig = { ...config };
108
- if (globalDependencies) {
312
+ if (globalDependencies && globalDependencies.length) {
109
313
  migratedConfig.globalDependencies = globalDependencies;
110
314
  } else {
111
315
  delete migratedConfig.globalDependencies;
@@ -120,9 +324,9 @@ function migrateGlobal(config) {
120
324
  function migrateConfig(config) {
121
325
  let migratedConfig = migrateGlobal(config);
122
326
  Object.keys(config.pipeline).forEach((pipelineKey) => {
123
- var _a, _b;
124
- if (migratedConfig.pipeline && ((_a = config.pipeline) == null ? void 0 : _a[pipelineKey])) {
125
- const pipeline = (_b = migratedConfig.pipeline) == null ? void 0 : _b[pipelineKey];
327
+ config.pipeline;
328
+ if (migratedConfig.pipeline && config.pipeline[pipelineKey]) {
329
+ const pipeline = migratedConfig.pipeline[pipelineKey];
126
330
  migratedConfig.pipeline[pipelineKey] = {
127
331
  ...pipeline,
128
332
  ...migratePipeline(pipeline)
@@ -131,55 +335,56 @@ function migrateConfig(config) {
131
335
  });
132
336
  return migratedConfig;
133
337
  }
134
- function migrateEnvVarDependencies(files, flags) {
135
- if (files.length === 1) {
136
- const dir = files[0];
137
- const root = import_path.default.resolve(process.cwd(), dir);
138
- console.log(`Migrating environment variable dependencies from "globalDependencies" and "dependsOn" to "env" in "turbo.json"...`);
139
- const turboConfigPath = import_path.default.join(root, "turbo.json");
140
- let modifiedCount = 0;
141
- let skippedCount = 0;
142
- let unmodifiedCount = 1;
143
- if (!import_fs_extra.default.existsSync(turboConfigPath)) {
144
- error(`No turbo.json found at ${root}. Is the path correct?`);
145
- process.exit(1);
146
- }
147
- const rootTurboJson = import_fs_extra.default.readJsonSync(turboConfigPath);
148
- if (hasLegacyEnvVarDependencies(rootTurboJson).hasKeys) {
149
- if (flags.dry) {
150
- if (flags.print) {
151
- console.log(JSON.stringify(migrateConfig(rootTurboJson), null, 2));
152
- }
153
- skip("turbo.json", import_chalk2.default.dim("(dry run)"));
154
- skippedCount += 1;
155
- } else {
156
- if (flags.print) {
157
- console.log(JSON.stringify(migrateConfig(rootTurboJson), null, 2));
158
- }
159
- ok("turbo.json");
160
- import_fs_extra.default.writeJsonSync(turboConfigPath, migrateConfig(rootTurboJson), {
161
- spaces: 2
162
- });
163
- modifiedCount += 1;
164
- unmodifiedCount -= 1;
165
- }
166
- } else {
167
- ok('no unmigrated environment variable dependencies found in "turbo.json"');
168
- process.exit(0);
169
- }
170
- console.log("All done.");
171
- console.log("Results:");
172
- console.log(import_chalk2.default.red(`0 errors`));
173
- console.log(import_chalk2.default.yellow(`${skippedCount} skipped`));
174
- console.log(import_chalk2.default.yellow(`${unmodifiedCount} unmodified`));
175
- console.log(import_chalk2.default.green(`${modifiedCount} modified`));
338
+ function transformer({
339
+ root,
340
+ options
341
+ }) {
342
+ const { log, runner } = getTransformerHelpers({
343
+ transformer: TRANSFORMER,
344
+ rootPath: root,
345
+ options
346
+ });
347
+ log.info(`Migrating environment variable dependencies from "globalDependencies" and "dependsOn" to "env" in "turbo.json"...`);
348
+ const turboConfigPath = import_path2.default.join(root, "turbo.json");
349
+ const packageJsonPath = import_path2.default.join(root, "package.json");
350
+ if (!import_fs_extra2.default.existsSync(turboConfigPath)) {
351
+ return runner.abortTransform({
352
+ reason: `No turbo.json found at ${root}. Is the path correct?`
353
+ });
354
+ }
355
+ let packageJSON = {};
356
+ try {
357
+ packageJSON = import_fs_extra2.default.readJSONSync(packageJsonPath);
358
+ } catch (e) {
176
359
  }
360
+ if ("turbo" in packageJSON) {
361
+ return runner.abortTransform({
362
+ reason: '"turbo" key detected in package.json. Run `npx @turbo/codemod transform create-turbo-config` first'
363
+ });
364
+ }
365
+ let turboJson = import_fs_extra2.default.readJsonSync(turboConfigPath);
366
+ if (hasLegacyEnvVarDependencies(turboJson).hasKeys) {
367
+ turboJson = migrateConfig(turboJson);
368
+ }
369
+ runner.modifyFile({
370
+ filePath: turboConfigPath,
371
+ after: turboJson
372
+ });
373
+ return runner.finish();
177
374
  }
375
+ var transformerMeta = {
376
+ name: `${TRANSFORMER}: ${DESCRIPTION}`,
377
+ value: TRANSFORMER,
378
+ introducedIn: INTRODUCED_IN,
379
+ transformer
380
+ };
381
+ var migrate_env_var_dependencies_default = transformerMeta;
178
382
  // Annotate the CommonJS export names for ESM import in node:
179
383
  0 && (module.exports = {
180
384
  hasLegacyEnvVarDependencies,
181
385
  migrateConfig,
182
386
  migrateDependencies,
183
387
  migrateGlobal,
184
- migratePipeline
388
+ migratePipeline,
389
+ transformer
185
390
  });