@kenkaiiii/gg-pixel 4.3.92 → 4.3.93

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
@@ -461,6 +461,95 @@ function injectNextClientComponent(layoutPath, clientInitPath) {
461
461
  let spec = relative(fromDir, clientInitPath).split(sep).join("/");
462
462
  if (!spec.startsWith(".")) spec = "./" + spec;
463
463
  spec = spec.replace(/\.tsx$/, "");
464
+ const astResult = injectClientComponentViaAst(layoutPath, content, spec);
465
+ if (astResult) return astResult;
466
+ return injectClientComponentViaRegex(layoutPath, content, spec);
467
+ }
468
+ function injectClientComponentViaAst(layoutPath, content, importSpec) {
469
+ let recast;
470
+ let bp;
471
+ let bt;
472
+ try {
473
+ recast = nodeRequire("recast");
474
+ bp = nodeRequire("@babel/parser");
475
+ bt = nodeRequire("@babel/types");
476
+ } catch {
477
+ return null;
478
+ }
479
+ let ast;
480
+ try {
481
+ ast = recast.parse(content, {
482
+ parser: {
483
+ parse: (src) => bp.parse(src, {
484
+ sourceType: "module",
485
+ plugins: ["jsx", "typescript"],
486
+ allowImportExportEverywhere: true,
487
+ tokens: true
488
+ })
489
+ }
490
+ });
491
+ } catch {
492
+ return null;
493
+ }
494
+ const program = ast.program;
495
+ if (!program || !Array.isArray(program.body)) return null;
496
+ const bodyEl = findFirstJsxElementByName(ast, "body");
497
+ if (!bodyEl) return null;
498
+ const newComponent = bt.jsxElement(
499
+ bt.jsxOpeningElement(bt.jsxIdentifier("GGPixelClient"), [], true),
500
+ null,
501
+ [],
502
+ true
503
+ );
504
+ const leadingText = bt.jsxText("\n ");
505
+ const trailingText = bt.jsxText("\n ");
506
+ bodyEl.children = [leadingText, newComponent, trailingText, ...bodyEl.children];
507
+ const importDecl = bt.importDeclaration(
508
+ [bt.importDefaultSpecifier(bt.identifier("GGPixelClient"))],
509
+ bt.stringLiteral(importSpec)
510
+ );
511
+ const body = program.body;
512
+ let insertAt = 0;
513
+ for (let i = 0; i < body.length; i++) {
514
+ if (body[i]?.type === "ImportDeclaration") insertAt = i + 1;
515
+ }
516
+ body.splice(insertAt, 0, importDecl);
517
+ let out;
518
+ try {
519
+ out = recast.print(ast).code;
520
+ } catch {
521
+ return null;
522
+ }
523
+ writeFileSync(layoutPath, out, "utf8");
524
+ return { kind: "injected", entryPath: layoutPath };
525
+ }
526
+ function findFirstJsxElementByName(ast, name) {
527
+ let found = null;
528
+ function walk(node) {
529
+ if (found || !node || typeof node !== "object") return;
530
+ if (Array.isArray(node)) {
531
+ for (const c of node) walk(c);
532
+ return;
533
+ }
534
+ const n = node;
535
+ if (n.type === "JSXElement") {
536
+ const opening = n.openingElement;
537
+ if (opening?.type === "JSXOpeningElement" && opening.name) {
538
+ const namedNode = opening.name;
539
+ if (namedNode.type === "JSXIdentifier" && namedNode.name === name) {
540
+ found = n;
541
+ return;
542
+ }
543
+ }
544
+ }
545
+ for (const key of Object.keys(n)) {
546
+ walk(n[key]);
547
+ }
548
+ }
549
+ walk(ast);
550
+ return found;
551
+ }
552
+ function injectClientComponentViaRegex(layoutPath, content, spec) {
464
553
  const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
465
554
  const lines = content.split("\n");
466
555
  let insertImportAt = 0;