@kenkaiiii/gg-pixel 4.3.72 → 4.3.73

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
@@ -408,7 +408,14 @@ var LocalSqliteSink = class {
408
408
  };
409
409
 
410
410
  // src/install.ts
411
- import { existsSync as existsSync2, readFileSync as readFileSync2, writeFileSync, appendFileSync, mkdirSync as mkdirSync2 } from "fs";
411
+ import {
412
+ existsSync as existsSync2,
413
+ readFileSync as readFileSync2,
414
+ writeFileSync,
415
+ appendFileSync,
416
+ mkdirSync as mkdirSync2,
417
+ readdirSync
418
+ } from "fs";
412
419
  import { homedir as homedir2 } from "os";
413
420
  import { dirname as dirname2, join as join2, relative, resolve, sep } from "path";
414
421
  import { spawnSync as spawnSync2 } from "child_process";
@@ -420,17 +427,25 @@ async function install(opts = {}) {
420
427
  const home = opts.homeDir ?? homedir2();
421
428
  const nodeRoot = findProjectRoot(cwd);
422
429
  const pythonRoot = findPythonProjectRoot(cwd);
423
- if (!nodeRoot && !pythonRoot) {
430
+ const goRoot = findGoProjectRoot(cwd);
431
+ const rubyRoot = findRubyProjectRoot(cwd);
432
+ if (!nodeRoot && !pythonRoot && !goRoot && !rubyRoot) {
424
433
  throw new Error(
425
- `No project found at ${cwd}: looked for package.json (Node/JS), pyproject.toml, setup.py, requirements.txt, Pipfile (Python).`
434
+ `No project found at ${cwd}: looked for package.json, pyproject.toml/setup.py/requirements.txt/Pipfile, go.mod, Gemfile/*.gemspec.`
426
435
  );
427
436
  }
428
- const useNode = pickCloserRoot(nodeRoot, pythonRoot) === nodeRoot;
429
- if (!useNode && pythonRoot) {
437
+ const closestRoot = pickClosestRoot([nodeRoot, pythonRoot, goRoot, rubyRoot]);
438
+ if (closestRoot === goRoot && goRoot) {
439
+ return installGo({ projectRoot: goRoot, opts, ingestUrl, fetchFn, home });
440
+ }
441
+ if (closestRoot === rubyRoot && rubyRoot) {
442
+ return installRuby({ projectRoot: rubyRoot, opts, ingestUrl, fetchFn, home });
443
+ }
444
+ if (closestRoot === pythonRoot && pythonRoot) {
430
445
  return installPython({ projectRoot: pythonRoot, opts, ingestUrl, fetchFn, home });
431
446
  }
432
447
  if (!nodeRoot) {
433
- throw new Error("Internal: no nodeRoot but useNode==true");
448
+ throw new Error("Internal: closest root is Node but nodeRoot is null");
434
449
  }
435
450
  const pkgPath = join2(nodeRoot, "package.json");
436
451
  const pkg = JSON.parse(readFileSync2(pkgPath, "utf8"));
@@ -666,6 +681,9 @@ function isCommonJsEntry(entryPath, pkg) {
666
681
  }
667
682
  function detectJsProjectKind(pkg, projectRoot) {
668
683
  const all = { ...pkg.dependencies ?? {}, ...pkg.devDependencies ?? {} };
684
+ if (existsSync2(join2(projectRoot, "wrangler.toml")) || existsSync2(join2(projectRoot, "wrangler.jsonc")) || existsSync2(join2(projectRoot, "wrangler.json"))) {
685
+ return "cloudflare-workers";
686
+ }
669
687
  if ("electron" in all) return "electron";
670
688
  if (existsSync2(join2(projectRoot, "src-tauri")) || "@tauri-apps/api" in all) return "tauri";
671
689
  if ("react-native" in all) return "react-native";
@@ -696,8 +714,12 @@ function wireFramework(w) {
696
714
  return wireTauri(w);
697
715
  case "react-native":
698
716
  return wireReactNative(w);
717
+ case "cloudflare-workers":
718
+ return wireWorkers(w);
699
719
  case "python":
700
- throw new Error("Internal: python should have been handled earlier");
720
+ case "go":
721
+ case "ruby":
722
+ throw new Error(`Internal: ${w.kind} should have been handled earlier`);
701
723
  }
702
724
  }
703
725
  function wireNode({ projectRoot, pkg, ingestUrl }) {
@@ -956,6 +978,48 @@ function wireTauri({ projectRoot, pkg, projectKey, ingestUrl }) {
956
978
  ]
957
979
  };
958
980
  }
981
+ function wireWorkers({ projectRoot, projectKey, ingestUrl }) {
982
+ const initPath = join2(projectRoot, "gg-pixel.workers.snippet.ts");
983
+ writeFileSync(
984
+ initPath,
985
+ `// gg-pixel \u2014 Cloudflare Workers wiring snippet.
986
+ // Auto-generated by ggcoder pixel install. Wrap your default export with
987
+ // withPixel(...) so any throw in your handler is auto-reported. Example:
988
+ //
989
+ // import { withPixel } from "@kenkaiiii/gg-pixel/workers";
990
+ //
991
+ // export default withPixel(
992
+ // { projectKey: ${JSON.stringify(projectKey)} },
993
+ // {
994
+ // async fetch(req, env, ctx) { /* your code */ },
995
+ // async scheduled(evt, env, ctx) { /* your code */ },
996
+ // },
997
+ // );
998
+ //
999
+ // For manual reports inside a handler:
1000
+ //
1001
+ // import { reportPixel } from "@kenkaiiii/gg-pixel/workers";
1002
+ // reportPixel(ctx, { projectKey: ${JSON.stringify(projectKey)} }, {
1003
+ // message: "user clicked the broken button",
1004
+ // });
1005
+ //
1006
+ // Your project_key is publishable \u2014 safe to commit.
1007
+
1008
+ import { withPixel, reportPixel } from "@kenkaiiii/gg-pixel/workers";
1009
+ export const PIXEL_KEY = ${JSON.stringify(projectKey)};
1010
+ export const PIXEL_INGEST = ${JSON.stringify(ingestUrl)};
1011
+ export { withPixel, reportPixel };
1012
+ `,
1013
+ "utf8"
1014
+ );
1015
+ return {
1016
+ primaryInitPath: initPath,
1017
+ entryWiring: { kind: "no_entry_found" },
1018
+ warnings: [
1019
+ `Cloudflare Workers default exports can't be auto-wrapped safely. Open ${initPath} for a 3-line snippet you can paste into your worker.`
1020
+ ]
1021
+ };
1022
+ }
959
1023
  function wireReactNative({ projectRoot }) {
960
1024
  return {
961
1025
  primaryInitPath: join2(projectRoot, "(not-installed)"),
@@ -1061,10 +1125,191 @@ function detectPythonPackageManager(projectRoot) {
1061
1125
  if (existsSync2(join2(projectRoot, "Pipfile.lock"))) return "pipenv";
1062
1126
  return "pip";
1063
1127
  }
1064
- function pickCloserRoot(a, b) {
1065
- if (!a) return b;
1066
- if (!b) return a;
1067
- return a.length >= b.length ? a : b;
1128
+ function pickClosestRoot(roots) {
1129
+ let best = null;
1130
+ for (const r of roots) {
1131
+ if (!r) continue;
1132
+ if (!best || r.length > best.length) best = r;
1133
+ }
1134
+ return best;
1135
+ }
1136
+ var GO_MARKER = "go.mod";
1137
+ var RUBY_MARKERS = ["Gemfile"];
1138
+ function findGoProjectRoot(start) {
1139
+ let dir = start;
1140
+ for (let i = 0; i < 20; i++) {
1141
+ if (existsSync2(join2(dir, GO_MARKER))) return dir;
1142
+ const parent = dirname2(dir);
1143
+ if (parent === dir) return null;
1144
+ dir = parent;
1145
+ }
1146
+ return null;
1147
+ }
1148
+ function findRubyProjectRoot(start) {
1149
+ let dir = start;
1150
+ for (let i = 0; i < 20; i++) {
1151
+ for (const m of RUBY_MARKERS) {
1152
+ if (existsSync2(join2(dir, m))) return dir;
1153
+ }
1154
+ try {
1155
+ const entries = readdirSync(dir);
1156
+ if (entries.some((e) => e.endsWith(".gemspec"))) return dir;
1157
+ } catch {
1158
+ }
1159
+ const parent = dirname2(dir);
1160
+ if (parent === dir) return null;
1161
+ dir = parent;
1162
+ }
1163
+ return null;
1164
+ }
1165
+ async function installGo(ctx) {
1166
+ const { projectRoot, opts, ingestUrl, fetchFn, home } = ctx;
1167
+ const projectName = opts.projectName ?? readGoModuleName(projectRoot) ?? projectRoot.split("/").pop() ?? "unnamed";
1168
+ const projectsJsonPath = join2(home, ".gg", "projects.json");
1169
+ const envFilePath = join2(projectRoot, ".env");
1170
+ const existing = findMappingByPath(projectsJsonPath, projectRoot);
1171
+ const existingKey = readEnvKey(envFilePath, "GG_PIXEL_KEY");
1172
+ let created;
1173
+ let reused = false;
1174
+ if (existing && existingKey) {
1175
+ created = { id: existing.id, key: existingKey };
1176
+ reused = true;
1177
+ } else {
1178
+ created = await createProject(fetchFn, ingestUrl, projectName);
1179
+ }
1180
+ const packageInstalled = opts.skipPackageInstall ? false : runGoGet(projectRoot);
1181
+ const initFilePath = join2(projectRoot, "gg_pixel_init.go");
1182
+ writeFileSync(
1183
+ initFilePath,
1184
+ `// gg-pixel init \u2014 auto-generated by ggcoder pixel install.
1185
+ package main
1186
+
1187
+ import (
1188
+ "os"
1189
+ gg "github.com/kenkaiiii/gg-pixel-go"
1190
+ )
1191
+
1192
+ func init() {
1193
+ key := os.Getenv("GG_PIXEL_KEY")
1194
+ if key == "" {
1195
+ key = ${JSON.stringify(created.key)}
1196
+ }
1197
+ _ = gg.Init(gg.Options{ProjectKey: key, IngestURL: ${JSON.stringify(`${ingestUrl}/ingest`)}})
1198
+ }
1199
+ `,
1200
+ "utf8"
1201
+ );
1202
+ writeEnvKey(envFilePath, "GG_PIXEL_KEY", created.key);
1203
+ writeProjectsMapping(projectsJsonPath, created.id, projectName, projectRoot);
1204
+ return {
1205
+ projectId: created.id,
1206
+ projectKey: created.key,
1207
+ projectName,
1208
+ projectKind: "go",
1209
+ initFilePath,
1210
+ envFilePath,
1211
+ projectsJsonPath,
1212
+ packageManager: "pip",
1213
+ packageInstalled,
1214
+ entryWiring: { kind: "no_entry_found" },
1215
+ reused,
1216
+ warnings: [
1217
+ "Add `defer ggpixel.Recover()` near the top of your main() so panics are captured before the process exits."
1218
+ ]
1219
+ };
1220
+ }
1221
+ function readGoModuleName(projectRoot) {
1222
+ try {
1223
+ const content = readFileSync2(join2(projectRoot, "go.mod"), "utf8");
1224
+ const match = /^\s*module\s+(\S+)\s*$/m.exec(content);
1225
+ if (!match) return null;
1226
+ return match[1].split("/").pop() ?? null;
1227
+ } catch {
1228
+ return null;
1229
+ }
1230
+ }
1231
+ function runGoGet(projectRoot) {
1232
+ const result = spawnSync2("go", ["get", "github.com/kenkaiiii/gg-pixel-go@latest"], {
1233
+ cwd: projectRoot,
1234
+ stdio: "inherit"
1235
+ });
1236
+ return result.status === 0;
1237
+ }
1238
+ async function installRuby(ctx) {
1239
+ const { projectRoot, opts, ingestUrl, fetchFn, home } = ctx;
1240
+ const projectName = opts.projectName ?? readRubyAppName(projectRoot) ?? projectRoot.split("/").pop() ?? "unnamed";
1241
+ const projectsJsonPath = join2(home, ".gg", "projects.json");
1242
+ const envFilePath = join2(projectRoot, ".env");
1243
+ const existing = findMappingByPath(projectsJsonPath, projectRoot);
1244
+ const existingKey = readEnvKey(envFilePath, "GG_PIXEL_KEY");
1245
+ let created;
1246
+ let reused = false;
1247
+ if (existing && existingKey) {
1248
+ created = { id: existing.id, key: existingKey };
1249
+ reused = true;
1250
+ } else {
1251
+ created = await createProject(fetchFn, ingestUrl, projectName);
1252
+ }
1253
+ const packageInstalled = opts.skipPackageInstall ? false : runRubyInstall(projectRoot);
1254
+ const initFilePath = join2(projectRoot, "gg_pixel_init.rb");
1255
+ writeFileSync(
1256
+ initFilePath,
1257
+ `# gg-pixel init \u2014 auto-generated by ggcoder pixel install.
1258
+ require "gg_pixel"
1259
+ GGPixel.init(
1260
+ project_key: ENV["GG_PIXEL_KEY"] || ${JSON.stringify(created.key)},
1261
+ ingest_url: ${JSON.stringify(`${ingestUrl}/ingest`)},
1262
+ )
1263
+ `,
1264
+ "utf8"
1265
+ );
1266
+ writeEnvKey(envFilePath, "GG_PIXEL_KEY", created.key);
1267
+ writeProjectsMapping(projectsJsonPath, created.id, projectName, projectRoot);
1268
+ return {
1269
+ projectId: created.id,
1270
+ projectKey: created.key,
1271
+ projectName,
1272
+ projectKind: "ruby",
1273
+ initFilePath,
1274
+ envFilePath,
1275
+ projectsJsonPath,
1276
+ packageManager: "pip",
1277
+ packageInstalled,
1278
+ entryWiring: { kind: "no_entry_found" },
1279
+ reused,
1280
+ warnings: [
1281
+ `Add \`require "./gg_pixel_init"\` at the top of your entry script (often \`config/application.rb\` for Rails, \`app.rb\` for Sinatra, or your main file).`
1282
+ ]
1283
+ };
1284
+ }
1285
+ function readRubyAppName(projectRoot) {
1286
+ try {
1287
+ const entries = readdirSync(projectRoot);
1288
+ const gemspec = entries.find((e) => e.endsWith(".gemspec"));
1289
+ if (!gemspec) return null;
1290
+ return gemspec.replace(/\.gemspec$/, "");
1291
+ } catch {
1292
+ return null;
1293
+ }
1294
+ }
1295
+ function runRubyInstall(projectRoot) {
1296
+ if (existsSync2(join2(projectRoot, "Gemfile"))) {
1297
+ try {
1298
+ const content = readFileSync2(join2(projectRoot, "Gemfile"), "utf8");
1299
+ if (!content.includes("gg_pixel")) {
1300
+ writeFileSync(
1301
+ join2(projectRoot, "Gemfile"),
1302
+ content + (content.endsWith("\n") ? "" : "\n") + 'gem "gg_pixel"\n',
1303
+ "utf8"
1304
+ );
1305
+ }
1306
+ } catch {
1307
+ }
1308
+ const r = spawnSync2("bundle", ["install"], { cwd: projectRoot, stdio: "inherit" });
1309
+ if (r.status === 0) return true;
1310
+ }
1311
+ const r2 = spawnSync2("gem", ["install", "gg_pixel"], { cwd: projectRoot, stdio: "inherit" });
1312
+ return r2.status === 0;
1068
1313
  }
1069
1314
  async function installPython(ctx) {
1070
1315
  const { projectRoot, opts, ingestUrl, fetchFn, home } = ctx;