@turbo/codemod 1.7.1-canary.2 → 1.7.1-canary.4

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.
@@ -1,302 +1 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- 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));
21
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
-
23
- // src/transforms/set-default-outputs.ts
24
- var set_default_outputs_exports = {};
25
- __export(set_default_outputs_exports, {
26
- default: () => set_default_outputs_default,
27
- transformer: () => transformer
28
- });
29
- module.exports = __toCommonJS(set_default_outputs_exports);
30
- var import_path2 = __toESM(require("path"));
31
- var import_fs_extra2 = __toESM(require("fs-extra"));
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");
39
- var import_fs_extra = __toESM(require("fs-extra"));
40
- var import_os = __toESM(require("os"));
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
- };
112
-
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
- };
136
-
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
- }
187
- }
188
- if (this.print) {
189
- mod.log({ diff: true });
190
- }
191
- } else {
192
- this.logger.unchanged(import_chalk3.default.dim(mod.fileName()));
193
- }
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();
225
- }
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/set-default-outputs.ts
247
- var DEFAULT_OUTPUTS = ["dist/**", "build/**"];
248
- var TRANSFORMER = "set-default-outputs";
249
- var DESCRIPTION = 'Add the "outputs" key with defaults where it is missing in `turbo.json`';
250
- var INTRODUCED_IN = "1.7.0";
251
- function transformer({
252
- root,
253
- options
254
- }) {
255
- const { log, runner } = getTransformerHelpers({
256
- transformer: TRANSFORMER,
257
- rootPath: root,
258
- options
259
- });
260
- const packageJsonPath = import_path2.default.join(root, "package.json");
261
- let packageJSON = {};
262
- try {
263
- packageJSON = import_fs_extra2.default.readJSONSync(packageJsonPath);
264
- } catch (e) {
265
- }
266
- if ("turbo" in packageJSON) {
267
- return runner.abortTransform({
268
- reason: '"turbo" key detected in package.json. Run `npx @turbo/codemod transform create-turbo-config` first'
269
- });
270
- }
271
- log.info(`Adding default \`outputs\` key into tasks if it doesn't exist`);
272
- const turboConfigPath = import_path2.default.join(root, "turbo.json");
273
- if (!import_fs_extra2.default.existsSync(turboConfigPath)) {
274
- return runner.abortTransform({
275
- reason: `No turbo.json found at ${root}. Is the path correct?`
276
- });
277
- }
278
- const turboJson = import_fs_extra2.default.readJsonSync(turboConfigPath);
279
- for (const [_, taskDef] of Object.entries(turboJson.pipeline)) {
280
- if (!taskDef.outputs) {
281
- taskDef.outputs = DEFAULT_OUTPUTS;
282
- } else if (Array.isArray(taskDef.outputs) && taskDef.outputs.length === 0) {
283
- delete taskDef.outputs;
284
- }
285
- }
286
- runner.modifyFile({
287
- filePath: turboConfigPath,
288
- after: turboJson
289
- });
290
- return runner.finish();
291
- }
292
- var transformerMeta = {
293
- name: `${TRANSFORMER}: ${DESCRIPTION}`,
294
- value: TRANSFORMER,
295
- introducedIn: INTRODUCED_IN,
296
- transformer
297
- };
298
- var set_default_outputs_default = transformerMeta;
299
- // Annotate the CommonJS export names for ESM import in node:
300
- 0 && (module.exports = {
301
- transformer
302
- });
1
+ "use strict";var S=Object.create;var p=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var D=Object.getPrototypeOf,I=Object.prototype.hasOwnProperty;var J=(r,e)=>{for(var t in e)p(r,t,{get:e[t],enumerable:!0})},w=(r,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of v(e))!I.call(r,o)&&o!==t&&p(r,o,{get:()=>e[o],enumerable:!(s=x(e,o))||s.enumerable});return r};var f=(r,e,t)=>(t=r!=null?S(D(r)):{},w(e||!r||!r.__esModule?p(t,"default",{value:r,enumerable:!0}):t,r)),U=r=>w(p({},"__esModule",{value:!0}),r);var $={};J($,{default:()=>_,transformer:()=>N});module.exports=U($);var E=f(require("path")),T=f(require("fs-extra"));var g=f(require("chalk"));var y=f(require("chalk")),b=require("diff"),c=f(require("fs-extra")),j=f(require("os")),P=f(require("path")),m=class{constructor(e){this.changes=[];if(this.filePath=e.filePath,this.rootPath=e.rootPath,this.after=e.after,this.error=e.error,e.before===void 0)try{P.default.extname(e.filePath)===".json"?this.before=c.default.readJsonSync(e.filePath):this.before=c.default.readFileSync(e.filePath)}catch{this.before=""}else e.before===null?this.before="":this.before=e.before;e.after?typeof this.before=="object"||typeof e.after=="object"?this.changes=(0,b.diffJson)(this.before,e.after):this.changes=(0,b.diffLines)(this.before,e.after):this.changes=[]}fileName(){return P.default.relative(this.rootPath,this.filePath)}write(){this.after&&(typeof this.after=="object"?c.default.writeJsonSync(this.filePath,this.after,{spaces:2}):c.default.writeFileSync(this.filePath,this.after))}additions(){return this.changes.filter(e=>e.added).length}deletions(){return this.changes.filter(e=>e.removed).length}hasChanges(){return this.additions()>0||this.deletions()>0}log(e){e.diff?(this.changes.forEach(t=>{t.added?process.stdout.write(y.default.green(t.value)):t.removed?process.stdout.write(y.default.red(t.value)):process.stdout.write(y.default.dim(t.value))}),console.log(j.default.EOL)):console.log(this.after)}};var n=f(require("chalk")),l=class{constructor(e){this.transform=e.transformer,this.dry=e.dry}modified(...e){console.log(n.default.green(" MODIFIED "),...e,this.dry?n.default.dim("(dry run)"):"")}unchanged(...e){console.log(n.default.gray(" UNCHANGED "),...e,this.dry?n.default.dim("(dry run)"):"")}skipped(...e){console.log(n.default.yellow(" SKIPPED "),...e,this.dry?n.default.dim("(dry run)"):"")}error(...e){console.log(n.default.red(" ERROR "),...e,this.dry?n.default.dim("(dry run)"):"")}info(...e){console.log(n.default.bold(" INFO "),...e,this.dry?n.default.dim("(dry run)"):"")}};var R=class{constructor(e){this.modifications={};this.transform=e.transformer,this.rootPath=e.rootPath,this.dry=e.dry,this.print=e.print,this.logger=new l(e)}abortTransform(e){return this.logger.error(e.reason),{fatalError:new Error(e.reason),changes:e.changes||{}}}modifyFile(e){this.modifications[e.filePath]=new m({rootPath:this.rootPath,...e})}finish(){let e={changes:{}};return Object.keys(this.modifications).forEach(s=>{let o=this.modifications[s],i={action:"unchanged",additions:o.additions(),deletions:o.deletions()};if(o.hasChanges()){if(this.dry)i.action="skipped",this.logger.skipped(g.default.dim(o.fileName()));else try{o.write(),i.action="modified",this.logger.modified(g.default.bold(o.fileName()))}catch(a){let h="Unknown error";a instanceof Error&&(h=a.message),i.error=new Error(h),i.action="error",this.logger.error(o.fileName(),h)}this.print&&o.log({diff:!0})}else this.logger.unchanged(g.default.dim(o.fileName()));e.changes[o.fileName()]=i}),Object.keys(e.changes).some(s=>e.changes[s].action==="error")?this.abortTransform({reason:"Encountered an error while transforming files",changes:e.changes}):e}static logResults(e){let t=Object.keys(e.changes);if(console.log(),t.length>0){console.log(g.default.bold("Results:"));let s={};t.forEach(o=>{var a;let i=e.changes[o];s[o]={action:i.action,additions:i.additions,deletions:i.deletions,error:((a=i.error)==null?void 0:a.message)||"None"}}),console.table(s),console.log()}}},A=R;function F({transformer:r,rootPath:e,options:t}){let s={transformer:r,rootPath:e,...t},o=new l(s),i=new A(s);return{log:o,runner:i}}var C=["dist/**","build/**"],k="set-default-outputs",L='Add the "outputs" key with defaults where it is missing in `turbo.json`',M="1.7.0";function N({root:r,options:e}){let{log:t,runner:s}=F({transformer:k,rootPath:r,options:e}),o=E.default.join(r,"package.json"),i={};try{i=T.default.readJSONSync(o)}catch{}if("turbo"in i)return s.abortTransform({reason:'"turbo" key detected in package.json. Run `npx @turbo/codemod transform create-turbo-config` first'});t.info("Adding default `outputs` key into tasks if it doesn't exist");let a=E.default.join(r,"turbo.json");if(!T.default.existsSync(a))return s.abortTransform({reason:`No turbo.json found at ${r}. Is the path correct?`});let h=T.default.readJsonSync(a);for(let[O,d]of Object.entries(h.pipeline))d.outputs?Array.isArray(d.outputs)&&d.outputs.length===0&&delete d.outputs:d.outputs=C;return s.modifyFile({filePath:a,after:h}),s.finish()}var H={name:`${k}: ${L}`,value:k,introducedIn:M,transformer:N},_=H;0&&(module.exports={transformer});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turbo/codemod",
3
- "version": "1.7.1-canary.2",
3
+ "version": "1.7.1-canary.4",
4
4
  "description": "Provides Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.",
