@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.cjs CHANGED
@@ -456,9 +456,12 @@ var LocalSqliteSink = class {
456
456
 
457
457
  // src/install.ts
458
458
  var import_node_fs3 = require("fs");
459
+ var import_node_module2 = require("module");
459
460
  var import_node_os2 = require("os");
460
461
  var import_node_path2 = require("path");
461
462
  var import_node_child_process2 = require("child_process");
463
+ var import_meta2 = {};
464
+ var nodeRequire = (0, import_node_module2.createRequire)(import_meta2.url);
462
465
  var DEFAULT_INGEST_URL = "https://gg-pixel-server.buzzbeamaustralia.workers.dev";
463
466
  async function install(opts = {}) {
464
467
  const cwd = (0, import_node_path2.resolve)(opts.cwd ?? process.cwd());
@@ -906,6 +909,95 @@ function injectNextClientComponent(layoutPath, clientInitPath) {
906
909
  let spec = (0, import_node_path2.relative)(fromDir, clientInitPath).split(import_node_path2.sep).join("/");
907
910
  if (!spec.startsWith(".")) spec = "./" + spec;
908
911
  spec = spec.replace(/\.tsx$/, "");
912
+ const astResult = injectClientComponentViaAst(layoutPath, content, spec);
913
+ if (astResult) return astResult;
914
+ return injectClientComponentViaRegex(layoutPath, content, spec);
915
+ }
916
+ function injectClientComponentViaAst(layoutPath, content, importSpec) {
917
+ let recast;
918
+ let bp;
919
+ let bt;
920
+ try {
921
+ recast = nodeRequire("recast");
922
+ bp = nodeRequire("@babel/parser");
923
+ bt = nodeRequire("@babel/types");
924
+ } catch {
925
+ return null;
926
+ }
927
+ let ast;
928
+ try {
929
+ ast = recast.parse(content, {
930
+ parser: {
931
+ parse: (src) => bp.parse(src, {
932
+ sourceType: "module",
933
+ plugins: ["jsx", "typescript"],
934
+ allowImportExportEverywhere: true,
935
+ tokens: true
936
+ })
937
+ }
938
+ });
939
+ } catch {
940
+ return null;
941
+ }
942
+ const program = ast.program;
943
+ if (!program || !Array.isArray(program.body)) return null;
944
+ const bodyEl = findFirstJsxElementByName(ast, "body");
945
+ if (!bodyEl) return null;
946
+ const newComponent = bt.jsxElement(
947
+ bt.jsxOpeningElement(bt.jsxIdentifier("GGPixelClient"), [], true),
948
+ null,
949
+ [],
950
+ true
951
+ );
952
+ const leadingText = bt.jsxText("\n ");
953
+ const trailingText = bt.jsxText("\n ");
954
+ bodyEl.children = [leadingText, newComponent, trailingText, ...bodyEl.children];
955
+ const importDecl = bt.importDeclaration(
956
+ [bt.importDefaultSpecifier(bt.identifier("GGPixelClient"))],
957
+ bt.stringLiteral(importSpec)
958
+ );
959
+ const body = program.body;
960
+ let insertAt = 0;
961
+ for (let i = 0; i < body.length; i++) {
962
+ if (body[i]?.type === "ImportDeclaration") insertAt = i + 1;
963
+ }
964
+ body.splice(insertAt, 0, importDecl);
965
+ let out;
966
+ try {
967
+ out = recast.print(ast).code;
968
+ } catch {
969
+ return null;
970
+ }
971
+ (0, import_node_fs3.writeFileSync)(layoutPath, out, "utf8");
972
+ return { kind: "injected", entryPath: layoutPath };
973
+ }
974
+ function findFirstJsxElementByName(ast, name) {
975
+ let found = null;
976
+ function walk(node) {
977
+ if (found || !node || typeof node !== "object") return;
978
+ if (Array.isArray(node)) {
979
+ for (const c of node) walk(c);
980
+ return;
981
+ }
982
+ const n = node;
983
+ if (n.type === "JSXElement") {
984
+ const opening = n.openingElement;
985
+ if (opening?.type === "JSXOpeningElement" && opening.name) {
986
+ const namedNode = opening.name;
987
+ if (namedNode.type === "JSXIdentifier" && namedNode.name === name) {
988
+ found = n;
989
+ return;
990
+ }
991
+ }
992
+ }
993
+ for (const key of Object.keys(n)) {
994
+ walk(n[key]);
995
+ }
996
+ }
997
+ walk(ast);
998
+ return found;
999
+ }
1000
+ function injectClientComponentViaRegex(layoutPath, content, spec) {
909
1001
  const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
910
1002
  const lines = content.split("\n");
911
1003
  let insertImportAt = 0;
@@ -955,26 +1047,85 @@ export default nextConfig;
955
1047
  );
956
1048
  return;
957
1049
  }
958
- const content = (0, import_node_fs3.readFileSync)(configPath, "utf8");
959
- if (content.includes("@kenkaiiii/gg-pixel")) return;
1050
+ patchNextConfigViaAst(configPath);
1051
+ }
1052
+ var PIXEL_PKG = "@kenkaiiii/gg-pixel";
1053
+ function patchNextConfigViaAst(configPath) {
1054
+ const original = (0, import_node_fs3.readFileSync)(configPath, "utf8");
1055
+ if (original.includes(PIXEL_PKG)) return;
1056
+ let parseModule;
1057
+ let generateCode;
1058
+ try {
1059
+ const mod = nodeRequire("magicast");
1060
+ parseModule = mod.parseModule;
1061
+ generateCode = mod.generateCode;
1062
+ } catch {
1063
+ return patchNextConfigViaRegex(configPath, original);
1064
+ }
1065
+ let module_;
1066
+ try {
1067
+ module_ = parseModule(original);
1068
+ } catch {
1069
+ return patchNextConfigViaRegex(configPath, original);
1070
+ }
1071
+ const cfg = resolveNextConfigObject(module_);
1072
+ if (!cfg) {
1073
+ return patchNextConfigViaRegex(configPath, original);
1074
+ }
1075
+ const existing = cfg.serverExternalPackages;
1076
+ const isArrayLike = Array.isArray(existing) || typeof existing === "object" && existing !== null && existing.$type === "array";
1077
+ if (isArrayLike) {
1078
+ const arr = existing;
1079
+ if (arr.includes(PIXEL_PKG)) return;
1080
+ arr.push(PIXEL_PKG);
1081
+ } else {
1082
+ cfg.serverExternalPackages = [PIXEL_PKG];
1083
+ }
1084
+ const out = generateCode(module_).code;
1085
+ if (out !== original) (0, import_node_fs3.writeFileSync)(configPath, out, "utf8");
1086
+ }
1087
+ function resolveNextConfigObject(mod) {
1088
+ const root = mod.exports.default ?? mod.exports;
1089
+ if (!root) return null;
1090
+ return unwrapWrappers(root);
1091
+ }
1092
+ function unwrapWrappers(node) {
1093
+ let cur = node;
1094
+ for (let i = 0; i < 6; i++) {
1095
+ if (cur.$type === "function-call") {
1096
+ const args = cur.$args ?? [];
1097
+ const objArg = args.find(
1098
+ (a) => typeof a === "object" && a !== null && a.$type !== "function-call"
1099
+ );
1100
+ if (!objArg) return null;
1101
+ cur = objArg;
1102
+ continue;
1103
+ }
1104
+ if (cur.$type === "object" || cur.$type === void 0) return cur;
1105
+ return null;
1106
+ }
1107
+ return null;
1108
+ }
1109
+ function patchNextConfigViaRegex(configPath, content) {
1110
+ if (content.includes(PIXEL_PKG)) return;
960
1111
  if (content.includes("serverExternalPackages")) {
961
1112
  const updated = content.replace(
962
1113
  /serverExternalPackages\s*:\s*\[([^\]]*)\]/,
963
1114
  (_match, inside) => {
964
1115
  const trimmed = inside.trim();
965
1116
  const sep2 = trimmed.length > 0 ? ", " : "";
966
- return `serverExternalPackages: [${trimmed}${sep2}"@kenkaiiii/gg-pixel"]`;
1117
+ return `serverExternalPackages: [${trimmed}${sep2}${JSON.stringify(PIXEL_PKG)}]`;
967
1118
  }
968
1119
  );
969
1120
  if (updated !== content) (0, import_node_fs3.writeFileSync)(configPath, updated, "utf8");
970
1121
  return;
971
1122
  }
972
- const objStart = /(const\s+\w+\s*:\s*NextConfig\s*=\s*\{|module\.exports\s*=\s*\{|export\s+default\s*\{)/;
1123
+ const objStart = /(const\s+\w+\s*(?::\s*\w+)?\s*=\s*\{|module\.exports\s*=\s*\{|export\s+default\s*\{)/;
973
1124
  const m = objStart.exec(content);
974
1125
  if (m) {
975
1126
  const insertAt = m.index + m[0].length;
976
1127
  const updated = content.slice(0, insertAt) + `
977
- serverExternalPackages: ["@kenkaiiii/gg-pixel"],` + content.slice(insertAt);
1128
+ serverExternalPackages: [${JSON.stringify(PIXEL_PKG)}],` + content.slice(insertAt);
978
1129
  (0, import_node_fs3.writeFileSync)(configPath, updated, "utf8");
979
1130
  }
980
1131
  }