@kenkaiiii/gg-pixel 4.3.92 → 4.3.94

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
@@ -353,7 +353,7 @@ function loadBetterSqlite3() {
353
353
  return typeof mod === "function" ? mod : mod.default;
354
354
  } catch (err) {
355
355
  throw new Error(
356
- `@kenkaiiii/gg-pixel: \`kind: "local"\` requires the optional peer dependency \`better-sqlite3\`. Install it with \`npm install better-sqlite3\` (or your package manager's equivalent). Underlying error: ${err.message}`,
356
+ `@kenkaiiii/gg-pixel: \`kind: "local"\` requires \`better-sqlite3\` to be installed. Install it with \`npm install better-sqlite3\` (or your package manager's equivalent). Underlying error: ${err.message}`,
357
357
  { cause: err }
358
358
  );
359
359
  }
@@ -569,7 +569,9 @@ async function safeText(r) {
569
569
  }
570
570
  function detectPackageManager(projectRoot) {
571
571
  if (existsSync2(join2(projectRoot, "pnpm-lock.yaml"))) return "pnpm";
572
- if (existsSync2(join2(projectRoot, "bun.lockb"))) return "bun";
572
+ if (existsSync2(join2(projectRoot, "bun.lock")) || existsSync2(join2(projectRoot, "bun.lockb"))) {
573
+ return "bun";
574
+ }
573
575
  if (existsSync2(join2(projectRoot, "yarn.lock"))) return "yarn";
574
576
  return "npm";
575
577
  }
@@ -881,6 +883,113 @@ function injectNextClientComponent(layoutPath, clientInitPath) {
881
883
  let spec = relative(fromDir, clientInitPath).split(sep).join("/");
882
884
  if (!spec.startsWith(".")) spec = "./" + spec;
883
885
  spec = spec.replace(/\.tsx$/, "");
886
+ const astResult = injectClientComponentViaAst(layoutPath, content, spec);
887
+ if (astResult) return astResult;
888
+ return injectClientComponentViaRegex(layoutPath, content, spec);
889
+ }
890
+ function injectClientComponentViaAst(layoutPath, content, importSpec) {
891
+ let recast;
892
+ let bp;
893
+ let bt;
894
+ try {
895
+ recast = nodeRequire("recast");
896
+ bp = nodeRequire("@babel/parser");
897
+ bt = nodeRequire("@babel/types");
898
+ } catch {
899
+ return null;
900
+ }
901
+ let ast;
902
+ try {
903
+ ast = recast.parse(content, {
904
+ parser: {
905
+ parse: (src) => bp.parse(src, {
906
+ sourceType: "module",
907
+ plugins: ["jsx", "typescript"],
908
+ allowImportExportEverywhere: true,
909
+ tokens: true
910
+ })
911
+ }
912
+ });
913
+ } catch {
914
+ return null;
915
+ }
916
+ const program = ast.program;
917
+ if (!program || !Array.isArray(program.body)) return null;
918
+ const bodyEl = findFirstJsxElementByName(ast, "body");
919
+ if (!bodyEl) return null;
920
+ const newComponent = bt.jsxElement(
921
+ bt.jsxOpeningElement(bt.jsxIdentifier("GGPixelClient"), [], true),
922
+ null,
923
+ [],
924
+ true
925
+ );
926
+ const leadingText = bt.jsxText("\n ");
927
+ const trailingText = bt.jsxText("\n ");
928
+ bodyEl.children = [leadingText, newComponent, trailingText, ...bodyEl.children];
929
+ const importDecl = bt.importDeclaration(
930
+ [bt.importDefaultSpecifier(bt.identifier("GGPixelClient"))],
931
+ bt.stringLiteral(importSpec)
932
+ );
933
+ const body = program.body;
934
+ let insertAt = 0;
935
+ for (let i = 0; i < body.length; i++) {
936
+ if (body[i]?.type === "ImportDeclaration") insertAt = i + 1;
937
+ }
938
+ body.splice(insertAt, 0, importDecl);
939
+ let out;
940
+ try {
941
+ out = recast.print(ast).code;
942
+ } catch {
943
+ return null;
944
+ }
945
+ writeFileSync(layoutPath, out, "utf8");
946
+ return { kind: "injected", entryPath: layoutPath };
947
+ }
948
+ function findFirstJsxElementByName(ast, name) {
949
+ let found = null;
950
+ const seen = /* @__PURE__ */ new WeakSet();
951
+ const MAX_DEPTH = 500;
952
+ const SKIP_KEYS = /* @__PURE__ */ new Set([
953
+ "loc",
954
+ "tokens",
955
+ "original",
956
+ "comments",
957
+ "leadingComments",
958
+ "trailingComments",
959
+ "innerComments",
960
+ "range",
961
+ "start",
962
+ "end"
963
+ ]);
964
+ function walk(node, depth) {
965
+ if (found || !node || typeof node !== "object") return;
966
+ if (depth > MAX_DEPTH) return;
967
+ if (seen.has(node)) return;
968
+ seen.add(node);
969
+ if (Array.isArray(node)) {
970
+ for (const c of node) walk(c, depth + 1);
971
+ return;
972
+ }
973
+ const n = node;
974
+ if (n.type === "JSXElement") {
975
+ const opening = n.openingElement;
976
+ if (opening?.type === "JSXOpeningElement" && opening.name) {
977
+ const namedNode = opening.name;
978
+ if (namedNode.type === "JSXIdentifier" && namedNode.name === name) {
979
+ found = n;
980
+ return;
981
+ }
982
+ }
983
+ }
984
+ for (const key of Object.keys(n)) {
985
+ if (SKIP_KEYS.has(key)) continue;
986
+ walk(n[key], depth + 1);
987
+ }
988
+ }
989
+ walk(ast, 0);
990
+ return found;
991
+ }
992
+ function injectClientComponentViaRegex(layoutPath, content, spec) {
884
993
  const importLine = `import GGPixelClient from ${JSON.stringify(spec)};`;
885
994
  const lines = content.split("\n");
886
995
  let insertImportAt = 0;