@kenkaiiii/gg-pixel 4.3.85 → 4.3.87

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
@@ -94,10 +94,13 @@ function findMappingByPath(projectsJsonPath, projectRoot) {
94
94
  } catch {
95
95
  return null;
96
96
  }
97
+ let fallback = null;
97
98
  for (const [id, entry] of Object.entries(map)) {
98
- if (entry.path === projectRoot) return { id, ...entry };
99
+ if (entry.path !== projectRoot) continue;
100
+ if (entry.secret) return { id, ...entry };
101
+ if (!fallback) fallback = { id, ...entry };
99
102
  }
100
- return null;
103
+ return fallback;
101
104
  }
102
105
  function readEnvKey(envPath, key) {
103
106
  if (!existsSync(envPath)) return null;
@@ -380,11 +383,12 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
380
383
  }
381
384
  function writeNextInstrumentation(path, ingestUrl, projectKey) {
382
385
  const existing = existsSync(path) ? readFileSync(path, "utf8") : "";
383
- if (existing.includes("@kenkaiiii/gg-pixel")) return;
384
- const newContent = existing ? existing + "\n" + nextInstrumentationAppend(ingestUrl, projectKey) : nextInstrumentationStandalone(ingestUrl, projectKey);
385
- writeFileSync(path, newContent, "utf8");
386
+ const cleaned = stripLegacyPixelContent(existing);
387
+ const block = nextInstrumentationBlock(ingestUrl, projectKey);
388
+ const next = upsertPixelBlock(cleaned, block);
389
+ if (next !== existing) writeFileSync(path, next, "utf8");
386
390
  }
387
- function nextInstrumentationStandalone(ingestUrl, projectKey) {
391
+ function nextInstrumentationBlock(ingestUrl, projectKey) {
388
392
  const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
389
393
  return `// Next.js auto-loads this file on server start. Pixel hooks the
390
394
  // uncaughtExceptionMonitor + unhandledRejection events for API routes,
@@ -397,20 +401,7 @@ export async function register() {
397
401
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
398
402
  });
399
403
  }
400
- }
401
- `;
402
- }
403
- function nextInstrumentationAppend(ingestUrl, projectKey) {
404
- const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
405
- return `// gg-pixel: server-side error tracking
406
- import { initPixel } from "@kenkaiiii/gg-pixel";
407
- if (typeof process !== "undefined" && process.env.NEXT_RUNTIME === "nodejs") {
408
- initPixel({
409
- projectKey: process.env.GG_PIXEL_KEY${fallback},
410
- sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
411
- });
412
- }
413
- `;
404
+ }`;
414
405
  }
415
406
  function findNextLayout(projectRoot) {
416
407
  const candidates = [
@@ -543,25 +534,21 @@ function wireSveltekit({ projectRoot, projectKey, ingestUrl }) {
543
534
  const serverPath = join(projectRoot, "src/hooks.server.ts");
544
535
  const clientPath = join(projectRoot, "src/hooks.client.ts");
545
536
  if (!existsSync(dirname(serverPath))) mkdirSync(dirname(serverPath), { recursive: true });
546
- appendOrCreate(
537
+ upsertPixelBlockInFile(
547
538
  serverPath,
548
539
  `import { initPixel } from "@kenkaiiii/gg-pixel";
549
540
  initPixel({
550
541
  projectKey: process.env.GG_PIXEL_KEY ?? ${JSON.stringify(projectKey)},
551
542
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
552
- });
553
- `,
554
- "@kenkaiiii/gg-pixel"
543
+ });`
555
544
  );
