@kenkaiiii/gg-pixel 4.3.92 → 4.3.94

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.
package/dist/cli.js CHANGED
@@ -149,7 +149,9 @@ async function safeText(r) {
149
149
  }
150
150
  function detectPackageManager(projectRoot) {
151
151
  if (existsSync(join(projectRoot, "pnpm-lock.yaml"))) return "pnpm";
152
- if (existsSync(join(projectRoot, "bun.lockb"))) return "bun";
152
+ if (existsSync(join(projectRoot, "bun.lock")) || existsSync(join(projectRoot, "bun.lockb"))) {
153
+ return "bun";
154
+ }
153
155
  if (existsSync(join(projectRoot, "yarn.lock"))) return "yarn";
154
156
  return "npm";
155
157
  }
@@ -461,6 +463,113 @@ function injectNextClientComponent(layoutPath, clientInitPath) {
461
463
  let spec = relative(fromDir, clientInitPath).split(sep).join("/");
462
464
  if (!spec.startsWith(".")) spec = "./" + spec;
463
465
  spec = spec.replace(/\.tsx$/, "");
466
+ const astResult = injectClientComponentViaAst(layoutPath, content, spec);
467
+ if (astResult) return astResult;
468
+ return injectClientComponentViaRegex(layoutPath, content, spec);
469
+ }
470
+ function injectClientComponentViaAst(layoutPath, content, importSpec) {
471
+ let recast;
472
+ let bp;
473
+ let bt;
474
+ try {
475
+ recast = nodeRequire("recast");
476
+ bp = nodeRequire("@babel/parser");
477
+ bt = nodeRequire("@babel/types");
478
+ } catch {
479
+ return null;
480
+ }
481
+ let ast;
482
+ try {
483
+ ast = recast.parse(content, {
484
+ parser: {
485
+ parse: (src) => bp.parse(src, {
486
+ sourceType: "module",
487
+ plugins: ["jsx", "typescript"],
488
+ allowImportExportEverywhere: true,
489
+ tokens: true
490
+ })
491
+ }
492
+ });
493
+ } catch {
494
+ return null;
495
+ }
496
+ const program = ast.program;
497
+ if (!program || !Array.isArray(program.body)) return null;
498
+ const bodyEl = findFirstJsxElementByName(ast, "body");
499
+ if (!bodyEl) return null;
500
+ const newComponent = bt.jsxElement(
501
+ bt.jsxOpeningElement(bt.jsxIdentifier("GGPixelClient"), [], true),
502
+ null,
503
+ [],
504
+ true
505
+ );
506
+ const leadingText = bt.jsxText("\n ");
507
+ const trailingText = bt.jsxText("\n ");
508
+ bodyEl.children = [leadingText, newComponent, trailingText, ...bodyEl.children];
509
+ const importDecl = bt.importDeclaration(
510
+ [bt.importDefaultSpecifier(bt.identifier("GGPixelClient"))],
511
+ bt.stringLiteral(importSpec)
512
+ );
513
+ const body = program.body;
514
+ let insertAt = 0;
515
+ for (let i = 0; i < body.length; i++) {
516
+ if (body[i]?.type === "ImportDeclaration") insertAt = i + 1;
517
+ }
518
+ body.splice(insertAt, 0, importDecl);
519
+ let out;
520
+ try {
521
+ out = recast.print(ast).code;
522
+ } catch {
523
+ return null;
524
+ }
525
+ writeFileSync(layoutPath, out, "utf8");
526
+ return { kind: "injected", entryPath: layoutPath };
527
+ }
528
+ function findFirstJsxElementByName(ast, name) {
529
+ let found = null;
530
+ const seen = /* @__PURE__ */ new WeakSet();
531
+ const MAX_DEPTH = 500;
532
+ const SKIP_KEYS = /* @__PURE__ */ new Set([
533
+ "loc",
534
+ "tokens",
535
+ "original",
536
+ "comments",
537
+ "leadingComments",
538
+ "trailingComments",
539
+ "innerComments",
540
+ "range",
541
+ "start",
542
+ "end"
543
+ ]);
544
+ function walk(node, depth) {
545
+ if (found || !node || typeof node !== "object") return;
546
+ if (depth > MAX_DEPTH) return;
547
+ if (seen.has(node)) return;
548
+ seen.add(node);
549
+ if (Array.isArray(node)) {
550
+ for (const c of node) walk(c, depth + 1);
551
+ return;
552
+ }
553
+ const n = node;
554
+ if (n.type === "JSXElement") {
555
+ const opening = n.openingElement;
556
+ if (opening?.type === "JSXOpeningElement" && opening.name) {
557
+ const namedNode = opening.name;
558
+ if (namedNode.type === "JSXIdentifier" && namedNode.name === name) {
559
+ found = n;
560
+ return;
561
+ }
562
+ }
563
+ }
564
+ for (const key of Object.keys(n)) {
565
+ if (SKIP_KEYS.has(key)) continue;
566
+ walk(n[key], depth + 1);
567
+ }
568
+ }
569
+ walk(ast, 0);
570
+ return found;
571
+ }
572
+ function injectClientComponentViaRegex(layoutPath, content, spec) {
464
573
  const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
465
574
  const lines = content.split("\n");
466
575
  let insertImportAt = 0;