create-db 1.0.4 → 1.0.6-pr49-version-bump-to-1.0.5-17274471643.0

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.
Files changed (3) hide show
  1. package/index.js +84 -89
  2. package/package.json +1 -1
  3. package/analytics.js +0 -63
package/index.js CHANGED
@@ -1,20 +1,43 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ import { select, spinner, intro, outro, log, cancel } from "@clack/prompts";
4
+ import { randomUUID } from "crypto";
3
5
  import dotenv from "dotenv";
4
6
  import fs from "fs";
5
7
  import path from "path";
8
+ import terminalLink from "terminal-link";
9
+ import chalk from "chalk";
6
10
 
7
11
  dotenv.config();
8
12
 
9
- import { select, spinner, intro, outro, log, cancel } from "@clack/prompts";
10
- import chalk from "chalk";
11
- import terminalLink from "terminal-link";
12
- import { analytics } from "./analytics.js";
13
+ const CLI_RUN_ID = randomUUID();
13
14
 
14
15
  const CREATE_DB_WORKER_URL =
15
- process.env.CREATE_DB_WORKER_URL || "https://create-db-temp.prisma.io";
16
+ process.env.CREATE_DB_WORKER_URL.replace(/\/+$/, "") ||
17
+ "https://create-db-temp.prisma.io";
16
18
  const CLAIM_DB_WORKER_URL =
17
- process.env.CLAIM_DB_WORKER_URL || "https://create-db.prisma.io";
19
+ process.env.CLAIM_DB_WORKER_URL.replace(/\/+$/, "") ||
20
+ "https://create-db.prisma.io";
21
+
22
+ async function sendAnalyticsToWorker(eventName, properties) {
23
+ const controller = new AbortController();
24
+ const timer = setTimeout(() => controller.abort(), 2000);
25
+ try {
26
+ const payload = {
27
+ eventName,
28
+ properties: { distinct_id: CLI_RUN_ID, ...(properties || {}) },
29
+ };
30
+ await fetch(`${CREATE_DB_WORKER_URL}/analytics`, {
31
+ method: "POST",
32
+ headers: { "Content-Type": "application/json" },
33
+ body: JSON.stringify(payload),
34
+ signal: controller.signal,
35
+ });
36
+ } catch (error) {
37
+ } finally {
38
+ clearTimeout(timer);
39
+ }
40
+ }
18
41
 
19
42
  async function detectUserLocation() {
20
43
  try {
@@ -43,7 +66,6 @@ async function detectUserLocation() {
43
66
  }
44
67
  }
45
68
 
46
- // Region coordinates (latitude, longitude)
47
69
  const REGION_COORDINATES = {
48
70
  "ap-southeast-1": { lat: 1.3521, lng: 103.8198 }, // Singapore
49
71
  "ap-northeast-1": { lat: 35.6762, lng: 139.6503 }, // Tokyo
@@ -359,16 +381,12 @@ async function promptForRegion(defaultRegion, userAgent) {
359
381
  process.exit(0);
360
382
  }
361
383
 
362
- try {
363
- const analyticsProps = {
364
- command: CLI_NAME,
365
- region: region,
366
- "selection-method": "interactive",
367
- "user-agent": userAgent,
368
- };
369
-
370
- await analytics.capture("create_db:region_selected", analyticsProps);
371
- } catch (error) {}
384
+ void sendAnalyticsToWorker("create_db:region_selected", {
385
+ command: CLI_NAME,
386
+ region: region,
387
+ "selection-method": "interactive",
388
+ "user-agent": userAgent,
389
+ });
372
390
 
373
391
  return region;
374
392
  }
@@ -406,20 +424,13 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
406
424
  );
407
425
  }
408
426
 
409
- try {
410
- const analyticsProps = {
411
- command: CLI_NAME,
412
- region: region,
413
- "error-type": "rate_limit",
414
- "status-code": 429,
415
- "user-agent": userAgent,
416
- };
417
-
418
- await analytics.capture(
419
- "create_db:database_creation_failed",
420
- analyticsProps
421
- );
422
- } catch (error) {}
427
+ void sendAnalyticsToWorker("create_db:database_creation_failed", {
428
+ command: CLI_NAME,
429
+ region: region,
430
+ "error-type": "rate_limit",
431
+ "status-code": 429,
432
+ "user-agent": userAgent,
433
+ });
423
434
 
424
435
  process.exit(1);
425
436
  }
@@ -441,20 +452,15 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
441
452
  if (s) {
442
453
  s.stop("Unexpected response from create service.");
443
454
  }
444
- try {
445
- const analyticsProps = {
446
- command: CLI_NAME,
447
- region,
448
- "error-type": "invalid_json",
449
- "status-code": resp.status,
450
- "user-agent": userAgent,
451
- };
452
455
 
453
- await analytics.capture(
454
- "create_db:database_creation_failed",
455
- analyticsProps
456
- );
457
- } catch (error) {}
456
+ void sendAnalyticsToWorker("create_db:database_creation_failed", {
457
+ command: CLI_NAME,
458
+ region,
459
+ "error-type": "invalid_json",
460
+ "status-code": resp.status,
461
+ "user-agent": userAgent,
462
+ });
463
+
458
464
  process.exit(1);
459
465
  }
460
466
 
@@ -517,20 +523,14 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
517
523
  );
518
524
  }
519
525
 
520
- try {
521
- const analyticsProps = {
522
- command: CLI_NAME,
523
- region: region,
524
- "error-type": "api_error",
525
- "error-message": result.error.message,
526
- "user-agent": userAgent,
527
- };
526
+ void sendAnalyticsToWorker("create_db:database_creation_failed", {
527
+ command: CLI_NAME,
528
+ region: region,
529
+ "error-type": "api_error",
530
+ "error-message": result.error.message,
531
+ "user-agent": userAgent,
532
+ });
528
533
 
529
- await analytics.capture(
530
- "create_db:database_creation_failed",
531
- analyticsProps
532
- );
533
- } catch (error) {}
534
534
  process.exit(1);
