@kenkaiiii/gg-pixel 4.3.85 → 4.3.86

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
@@ -787,11 +787,11 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
787
787
  }
788
788
  function writeNextInstrumentation(path, ingestUrl, projectKey) {
789
789
  const existing = existsSync2(path) ? readFileSync2(path, "utf8") : "";
790
- if (existing.includes("@kenkaiiii/gg-pixel")) return;
791
- const newContent = existing ? existing + "\n" + nextInstrumentationAppend(ingestUrl, projectKey) : nextInstrumentationStandalone(ingestUrl, projectKey);
792
- writeFileSync(path, newContent, "utf8");
790
+ const block = nextInstrumentationBlock(ingestUrl, projectKey);
791
+ const next = upsertPixelBlock(existing, block);
792
+ if (next !== existing) writeFileSync(path, next, "utf8");
793
793
  }
794
- function nextInstrumentationStandalone(ingestUrl, projectKey) {
794
+ function nextInstrumentationBlock(ingestUrl, projectKey) {
795
795
  const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
796
796
  return `// Next.js auto-loads this file on server start. Pixel hooks the
797
797
  // uncaughtExceptionMonitor + unhandledRejection events for API routes,
@@ -804,20 +804,7 @@ export async function register() {
804
804
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
805
805
  });
806
806
  }
807
- }
808
- `;
809
- }
810
- function nextInstrumentationAppend(ingestUrl, projectKey) {
811
- const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
812
- return `// gg-pixel: server-side error tracking
813
- import { initPixel } from "@kenkaiiii/gg-pixel";
814
- if (typeof process !== "undefined" && process.env.NEXT_RUNTIME === "nodejs") {
815
- initPixel({
816
- projectKey: process.env.GG_PIXEL_KEY${fallback},
817
- sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
818
- });
819
- }
820
- `;
807
+ }`;
821
808
  }
822
809
  function findNextLayout(projectRoot) {
823
810
  const candidates = [
@@ -950,25 +937,21 @@ function wireSveltekit({ projectRoot, projectKey, ingestUrl }) {
950
937
  const serverPath = join2(projectRoot, "src/hooks.server.ts");
951
938
  const clientPath = join2(projectRoot, "src/hooks.client.ts");
952
939
  if (!existsSync2(dirname2(serverPath))) mkdirSync2(dirname2(serverPath), { recursive: true });
953
- appendOrCreate(
940
+ upsertPixelBlockInFile(
954
941
  serverPath,
955
942
  `import { initPixel } from "@kenkaiiii/gg-pixel";
956
943
  initPixel({
957
944
  projectKey: process.env.GG_PIXEL_KEY ?? ${JSON.stringify(projectKey)},
958
945
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
959
- });
960
- `,
961
- "@kenkaiiii/gg-pixel"
946
+ });`
962
947
  );
963
- appendOrCreate(
948
+ upsertPixelBlockInFile(
964
949
  clientPath,
965
950
  `import { initPixel } from "@kenkaiiii/gg-pixel/browser";
966
951
  initPixel({
967
952
  projectKey: ${JSON.stringify(projectKey)},
968
953
  ingestUrl: ${JSON.stringify(ingestUrl)},
969
- });
970
- `,
971
- "@kenkaiiii/gg-pixel/browser"
954
+ });`
972
955
  );
973
956
  return {
974
957
  primaryInitPath: clientPath,
@@ -1310,14 +1293,33 @@ function pickPath(root, candidates) {
1310
1293
  }
1311
1294
  return null;
1312
1295
  }
1313
- function appendOrCreate(filePath, snippet, marker) {
1314
- if (existsSync2(filePath)) {
1315
- const existing = readFileSync2(filePath, "utf8");
1316
- if (existing.includes(marker)) return;
1317
- writeFileSync(filePath, existing + "\n" + snippet, "utf8");
1318
- return;
1296
+ var PIXEL_MARK_BEGIN = "// >>> gg-pixel auto-generated \u2014 do not edit between these markers <<<";
1297
+ var PIXEL_MARK_END = "// >>> /gg-pixel <<<";
1298
+ function wrapPixelBlock(content) {
1299
+ return `${PIXEL_MARK_BEGIN}
1300
+ ${content.replace(/\s+$/, "")}
1301
+ ${PIXEL_MARK_END}
1302
+ `;
1303
+ }
1304
+ function upsertPixelBlock(existing, block) {
1305
+ const wrapped = wrapPixelBlock(block);
1306
+ const beginIdx = existing.indexOf(PIXEL_MARK_BEGIN);
1307
+ if (beginIdx !== -1) {
1308
+ const endIdx = existing.indexOf(PIXEL_MARK_END, beginIdx);
1309
+ if (endIdx !== -1) {
1310
+ const after = endIdx + PIXEL_MARK_END.length;
1311
+ const trailNL = existing[after] === "\n" ? 1 : 0;
1312
+ return existing.slice(0, beginIdx) + wrapped + existing.slice(after + trailNL);
1313
+ }
1319
1314
  }
1320
- writeFileSync(filePath, snippet, "utf8");
1315
+ if (existing.length === 0) return wrapped;
1316
+ const sep2 = existing.endsWith("\n") ? "" : "\n";
1317
+ return existing + sep2 + "\n" + wrapped;
1318
+ }
1319
+ function upsertPixelBlockInFile(filePath, block) {
1320
+ const existing = existsSync2(filePath) ? readFileSync2(filePath, "utf8") : "";
1321
+ const next = upsertPixelBlock(existing, block);
1322
+ if (next !== existing) writeFileSync(filePath, next, "utf8");
1321
1323
  }
1322
1324
  function injectImport(entryPath, initFilePath) {
1323
1325
  let content;