@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.
@@ -23,82 +23,279 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
23
23
  // src/transforms/create-turbo-config.ts
24
24
  var create_turbo_config_exports = {};
25
25
  __export(create_turbo_config_exports, {
26
- default: () => createTurboConfig
26
+ default: () => create_turbo_config_default,
27
+ transformer: () => transformer
27
28
  });
28
29
  module.exports = __toCommonJS(create_turbo_config_exports);
30
+ var import_fs_extra2 = __toESM(require("fs-extra"));
31
+ var import_path2 = __toESM(require("path"));
32
+
33
+ // src/runner/Runner.ts
34
+ var import_chalk3 = __toESM(require("chalk"));
35
+
36
+ // src/runner/FileTransform.ts
37
+ var import_chalk = __toESM(require("chalk"));
38
+ var import_diff = require("diff");
29
39
  var import_fs_extra = __toESM(require("fs-extra"));
30
- var import_chalk2 = __toESM(require("chalk"));
40
+ var import_os = __toESM(require("os"));
31
41
  var import_path = __toESM(require("path"));
42
+ var FileTransform = class {
43
+ constructor(args) {
44
+ this.changes = [];
45
+ this.filePath = args.filePath;
46
+ this.rootPath = args.rootPath;
47
+ this.after = args.after;
48
+ this.error = args.error;
49
+ if (args.before === void 0) {
50
+ try {
51
+ if (import_path.default.extname(args.filePath) === ".json") {
52
+ this.before = import_fs_extra.default.readJsonSync(args.filePath);
53
+ } else {
54
+ this.before = import_fs_extra.default.readFileSync(args.filePath);
55
+ }
56
+ } catch (err) {
57
+ this.before = "";
58
+ }
59
+ } else if (args.before === null) {
60
+ this.before = "";
61
+ } else {
62
+ this.before = args.before;
63
+ }
64
+ if (args.after) {
65
+ if (typeof this.before === "object" || typeof args.after === "object") {
66
+ this.changes = (0, import_diff.diffJson)(this.before, args.after);
67
+ } else {
68
+ this.changes = (0, import_diff.diffLines)(this.before, args.after);
69
+ }
70
+ } else {
71
+ this.changes = [];
72
+ }
73
+ }
74
+ fileName() {
75
+ return import_path.default.relative(this.rootPath, this.filePath);
76
+ }
77
+ write() {
78
+ if (this.after) {
79
+ if (typeof this.after === "object") {
80
+ import_fs_extra.default.writeJsonSync(this.filePath, this.after, { spaces: 2 });
81
+ } else {
82
+ import_fs_extra.default.writeFileSync(this.filePath, this.after);
83
+ }
84
+ }
85
+ }
86
+ additions() {
87
+ return this.changes.filter((c) => c.added).length;
88
+ }
89
+ deletions() {
90
+ return this.changes.filter((c) => c.removed).length;
91
+ }
92
+ hasChanges() {
93
+ return this.additions() > 0 || this.deletions() > 0;
94
+ }
95
+ log(args) {
96
+ if (args.diff) {
97
+ this.changes.forEach((part) => {
98
+ if (part.added) {
99
+ process.stdout.write(import_chalk.default.green(part.value));
100
+ } else if (part.removed) {
101
+ process.stdout.write(import_chalk.default.red(part.value));
102
+ } else {
103
+ process.stdout.write(import_chalk.default.dim(part.value));
104
+ }
105
+ });
106
+ console.log(import_os.default.EOL);
107
+ } else {
108
+ console.log(this.after);
109
+ }
110
+ }
111
+ };
32
112
 
