@turbo/codemod 1.7.1-canary.3 → 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,351 +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/add-package-manager.ts
|
24
|
-
var add_package_manager_exports = {};
|
25
|
-
__export(add_package_manager_exports, {
|
26
|
-
default: () => add_package_manager_default,
|
27
|
-
transformer: () => transformer
|
28
|
-
});
|
29
|
-
module.exports = __toCommonJS(add_package_manager_exports);
|
30
|
-
var import_path3 = __toESM(require("path"));
|
31
|
-
var import_fs_extra2 = __toESM(require("fs-extra"));
|
32
|
-
|
33
|
-
// src/utils/getPackageManager.ts
|
34
|
-
var import_find_up = __toESM(require("find-up"));
|
35
|
-
var import_path = __toESM(require("path"));
|
36
|
-
var cache = {};
|
37
|
-
function getPackageManager({
|
38
|
-
directory
|
39
|
-
}) {
|
40
|
-
const cwd = directory || process.cwd();
|
41
|
-
if (cache[cwd]) {
|
42
|
-
return cache[cwd];
|
43
|
-
}
|
44
|
-
const lockFile = import_find_up.default.sync(["yarn.lock", "pnpm-lock.yaml", "package-lock.json"], {
|
45
|
-
cwd
|
46
|
-
});
|
47
|
-
if (!lockFile) {
|
48
|
-
return;
|
49
|
-
}
|
50
|
-
switch (import_path.default.basename(lockFile)) {
|
51
|
-
case "yarn.lock":
|
52
|
-
cache[cwd] = "yarn";
|
53
|
-
break;
|
54
|
-
case "pnpm-lock.yaml":
|
55
|
-
cache[cwd] = "pnpm";
|
56
|
-
break;
|
57
|
-
case "package-lock.json":
|
58
|
-
cache[cwd] = "npm";
|
59
|
-
break;
|
60
|
-
}
|
61
|
-
return cache[cwd];
|
62
|
-
}
|
63
|
-
|
64
|
-
// src/utils/getPackageManagerVersion.ts
|
65
|
-
var import_child_process = require("child_process");
|
66
|
-
function getPackageManagerVersion(packageManager, root) {
|
67
|
-
switch (packageManager) {
|
68
|
-
case "yarn":
|
69
|
-
return (0, import_child_process.execSync)("yarn --version", { cwd: root }).toString().trim();
|
70
|
-
case "pnpm":
|
71
|
-
return (0, import_child_process.execSync)("pnpm --version", { cwd: root }).toString().trim();
|
72
|
-
case "npm":
|
73
|
-
return (0, import_child_process.execSync)("npm --version", { cwd: root }).toString().trim();
|
74
|
-
}
|
75
|
-
}
|
76
|
-
|
77
|
-
// src/runner/Runner.ts
|
78
|
-
var import_chalk3 = __toESM(require("chalk"));
|
79
|
-
|
80
|
-
// src/runner/FileTransform.ts
|
81
|
-
var import_chalk = __toESM(require("chalk"));
|
82
|
-
var import_diff = require("diff");
|
83
|
-
var import_fs_extra = __toESM(require("fs-extra"));
|
84
|
-
var import_os = __toESM(require("os"));
|
85
|
-
var import_path2 = __toESM(require("path"));
|
86
|
-
var FileTransform = class {
|
87
|
-
constructor(args) {
|
88
|
-
this.changes = [];
|
89
|
-
this.filePath = args.filePath;
|
90
|
-
this.rootPath = args.rootPath;
|
91
|
-
this.after = args.after;
|
92
|
-
this.error = args.error;
|
93
|
-
if (args.before === void 0) {
|
94
|
-
try {
|
95
|
-
if (import_path2.default.extname(args.filePath) === ".json") {
|
96
|
-
this.before = import_fs_extra.default.readJsonSync(args.filePath);
|
97
|
-
} else {
|
98
|
-
this.before = import_fs_extra.default.readFileSync(args.filePath);
|
99
|
-
}
|
100
|
-
} catch (err) {
|
101
|
-
this.before = "";
|
102
|
-
}
|
103
|
-
} else if (args.before === null) {
|
104
|
-
this.before = "";
|
105
|
-
} else {
|
106
|
-
this.before = args.before;
|
107
|
-
}
|
108
|
-
if (args.after) {
|
109
|
-
if (typeof this.before === "object" || typeof args.after === "object") {
|
110
|
-
this.changes = (0, import_diff.diffJson)(this.before, args.after);
|
111
|
-
} else {
|
112
|
-
this.changes = (0, import_diff.diffLines)(this.before, args.after);
|
113
|
-
}
|
114
|
-
} else {
|
115
|
-
this.changes = [];
|
116
|
-
}
|
117
|
-
}
|
118
|
-
fileName() {
|
119
|
-
return import_path2.default.relative(this.rootPath, this.filePath);
|
120
|
-
}
|
121
|
-
write() {
|
122
|
-
if (this.after) {
|
123
|
-
if (typeof this.after === "object") {
|
124
|
-
import_fs_extra.default.writeJsonSync(this.filePath, this.after, { spaces: 2 });
|
125
|
-
} else {
|
126
|
-
import_fs_extra.default.writeFileSync(this.filePath, this.after);
|
127
|
-
}
|
128
|
-
}
|
129
|
-
}
|
130
|
-
additions() {
|
131
|
-
return this.changes.filter((c) => c.added).length;
|
132
|
-
}
|
133
|
-
deletions() {
|
134
|
-
return this.changes.filter((c) => c.removed).length;
|
135
|
-
}
|
136
|
-
hasChanges() {
|
137
|
-
return this.additions() > 0 || this.deletions() > 0;
|
138
|
-
}
|
139
|
-
log(args) {
|
140
|
-
if (args.diff) {
|
141
|
-
this.changes.forEach((part) => {
|
142
|
-
if (part.added) {
|
143
|
-
process.stdout.write(import_chalk.default.green(part.value));
|
144
|
-
} else if (part.removed) {
|
145
|
-
process.stdout.write(import_chalk.default.red(part.value));
|
146
|
-
} else {
|
147
|
-
process.stdout.write(import_chalk.default.dim(part.value));
|
148
|
-
}
|
149
|
-
});
|
150
|
-
console.log(import_os.default.EOL);
|
151
|
-
} else {
|
152
|
-
console.log(this.after);
|
153
|
-
}
|
154
|
-
}
|
155
|
-
};
|
156
|
-
|
157
|
-
// src/utils/logger.ts
|
158
|
-
var import_chalk2 = __toESM(require("chalk"));
|
159
|
-
var Logger = class {
|
160
|
-
constructor(args) {
|
161
|
-
this.transform = args.transformer;
|
162
|
-
this.dry = args.dry;
|
163
|
-
}
|
164
|
-
modified(...args) {
|
165
|
-
console.log(import_chalk2.default.green(` MODIFIED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
|
166
|
-
}
|
167
|
-
unchanged(...args) {
|
168
|
-
console.log(import_chalk2.default.gray(` UNCHANGED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
|
169
|
-
}
|
170
|
-
skipped(...args) {
|
171
|
-
console.log(import_chalk2.default.yellow(` SKIPPED `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
|
172
|
-
}
|
173
|
-
error(...args) {
|
174
|
-
console.log(import_chalk2.default.red(` ERROR `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
|
175
|
-
}
|
176
|
-
info(...args) {
|
177
|
-
console.log(import_chalk2.default.bold(` INFO `), ...args, this.dry ? import_chalk2.default.dim(`(dry run)`) : "");
|
178
|
-
}
|
179
|
-
};
|
180
|
-
|
181
|
-
// src/runner/Runner.ts
|
182
|
-
var Runner = class {
|
183
|
-
constructor(options) {
|
184
|
-
this.modifications = {};
|
185
|
-
this.transform = options.transformer;
|
186
|
-
this.rootPath = options.rootPath;
|
187
|
-
this.dry = options.dry;
|
188
|
-
this.print = options.print;
|
189
|
-
this.logger = new Logger(options);
|
190
|
-
}
|
191
|
-
abortTransform(args) {
|
192
|
-
this.logger.error(args.reason);
|
193
|
-
return {
|
194
|
-
fatalError: new Error(args.reason),
|
195
|
-
changes: args.changes || {}
|
196
|
-
};
|
197
|
-
}
|
198
|
-
modifyFile(args) {
|
199
|
-
this.modifications[args.filePath] = new FileTransform({
|
200
|
-
rootPath: this.rootPath,
|
201
|
-
...args
|
202
|
-
});
|
203
|
-
}
|
204
|
-
finish() {
|
205
|
-
const results = { changes: {} };
|
206
|
-
Object.keys(this.modifications).forEach((filePath) => {
|
207
|
-
const mod = this.modifications[filePath];
|
208
|
-
const result = {
|
209
|
-
action: "unchanged",
|
210
|
-
additions: mod.additions(),
|
211
|
-
deletions: mod.deletions()
|
212
|
-
};
|
213
|
-
if (mod.hasChanges()) {
|
214
|
-
if (this.dry) {
|
215
|
-
result.action = "skipped";
|
216
|
-
this.logger.skipped(import_chalk3.default.dim(mod.fileName()));
|
217
|
-
} else {
|
218
|
-
try {
|
219
|
-
mod.write();
|
220
|
-
result.action = "modified";
|
221
|
-
this.logger.modified(import_chalk3.default.bold(mod.fileName()));
|
222
|
-
} catch (err) {
|
223
|
-
let message = "Unknown error";
|
224
|
-
if (err instanceof Error) {
|
225
|
-
message = err.message;
|
226
|
-
}
|
227
|
-
result.error = new Error(message);
|
228
|
-
result.action = "error";
|
229
|
-
this.logger.error(mod.fileName(), message);
|
230
|
-
}
|
231
|
-
}
|
232
|
-
if (this.print) {
|
233
|
-
mod.log({ diff: true });
|
234
|
-
}
|
235
|
-
} else {
|
236
|
-
this.logger.unchanged(import_chalk3.default.dim(mod.fileName()));
|
237
|
-
}
|
238
|
-
results.changes[mod.fileName()] = result;
|
239
|
-
});
|
240
|
-
const encounteredError = Object.keys(results.changes).some((fileName) => {
|
241
|
-
return results.changes[fileName].action === "error";
|
242
|
-
});
|
243
|
-
if (encounteredError) {
|
244
|
-
return this.abortTransform({
|
245
|
-
reason: "Encountered an error while transforming files",
|
246
|
-
changes: results.changes
|
247
|
-
});
|
248
|
-
}
|
249
|
-
return results;
|
250
|
-
}
|
251
|
-
static logResults(results) {
|
252
|
-
const changedFiles = Object.keys(results.changes);
|
253
|
-
console.log();
|
254
|
-
if (changedFiles.length > 0) {
|
255
|
-
console.log(import_chalk3.default.bold(`Results:`));
|
256
|
-
const table = {};
|
257
|
-
changedFiles.forEach((fileName) => {
|
258
|
-
var _a;
|
259
|
-
const fileChanges = results.changes[fileName];
|
260
|
-
table[fileName] = {
|
261
|
-
action: fileChanges.action,
|
262
|
-
additions: fileChanges.additions,
|
263
|
-
deletions: fileChanges.deletions,
|
264
|
-
error: ((_a = fileChanges.error) == null ? void 0 : _a.message) || "None"
|
265
|
-
};
|
266
|
-
});
|
267
|
-
console.table(table);
|
268
|
-
console.log();
|
269
|
-
}
|
270
|
-
}
|
271
|
-
};
|
272
|
-
var Runner_default = Runner;
|
273
|
-
|
274
|
-
// src/utils/getTransformerHelpers.ts
|
275
|
-
function getTransformerHelpers({
|
276
|
-
transformer: transformer2,
|
277
|
-
rootPath,
|
278
|
-
options
|
279
|
-
}) {
|
280
|
-
const utilArgs = {
|
281
|
-
transformer: transformer2,
|
282
|
-
rootPath,
|
283
|
-
...options
|
284
|
-
};
|
285
|
-
const log = new Logger(utilArgs);
|
286
|
-
const runner = new Runner_default(utilArgs);
|
287
|
-
return { log, runner };
|
288
|
-
}
|
289
|
-
|
290
|
-
// src/transforms/add-package-manager.ts
|
291
|
-
var TRANSFORMER = "add-package-manager";
|
292
|
-
var DESCRIPTION = "Set the `packageManager` key in root `package.json` file";
|
293
|
-
var INTRODUCED_IN = "1.1.0";
|
294
|
-
function transformer({
|
295
|
-
root,
|
296
|
-
options
|
297
|
-
}) {
|
298
|
-
const { log, runner } = getTransformerHelpers({
|
299
|
-
transformer: TRANSFORMER,
|
300
|
-
rootPath: root,
|
301
|
-
options
|
302
|
-
});
|
303
|
-
log.info(`Set "packageManager" key in root "package.json" file...`);
|
304
|
-
const packageManager = getPackageManager({ directory: root });
|
305
|
-
if (!packageManager) {
|
306
|
-
return runner.abortTransform({
|
307
|
-
reason: `Unable to determine package manager for ${root}`
|
308
|
-
});
|
309
|
-
}
|
310
|
-
let version = null;
|
311
|
-
try {
|
312
|
-
version = getPackageManagerVersion(packageManager, root);
|
313
|
-
} catch (err) {
|
314
|
-
return runner.abortTransform({
|
315
|
-
reason: `Unable to determine package manager version for ${root}`
|
316
|
-
});
|
317
|
-
}
|
318
|
-
const pkgManagerString = `${packageManager}@${version}`;
|
319
|
-
const rootPackageJsonPath = import_path3.default.join(root, "package.json");
|
320
|
-
const rootPackageJson = import_fs_extra2.default.readJsonSync(rootPackageJsonPath);
|
321
|
-
const allWorkspaces = [
|
322
|
-
{
|
323
|
-
name: "package.json",
|
324
|
-
path: root,
|
325
|
-
packageJson: {
|
326
|
-
...rootPackageJson,
|
327
|
-
packageJsonPath: rootPackageJsonPath
|
328
|
-
}
|
329
|
-
}
|
330
|
-
];
|
331
|
-
for (const workspace of allWorkspaces) {
|
332
|
-
const { packageJsonPath, ...pkgJson } = workspace.packageJson;
|
333
|
-
const newJson = { ...pkgJson, packageManager: pkgManagerString };
|
334
|
-
runner.modifyFile({
|
335
|
-
filePath: packageJsonPath,
|
336
|
-
after: newJson
|
337
|
-
});
|
338
|
-
}
|
339
|
-
return runner.finish();
|
340
|
-
}
|
341
|
-
var transformerMeta = {
|
342
|
-
name: `${TRANSFORMER}: ${DESCRIPTION}`,
|
343
|
-
value: TRANSFORMER,
|
344
|
-
introducedIn: INTRODUCED_IN,
|
345
|
-
transformer
|
346
|
-
};
|
347
|
-
var add_package_manager_default = transformerMeta;
|
348
|
-
// Annotate the CommonJS export names for ESM import in node:
|
349
|
-
0 && (module.exports = {
|
350
|
-
transformer
|
351
|
-
});
|
1
|
+
"use strict";var $=Object.create;var u=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var K=(r,e)=>{for(var t in e)u(r,t,{get:e[t],enumerable:!0})},j=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of V(e))!G.call(r,o)&&o!==t&&u(r,o,{get:()=>e[o],enumerable:!(n=H(e,o))||n.enumerable});return r};var a=(r,e,t)=>(t=r!=null?$(W(r)):{},j(e||!r||!r.__esModule?u(t,"default",{value:r,enumerable:!0}):t,r)),_=r=>j(u({},"__esModule",{value:!0}),r);var X={};K(X,{default:()=>Q,transformer:()=>O});module.exports=_(X);var N=a(require("path")),J=a(require("fs-extra"));var v=a(require("find-up")),S=a(require("path")),m={};function P({directory:r}){let e=r||process.cwd();if(m[e])return m[e];let t=v.default.sync(["yarn.lock","pnpm-lock.yaml","package-lock.json"],{cwd:e});if(!!t){switch(S.default.basename(t)){case"yarn.lock":m[e]="yarn";break;case"pnpm-lock.yaml":m[e]="pnpm";break;case"package-lock.json":m[e]="npm";break}return m[e]}}var y=require("child_process");function w(r,e){switch(r){case"yarn":return(0,y.execSync)("yarn --version",{cwd:e}).toString().trim();case"pnpm":return(0,y.execSync)("pnpm --version",{cwd:e}).toString().trim();case"npm":return(0,y.execSync)("npm --version",{cwd:e}).toString().trim()}}var p=a(require("chalk"));var k=a(require("chalk")),b=require("diff"),d=a(require("fs-extra")),x=a(require("os")),R=a(require("path")),h=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{R.default.extname(e.filePath)===".json"?this.before=d.default.readJsonSync(e.filePath):this.before=d.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 R.default.relative(this.rootPath,this.filePath)}write(){this.after&&(typeof this.after=="object"?d.default.writeJsonSync(this.filePath,this.after,{spaces:2}):d.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(k.default.green(t.value)):t.removed?process.stdout.write(k.default.red(t.value)):process.stdout.write(k.default.dim(t.value))}),console.log(x.default.EOL)):console.log(this.after)}};var i=a(require("chalk")),l=class{constructor(e){this.transform=e.transformer,this.dry=e.dry}modified(...e){console.log(i.default.green(" MODIFIED "),...e,this.dry?i.default.dim("(dry run)"):"")}unchanged(...e){console.log(i.default.gray(" UNCHANGED "),...e,this.dry?i.default.dim("(dry run)"):"")}skipped(...e){console.log(i.default.yellow(" SKIPPED "),...e,this.dry?i.default.dim("(dry run)"):"")}error(...e){console.log(i.default.red(" ERROR "),...e,this.dry?i.default.dim("(dry run)"):"")}info(...e){console.log(i.default.bold(" INFO "),...e,this.dry?i.default.dim("(dry run)"):"")}};var T=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 h({rootPath:this.rootPath,...e})}finish(){let e={changes:{}};return Object.keys(this.modifications).forEach(n=>{let o=this.modifications[n],s={action:"unchanged",additions:o.additions(),deletions:o.deletions()};if(o.hasChanges()){if(this.dry)s.action="skipped",this.logger.skipped(p.default.dim(o.fileName()));else try{o.write(),s.action="modified",this.logger.modified(p.default.bold(o.fileName()))}catch(f){let g="Unknown error";f instanceof Error&&(g=f.message),s.error=new Error(g),s.action="error",this.logger.error(o.fileName(),g)}this.print&&o.log({diff:!0})}else this.logger.unchanged(p.default.dim(o.fileName()));e.changes[o.fileName()]=s}),Object.keys(e.changes).some(n=>e.changes[n].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(p.default.bold("Results:"));let n={};t.forEach(o=>{var f;let s=e.changes[o];n[o]={action:s.action,additions:s.additions,deletions:s.deletions,error:((f=s.error)==null?void 0:f.message)||"None"}}),console.table(n),console.log()}}},F=T;function A({transformer:r,rootPath:e,options:t}){let n={transformer:r,rootPath:e,...t},o=new l(n),s=new F(n);return{log:o,runner:s}}var M="add-package-manager",q="Set the `packageManager` key in root `package.json` file",z="1.1.0";function O({root:r,options:e}){let{log:t,runner:n}=A({transformer:M,rootPath:r,options:e});t.info('Set "packageManager" key in root "package.json" file...');let o=P({directory:r});if(!o)return n.abortTransform({reason:`Unable to determine package manager for ${r}`});let s=null;try{s=w(o,r)}catch{return n.abortTransform({reason:`Unable to determine package manager version for ${r}`})}let f=`${o}@${s}`,g=N.default.join(r,"package.json"),U=J.default.readJsonSync(g),I=[{name:"package.json",path:r,packageJson:{...U,packageJsonPath:g}}];for(let E of I){let{packageJsonPath:C,...D}=E.packageJson,L={...D,packageManager:f};n.modifyFile({filePath:C,after:L})}return n.finish()}var B={name:`${M}: ${q}`,value:M,introducedIn:z,transformer:O},Q=B;0&&(module.exports={transformer});
|
@@ -1,301 +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/create-turbo-config.ts
|
24
|
-
var create_turbo_config_exports = {};
|
25
|
-
__export(create_turbo_config_exports, {
|
26
|
-
default: () => create_turbo_config_default,
|
27
|
-
transformer: () => transformer
|
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");
|
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/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();
|
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;
|
298
|
-
// Annotate the CommonJS export names for ESM import in node:
|
299
|
-
0 && (module.exports = {
|
300
|
-
transformer
|
301
|
-
});
|
1
|
+
"use strict";var x=Object.create;var u=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var J=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var U=(r,e)=>{for(var t in e)u(r,t,{get:e[t],enumerable:!0})},N=(r,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of I(e))!D.call(r,o)&&o!==t&&u(r,o,{get:()=>e[o],enumerable:!(s=S(e,o))||s.enumerable});return r};var l=(r,e,t)=>(t=r!=null?x(J(r)):{},N(e||!r||!r.__esModule?u(t,"default",{value:r,enumerable:!0}):t,r)),L=r=>N(u({},"__esModule",{value:!0}),r);var K={};U(K,{default:()=>G,transformer:()=>C});module.exports=L(K);var b=l(require("fs-extra")),k=l(require("path"));var g=l(require("chalk"));var y=l(require("chalk")),p=require("diff"),c=l(require("fs-extra")),O=l(require("os")),P=l(require("path")),d=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,p.diffJson)(this.before,e.after):this.changes=(0,p.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(O.default.EOL)):console.log(this.after)}};var n=l(require("chalk")),h=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 T=class{constructor(e){this.modifications={};this.transform=e.transformer,this.rootPath=e.rootPath,this.dry=e.dry,this.print=e.print,this.logger=new h(e)}abortTransform(e){return this.logger.error(e.reason),{fatalError:new Error(e.reason),changes:e.changes||{}}}modifyFile(e){this.modifications[e.filePath]=new d({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 f="Unknown error";a instanceof Error&&(f=a.message),i.error=new Error(f),i.action="error",this.logger.error(o.fileName(),f)}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()}}},R=T;function F({transformer:r,rootPath:e,options:t}){let s={transformer:r,rootPath:e,...t},o=new h(s),i=new R(s);return{log:o,runner:i}}var A="create-turbo-config",M='Create the `turbo.json` file from an existing "turbo" key in `package.json`',H="1.1.0";function C({root:r,options:e}){let{log:t,runner:s}=F({transformer:A,rootPath:r,options:e});t.info('Migrating "package.json" "turbo" key to "turbo.json" file...');let o=k.default.join(r,"turbo.json"),i=k.default.join(r,"package.json");if(!b.default.existsSync(i))return s.abortTransform({reason:`No package.json found at ${r}. Is the path correct?`});let a=b.default.readJsonSync(i),f=null;try{f=b.default.readJSONSync(o)}catch{f=null}let E=a,j=f;if(!f&&a.turbo){let{turbo:w,...v}=a;j=w,E=v}return s.modifyFile({filePath:o,after:j}),s.modifyFile({filePath:i,after:E}),s.finish()}var $={name:`${A}: ${M}`,value:A,introducedIn:H,transformer:C},G=$;0&&(module.exports={transformer});
|