@kenkaiiii/gg-pixel 4.3.91 → 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
@@ -9,9 +9,11 @@ import {
9
9
  mkdirSync,
10
10
  readdirSync
11
11
  } from "fs";
12
+ import { createRequire } from "module";
12
13
  import { homedir } from "os";
13
14
  import { dirname, join, relative, resolve, sep } from "path";
14
15
  import { spawnSync } from "child_process";
16
+ var nodeRequire = createRequire(import.meta.url);
15
17
  var DEFAULT_INGEST_URL = "https://gg-pixel-server.buzzbeamaustralia.workers.dev";
16
18
  async function install(opts = {}) {
17
19
  const cwd = resolve(opts.cwd ?? process.cwd());
@@ -459,6 +461,95 @@ function injectNextClientComponent(layoutPath, clientInitPath) {
459
461
  let spec = relative(fromDir, clientInitPath).split(sep).join("/");
460
462
  if (!spec.startsWith(".")) spec = "./" + spec;
461
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) {
462
553
  const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
463
554
  const lines = content.split("\n");
464
555
  let insertImportAt = 0;
@@ -508,26 +599,85 @@ export default nextConfig;
508
599
  );
509
600
  return;
510
601
  }
511
- const content = readFileSync(configPath, "utf8");
512
- if (content.includes("@kenkaiiii/gg-pixel")) return;
602
+ patchNextConfigViaAst(configPath);
603
+ }
604
+ var PIXEL_PKG = "@kenkaiiii/gg-pixel";
605
+ function patchNextConfigViaAst(configPath) {
606
+ const original = readFileSync(configPath, "utf8");
607
+ if (original.includes(PIXEL_PKG)) return;
608
+ let parseModule;
609
+ let generateCode;
610
+ try {
611
+ const mod = nodeRequire("magicast");
612
+ parseModule = mod.parseModule;
613
+ generateCode = mod.generateCode;
614
+ } catch {
615
+ return patchNextConfigViaRegex(configPath, original);
616
+ }
617
+ let module_;
618
+ try {
619
+ module_ = parseModule(original);
620
+ } catch {
621
+ return patchNextConfigViaRegex(configPath, original);
622
+ }
623
+ const cfg = resolveNextConfigObject(module_);
624
+ if (!cfg) {
625
+ return patchNextConfigViaRegex(configPath, original);
626
+ }
627
+ const existing = cfg.serverExternalPackages;
628
+ const isArrayLike = Array.isArray(existing) || typeof existing === "object" && existing !== null && existing.$type === "array";
629
+ if (isArrayLike) {
630
+ const arr = existing;
631
+ if (arr.includes(PIXEL_PKG)) return;
632
+ arr.push(PIXEL_PKG);
633
+ } else {
634
+ cfg.serverExternalPackages = [PIXEL_PKG];
635
+ }
636
+ const out = generateCode(module_).code;
637
+ if (out !== original) writeFileSync(configPath, out, "utf8");
638
+ }
639
+ function resolveNextConfigObject(mod) {
640
+ const root = mod.exports.default ?? mod.exports;
641
+ if (!root) return null;
642
+ return unwrapWrappers(root);
643
+ }
644
+ function unwrapWrappers(node) {
645
+ let cur = node;
646
+ for (let i = 0; i < 6; i++) {
647
+ if (cur.$type === "function-call") {
648
+ const args = cur.$args ?? [];
649
+ const objArg = args.find(
650
+ (a) => typeof a === "object" && a !== null && a.$type !== "function-call"
651
+ );
652
+ if (!objArg) return null;
653
+ cur = objArg;
654
+ continue;
655
+ }
656
+ if (cur.$type === "object" || cur.$type === void 0) return cur;
657
+ return null;
658
+ }
659
+ return null;
660
+ }
661
+ function patchNextConfigViaRegex(configPath, content) {
662
+ if (content.includes(PIXEL_PKG)) return;
513
663
  if (content.includes("serverExternalPackages")) {
514
664
  const updated = content.replace(
515
665
  /serverExternalPackages\s*:\s*\[([^\]]*)\]/,
516
666
  (_match, inside) => {
517
667
  const trimmed = inside.trim();
518
668
  const sep2 = trimmed.length > 0 ? ", " : "";
519
- return `serverExternalPackages: [${trimmed}${sep2}"@kenkaiiii/gg-pixel"]`;
669
+ return `serverExternalPackages: [${trimmed}${sep2}${JSON.stringify(PIXEL_PKG)}]`;
520
670
  }
521
671
  );
522
672
  if (updated !== content) writeFileSync(configPath, updated, "utf8");
523
673
  return;
524
674
  }
525
- const objStart = /(const\s+\w+\s*:\s*NextConfig\s*=\s*\{|module\.exports\s*=\s*\{|export\s+default\s*\{)/;
675
+ const objStart = /(const\s+\w+\s*(?::\s*\w+)?\s*=\s*\{|module\.exports\s*=\s*\{|export\s+default\s*\{)/;
526
676
  const m = objStart.exec(content);
527
677
  if (m) {
528
678
  const insertAt = m.index + m[0].length;
529
679
  const updated = content.slice(0, insertAt) + `
530
- serverExternalPackages: ["@kenkaiiii/gg-pixel"],` + content.slice(insertAt);
680
+ serverExternalPackages: [${JSON.stringify(PIXEL_PKG)}],` + content.slice(insertAt);
531
681
  writeFileSync(configPath, updated, "utf8");
532
682
  }
533
683
  }