535
535
  }
536
536
 
@@ -581,6 +581,12 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
581
581
  )
582
582
  )
583
583
  );
584
+
585
+ void sendAnalyticsToWorker("create_db:database_created", {
586
+ command: CLI_NAME,
587
+ region,
588
+ utm_source: CLI_NAME,
589
+ });
584
590
  }
585
591
 
586
592
  async function main() {
@@ -595,28 +601,21 @@ async function main() {
595
601
  userAgent = `${userEnvVars.PRISMA_ACTOR_NAME}/${userEnvVars.PRISMA_ACTOR_PROJECT}`;
596
602
  }
597
603
 
598
- try {
599
- const analyticsProps = {
600
- command: CLI_NAME,
601
- "full-command": `${CLI_NAME} ${rawArgs.join(" ")}`.trim(),
602
- "has-region-flag":
603
- rawArgs.includes("--region") || rawArgs.includes("-r"),
604
- "has-interactive-flag":
605
- rawArgs.includes("--interactive") || rawArgs.includes("-i"),
606
- "has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
607
- "has-list-regions-flag": rawArgs.includes("--list-regions"),
608
- "has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
609
- "has-user-agent-from-env": !!userAgent,
610
- "node-version": process.version,
611
- platform: process.platform,
612
- arch: process.arch,
613
- "user-agent": userAgent,
614
- };
615
-
616
- await analytics.capture("create_db:cli_command_ran", analyticsProps);
617
- } catch (error) {
618
- console.error("Error:", error.message);
619
- }
604
+ void sendAnalyticsToWorker("create_db:cli_command_ran", {
605
+ command: CLI_NAME,
606
+ "full-command": `${CLI_NAME} ${rawArgs.join(" ")}`.trim(),
607
+ "has-region-flag": rawArgs.includes("--region") || rawArgs.includes("-r"),
608
+ "has-interactive-flag":
609
+ rawArgs.includes("--interactive") || rawArgs.includes("-i"),
610
+ "has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
611
+ "has-list-regions-flag": rawArgs.includes("--list-regions"),
612
+ "has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
613
+ "has-user-agent-from-env": !!userAgent,
614
+ "node-version": process.version,
615
+ platform: process.platform,
616
+ arch: process.arch,
617
+ "user-agent": userAgent,
618
+ });
620
619
 
621
620
  if (!flags.help && !flags.json) {
622
621
  await isOffline();
@@ -639,16 +638,12 @@ async function main() {
639
638
  if (flags.region) {
640
639
  region = flags.region;
641
640
 
642
- try {
643
- const analyticsProps = {
644
- command: CLI_NAME,
645
- region: region,
646
- "selection-method": "flag",
647
- "user-agent": userAgent,
648
- };
649
-
650
- await analytics.capture("create_db:region_selected", analyticsProps);
651
- } catch (error) {}
641
+ void sendAnalyticsToWorker("create_db:region_selected", {
642
+ command: CLI_NAME,
643
+ region: region,
644
+ "selection-method": "flag",
645
+ "user-agent": userAgent,
646
+ });
652
647
  }
653
648
 
654
649
  if (flags.interactive) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "1.0.4",
3
+ "version": "1.0.6-pr49-version-bump-to-1.0.5-17274471643.0",
4
4
  "description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
5
5
  "main": "index.js",
6
6
  "author": "",
package/analytics.js DELETED
@@ -1,63 +0,0 @@
1
- import { randomUUID } from "crypto";
2
-
3
- class EventCaptureError extends Error {
4
- constructor(event, status) {
5
- super(`Failed to submit PostHog event '${event}': ${status}`);
6
- }
7
- }
8
-
9
- class PosthogEventCapture {
10
- async capture(eventName, properties = {}) {
11
- const POSTHOG_API_HOST = process.env.POSTHOG_API_HOST;
12
- const POSTHOG_KEY = process.env.POSTHOG_API_KEY;
13
-
14
- if (
15
- !POSTHOG_API_HOST ||
16
- !POSTHOG_KEY ||
17
- POSTHOG_API_HOST.trim() === "" ||
18
- POSTHOG_KEY.trim() === ""
19
- ) {
20
- if (process.env.NODE_ENV === "development") {
21
- console.warn(
22
- "Analytics disabled: missing POSTHOG_API_HOST or POSTHOG_API_KEY."
23
- );
24
- }
25
- return;
26
- }
27
-
28
- const POSTHOG_CAPTURE_URL = `${POSTHOG_API_HOST.replace(/\/+$/, "")}/capture`;
29
-
30
- const payload = {
31
- api_key: POSTHOG_KEY,
32
- event: eventName,
33
- distinct_id: randomUUID(),
34
- properties: {
35
- $process_person_profile: false,
36
- ...properties,
37
- },
38
- };
39
-
40
- try {
41
- const response = await fetch(POSTHOG_CAPTURE_URL, {
42
- method: "POST",
43
- headers: {
44
- "Content-Type": "application/json",
45
- },
46
- body: JSON.stringify(payload),
47
- });
48
-
49
- if (!response.ok) {
50
- throw new EventCaptureError(eventName, response.statusText);
51
- }
52
- } catch (error) {
53
- if (process.env.NODE_ENV === "development") {
54
- console.error("Analytics error:", error.message);
55
- }
56
- }
57
- }
58
- }
59
-
60
- // Create a singleton instance
61
- const analytics = new PosthogEventCapture();
62
-
63
- export { analytics, EventCaptureError };