@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/index.js CHANGED
@@ -429,9 +429,11 @@ import {
429
429
  mkdirSync as mkdirSync2,
430
430
  readdirSync
431
431
  } from "fs";
432
+ import { createRequire as createRequire2 } from "module";
432
433
  import { homedir as homedir2 } from "os";
433
434
  import { dirname as dirname2, join as join2, relative, resolve, sep } from "path";
434
435
  import { spawnSync as spawnSync2 } from "child_process";
436
+ var nodeRequire = createRequire2(import.meta.url);
435
437
  var DEFAULT_INGEST_URL = "https://gg-pixel-server.buzzbeamaustralia.workers.dev";
436
438
  async function install(opts = {}) {
437
439
  const cwd = resolve(opts.cwd ?? process.cwd());
@@ -879,6 +881,95 @@ function injectNextClientComponent(layoutPath, clientInitPath) {
879
881
  let spec = relative(fromDir, clientInitPath).split(sep).join("/");
880
882
  if (!spec.startsWith(".")) spec = "./" + spec;
881
883
  spec = spec.replace(/\.tsx$/, "");
884
+ const astResult = injectClientComponentViaAst(layoutPath, content, spec);
885
+ if (astResult) return astResult;
886
+ return injectClientComponentViaRegex(layoutPath, content, spec);
887
+ }
888
+ function injectClientComponentViaAst(layoutPath, content, importSpec) {
889
+ let recast;
890
+ let bp;
891
+ let bt;
892
+ try {
893
+ recast = nodeRequire("recast");
894
+ bp = nodeRequire("@babel/parser");
895
+ bt = nodeRequire("@babel/types");
896
+ } catch {
897
+ return null;
898
+ }
899
+ let ast;
900
+ try {
901
+ ast = recast.parse(content, {
902
+ parser: {
903
+ parse: (src) => bp.parse(src, {
904
+ sourceType: "module",
905
+ plugins: ["jsx", "typescript"],
906
+ allowImportExportEverywhere: true,
907
+ tokens: true
908
+ })
909
+ }
910
+ });
911
+ } catch {
912
+ return null;
913
+ }
914
+ const program = ast.program;
915
+ if (!program || !Array.isArray(program.body)) return null;
916
+ const bodyEl = findFirstJsxElementByName(ast, "body");
917
+ if (!bodyEl) return null;
918
+ const newComponent = bt.jsxElement(
919
+ bt.jsxOpeningElement(bt.jsxIdentifier("GGPixelClient"), [], true),
920
+ null,
921
+ [],
922
+ true
923
+ );
924
+ const leadingText = bt.jsxText("\n ");
925
+ const trailingText = bt.jsxText("\n ");
926
+ bodyEl.children = [leadingText, newComponent, trailingText, ...bodyEl.children];
927
+ const importDecl = bt.importDeclaration(
928
+ [bt.importDefaultSpecifier(bt.identifier("GGPixelClient"))],
929
+ bt.stringLiteral(importSpec)
930
+ );
931
+ const body = program.body;
932
+ let insertAt = 0;
933
+ for (let i = 0; i < body.length; i++) {
934
+ if (body[i]?.type === "ImportDeclaration") insertAt = i + 1;
935
+ }
936
+ body.splice(insertAt, 0, importDecl);
937
+ let out;
938
+ try {
939
+ out = recast.print(ast).code;
940
+ } catch {
941
+ return null;
942
+ }
943
+ writeFileSync(layoutPath, out, "utf8");
944
+ return { kind: "injected", entryPath: layoutPath };
945
+ }
946
+ function findFirstJsxElementByName(ast, name) {
947
+ let found = null;
948
+ function walk(node) {
949
+ if (found || !node || typeof node !== "object") return;
950
+ if (Array.isArray(node)) {
951
+ for (const c of node) walk(c);
952
+ return;
953
+ }
954
+ const n = node;
955
+ if (n.type === "JSXElement") {
956
+ const opening = n.openingElement;
957
+ if (opening?.type === "JSXOpeningElement" && opening.name) {
958
+ const namedNode = opening.name;
959
+ if (namedNode.type === "JSXIdentifier" && namedNode.name === name) {
960
+ found = n;
961
+ return;
962
+ }
963
+ }
964
+ }
965
+ for (const key of Object.keys(n)) {
966
+ walk(n[key]);
967
+ }
968
+ }
969
+ walk(ast);
970
+ return found;
971
+ }
972
+ function injectClientComponentViaRegex(layoutPath, content, spec) {
882
973
  const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
883
974
  const lines = content.split("\n");
884
975
  let insertImportAt = 0;
@@ -928,26 +1019,85 @@ export default nextConfig;
928
1019
  );
929
1020
  return;
930
1021
  }
