@kenkaiiii/gg-pixel 4.3.86 → 4.3.88

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,8 +383,9 @@ function wireNextjs({ projectRoot, projectKey, ingestUrl }) {
380
383
  }
381
384
  function writeNextInstrumentation(path, ingestUrl, projectKey) {
382
385
  const existing = existsSync(path) ? readFileSync(path, "utf8") : "";
386
+ const cleaned = stripLegacyPixelContent(existing);
383
387
  const block = nextInstrumentationBlock(ingestUrl, projectKey);
384
- const next = upsertPixelBlock(existing, block);
388
+ const next = upsertPixelBlock(cleaned, block);
385
389
  if (next !== existing) writeFileSync(path, next, "utf8");
386
390
  }
387
391
  function nextInstrumentationBlock(ingestUrl, projectKey) {
@@ -647,13 +651,12 @@ function wireElectron({ projectRoot, pkg, projectKey, ingestUrl }) {
647
651
  "Could not copy gg-pixel browser IIFE bundle \u2014 install @kenkaiiii/gg-pixel and re-run."
648
652
  );
649
653
  }
650
- let patchedAny = false;
654
+ let wiredAny = false;
651
655
  for (const html of htmlFiles) {
652
- if (patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl) === "patched") {
653
- patchedAny = true;
654
- }
656
+ const r = patchRendererHtml(html, rendererInitPath, projectKey, ingestUrl);
657
+ if (r === "patched" || r === "already") wiredAny = true;
655
658
  }
