@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.cjs CHANGED
@@ -821,11 +821,11 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
821
821
  }
822
822
  function writeNextInstrumentation(path, ingestUrl, projectKey) {
823
823
  const existing = (0, import_node_fs3.existsSync)(path) ? (0, import_node_fs3.readFileSync)(path, "utf8") : "";
824
- if (existing.includes("@kenkaiiii/gg-pixel")) return;
825
- const newContent = existing ? existing + "\n" + nextInstrumentationAppend(ingestUrl, projectKey) : nextInstrumentationStandalone(ingestUrl, projectKey);
826
- (0, import_node_fs3.writeFileSync)(path, newContent, "utf8");
824
+ const block = nextInstrumentationBlock(ingestUrl, projectKey);
825
+ const next = upsertPixelBlock(existing, block);
826
+ if (next !== existing) (0, import_node_fs3.writeFileSync)(path, next, "utf8");
827
827
  }
828
- function nextInstrumentationStandalone(ingestUrl, projectKey) {
828
+ function nextInstrumentationBlock(ingestUrl, projectKey) {
829
829
  const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
830
830
  return `// Next.js auto-loads this file on server start. Pixel hooks the
831
831
  // uncaughtExceptionMonitor + unhandledRejection events for API routes,
@@ -838,20 +838,7 @@ export async function register() {
838
838
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
839
839
  });
840
840
  }
841
- }
842
- `;
843
- }
844
- function nextInstrumentationAppend(ingestUrl, projectKey) {
845
- const fallback = projectKey ? ` ?? ${JSON.stringify(projectKey)}` : "";
846
- return `// gg-pixel: server-side error tracking
847
- import { initPixel } from "@kenkaiiii/gg-pixel";
848
- if (typeof process !== "undefined" && process.env.NEXT_RUNTIME === "nodejs") {
849
- initPixel({
850
- projectKey: process.env.GG_PIXEL_KEY${fallback},
851
- sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
852
- });
853
- }
854
- `;
841
+ }`;
855
842
  }
856
843
  function findNextLayout(projectRoot) {
857
844
  const candidates = [
@@ -984,25 +971,21 @@ function wireSveltekit({ projectRoot, projectKey, ingestUrl }) {
984
971
  const serverPath = (0, import_node_path2.join)(projectRoot, "src/hooks.server.ts");
985
972
  const clientPath = (0, import_node_path2.join)(projectRoot, "src/hooks.client.ts");
986
973
  if (!(0, import_node_fs3.existsSync)((0, import_node_path2.dirname)(serverPath))) (0, import_node_fs3.mkdirSync)((0, import_node_path2.dirname)(serverPath), { recursive: true });
987
- appendOrCreate(
974
+ upsertPixelBlockInFile(
988
975
  serverPath,
989
976
  `import { initPixel } from "@kenkaiiii/gg-pixel";
990
977
  initPixel({
991
978
  projectKey: process.env.GG_PIXEL_KEY ?? ${JSON.stringify(projectKey)},
992
979
  sink: { kind: "http", ingestUrl: ${JSON.stringify(`${ingestUrl}/ingest`)} },
993
- });
994
- `,
995
- "@kenkaiiii/gg-pixel"
980
+ });`
996
981
  );
997
- appendOrCreate(
982
+ upsertPixelBlockInFile(
998
983
  clientPath,
999
984
  `import { initPixel } from "@kenkaiiii/gg-pixel/browser";
1000
985
  initPixel({
1001
986
  projectKey: ${JSON.stringify(projectKey)},
1002
987
  ingestUrl: ${JSON.stringify(ingestUrl)},
1003
- });
1004
- `,
1005
- "@kenkaiiii/gg-pixel/browser"
988
+ });`
1006
989
  );
1007
990
  return {
1008
991
  primaryInitPath: clientPath,
@@ -1344,14 +1327,33 @@ function pickPath(root, candidates) {
1344
1327
  }
1345
1328
  return null;
1346
1329
  }
1347
- function appendOrCreate(filePath, snippet, marker) {
1348
- if ((0, import_node_fs3.existsSync)(filePath)) {
1349
- const existing = (0, import_node_fs3.readFileSync)(filePath, "utf8");
1350
- if (existing.includes(marker)) return;
1351
- (0, import_node_fs3.writeFileSync)(filePath, existing + "\n" + snippet, "utf8");
1352
- return;
1330
+ var PIXEL_MARK_BEGIN = "// >>> gg-pixel auto-generated \u2014 do not edit between these markers <<<";
1331
+ var PIXEL_MARK_END = "// >>> /gg-pixel <<<";
1332
+ function wrapPixelBlock(content) {
1333
+ return `${PIXEL_MARK_BEGIN}
1334
+ ${content.replace(/\s+$/, "")}
1335
+ ${PIXEL_MARK_END}
1336
+ `;
1337
+ }
1338
+ function upsertPixelBlock(existing, block) {
1339
+ const wrapped = wrapPixelBlock(block);
1340
+ const beginIdx = existing.indexOf(PIXEL_MARK_BEGIN);
1341
+ if (beginIdx !== -1) {
1342
+ const endIdx = existing.indexOf(PIXEL_MARK_END, beginIdx);
1343
+ if (endIdx !== -1) {
1344
+ const after = endIdx + PIXEL_MARK_END.length;
1345
+ const trailNL = existing[after] === "\n" ? 1 : 0;
1346
+ return existing.slice(0, beginIdx) + wrapped + existing.slice(after + trailNL);
1347
+ }
1353
1348
  }
1354
- (0, import_node_fs3.writeFileSync)(filePath, snippet, "utf8");
1349
+ if (existing.length === 0) return wrapped;
1350
+ const sep2 = existing.endsWith("\n") ? "" : "\n";
1351
+ return existing + sep2 + "\n" + wrapped;
1352
+ }
1353
+ function upsertPixelBlockInFile(filePath, block) {
1354
+ const existing = (0, import_node_fs3.existsSync)(filePath) ? (0, import_node_fs3.readFileSync)(filePath, "utf8") : "";
1355
+ const next = upsertPixelBlock(existing, block);
1356
+ if (next !== existing) (0, import_node_fs3.writeFileSync)(filePath, next, "utf8");
1355
1357
  }
1356
1358
  function injectImport(entryPath, initFilePath) {
1357
1359
  let content;