@valbuild/init 0.51.0

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.
@@ -0,0 +1,1373 @@
1
+ 'use strict';
2
+
3
+ var meow = require('meow');
4
+ var chalk = require('chalk');
5
+ var fs = require('fs');
6
+ var path = require('path');
7
+ var simpleGit = require('simple-git');
8
+ var prompts = require('@inquirer/prompts');
9
+ var j = require('jscodeshift');
10
+ var diff = require('diff');
11
+ var semver = require('semver');
12
+
13
+ function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
14
+
15
+ var meow__default = /*#__PURE__*/_interopDefault(meow);
16
+ var chalk__default = /*#__PURE__*/_interopDefault(chalk);
17
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
18
+ var path__default = /*#__PURE__*/_interopDefault(path);
19
+ var simpleGit__default = /*#__PURE__*/_interopDefault(simpleGit);
20
+ var j__default = /*#__PURE__*/_interopDefault(j);
21
+ var semver__default = /*#__PURE__*/_interopDefault(semver);
22
+
23
+ function _regeneratorRuntime() {
24
+ _regeneratorRuntime = function () {
25
+ return e;
26
+ };
27
+ var t,
28
+ e = {},
29
+ r = Object.prototype,
30
+ n = r.hasOwnProperty,
31
+ o = Object.defineProperty || function (t, e, r) {
32
+ t[e] = r.value;
33
+ },
34
+ i = "function" == typeof Symbol ? Symbol : {},
35
+ a = i.iterator || "@@iterator",
36
+ c = i.asyncIterator || "@@asyncIterator",
37
+ u = i.toStringTag || "@@toStringTag";
38
+ function define(t, e, r) {
39
+ return Object.defineProperty(t, e, {
40
+ value: r,
41
+ enumerable: !0,
42
+ configurable: !0,
43
+ writable: !0
44
+ }), t[e];
45
+ }
46
+ try {
47
+ define({}, "");
48
+ } catch (t) {
49
+ define = function (t, e, r) {
50
+ return t[e] = r;
51
+ };
52
+ }
53
+ function wrap(t, e, r, n) {
54
+ var i = e && e.prototype instanceof Generator ? e : Generator,
55
+ a = Object.create(i.prototype),
56
+ c = new Context(n || []);
57
+ return o(a, "_invoke", {
58
+ value: makeInvokeMethod(t, r, c)
59
+ }), a;
60
+ }
61
+ function tryCatch(t, e, r) {
62
+ try {
63
+ return {
64
+ type: "normal",
65
+ arg: t.call(e, r)
66
+ };
67
+ } catch (t) {
68
+ return {
69
+ type: "throw",
70
+ arg: t
71
+ };
72
+ }
73
+ }
74
+ e.wrap = wrap;
75
+ var h = "suspendedStart",
76
+ l = "suspendedYield",
77
+ f = "executing",
78
+ s = "completed",
79
+ y = {};
80
+ function Generator() {}
81
+ function GeneratorFunction() {}
82
+ function GeneratorFunctionPrototype() {}
83
+ var p = {};
84
+ define(p, a, function () {
85
+ return this;
86
+ });
87
+ var d = Object.getPrototypeOf,
88
+ v = d && d(d(values([])));
89
+ v && v !== r && n.call(v, a) && (p = v);
90
+ var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p);
91
+ function defineIteratorMethods(t) {
92
+ ["next", "throw", "return"].forEach(function (e) {
93
+ define(t, e, function (t) {
94
+ return this._invoke(e, t);
95
+ });
96
+ });
97
+ }
98
+ function AsyncIterator(t, e) {
99
+ function invoke(r, o, i, a) {
100
+ var c = tryCatch(t[r], t, o);
101
+ if ("throw" !== c.type) {
102
+ var u = c.arg,
103
+ h = u.value;
104
+ return h && "object" == typeof h && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) {
105
+ invoke("next", t, i, a);
106
+ }, function (t) {
107
+ invoke("throw", t, i, a);
108
+ }) : e.resolve(h).then(function (t) {
109
+ u.value = t, i(u);
110
+ }, function (t) {
111
+ return invoke("throw", t, i, a);
112
+ });
113
+ }
114
+ a(c.arg);
115
+ }
116
+ var r;
117
+ o(this, "_invoke", {
118
+ value: function (t, n) {
119
+ function callInvokeWithMethodAndArg() {
120
+ return new e(function (e, r) {
121
+ invoke(t, n, e, r);
122
+ });
123
+ }
124
+ return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg();
125
+ }
126
+ });
127
+ }
128
+ function makeInvokeMethod(e, r, n) {
129
+ var o = h;
130
+ return function (i, a) {
131
+ if (o === f) throw new Error("Generator is already running");
132
+ if (o === s) {
133
+ if ("throw" === i) throw a;
134
+ return {
135
+ value: t,
136
+ done: !0
137
+ };
138
+ }
139
+ for (n.method = i, n.arg = a;;) {
140
+ var c = n.delegate;
141
+ if (c) {
142
+ var u = maybeInvokeDelegate(c, n);
143
+ if (u) {
144
+ if (u === y) continue;
145
+ return u;
146
+ }
147
+ }
148
+ if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) {
149
+ if (o === h) throw o = s, n.arg;
150
+ n.dispatchException(n.arg);
151
+ } else "return" === n.method && n.abrupt("return", n.arg);
152
+ o = f;
153
+ var p = tryCatch(e, r, n);
154
+ if ("normal" === p.type) {
155
+ if (o = n.done ? s : l, p.arg === y) continue;
156
+ return {
157
+ value: p.arg,
158
+ done: n.done
159
+ };
160
+ }
161
+ "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg);
162
+ }
163
+ };
164
+ }
165
+ function maybeInvokeDelegate(e, r) {
166
+ var n = r.method,
167
+ o = e.iterator[n];
168
+ if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y;
169
+ var i = tryCatch(o, e.iterator, r.arg);
170
+ if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y;
171
+ var a = i.arg;
172
+ return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y);
173
+ }
174
+ function pushTryEntry(t) {
175
+ var e = {
176
+ tryLoc: t[0]
177
+ };
178
+ 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e);
179
+ }
180
+ function resetTryEntry(t) {
181
+ var e = t.completion || {};
182
+ e.type = "normal", delete e.arg, t.completion = e;
183
+ }
184
+ function Context(t) {
185
+ this.tryEntries = [{
186
+ tryLoc: "root"
187
+ }], t.forEach(pushTryEntry, this), this.reset(!0);
188
+ }
189
+ function values(e) {
190
+ if (e || "" === e) {
191
+ var r = e[a];
192
+ if (r) return r.call(e);
193
+ if ("function" == typeof e.next) return e;
194
+ if (!isNaN(e.length)) {
195
+ var o = -1,
196
+ i = function next() {
197
+ for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next;
198
+ return next.value = t, next.done = !0, next;
199
+ };
200
+ return i.next = i;
201
+ }
202
+ }
203
+ throw new TypeError(typeof e + " is not iterable");
204
+ }
205
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", {
206
+ value: GeneratorFunctionPrototype,
207
+ configurable: !0
208
+ }), o(GeneratorFunctionPrototype, "constructor", {
209
+ value: GeneratorFunction,
210
+ configurable: !0
211
+ }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) {
212
+ var e = "function" == typeof t && t.constructor;
213
+ return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name));
214
+ }, e.mark = function (t) {
215
+ return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t;
216
+ }, e.awrap = function (t) {
217
+ return {
218
+ __await: t
219
+ };
220
+ }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () {
221
+ return this;
222
+ }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) {
223
+ void 0 === i && (i = Promise);
224
+ var a = new AsyncIterator(wrap(t, r, n, o), i);
225
+ return e.isGeneratorFunction(r) ? a : a.next().then(function (t) {
226
+ return t.done ? t.value : a.next();
227
+ });
228
+ }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () {
229
+ return this;
230
+ }), define(g, "toString", function () {
231
+ return "[object Generator]";
232
+ }), e.keys = function (t) {
233
+ var e = Object(t),
234
+ r = [];
235
+ for (var n in e) r.push(n);
236
+ return r.reverse(), function next() {
237
+ for (; r.length;) {
238
+ var t = r.pop();
239
+ if (t in e) return next.value = t, next.done = !1, next;
240
+ }
241
+ return next.done = !0, next;
242
+ };
243
+ }, e.values = values, Context.prototype = {
244
+ constructor: Context,
245
+ reset: function (e) {
246
+ if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t);
247
+ },
248
+ stop: function () {
249
+ this.done = !0;
250
+ var t = this.tryEntries[0].completion;
251
+ if ("throw" === t.type) throw t.arg;
252
+ return this.rval;
253
+ },
254
+ dispatchException: function (e) {
255
+ if (this.done) throw e;
256
+ var r = this;
257
+ function handle(n, o) {
258
+ return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o;
259
+ }
260
+ for (var o = this.tryEntries.length - 1; o >= 0; --o) {
261
+ var i = this.tryEntries[o],
262
+ a = i.completion;
263
+ if ("root" === i.tryLoc) return handle("end");
264
+ if (i.tryLoc <= this.prev) {
265
+ var c = n.call(i, "catchLoc"),
266
+ u = n.call(i, "finallyLoc");
267
+ if (c && u) {
268
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
269
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
270
+ } else if (c) {
271
+ if (this.prev < i.catchLoc) return handle(i.catchLoc, !0);
272
+ } else {
273
+ if (!u) throw new Error("try statement without catch or finally");
274
+ if (this.prev < i.finallyLoc) return handle(i.finallyLoc);
275
+ }
276
+ }
277
+ }
278
+ },
279
+ abrupt: function (t, e) {
280
+ for (var r = this.tryEntries.length - 1; r >= 0; --r) {
281
+ var o = this.tryEntries[r];
282
+ if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) {
283
+ var i = o;
284
+ break;
285
+ }
286
+ }
287
+ i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null);
288
+ var a = i ? i.completion : {};
289
+ return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a);
290
+ },
291
+ complete: function (t, e) {
292
+ if ("throw" === t.type) throw t.arg;
293
+ return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y;
294
+ },
295
+ finish: function (t) {
296
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
297
+ var r = this.tryEntries[e];
298
+ if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y;
299
+ }
300
+ },
301
+ catch: function (t) {
302
+ for (var e = this.tryEntries.length - 1; e >= 0; --e) {
303
+ var r = this.tryEntries[e];
304
+ if (r.tryLoc === t) {
305
+ var n = r.completion;
306
+ if ("throw" === n.type) {
307
+ var o = n.arg;
308
+ resetTryEntry(r);
309
+ }
310
+ return o;
311
+ }
312
+ }
313
+ throw new Error("illegal catch attempt");
314
+ },
315
+ delegateYield: function (e, r, n) {
316
+ return this.delegate = {
317
+ iterator: values(e),
318
+ resultName: r,
319
+ nextLoc: n
320
+ }, "next" === this.method && (this.arg = t), y;
321
+ }
322
+ }, e;
323
+ }
324
+
325
+ function _typeof(o) {
326
+ "@babel/helpers - typeof";
327
+
328
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
329
+ return typeof o;
330
+ } : function (o) {
331
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
332
+ }, _typeof(o);
333
+ }
334
+
335
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
336
+ try {
337
+ var info = gen[key](arg);
338
+ var value = info.value;
339
+ } catch (error) {
340
+ reject(error);
341
+ return;
342
+ }
343
+ if (info.done) {
344
+ resolve(value);
345
+ } else {
346
+ Promise.resolve(value).then(_next, _throw);
347
+ }
348
+ }
349
+ function _asyncToGenerator(fn) {
350
+ return function () {
351
+ var self = this,
352
+ args = arguments;
353
+ return new Promise(function (resolve, reject) {
354
+ var gen = fn.apply(self, args);
355
+ function _next(value) {
356
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
357
+ }
358
+ function _throw(err) {
359
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
360
+ }
361
+ _next(undefined);
362
+ });
363
+ };
364
+ }
365
+
366
+ function _arrayWithHoles(arr) {
367
+ if (Array.isArray(arr)) return arr;
368
+ }
369
+
370
+ function _iterableToArrayLimit(r, l) {
371
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
372
+ if (null != t) {
373
+ var e,
374
+ n,
375
+ i,
376
+ u,
377
+ a = [],
378
+ f = !0,
379
+ o = !1;
380
+ try {
381
+ if (i = (t = t.call(r)).next, 0 === l) {
382
+ if (Object(t) !== t) return;
383
+ f = !1;
384
+ } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
385
+ } catch (r) {
386
+ o = !0, n = r;
387
+ } finally {
388
+ try {
389
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
390
+ } finally {
391
+ if (o) throw n;
392
+ }
393
+ }
394
+ return a;
395
+ }
396
+ }
397
+
398
+ function _arrayLikeToArray(arr, len) {
399
+ if (len == null || len > arr.length) len = arr.length;
400
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
401
+ return arr2;
402
+ }
403
+
404
+ function _unsupportedIterableToArray(o, minLen) {
405
+ if (!o) return;
406
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
407
+ var n = Object.prototype.toString.call(o).slice(8, -1);
408
+ if (n === "Object" && o.constructor) n = o.constructor.name;
409
+ if (n === "Map" || n === "Set") return Array.from(o);
410
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
411
+ }
412
+
413
+ function _nonIterableRest() {
414
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
415
+ }
416
+
417
+ function _slicedToArray(arr, i) {
418
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
419
+ }
420
+
421
+ function transformNextAppRouterValProvider(fileInfo, api, options) {
422
+ if (!options.configImportPath) {
423
+ throw new Error("configImportPath is required");
424
+ }
425
+ var root = api.jscodeshift(fileInfo.source);
426
+ root.find(j__default["default"].ImportDeclaration).at(0).insertBefore(j__default["default"].importDeclaration([j__default["default"].importSpecifier(j__default["default"].identifier("ValProvider"))], j__default["default"].literal("@valbuild/next"))).insertBefore(j__default["default"].importDeclaration([j__default["default"].importSpecifier(j__default["default"].identifier("config"))], j__default["default"].literal(options.configImportPath)));
427
+ root.findJSXElements("body").childNodes().forEach(function (el) {
428
+ if (el.value.type === "JSXExpressionContainer") {
429
+ if (el.value.expression.type === "Identifier" && el.value.expression.name === "children") {
430
+ el.replace(j__default["default"].jsxElement({
431
+ name: {
432
+ type: "JSXIdentifier",
433
+ name: "ValProvider"
434
+ },
435
+ type: "JSXOpeningElement",
436
+ selfClosing: false,
437
+ attributes: [{
438
+ type: "JSXAttribute",
439
+ name: {
440
+ type: "JSXIdentifier",
441
+ name: "config"
442
+ },
443
+ value: {
444
+ type: "JSXExpressionContainer",
445
+ expression: {
446
+ type: "Identifier",
447
+ name: "config"
448
+ }
449
+ }
450
+ }]
451
+ }, {
452
+ name: {
453
+ type: "JSXIdentifier",
454
+ name: "ValProvider"
455
+ },
456
+ type: "JSXClosingElement"
457
+ }, [j__default["default"].jsxExpressionContainer(j__default["default"].identifier("children"))]));
458
+ }
459
+ }
460
+ });
461
+ return root.toSource();
462
+ }
463
+
464
+ var packageJson = {
465
+ name: "@valbuild/init",
466
+ version: "0.51.0",
467
+ description: "",
468
+ main: "dist/valbuild-init.cjs.js",
469
+ module: "dist/valbuild-init.esm.js",
470
+ exports: {
471
+ ".": {
472
+ module: "./dist/valbuild-init.esm.js",
473
+ "default": "./dist/valbuild-init.cjs.js"
474
+ },
475
+ "./package.json": "./package.json"
476
+ },
477
+ scripts: {
478
+ test: "jest",
479
+ build: "ncc build ./src/index.ts -o ./out/ --minify --no-cache --no-source-map-register"
480
+ },
481
+ bin: {
482
+ "@valbuild/init": "./out/index.js"
483
+ },
484
+ dependencies: {
485
+ "@inquirer/confirm": "^2.0.15",
486
+ "@inquirer/prompts": "^3.0.2",
487
+ "@types/diff": "^5.0.9",
488
+ chalk: "^4.1.2",
489
+ cors: "^2.8.5",
490
+ diff: "^5.1.0",
491
+ eslint: "^8.31.0",
492
+ express: "^4.18.2",
493
+ "fast-glob": "^3.3.1",
494
+ jscodeshift: "^0.15.1",
495
+ meow: "^9.0.0",
496
+ open: "^9.1.0",
497
+ picocolors: "^1.0.0",
498
+ recast: "^0.23.4",
499
+ semver: "^7.5.4",
500
+ "simple-git": "^3.22.0",
501
+ typescript: "^5.3.3",
502
+ zod: "^3.22.4"
503
+ },
504
+ preconstruct: {
505
+ entrypoints: [
506
+ "./index.ts"
507
+ ],
508
+ exports: true
509
+ },
510
+ devDependencies: {
511
+ "@babel/preset-env": "^7.23.8",
512
+ "@babel/preset-typescript": "^7.23.3",
513
+ "@types/jest": "^29.5.11",
514
+ "@types/jscodeshift": "^0.11.11",
515
+ "@vercel/ncc": "^0.38.1",
516
+ jest: "^29.7.0"
517
+ }
518
+ };
519
+
520
+ var VAL_CLIENT = function VAL_CLIENT(configImportPath) {
521
+ return "import \"client-only\";\nimport { initValClient } from \"@valbuild/next/client\";\nimport { config } from \"".concat(configImportPath, "\";\n\nconst { useValStega: useVal } = initValClient(config);\n\nexport { useVal };\n");
522
+ };
523
+ var VAL_SERVER = function VAL_SERVER(configImportPath) {
524
+ return "import \"server-only\";\nimport { initValServer } from \"@valbuild/next/server\";\nimport { config } from \"".concat(configImportPath, "\";\nimport { draftMode } from \"next/headers\";\n\nconst { valNextAppRouter } = initValServer(\n { ...config },\n {\n draftMode,\n }\n);\n\nexport { valNextAppRouter };\n");
525
+ };
526
+
527
+ // TODO: use Val config
528
+
529
+ var VAL_CONFIG = function VAL_CONFIG(options) {
530
+ return "import { initVal } from \"@valbuild/next\";\n\nconst { s, val, config } = initVal(".concat(JSON.stringify(options, null, 2), ");\n\nexport { s, val, config };\n");
531
+ };
532
+ var VAL_API_ROUTER = function VAL_API_ROUTER(valServerPath) {
533
+ return "import { valNextAppRouter } from \"".concat(valServerPath, "\";\n\nexport const GET = valNextAppRouter;\nexport const POST = valNextAppRouter;\nexport const PATCH = valNextAppRouter;\nexport const DELETE = valNextAppRouter;\nexport const PUT = valNextAppRouter;\nexport const HEAD = valNextAppRouter;\n");
534
+ };
535
+ var VAL_APP_PAGE = function VAL_APP_PAGE(configImportPath) {
536
+ return "import { ValApp } from \"@valbuild/next\";\nimport { config } from \"".concat(configImportPath, "\";\n\nexport default function Val() {\n return <ValApp config={config} />;\n}\n");
537
+ };
538
+
539
+ function error(message) {
540
+ console.error(chalk__default["default"].red("❌ ERROR: ") + message);
541
+ }
542
+ function warn(message) {
543
+ console.error(chalk__default["default"].yellow("⚠️ WARN:") + message);
544
+ }
545
+ function info(message) {
546
+ var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
547
+ if (opts.isCodeSnippet) {
548
+ console.log(chalk__default["default"].cyanBright("$ > ") + chalk__default["default"].cyan(message));
549
+ return;
550
+ }
551
+ if (opts.isGood) {
552
+ console.log(chalk__default["default"].green("✅ ") + message);
553
+ return;
554
+ }
555
+ console.log(message);
556
+ }
557
+
558
+ var MIN_VAL_VERSION = packageJson.version;
559
+ var MIN_NEXT_VERSION = "13.4.0";
560
+ var maxResetLength = 0;
561
+ function init() {
562
+ return _init.apply(this, arguments);
563
+ }
564
+ function _init() {
565
+ _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
566
+ var root,
567
+ _ref,
568
+ defaultAnswers,
569
+ analysis,
570
+ currentPlan,
571
+ _args2 = arguments;
572
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
573
+ while (1) switch (_context2.prev = _context2.next) {
574
+ case 0:
575
+ root = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : process.cwd();
576
+ _ref = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {}, defaultAnswers = _ref.yes;
577
+ info('Initializing Val in "' + root + '"...\n');
578
+ process.stdout.write("Analyzing project...");
579
+ _context2.next = 6;
580
+ return analyze(path__default["default"].resolve(root), walk(path__default["default"].resolve(root)));
581
+ case 6:
582
+ analysis = _context2.sent;
583
+ // reset cursor:
584
+ process.stdout.write("\x1b[0G");
585
+ info("Analysis:" + " ".repeat(maxResetLength));
586
+ _context2.next = 11;
587
+ return plan(analysis, defaultAnswers);
588
+ case 11:
589
+ currentPlan = _context2.sent;
590
+ if (!currentPlan.abort) {
591
+ _context2.next = 14;
592
+ break;
593
+ }
594
+ return _context2.abrupt("return");
595
+ case 14:
596
+ _context2.next = 16;
597
+ return execute(currentPlan);
598
+ case 16:
599
+ case "end":
600
+ return _context2.stop();
601
+ }
602
+ }, _callee2);
603
+ }));
604
+ return _init.apply(this, arguments);
605
+ }
606
+ var sep = "/";
607
+ function walk(dir) {
608
+ var skip = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : /node_modules|.git/;
609
+ if (!fs__default["default"].existsSync(dir)) return [];
610
+ process.stdout.write("\x1b[0G");
611
+ var m = "Analyzing project... " + (dir.length > 30 ? "..." : "") + dir.slice(Math.max(dir.length - 30, 0));
612
+ maxResetLength = Math.max(maxResetLength, m.length);
613
+ process.stdout.write(m + " ".repeat(maxResetLength - m.length));
614
+ return fs__default["default"].readdirSync(dir).reduce(function (files, fileOrDirName) {
615
+ var fileOrDirPath = [dir, fileOrDirName].join("/"); // always use / as path separator - should work on windows as well?
616
+ if (fs__default["default"].statSync(fileOrDirPath).isDirectory() && !skip.test(fileOrDirName)) {
617
+ return files.concat(walk(fileOrDirPath));
618
+ }
619
+ return files.concat(fileOrDirPath);
620
+ }, []);
621
+ }
622
+ var analyze = /*#__PURE__*/function () {
623
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(root, files) {
624
+ var analysis, packageJsonPath, packageJsonText, _packageJson, _semver$minVersion, minNextVersion, _semver$minVersion2, minValVersion, pagesRouterAppPath, appRouterLayoutPath, git, gitStatus, gitRemoteOrigin;
625
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
626
+ while (1) switch (_context.prev = _context.next) {
627
+ case 0:
628
+ if (fs__default["default"].existsSync(root)) {
629
+ _context.next = 2;
630
+ break;
631
+ }
632
+ return _context.abrupt("return", {});
633
+ case 2:
634
+ analysis = {
635
+ root: root
636
+ };
637
+ packageJsonPath = files.find(function (file) {
638
+ return file === [root, "package.json"].join(sep);
639
+ });
640
+ analysis.packageJsonDir = packageJsonPath && path__default["default"].dirname(packageJsonPath);
641
+ if (!packageJsonPath) {
642
+ _context.next = 20;
643
+ break;
644
+ }
645
+ packageJsonText = fs__default["default"].readFileSync(packageJsonPath, "utf8");
646
+ if (!packageJsonText) {
647
+ _context.next = 20;
648
+ break;
649
+ }
650
+ _context.prev = 8;
651
+ _packageJson = JSON.parse(packageJsonText);
652
+ analysis.isValInstalled = !!_packageJson.dependencies["@valbuild/next"];
653
+ analysis.isNextInstalled = !!_packageJson.dependencies["next"];
654
+ analysis.valEslintVersion = _packageJson.devDependencies["@valbuild/eslint-plugin"] || _packageJson.dependencies["@valbuild/eslint-plugin"];
655
+ analysis.nextVersion = _packageJson.dependencies["next"];
656
+ analysis.valVersion = _packageJson.dependencies["@valbuild/next"];
657
+ _context.next = 20;
658
+ break;
659
+ case 17:
660
+ _context.prev = 17;
661
+ _context.t0 = _context["catch"](8);
662
+ throw new Error("Failed to parse package.json in file: ".concat(packageJsonPath));
663
+ case 20:
664
+ if (analysis.nextVersion) {
665
+ minNextVersion = (_semver$minVersion = semver__default["default"].minVersion(analysis.nextVersion)) === null || _semver$minVersion === void 0 ? void 0 : _semver$minVersion.version;
666
+ if (minNextVersion) {
667
+ analysis.nextVersionIsSatisfied = semver__default["default"].satisfies(minNextVersion, ">=" + MIN_NEXT_VERSION);
668
+ }
669
+ }
670
+ if (analysis.valVersion) {
671
+ minValVersion = (_semver$minVersion2 = semver__default["default"].minVersion(analysis.valVersion)) === null || _semver$minVersion2 === void 0 ? void 0 : _semver$minVersion2.version;
672
+ if (minValVersion) {
673
+ analysis.valVersionIsSatisfied = semver__default["default"].satisfies(minValVersion, ">=" + MIN_VAL_VERSION);
674
+ }
675
+ }
676
+ analysis.eslintRcJsPath = files.find(function (file) {
677
+ return file.endsWith(".eslintrc.js");
678
+ });
679
+ if (analysis.eslintRcJsPath) {
680
+ analysis.eslintRcJsText = fs__default["default"].readFileSync(analysis.eslintRcJsPath, "utf8");
681
+ if (analysis.eslintRcJsText) {
682
+ // TODO: Evaluate and extract config?
683
+ analysis.isValEslintRulesConfigured = analysis.eslintRcJsText.includes("plugin:@valbuild/recommended");
684
+ }
685
+ }
686
+ analysis.eslintRcJsonPath = files.find(function (file) {
687
+ return file.endsWith(".eslintrc.json");
688
+ }) || files.find(function (file) {
689
+ return file.endsWith(".eslintrc");
690
+ });
691
+ if (analysis.eslintRcJsonPath) {
692
+ analysis.eslintRcJsonText = fs__default["default"].readFileSync(analysis.eslintRcJsonPath, "utf8");
693
+ if (analysis.eslintRcJsonText) {
694
+ // TODO: Parse properly
695
+ analysis.isValEslintRulesConfigured = analysis.eslintRcJsonText.includes("plugin:@valbuild/recommended");
696
+ }
697
+ }
698
+ pagesRouterAppPath = files.find(function (file) {
699
+ return file.endsWith("/pages/_app.tsx");
700
+ }) || files.find(function (file) {
701
+ return file.endsWith("/pages/_app.jsx");
702
+ });
703
+ analysis.pagesRouter = !!pagesRouterAppPath;
704
+ if (pagesRouterAppPath) {
705
+ analysis.isTypescript = !!pagesRouterAppPath.endsWith(".tsx");
706
+ analysis.isJavascript = !!pagesRouterAppPath.endsWith(".jsx");
707
+ analysis.srcDir = path__default["default"].dirname(path__default["default"].dirname(pagesRouterAppPath));
708
+ }
709
+ appRouterLayoutPath = files.find(function (file) {
710
+ return file.endsWith("/app/layout.tsx");
711
+ }) || files.find(function (file) {
712
+ return file.endsWith("/app/layout.jsx");
713
+ });
714
+ if (appRouterLayoutPath) {
715
+ analysis.appRouter = true;
716
+ analysis.appRouterLayoutPath = appRouterLayoutPath;
717
+ analysis.appRouterLayoutFile = fs__default["default"].readFileSync(appRouterLayoutPath, "utf8");
718
+ analysis.isTypescript = !!appRouterLayoutPath.endsWith(".tsx");
719
+ analysis.isJavascript = !!appRouterLayoutPath.endsWith(".jsx");
720
+ analysis.appRouterPath = path__default["default"].dirname(appRouterLayoutPath);
721
+ analysis.srcDir = path__default["default"].dirname(analysis.appRouterPath);
722
+ }
723
+ _context.prev = 31;
724
+ git = simpleGit__default["default"](root);
725
+ _context.next = 35;
726
+ return git.status();
727
+ case 35:
728
+ gitStatus = _context.sent;
729
+ _context.next = 38;
730
+ return git.remote(["-v"]);
731
+ case 38:
732
+ gitRemoteOrigin = _context.sent;
733
+ analysis.hasGit = true;
734
+ analysis.isGitHub = gitRemoteOrigin ? !!gitRemoteOrigin.includes("github.com") : false;
735
+ analysis.isGitClean = gitStatus.isClean();
736
+ _context.next = 46;
737
+ break;
738
+ case 44:
739
+ _context.prev = 44;
740
+ _context.t1 = _context["catch"](31);
741
+ case 46:
742
+ return _context.abrupt("return", analysis);
743
+ case 47:
744
+ case "end":
745
+ return _context.stop();
746
+ }
747
+ }, _callee, null, [[8, 17], [31, 44]]);
748
+ }));
749
+ return function analyze(_x, _x2) {
750
+ return _ref2.apply(this, arguments);
751
+ };
752
+ }();
753
+ function plan(_x3) {
754
+ return _plan.apply(this, arguments);
755
+ }
756
+ function _plan() {
757
+ _plan = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(analysis) {
758
+ var defaultAnswers,
759
+ plan,
760
+ tsconfigJsonPath,
761
+ jsconfigJsonPath,
762
+ answer,
763
+ _answer,
764
+ currentEslintRc,
765
+ parsedEslint,
766
+ _answer2,
767
+ valConfigPath,
768
+ valUtilsDir,
769
+ valUtilsImportPath,
770
+ valServerPath,
771
+ valAppPagePath,
772
+ valPageImportPath,
773
+ valRouterPath,
774
+ valRouterImportPath,
775
+ _answer3,
776
+ _answer4,
777
+ NO_PATCH_WARNING,
778
+ res,
779
+ diff$1,
780
+ s,
781
+ _answer5,
782
+ _answer6,
783
+ _args3 = arguments;
784
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
785
+ while (1) switch (_context3.prev = _context3.next) {
786
+ case 0:
787
+ defaultAnswers = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : false;
788
+ plan = {
789
+ root: analysis.root
790
+ };
791
+ if (!analysis.root) {
792
+ _context3.next = 6;
793
+ break;
794
+ }
795
+ info(" Root: " + analysis.root, {
796
+ isGood: true
797
+ });
798
+ _context3.next = 8;
799
+ break;
800
+ case 6:
801
+ error("Failed to find root directory");
802
+ return _context3.abrupt("return", {
803
+ abort: true
804
+ });
805
+ case 8:
806
+ if (!(!analysis.srcDir || !fs__default["default"].statSync(analysis.srcDir).isDirectory() || !analysis.isNextInstalled)) {
807
+ _context3.next = 11;
808
+ break;
809
+ }
810
+ error("Val requires a Next.js project");
811
+ return _context3.abrupt("return", {
812
+ abort: true
813
+ });
814
+ case 11:
815
+ if (!analysis.srcDir) {
816
+ _context3.next = 15;
817
+ break;
818
+ }
819
+ info(" Source dir: " + analysis.root, {
820
+ isGood: true
821
+ });
822
+ _context3.next = 17;
823
+ break;
824
+ case 15:
825
+ error("Failed to determine source directory");
826
+ return _context3.abrupt("return", {
827
+ abort: true
828
+ });
829
+ case 17:
830
+ if (analysis.isNextInstalled) {
831
+ _context3.next = 20;
832
+ break;
833
+ }
834
+ error("Val requires a Next.js project");
835
+ return _context3.abrupt("return", {
836
+ abort: true
837
+ });
838
+ case 20:
839
+ if (analysis.isValInstalled) {
840
+ _context3.next = 25;
841
+ break;
842
+ }
843
+ error("Install @valbuild/next first");
844
+ return _context3.abrupt("return", {
845
+ abort: true
846
+ });
847
+ case 25:
848
+ info(" Val version: found ".concat(analysis.valVersion, " >= ").concat(MIN_VAL_VERSION), {
849
+ isGood: true
850
+ });
851
+ case 26:
852
+ if (analysis.nextVersionIsSatisfied) {
853
+ _context3.next = 31;
854
+ break;
855
+ }
856
+ error("Val requires Next.js >= ".concat(MIN_NEXT_VERSION, ". Found: ").concat(analysis.nextVersion));
857
+ return _context3.abrupt("return", {
858
+ abort: true
859
+ });
860
+ case 31:
861
+ info(" Next.js version: found ".concat(analysis.nextVersion, " >= ").concat(MIN_NEXT_VERSION), {
862
+ isGood: true
863
+ });
864
+ case 32:
865
+ if (analysis.isTypescript) {
866
+ info(" Use: TypeScript", {
867
+ isGood: true
868
+ });
869
+ plan.useTypescript = true;
870
+ }
871
+ if (analysis.isJavascript) {
872
+ info(" Use: JavaScript", {
873
+ isGood: true
874
+ });
875
+ if (!plan.useTypescript) {
876
+ plan.useJavascript = true;
877
+ }
878
+ }
879
+ if (!analysis.isTypescript) {
880
+ _context3.next = 44;
881
+ break;
882
+ }
883
+ tsconfigJsonPath = path__default["default"].join(analysis.root, "tsconfig.json");
884
+ if (!fs__default["default"].statSync(tsconfigJsonPath).isFile()) {
885
+ _context3.next = 40;
886
+ break;
887
+ }
888
+ info(" tsconfig.json: found", {
889
+ isGood: true
890
+ });
891
+ _context3.next = 42;
892
+ break;
893
+ case 40:
894
+ error("tsconfig.json: Failed to find tsconfig.json");
895
+ return _context3.abrupt("return", {
896
+ abort: true
897
+ });
898
+ case 42:
899
+ _context3.next = 51;
900
+ break;
901
+ case 44:
902
+ jsconfigJsonPath = path__default["default"].join(analysis.root, "jsconfig.json");
903
+ if (!fs__default["default"].statSync(jsconfigJsonPath).isFile()) {
904
+ _context3.next = 49;
905
+ break;
906
+ }
907
+ info(" jsconfig.json: found", {
908
+ isGood: true
909
+ });
910
+ _context3.next = 51;
911
+ break;
912
+ case 49:
913
+ error(" jsconfig.json: failed to find jsconfig.json");
914
+ return _context3.abrupt("return", {
915
+ abort: true
916
+ });
917
+ case 51:
918
+ if (!(analysis.valEslintVersion === undefined)) {
919
+ _context3.next = 65;
920
+ break;
921
+ }
922
+ if (defaultAnswers) {
923
+ _context3.next = 58;
924
+ break;
925
+ }
926
+ _context3.next = 55;
927
+ return prompts.confirm({
928
+ message: "The recommended Val eslint plugin (@valbuild/eslint-plugin) is not installed. Continue?",
929
+ "default": false
930
+ });
931
+ case 55:
932
+ _context3.t0 = _context3.sent;
933
+ _context3.next = 59;
934
+ break;
935
+ case 58:
936
+ _context3.t0 = false;
937
+ case 59:
938
+ answer = _context3.t0;
939
+ if (answer) {
940
+ _context3.next = 63;
941
+ break;
942
+ }
943
+ error("Aborted: the Val eslint plugin is not installed.\n\nInstall the @valbuild/eslint-plugin package with your favorite package manager.\n\nExample:\n\n npm install -D @valbuild/eslint-plugin\n");
944
+ return _context3.abrupt("return", {
945
+ abort: true
946
+ });
947
+ case 63:
948
+ _context3.next = 66;
949
+ break;
950
+ case 65:
951
+ info(" @valbuild/eslint-plugin: installed", {
952
+ isGood: true
953
+ });
954
+ case 66:
955
+ if (analysis.appRouter) {
956
+ info(" Use: App Router", {
957
+ isGood: true
958
+ });
959
+ }
960
+ if (analysis.pagesRouter) {
961
+ info(" Use: Pages Router", {
962
+ isGood: true
963
+ });
964
+ }
965
+ if (analysis.isGitClean) {
966
+ info(" Git state: clean", {
967
+ isGood: true
968
+ });
969
+ }
970
+ if (!analysis.isGitClean) {
971
+ warn(" Git state: dirty");
972
+ }
973
+ if (!analysis.valEslintVersion) {
974
+ _context3.next = 101;
975
+ break;
976
+ }
977
+ if (!analysis.isValEslintRulesConfigured) {
978
+ _context3.next = 75;
979
+ break;
980
+ }
981
+ info(" @valbuild/eslint-plugin rules configured", {
982
+ isGood: true
983
+ });
984
+ _context3.next = 101;
985
+ break;
986
+ case 75:
987
+ if (!analysis.eslintRcJsPath) {
988
+ _context3.next = 79;
989
+ break;
990
+ }
991
+ warn('Cannot patch eslint: found .eslintrc.js but can only patch JSON files (at the moment).\nAdd the following to your eslint config:\n\n "extends": ["plugin:@valbuild/recommended"]\n');
992
+ _context3.next = 101;
993
+ break;
994
+ case 79:
995
+ if (!analysis.eslintRcJsonPath) {
996
+ _context3.next = 100;
997
+ break;
998
+ }
999
+ if (defaultAnswers) {
1000
+ _context3.next = 86;
1001
+ break;
1002
+ }
1003
+ _context3.next = 83;
1004
+ return prompts.confirm({
1005
+ message: "Patch eslintrc.json to use the recommended Val eslint rules?",
1006
+ "default": true
1007
+ });
1008
+ case 83:
1009
+ _context3.t1 = _context3.sent;
1010
+ _context3.next = 87;
1011
+ break;
1012
+ case 86:
1013
+ _context3.t1 = true;
1014
+ case 87:
1015
+ _answer = _context3.t1;
1016
+ if (!_answer) {
1017
+ _context3.next = 98;
1018
+ break;
1019
+ }
1020
+ currentEslintRc = fs__default["default"].readFileSync(analysis.eslintRcJsonPath, "utf-8");
1021
+ parsedEslint = JSON.parse(currentEslintRc);
1022
+ if (!(_typeof(parsedEslint) !== "object")) {
1023
+ _context3.next = 94;
1024
+ break;
1025
+ }
1026
+ error("Could not patch eslint: ".concat(analysis.eslintRcJsonPath, " was not an object"));
1027
+ return _context3.abrupt("return", {
1028
+ abort: true
1029
+ });
1030
+ case 94:
1031
+ if (typeof parsedEslint["extends"] === "string") {
1032
+ parsedEslint["extends"] = [parsedEslint["extends"]];
1033
+ }
1034
+ parsedEslint["extends"] = parsedEslint["extends"] || [];
1035
+ parsedEslint["extends"].push("plugin:@valbuild/recommended");
1036
+ plan.updateEslint = {
1037
+ path: analysis.eslintRcJsonPath,
1038
+ source: JSON.stringify(parsedEslint, null, 2) + "\n"
1039
+ };
1040
+ case 98:
1041
+ _context3.next = 101;
1042
+ break;
1043
+ case 100:
1044
+ warn("Cannot patch eslint: failed to find eslint config file");
1045
+ case 101:
1046
+ if (analysis.isGitClean) {
1047
+ _context3.next = 117;
1048
+ break;
1049
+ }
1050
+ case 102:
1051
+ if (!(plan.ignoreGitDirty === undefined)) {
1052
+ _context3.next = 117;
1053
+ break;
1054
+ }
1055
+ if (defaultAnswers) {
1056
+ _context3.next = 109;
1057
+ break;
1058
+ }
1059
+ _context3.next = 106;
1060
+ return prompts.confirm({
1061
+ message: "You have uncommitted changes. Continue?",
1062
+ "default": false
1063
+ });
1064
+ case 106:
1065
+ _context3.t2 = _context3.sent;
1066
+ _context3.next = 110;
1067
+ break;
1068
+ case 109:
1069
+ _context3.t2 = false;
1070
+ case 110:
1071
+ _answer2 = _context3.t2;
1072
+ plan.ignoreGitDirty = _answer2;
1073
+ if (_answer2) {
1074
+ _context3.next = 115;
1075
+ break;
1076
+ }
1077
+ error("Aborted: git state dirty");
1078
+ return _context3.abrupt("return", {
1079
+ abort: true,
1080
+ ignoreGitDirty: true
1081
+ });
1082
+ case 115:
1083
+ _context3.next = 102;
1084
+ break;
1085
+ case 117:
1086
+ // New required files:
1087
+ valConfigPath = path__default["default"].join(analysis.root, "val.config.ts");
1088
+ plan.createConfigFile = {
1089
+ path: valConfigPath,
1090
+ source: VAL_CONFIG({})
1091
+ };
1092
+ valUtilsDir = path__default["default"].join(analysis.srcDir, "val");
1093
+ valUtilsImportPath = path__default["default"].relative(valUtilsDir, valConfigPath).replace(".js", "").replace(".ts", "");
1094
+ valServerPath = path__default["default"].join(valUtilsDir, analysis.isTypescript ? "val.server.ts" : "val.server.js");
1095
+ plan.createValServer = {
1096
+ path: valServerPath,
1097
+ source: VAL_SERVER(valUtilsImportPath)
1098
+ };
1099
+ if (!analysis.appRouterPath) {
1100
+ warn('Creating a new "app" router');
1101
+ }
1102
+ valAppPagePath = path__default["default"].join(analysis.appRouterPath || path__default["default"].join(analysis.srcDir, "app"), "(val)", "val", analysis.isTypescript ? "page.tsx" : "page.jsx");
1103
+ valPageImportPath = path__default["default"].relative(path__default["default"].dirname(valAppPagePath), valConfigPath).replace(".js", "").replace(".ts", "");
1104
+ plan.createValAppPage = {
1105
+ path: valAppPagePath,
1106
+ source: VAL_APP_PAGE(valPageImportPath)
1107
+ };
1108
+ valRouterPath = path__default["default"].join(analysis.appRouterPath || path__default["default"].join(analysis.srcDir, "app"), "(val)", "api", "val", analysis.isTypescript ? "router.tsx" : "router.jsx");
1109
+ valRouterImportPath = path__default["default"].relative(path__default["default"].dirname(valRouterPath), valServerPath).replace(".js", "").replace(".ts", "");
1110
+ plan.createValRouter = {
1111
+ path: valRouterPath,
1112
+ source: VAL_API_ROUTER(valRouterImportPath)
1113
+ };
1114
+
1115
+ // Util files:
1116
+ case 130:
1117
+ if (!(plan.createValClient === undefined)) {
1118
+ _context3.next = 142;
1119
+ break;
1120
+ }
1121
+ if (defaultAnswers) {
1122
+ _context3.next = 137;
1123
+ break;
1124
+ }
1125
+ _context3.next = 134;
1126
+ return prompts.confirm({
1127
+ message: "Setup useVal for Client Components",
1128
+ "default": true
1129
+ });
1130
+ case 134:
1131
+ _context3.t3 = _context3.sent;
1132
+ _context3.next = 138;
1133
+ break;
1134
+ case 137:
1135
+ _context3.t3 = true;
1136
+ case 138:
1137
+ _answer3 = _context3.t3;
1138
+ if (_answer3) {
1139
+ plan.createValClient = {
1140
+ path: path__default["default"].join(valUtilsDir, analysis.isTypescript ? "val.client.ts" : "val.client.js"),
1141
+ source: VAL_CLIENT(valUtilsImportPath)
1142
+ };
1143
+ } else {
1144
+ plan.createValClient = false;
1145
+ }
1146
+ _context3.next = 130;
1147
+ break;
1148
+ case 142:
1149
+ if (!(plan.createValRsc === undefined)) {
1150
+ _context3.next = 154;
1151
+ break;
1152
+ }
1153
+ if (defaultAnswers) {
1154
+ _context3.next = 149;
1155
+ break;
1156
+ }
1157
+ _context3.next = 146;
1158
+ return prompts.confirm({
1159
+ message: "Setup fetchVal for React Server Components",
1160
+ "default": true
1161
+ });
1162
+ case 146:
1163
+ _context3.t4 = _context3.sent;
1164
+ _context3.next = 150;
1165
+ break;
1166
+ case 149:
1167
+ _context3.t4 = true;
1168
+ case 150:
1169
+ _answer4 = _context3.t4;
1170
+ if (_answer4) {
1171
+ plan.createValRsc = {
1172
+ path: path__default["default"].join(valUtilsDir, analysis.isTypescript ? "val.rsc.ts" : "val.rsc.js"),
1173
+ source: VAL_SERVER(valUtilsImportPath)
1174
+ };
1175
+ } else {
1176
+ plan.createValRsc = false;
1177
+ }
1178
+ _context3.next = 142;
1179
+ break;
1180
+ case 154:
1181
+ if (analysis.eslintRcJsPath) {
1182
+ warn("ESLint config found: " + analysis.eslintRcJsPath);
1183
+ }
1184
+
1185
+ // Patches:
1186
+ NO_PATCH_WARNING = "Remember to manually patch your pages/_app.tsx file to use Val Provider.\n";
1187
+ if (!analysis.appRouterLayoutPath) {
1188
+ _context3.next = 185;
1189
+ break;
1190
+ }
1191
+ if (analysis.appRouterLayoutFile) {
1192
+ _context3.next = 160;
1193
+ break;
1194
+ }
1195
+ error("Failed to read app router layout file");
1196
+ return _context3.abrupt("return", {
1197
+ abort: true
1198
+ });
1199
+ case 160:
1200
+ res = transformNextAppRouterValProvider({
1201
+ path: analysis.appRouterLayoutPath,
1202
+ source: analysis.appRouterLayoutFile
1203
+ }, {
1204
+ j: j__default["default"],
1205
+ jscodeshift: j__default["default"].withParser("tsx"),
1206
+ stats: function stats() {},
1207
+ report: function report() {}
1208
+ }, {
1209
+ configImportPath: path__default["default"].relative(path__default["default"].dirname(analysis.appRouterLayoutPath), valConfigPath).replace(".js", "").replace(".ts", "")
1210
+ });
1211
+ diff$1 = diff.diffLines(analysis.appRouterLayoutFile, res, {});
1212
+ s = "";
1213
+ diff$1.forEach(function (part) {
1214
+ if (part.added) {
1215
+ s += chalk__default["default"].green(part.value);
1216
+ } else if (part.removed) {
1217
+ s += chalk__default["default"].red(part.value);
1218
+ } else {
1219
+ s += part.value;
1220
+ }
1221
+ });
1222
+ if (defaultAnswers) {
1223
+ _context3.next = 170;
1224
+ break;
1225
+ }
1226
+ _context3.next = 167;
1227
+ return prompts.confirm({
1228
+ message: "Automatically patch ".concat(analysis.appRouterLayoutPath, " file?"),
1229
+ "default": true
1230
+ });
1231
+ case 167:
1232
+ _context3.t5 = _context3.sent;
1233
+ _context3.next = 171;
1234
+ break;
1235
+ case 170:
1236
+ _context3.t5 = true;
1237
+ case 171:
1238
+ _answer5 = _context3.t5;
1239
+ if (!_answer5) {
1240
+ _context3.next = 184;
1241
+ break;
1242
+ }
1243
+ if (defaultAnswers) {
1244
+ _context3.next = 179;
1245
+ break;
1246
+ }
1247
+ _context3.next = 176;
1248
+ return prompts.confirm({
1249
+ message: "Do you accept the following patch:\n".concat(s, "\n"),
1250
+ "default": true
1251
+ });
1252
+ case 176:
1253
+ _context3.t6 = _context3.sent;
1254
+ _context3.next = 180;
1255
+ break;
1256
+ case 179:
1257
+ _context3.t6 = true;
1258
+ case 180:
1259
+ _answer6 = _context3.t6;
1260
+ if (!_answer6) {
1261
+ warn(NO_PATCH_WARNING);
1262
+ plan.updateAppLayout = false;
1263
+ } else {
1264
+ plan.updateAppLayout = {
1265
+ path: analysis.appRouterLayoutPath,
1266
+ source: res
1267
+ };
1268
+ }
1269
+ _context3.next = 185;
1270
+ break;
1271
+ case 184:
1272
+ warn(NO_PATCH_WARNING);
1273
+ case 185:
1274
+ if (analysis.pagesRouter) {
1275
+ warn(NO_PATCH_WARNING);
1276
+ }
1277
+ return _context3.abrupt("return", plan);
1278
+ case 187:
1279
+ case "end":
1280
+ return _context3.stop();
1281
+ }
1282
+ }, _callee3);
1283
+ }));
1284
+ return _plan.apply(this, arguments);
1285
+ }
1286
+ function execute(_x4) {
1287
+ return _execute.apply(this, arguments);
1288
+ }
1289
+ function _execute() {
1290
+ _execute = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(plan) {
1291
+ var _i, _Object$entries, _Object$entries$_i, key, fileOp;
1292
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
1293
+ while (1) switch (_context4.prev = _context4.next) {
1294
+ case 0:
1295
+ if (!plan.abort) {
1296
+ _context4.next = 2;
1297
+ break;
1298
+ }
1299
+ return _context4.abrupt("return", warn("Aborted"));
1300
+ case 2:
1301
+ if (plan.root) {
1302
+ _context4.next = 4;
1303
+ break;
1304
+ }
1305
+ return _context4.abrupt("return", error("Failed to find root directory"));
1306
+ case 4:
1307
+ info("Executing...");
1308
+ for (_i = 0, _Object$entries = Object.entries(plan); _i < _Object$entries.length; _i++) {
1309
+ _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), key = _Object$entries$_i[0], fileOp = _Object$entries$_i[1];
1310
+ writeFile(fileOp, plan.root, key.startsWith("update"));
1311
+ }
1312
+ case 6:
1313
+ case "end":
1314
+ return _context4.stop();
1315
+ }
1316
+ }, _callee4);
1317
+ }));
1318
+ return _execute.apply(this, arguments);
1319
+ }
1320
+ function writeFile(fileOp, rootDir, isUpdate) {
1321
+ if (fileOp && typeof fileOp !== "boolean" && typeof fileOp !== "string") {
1322
+ fs__default["default"].mkdirSync(path__default["default"].dirname(fileOp.path), {
1323
+ recursive: true
1324
+ });
1325
+ fs__default["default"].writeFileSync(fileOp.path, fileOp.source);
1326
+ info(" ".concat(isUpdate ? "Patched" : "Created", " file: ").concat(fileOp.path.replace(rootDir, "")), {
1327
+ isGood: true
1328
+ });
1329
+ }
1330
+ }
1331
+
1332
+ function main() {
1333
+ return _main.apply(this, arguments);
1334
+ }
1335
+ function _main() {
1336
+ _main = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
1337
+ var _meow, flags;
1338
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
1339
+ while (1) switch (_context.prev = _context.next) {
1340
+ case 0:
1341
+ _meow = meow__default["default"]("\n Usage\n $ npx @valbuild/init\n Commands\n Initialize Val in a project\n\n Options\n --help Show this message\n --root [root], -r [root] Set project root directory (default process.cwd())\n --yes [yes], -y [yes] Accept all prompts with defaults.\n ", {
1342
+ flags: {
1343
+ root: {
1344
+ type: "string",
1345
+ alias: "r"
1346
+ },
1347
+ yes: {
1348
+ type: "boolean",
1349
+ alias: "y"
1350
+ }
1351
+ },
1352
+ hardRejection: false
1353
+ }), flags = _meow.flags;
1354
+ _context.next = 3;
1355
+ return init(flags.root, {
1356
+ yes: flags.yes
1357
+ });
1358
+ case 3:
1359
+ case "end":
1360
+ return _context.stop();
1361
+ }
1362
+ }, _callee);
1363
+ }));
1364
+ return _main.apply(this, arguments);
1365
+ }
1366
+ void main()["catch"](function (err) {
1367
+ if (err.message.includes("force closed the prompt")) {
1368
+ process.exitCode = 2;
1369
+ return;
1370
+ }
1371
+ error(err instanceof Error ? err.message + "\n" + err.stack : _typeof(err) === "object" ? JSON.stringify(err, null, 2) : err);
1372
+ process.exitCode = 1;
1373
+ });