656
- if (!patchedAny) {
659
+ if (!wiredAny) {
657
660
  warnings.push(
658
661
  `Found HTML files in ${rendererDir} but couldn't patch any \u2014 they may have unusual CSP or no <head>.`
659
662
  );
@@ -772,6 +775,7 @@ function copyIifeBundle(projectRoot, dest) {
772
775
  }
773
776
  return false;
774
777
  }
778
+ var PIXEL_HTML_MARKER = "<!-- gg-pixel: auto-wired by ggcoder pixel install -->";
775
779
  function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
776
780
  let content;
777
781
  try {
@@ -779,7 +783,20 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
779
783
  } catch {
780
784
  return "not-applicable";
781
785
  }
782
- if (content.includes("gg-pixel.browser.iife")) return "already";
786
+ const original = content;
787
+ const markerIdx = content.indexOf(PIXEL_HTML_MARKER);
788
+ if (markerIdx !== -1) {
789
+ const firstScriptEnd = content.indexOf("</script>", markerIdx);
790
+ const secondScriptEnd = firstScriptEnd !== -1 ? content.indexOf("</script>", firstScriptEnd + "</script>".length) : -1;
791
+ if (secondScriptEnd !== -1) {
792
+ let stripEnd = secondScriptEnd + "</script>".length;
793
+ while (stripEnd < content.length && /\s/.test(content[stripEnd])) stripEnd++;
794
+ let stripStart = markerIdx;
795
+ while (stripStart > 0 && /[ \t]/.test(content[stripStart - 1])) stripStart--;
796
+ if (stripStart > 0 && content[stripStart - 1] === "\n") stripStart--;
797
+ content = content.slice(0, stripStart) + content.slice(stripEnd);
798
+ }
799
+ }
783
800
  const ingestOrigin = new URL(ingestUrl).origin;
784
801
  content = content.replace(
785
802
  /(<meta[^>]+http-equiv=["']?content-security-policy["']?[^>]*content=)("([^"]+)"|'([^']+)')/i,
@@ -799,7 +816,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
799
816
  );
800
817
  const relScript = relative(dirname(htmlPath), iifePath).split(sep).join("/");
801
818
  const inject = `
802
- <!-- gg-pixel: auto-wired by ggcoder pixel install -->
819
+ ${PIXEL_HTML_MARKER}
803
820
  <script src="${relScript}"></script>
804
821
  <script>
805
822
  if (window.GGPixel) GGPixel.initPixel({ projectKey: ${JSON.stringify(projectKey)}, ingestUrl: ${JSON.stringify(ingestUrl)} });
@@ -812,6 +829,7 @@ function patchRendererHtml(htmlPath, iifePath, projectKey, ingestUrl) {
812
829
  } else {
813
830
  return "not-applicable";
814
831
  }
832
+ if (content === original) return "already";
815
833
  writeFileSync(htmlPath, content, "utf8");
816
834
  return "patched";
817
835
  }
@@ -911,9 +929,64 @@ function upsertPixelBlock(existing, block) {
911
929
  }
912
930
  function upsertPixelBlockInFile(filePath, block) {
913
931
  const existing = existsSync(filePath) ? readFileSync(filePath, "utf8") : "";
914
- const next = upsertPixelBlock(existing, block);
932
+ const cleaned = stripLegacyPixelContent(existing);
933
+ const next = upsertPixelBlock(cleaned, block);
915
934
  if (next !== existing) writeFileSync(filePath, next, "utf8");
916
935
  }
936
+ function stripLegacyPixelContent(content) {
937
+ if (!content.includes("@kenkaiiii/gg-pixel")) return content;
938
+ const beginIdx = content.indexOf(PIXEL_MARK_BEGIN);
939
+ const endIdx = beginIdx === -1 ? -1 : content.indexOf(PIXEL_MARK_END, beginIdx);
940
+ const insideMarkers = (start, end) => beginIdx !== -1 && endIdx !== -1 && start >= beginIdx && end <= endIdx + PIXEL_MARK_END.length;
941
+ const ranges = [];
942
+ const registerRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*export\s+async\s+function\s+register\s*\(\s*\)\s*\{/g;
943
+ let m;
944
+ while ((m = registerRe.exec(content)) !== null) {
945
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
946
+ const openBraceIdx = m.index + m[0].length - 1;
947
+ let depth = 1;
948
+ let i = openBraceIdx + 1;
949
+ while (i < content.length && depth > 0) {
950
+ const ch = content[i];
951
+ if (ch === "{") depth++;
952
+ else if (ch === "}") depth--;
953
+ i++;
954
+ }
955
+ if (depth !== 0) continue;
956
+ const blockEnd = i;
957
+ const blockText = content.slice(blockStart, blockEnd);
958
+ if (!blockText.includes("@kenkaiiii/gg-pixel")) continue;
959
+ if (insideMarkers(blockStart, blockEnd)) continue;
960
+ const trailingNL = content[blockEnd] === "\n" ? 1 : 0;
961
+ ranges.push({ start: blockStart, end: blockEnd + trailingNL });
962
+ }
963
+ const importRe = /(?:^|\n)((?:[ \t]*\/\/[^\n]*\n)*)[ \t]*import\s*\{\s*initPixel[^}]*\}\s*from\s*"@kenkaiiii\/gg-pixel(?:\/[\w-]+)?"\s*;?\s*\n/g;
964
+ while ((m = importRe.exec(content)) !== null) {
965
+ const blockStart = m.index + (content[m.index] === "\n" ? 1 : 0);
966
+ const callIdx = content.indexOf("initPixel(", importRe.lastIndex);
967
+ if (callIdx === -1 || callIdx - importRe.lastIndex > 2048) continue;
968
+ const openParen = content.indexOf("(", callIdx);
969
+ let depth = 1;
970
+ let i = openParen + 1;
971
+ while (i < content.length && depth > 0) {
972
+ const ch = content[i];
973
+ if (ch === "(" || ch === "{") depth++;
974
+ else if (ch === ")" || ch === "}") depth--;
975
+ i++;
976
+ }
977
+ if (depth !== 0) continue;
978
+ while (i < content.length && (content[i] === ";" || content[i] === " ")) i++;
979
+ const trailingNL = content[i] === "\n" ? 1 : 0;
980
+ const blockEnd = i + trailingNL;
981
+ if (insideMarkers(blockStart, blockEnd)) continue;
982
+ ranges.push({ start: blockStart, end: blockEnd });
983
+ }
984
+ if (ranges.length === 0) return content;
985
+ ranges.sort((a, b) => b.start - a.start);
986
+ let out = content;
987
+ for (const r of ranges) out = out.slice(0, r.start) + out.slice(r.end);
988
+ return out.replace(/\n{3,}/g, "\n\n");
989
+ }
917
990
  function injectImport(entryPath, initFilePath) {
918
991
  let content;
919
992
  try {