931
- const content = readFileSync2(configPath, "utf8");
932
- if (content.includes("@kenkaiiii/gg-pixel")) return;
1022
+ patchNextConfigViaAst(configPath);
1023
+ }
1024
+ var PIXEL_PKG = "@kenkaiiii/gg-pixel";
1025
+ function patchNextConfigViaAst(configPath) {
1026
+ const original = readFileSync2(configPath, "utf8");
1027
+ if (original.includes(PIXEL_PKG)) return;
1028
+ let parseModule;
1029
+ let generateCode;
1030
+ try {
1031
+ const mod = nodeRequire("magicast");
1032
+ parseModule = mod.parseModule;
1033
+ generateCode = mod.generateCode;
1034
+ } catch {
1035
+ return patchNextConfigViaRegex(configPath, original);
1036
+ }
1037
+ let module_;
1038
+ try {
1039
+ module_ = parseModule(original);
1040
+ } catch {
1041
+ return patchNextConfigViaRegex(configPath, original);
1042
+ }
1043
+ const cfg = resolveNextConfigObject(module_);
1044
+ if (!cfg) {
1045
+ return patchNextConfigViaRegex(configPath, original);
1046
+ }
1047
+ const existing = cfg.serverExternalPackages;
1048
+ const isArrayLike = Array.isArray(existing) || typeof existing === "object" && existing !== null && existing.$type === "array";
1049
+ if (isArrayLike) {
1050
+ const arr = existing;
1051
+ if (arr.includes(PIXEL_PKG)) return;
1052
+ arr.push(PIXEL_PKG);
1053
+ } else {
1054
+ cfg.serverExternalPackages = [PIXEL_PKG];
1055
+ }
1056
+ const out = generateCode(module_).code;
1057
+ if (out !== original) writeFileSync(configPath, out, "utf8");
1058
+ }
1059
+ function resolveNextConfigObject(mod) {
1060
+ const root = mod.exports.default ?? mod.exports;
1061
+ if (!root) return null;
1062
+ return unwrapWrappers(root);
1063
+ }
1064
+ function unwrapWrappers(node) {
1065
+ let cur = node;
1066
+ for (let i = 0; i < 6; i++) {
1067
+ if (cur.$type === "function-call") {
1068
+ const args = cur.$args ?? [];
1069
+ const objArg = args.find(
1070
+ (a) => typeof a === "object" && a !== null && a.$type !== "function-call"
1071
+ );
1072
+ if (!objArg) return null;
1073
+ cur = objArg;
1074
+ continue;
1075
+ }
1076
+ if (cur.$type === "object" || cur.$type === void 0) return cur;
1077
+ return null;
1078
+ }
1079
+ return null;
1080
+ }
1081
+ function patchNextConfigViaRegex(configPath, content) {
1082
+ if (content.includes(PIXEL_PKG)) return;
933
1083
  if (content.includes("serverExternalPackages")) {
934
1084
  const updated = content.replace(
935
1085
  /serverExternalPackages\s*:\s*\[([^\]]*)\]/,
936
1086
  (_match, inside) => {
937
1087
  const trimmed = inside.trim();
938
1088
  const sep2 = trimmed.length > 0 ? ", " : "";
939
- return `serverExternalPackages: [${trimmed}${sep2}"@kenkaiiii/gg-pixel"]`;
1089
+ return `serverExternalPackages: [${trimmed}${sep2}${JSON.stringify(PIXEL_PKG)}]`;
940
1090
  }
941
1091
  );
942
1092
  if (updated !== content) writeFileSync(configPath, updated, "utf8");
943
1093
  return;
944
1094
  }
945
- const objStart = /(const\s+\w+\s*:\s*NextConfig\s*=\s*\{|module\.exports\s*=\s*\{|export\s+default\s*\{)/;
1095
+ const objStart = /(const\s+\w+\s*(?::\s*\w+)?\s*=\s*\{|module\.exports\s*=\s*\{|export\s+default\s*\{)/;
946
1096
  const m = objStart.exec(content);
947
1097
  if (m) {
948
1098
  const insertAt = m.index + m[0].length;
949
1099
  const updated = content.slice(0, insertAt) + `
950
- serverExternalPackages: ["@kenkaiiii/gg-pixel"],` + content.slice(insertAt);
1100
+ serverExternalPackages: [${JSON.stringify(PIXEL_PKG)}],` + content.slice(insertAt);
951
1101
  writeFileSync(configPath, updated, "utf8");
952
1102
  }
953
1103
  }