556
- appendOrCreate(
545
+ upsertPixelBlockInFile(
557
546
  clientPath,
558
547
  `import { initPixel } from "@kenkaiiii/gg-pixel/browser";
559
548
  initPixel({
560
549
  projectKey: ${JSON.stringify(projectKey)},
561
550
  ingestUrl: ${JSON.stringify(ingestUrl)},
562
- });
563
- `,
564
- "@kenkaiiii/gg-pixel/browser"
551
+ });`
565
552
  );
566
553
  return {
567
554
  primaryInitPath: clientPath,
@@ -903,14 +890,88 @@ function pickPath(root, candidates) {
903
890
  }
904
891
  return null;
905
892
  }
906
- function appendOrCreate(filePath, snippet, marker) {
907
- if (existsSync(filePath)) {
908
- const existing = readFileSync(filePath, "utf8");
909
- if (existing.includes(marker)) return;
910
- writeFileSync(filePath, existing + "\n" + snippet, "utf8");
911
- return;
893
+ var PIXEL_MARK_BEGIN = "// >>> gg-pixel auto-generated \u2014 do not edit between these markers <<<";
894
+ var PIXEL_MARK_END = "// >>> /gg-pixel <<<";
895
+ function wrapPixelBlock(content) {
896
+ return `${PIXEL_MARK_BEGIN}
897
+ ${content.replace(/\s+$/, "")}
898
+ ${PIXEL_MARK_END}
899
+ `;
900
+ }
901
+ function upsertPixelBlock(existing, block) {
902
+ const wrapped = wrapPixelBlock(block);
903
+ const beginIdx = existing.indexOf(PIXEL_MARK_BEGIN);
904
+ if (beginIdx !== -1) {
905
+ const endIdx = existing.indexOf(PIXEL_MARK_END, beginIdx);
906
+ if (endIdx !== -1) {
907
+ const after = endIdx + PIXEL_MARK_END.length;
908
+ const trailNL = existing[after] === "\n" ? 1 : 0;
909
+ return existing.slice(0, beginIdx) + wrapped + existing.slice(after + trailNL);
910
+ }
912
911
  }
913
- writeFileSync(filePath, snippet, "utf8");
912
+ if (existing.length === 0) return wrapped;
913
+ const sep2 = existing.endsWith("\n") ? "" : "\n";
914
+ return existing + sep2 + "\n" + wrapped;
915
+ }
916
+ function upsertPixelBlockInFile(filePath, block) {
917
+ const existing = existsSync(filePath) ? readFileSync(filePath, "utf8") : "";
918
+ const cleaned = stripLegacyPixelContent(existing);
919
+ const next = upsertPixelBlock(cleaned, block);
920
+ if (next !== existing) writeFileSync(filePath, next, "utf8");
921
+ }
922
+ function stripLegacyPixelContent(content) {
923
+ if (!content.includes("@kenkaiiii/gg-pixel")) return content;
924
+ const beginIdx = content.indexOf(PIXEL_MARK_BEGIN);
925
+ const endIdx = beginIdx === -1 ? -1 : content.indexOf(PIXEL_MARK_END, beginIdx);
926
+ const insideMarkers = (start, end) => beginIdx !== -1 && endIdx !== -1 && start >= beginIdx && end <= endIdx + PIXEL_MARK_END.length;
927
+ const ranges = [];
928
+ const registerRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*export\s+async\s+function\s+register\s*\(\s*\)\s*\{/g;
929
+ let m;
930
+ while ((m = registerRe.exec(content)) !== null) {
931
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
932
+ const openBraceIdx = m.index + m[0].length - 1;
933
+ let depth = 1;
934
+ let i = openBraceIdx + 1;
935
+ while (i < content.length && depth > 0) {
936
+ const ch = content[i];
937
+ if (ch === "{") depth++;
938
+ else if (ch === "}") depth--;
939
+ i++;
940
+ }
941
+ if (depth !== 0) continue;
942
+ const blockEnd = i;
943
+ const blockText = content.slice(blockStart, blockEnd);
944
+ if (!blockText.includes("@kenkaiiii/gg-pixel")) continue;
945
+ if (insideMarkers(blockStart, blockEnd)) continue;
946
+ const trailingNL = content[blockEnd] === "\n" ? 1 : 0;
947
+ ranges.push({ start: blockStart, end: blockEnd + trailingNL });
948
+ }
949
+ const importRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*import\s*\{\s*initPixel[^}]*\}\s*from\s*"@kenkaiiii\/gg-pixel(?:\/[\w-]+)?"\s*;?\s*\n/g;
950
+ while ((m = importRe.exec(content)) !== null) {
951
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
952
+ const callIdx = content.indexOf("initPixel(", importRe.lastIndex);
953
+ if (callIdx === -1 || callIdx - importRe.lastIndex > 2048) continue;
954
+ const openParen = content.indexOf("(", callIdx);
955
+ let depth = 1;
956
+ let i = openParen + 1;
957
+ while (i < content.length && depth > 0) {
958
+ const ch = content[i];
959
+ if (ch === "(" || ch === "{") depth++;
960
+ else if (ch === ")" || ch === "}") depth--;
961
+ i++;
962
+ }
963
+ if (depth !== 0) continue;
964
+ while (i < content.length && (content[i] === ";" || content[i] === " ")) i++;
965
+ const trailingNL = content[i] === "\n" ? 1 : 0;
966
+ const blockEnd = i + trailingNL;
967
+ if (insideMarkers(blockStart, blockEnd)) continue;
968
+ ranges.push({ start: blockStart, end: blockEnd });
969
+ }
970
+ if (ranges.length === 0) return content;
971
+ ranges.sort((a, b) => b.start - a.start);
972
+ let out = content;
973
+ for (const r of ranges) out = out.slice(0, r.start) + out.slice(r.end);
974
+ return out.replace(/\n{3,}/g, "\n\n");
914
975
  }
915
976
  function injectImport(entryPath, initFilePath) {
916
977
  let content;