5
5
  "homepage": "https://turbo.build/repo",
6
6
  "license": "MPL-2.0",
@@ -12,14 +12,7 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/vercel/turbo/issues"
14
14
  },
15
- "bin": "dist/index.js",
16
- "scripts": {
17
- "build": "tsup",
18
- "test": "jest",
19
- "lint": "eslint src/**/*.ts",
20
- "check-types": "tsc --noEmit",
21
- "add-transformer": "plop"
22
- },
15
+ "bin": "dist/cli.js",
23
16
  "dependencies": {
24
17
  "axios": "0.27.2",
25
18
  "chalk": "2.4.2",
@@ -33,7 +26,6 @@
33
26
  "is-git-clean": "^1.1.0",
34
27
  "ora": "4.1.1",
35
28
  "semver": "^7.3.7",
36
- "turbo-utils": "workspace:*",
37
29
  "update-check": "^1.5.4"
38
30
  },
39
31
  "devDependencies": {
@@ -52,9 +44,9 @@
52
44
  "plop": "^3.1.1",
53
45
  "semver": "^7.3.5",
54
46
  "ts-jest": "^27.1.1",
55
- "tsconfig": "workspace:*",
47
+ "tsconfig": "0.0.0",
56
48
  "tsup": "^5.10.3",
57
- "turbo-types": "workspace:*",
49
+ "turbo-types": "0.0.0",
58
50
  "typescript": "^4.5.5",
59
51
  "uuid": "^9.0.0"
60
52
  },
@@ -63,5 +55,12 @@
63
55
  ],
64
56
  "publishConfig": {
65
57
  "access": "public"
58
+ },
59
+ "scripts": {
60
+ "build": "tsup",
61
+ "test": "jest",
62
+ "lint": "eslint src/**/*.ts",
63
+ "check-types": "tsc --noEmit",
64
+ "add-transformer": "plop"
66
65
  }
67
- }
66
+ }