33
- // src/logger.ts
34
- var import_chalk = __toESM(require("chalk"));
35
- function skip(...args) {
36
- console.log(import_chalk.default.yellow.inverse(` SKIP `), ...args);
37
- }
38
- function error(...args) {
39
- console.log(import_chalk.default.red.inverse(` ERROR `), ...args);
40
- }
41
- function ok(...args) {
42
- console.log(import_chalk.default.green.inverse(` OK `), ...args);
43
- }
113
+ // src/utils/logger.ts
114
+ var import_chalk2 = __toESM(require("chalk"));
115
+ var Logger = class {
116
+ constructor(args) {
117
+ this.transform = args.transformer;
118
+ this.dry = args.dry;
119
+ }
120
+ modified(...args) {
121
+ console.log(import_chalk2.default.green(` MODIFIED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
122
+ }
123
+ unchanged(...args) {
124
+ console.log(import_chalk2.default.gray(` UNCHANGED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
125
+ }
126
+ skipped(...args) {
127
+ console.log(import_chalk2.default.yellow(` SKIPPED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
128
+ }
129
+ error(...args) {
130
+ console.log(import_chalk2.default.red(` ERROR `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
131
+ }
132
+ info(...args) {
133
+ console.log(import_chalk2.default.bold(` INFO `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
134
+ }
135
+ };
44
136
 
45
- // src/transforms/create-turbo-config.ts
46
- function createTurboConfig(files, flags) {
47
- if (files.length === 1) {
48
- const dir = files[0];
49
- const root = import_path.default.resolve(process.cwd(), dir);
50
- console.log(`Migrating "package.json" "turbo" key to "turbo.json" file...`);
51
- const turboConfigPath = import_path.default.join(root, "turbo.json");
52
- const rootPackageJsonPath = import_path.default.join(root, "package.json");
53
- let modifiedCount = 0;
54
- let skippedCount = 0;
55
- let unmodifiedCount = 2;
56
- if (!import_fs_extra.default.existsSync(rootPackageJsonPath)) {
57
- error(`No package.json found at ${root}. Is the path correct?`);
58
- process.exit(1);
59
- }
60
- const rootPackageJson = import_fs_extra.default.readJsonSync(rootPackageJsonPath);
61
- if (import_fs_extra.default.existsSync(turboConfigPath)) {
62
- skip("turbo.json", import_chalk2.default.dim("(already exists)"));
63
- skip("package.json", import_chalk2.default.dim("(skipped)"));
64
- skippedCount += 2;
65
- } else if (rootPackageJson.hasOwnProperty("turbo")) {
66
- const { turbo: turboConfig, ...remainingPkgJson } = rootPackageJson;
67
- if (flags.dry) {
68
- if (flags.print) {
69
- console.log(JSON.stringify(turboConfig, null, 2));
137
+ // src/runner/Runner.ts
138
+ var Runner = class {
139
+ constructor(options) {
140
+ this.modifications = {};
141
+ this.transform = options.transformer;
142
+ this.rootPath = options.rootPath;
143
+ this.dry = options.dry;
144
+ this.print = options.print;
145
+ this.logger = new Logger(options);
146
+ }
147
+ abortTransform(args) {
148
+ this.logger.error(args.reason);
149
+ return {
150
+ fatalError: new Error(args.reason),
151
+ changes: args.changes || {}
152
+ };
153
+ }
154
+ modifyFile(args) {
155
+ this.modifications[args.filePath] = new FileTransform({
156
+ rootPath: this.rootPath,
157
+ ...args
158
+ });
159
+ }
160
+ finish() {
161
+ const results = { changes: {} };
162
+ Object.keys(this.modifications).forEach((filePath) => {
163
+ const mod = this.modifications[filePath];
164
+ const result = {
165
+ action: "unchanged",
166
+ additions: mod.additions(),
167
+ deletions: mod.deletions()
168
+ };
169
+ if (mod.hasChanges()) {
170
+ if (this.dry) {
171
+ result.action = "skipped";
172
+ this.logger.skipped(import_chalk3.default.dim(mod.fileName()));
173
+ } else {
174
+ try {
175
+ mod.write();
176
+ result.action = "modified";
177
+ this.logger.modified(import_chalk3.default.bold(mod.fileName()));
178
+ } catch (err) {
179
+ let message = "Unknown error";
180
+ if (err instanceof Error) {
181
+ message = err.message;
182
+ }
183
+ result.error = new Error(message);
184
+ result.action = "error";
185
+ this.logger.error(mod.fileName(), message);
186
+ }
70
187
  }
71
- skip("turbo.json", import_chalk2.default.dim("(dry run)"));
72
- if (flags.print) {
73
- console.log(JSON.stringify(remainingPkgJson, null, 2));
188
+ if (this.print) {
189
+ mod.log({ diff: true });
74
190
  }
75
- skip("package.json", import_chalk2.default.dim("(dry run)"));
76
- skippedCount += 2;
77
191
  } else {
78
- if (flags.print) {
79
- console.log(JSON.stringify(turboConfig, null, 2));
80
- }
81
- ok("turbo.json", import_chalk2.default.dim("(created)"));
82
- import_fs_extra.default.writeJsonSync(turboConfigPath, turboConfig, { spaces: 2 });
83
- if (flags.print) {
84
- console.log(JSON.stringify(remainingPkgJson, null, 2));
85
- }
86
- ok("package.json", import_chalk2.default.dim("(remove turbo key)"));
87
- import_fs_extra.default.writeJsonSync(rootPackageJsonPath, remainingPkgJson, { spaces: 2 });
88
- modifiedCount += 2;
89
- unmodifiedCount -= 2;
192
+ this.logger.unchanged(import_chalk3.default.dim(mod.fileName()));
90
193
  }
91
- } else {
92
- error('"turbo" key does not exist in "package.json"');
93
- process.exit(1);
194
+ results.changes[mod.fileName()] = result;
195
+ });
196
+ const encounteredError = Object.keys(results.changes).some((fileName) => {
197
+ return results.changes[fileName].action === "error";
198
+ });
199
+ if (encounteredError) {
200
+ return this.abortTransform({
201
+ reason: "Encountered an error while transforming files",
202
+ changes: results.changes
203
+ });
204
+ }
205
+ return results;
206
+ }
207
+ static logResults(results) {
208
+ const changedFiles = Object.keys(results.changes);
209
+ console.log();
210
+ if (changedFiles.length > 0) {
211
+ console.log(import_chalk3.default.bold(`Results:`));
212
+ const table = {};
213
+ changedFiles.forEach((fileName) => {
214
+ var _a;
215
+ const fileChanges = results.changes[fileName];
216
+ table[fileName] = {
217
+ action: fileChanges.action,
218
+ additions: fileChanges.additions,
219
+ deletions: fileChanges.deletions,
220
+ error: ((_a = fileChanges.error) == null ? void 0 : _a.message) || "None"
221
+ };
222
+ });
223
+ console.table(table);
224
+ console.log();
94
225
  }
95
- console.log("All done.");
96
- console.log("Results:");
97
- console.log(import_chalk2.default.red(`0 errors`));
98
- console.log(import_chalk2.default.yellow(`${skippedCount} skipped`));
99
- console.log(import_chalk2.default.yellow(`${unmodifiedCount} unmodified`));
100
- console.log(import_chalk2.default.green(`${modifiedCount} modified`));
101
226
  }
227
+ };
228
+ var Runner_default = Runner;
229
+
230
+ // src/utils/getTransformerHelpers.ts
231
+ function getTransformerHelpers({
232
+ transformer: transformer2,
233
+ rootPath,
234
+ options
235
+ }) {
236
+ const utilArgs = {
237
+ transformer: transformer2,
238
+ rootPath,
239
+ ...options
240
+ };
241
+ const log = new Logger(utilArgs);
242
+ const runner = new Runner_default(utilArgs);
243
+ return { log, runner };
244
+ }
245
+
246
+ // src/transforms/create-turbo-config.ts
247
+ var TRANSFORMER = "create-turbo-config";
248
+ var DESCRIPTION = 'Create the `turbo.json` file from an existing "turbo" key in `package.json`';
249
+ var INTRODUCED_IN = "1.1.0";
250
+ function transformer({
251
+ root,
252
+ options
253
+ }) {
254
+ const { log, runner } = getTransformerHelpers({
255
+ transformer: TRANSFORMER,
256
+ rootPath: root,
257
+ options
258
+ });
259
+ log.info(`Migrating "package.json" "turbo" key to "turbo.json" file...`);
260
+ const turboConfigPath = import_path2.default.join(root, "turbo.json");
261
+ const rootPackageJsonPath = import_path2.default.join(root, "package.json");
262
+ if (!import_fs_extra2.default.existsSync(rootPackageJsonPath)) {
263
+ return runner.abortTransform({
264
+ reason: `No package.json found at ${root}. Is the path correct?`
265
+ });
266
+ }
267
+ const rootPackageJson = import_fs_extra2.default.readJsonSync(rootPackageJsonPath);
268
+ let rootTurboJson = null;
269
+ try {
270
+ rootTurboJson = import_fs_extra2.default.readJSONSync(turboConfigPath);
271
+ } catch (err) {
272
+ rootTurboJson = null;
273
+ }
274
+ let transformedPackageJson = rootPackageJson;
275
+ let transformedTurboConfig = rootTurboJson;
276
+ if (!rootTurboJson && rootPackageJson["turbo"]) {
277
+ const { turbo: turboConfig, ...remainingPkgJson } = rootPackageJson;
278
+ transformedTurboConfig = turboConfig;
279
+ transformedPackageJson = remainingPkgJson;
280
+ }
281
+ runner.modifyFile({
282
+ filePath: turboConfigPath,
283
+ after: transformedTurboConfig
284
+ });
285
+ runner.modifyFile({
286
+ filePath: rootPackageJsonPath,
287
+ after: transformedPackageJson
288
+ });
289
+ return runner.finish();
102
290
  }
291
+ var transformerMeta = {
292
+ name: `${TRANSFORMER}: ${DESCRIPTION}`,
293
+ value: TRANSFORMER,
294
+ introducedIn: INTRODUCED_IN,
295
+ transformer
296
+ };
297
+ var create_turbo_config_default = transformerMeta;
103
298
  // Annotate the CommonJS export names for ESM import in node:
104
- 0 && (module.exports = {});
299
+ 0 && (module.exports = {
300
+ transformer
301
+ });