create-db 1.0.3-pr45-DC-4829-source-flag-17136985429.0 → 1.0.3-pr45-DC-4829-source-flag-17164722487.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.
- package/analytics.js +2 -4
- package/index.js +64 -57
- package/package.json +1 -1
package/analytics.js
CHANGED
|
@@ -8,10 +8,8 @@ class EventCaptureError extends Error {
|
|
|
8
8
|
|
|
9
9
|
class PosthogEventCapture {
|
|
10
10
|
async capture(eventName, properties = {}) {
|
|
11
|
-
const POSTHOG_CAPTURE_URL = process.env.POSTHOG_API_HOST
|
|
12
|
-
|
|
13
|
-
: "https://proxyhog.prisma-data.net/capture";
|
|
14
|
-
const POSTHOG_KEY = process.env.POSTHOG_API_KEY || "phc_cmc85avbWyuJ2JyKdGPdv7dxXli8xLdWDBPbvIXWJfs";
|
|
11
|
+
const POSTHOG_CAPTURE_URL = process.env.POSTHOG_API_HOST + "/capture";
|
|
12
|
+
const POSTHOG_KEY = process.env.POSTHOG_API_KEY;
|
|
15
13
|
|
|
16
14
|
const payload = {
|
|
17
15
|
api_key: POSTHOG_KEY,
|
package/index.js
CHANGED
|
@@ -61,26 +61,26 @@ const CLI_NAME = getCommandName();
|
|
|
61
61
|
|
|
62
62
|
function readUserEnvFile() {
|
|
63
63
|
const userCwd = process.cwd();
|
|
64
|
-
const envPath = path.join(userCwd,
|
|
65
|
-
|
|
64
|
+
const envPath = path.join(userCwd, ".env");
|
|
65
|
+
|
|
66
66
|
if (!fs.existsSync(envPath)) {
|
|
67
67
|
return {};
|
|
68
68
|
}
|
|
69
|
-
|
|
70
|
-
const envContent = fs.readFileSync(envPath,
|
|
69
|
+
|
|
70
|
+
const envContent = fs.readFileSync(envPath, "utf8");
|
|
71
71
|
const envVars = {};
|
|
72
|
-
|
|
73
|
-
envContent.split(
|
|
72
|
+
|
|
73
|
+
envContent.split("\n").forEach((line) => {
|
|
74
74
|
const trimmed = line.trim();
|
|
75
|
-
if (trimmed && !trimmed.startsWith(
|
|
76
|
-
const [key, ...valueParts] = trimmed.split(
|
|
75
|
+
if (trimmed && !trimmed.startsWith("#")) {
|
|
76
|
+
const [key, ...valueParts] = trimmed.split("=");
|
|
77
77
|
if (key && valueParts.length > 0) {
|
|
78
|
-
const value = valueParts.join(
|
|
78
|
+
const value = valueParts.join("=").replace(/^["']|["']$/g, "");
|
|
79
79
|
envVars[key.trim()] = value.trim();
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
});
|
|
83
|
-
|
|
83
|
+
|
|
84
84
|
return envVars;
|
|
85
85
|
}
|
|
86
86
|
|
|
@@ -266,7 +266,7 @@ function handleError(message, extra = "") {
|
|
|
266
266
|
process.exit(1);
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
-
async function promptForRegion(defaultRegion,
|
|
269
|
+
async function promptForRegion(defaultRegion, userAgent) {
|
|
270
270
|
let regions;
|
|
271
271
|
try {
|
|
272
272
|
regions = await getRegions();
|
|
@@ -296,18 +296,18 @@ async function promptForRegion(defaultRegion, source) {
|
|
|
296
296
|
region: region,
|
|
297
297
|
"selection-method": "interactive",
|
|
298
298
|
};
|
|
299
|
-
|
|
300
|
-
if (
|
|
301
|
-
analyticsProps
|
|
299
|
+
|
|
300
|
+
if (userAgent) {
|
|
301
|
+
analyticsProps["user-agent"] = userAgent;
|
|
302
302
|
}
|
|
303
|
-
|
|
303
|
+
|
|
304
304
|
await analytics.capture("create_db:region_selected", analyticsProps);
|
|
305
305
|
} catch (error) {}
|
|
306
306
|
|
|
307
307
|
return region;
|
|
308
308
|
}
|
|
309
309
|
|
|
310
|
-
async function createDatabase(name, region,
|
|
310
|
+
async function createDatabase(name, region, userAgent, returnJson = false) {
|
|
311
311
|
let s;
|
|
312
312
|
if (!returnJson) {
|
|
313
313
|
s = spinner();
|
|
@@ -317,7 +317,7 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
317
317
|
const resp = await fetch(`${CREATE_DB_WORKER_URL}/create`, {
|
|
318
318
|
method: "POST",
|
|
319
319
|
headers: { "Content-Type": "application/json" },
|
|
320
|
-
body: JSON.stringify({ region, name, utm_source:
|
|
320
|
+
body: JSON.stringify({ region, name, utm_source: userAgent }),
|
|
321
321
|
});
|
|
322
322
|
|
|
323
323
|
if (resp.status === 429) {
|
|
@@ -343,12 +343,15 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
343
343
|
"error-type": "rate_limit",
|
|
344
344
|
"status-code": 429,
|
|
345
345
|
};
|
|
346
|
-
|
|
347
|
-
if (
|
|
348
|
-
analyticsProps
|
|
346
|
+
|
|
347
|
+
if (userAgent) {
|
|
348
|
+
analyticsProps["user-agent"] = userAgent;
|
|
349
349
|
}
|
|
350
|
-
|
|
351
|
-
await analytics.capture(
|
|
350
|
+
|
|
351
|
+
await analytics.capture(
|
|
352
|
+
"create_db:database_creation_failed",
|
|
353
|
+
analyticsProps
|
|
354
|
+
);
|
|
352
355
|
} catch (error) {}
|
|
353
356
|
|
|
354
357
|
process.exit(1);
|
|
@@ -378,12 +381,15 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
378
381
|
"error-type": "invalid_json",
|
|
379
382
|
"status-code": resp.status,
|
|
380
383
|
};
|
|
381
|
-
|
|
382
|
-
if (
|
|
383
|
-
analyticsProps
|
|
384
|
+
|
|
385
|
+
if (userAgent) {
|
|
386
|
+
analyticsProps["user-agent"] = userAgent;
|
|
384
387
|
}
|
|
385
|
-
|
|
386
|
-
await analytics.capture(
|
|
388
|
+
|
|
389
|
+
await analytics.capture(
|
|
390
|
+
"create_db:database_creation_failed",
|
|
391
|
+
analyticsProps
|
|
392
|
+
);
|
|
387
393
|
} catch {}
|
|
388
394
|
process.exit(1);
|
|
389
395
|
}
|
|
@@ -411,7 +417,7 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
411
417
|
? `postgresql://${directUser}:${directPass}@${directHost}${directPort}/${directDbName}`
|
|
412
418
|
: null;
|
|
413
419
|
|
|
414
|
-
const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${
|
|
420
|
+
const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${userAgent}&utm_medium=cli`;
|
|
415
421
|
const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
|
416
422
|
|
|
417
423
|
if (returnJson && !result.error) {
|
|
@@ -424,11 +430,11 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
424
430
|
name: database?.name,
|
|
425
431
|
projectId: projectId,
|
|
426
432
|
};
|
|
427
|
-
|
|
428
|
-
if (
|
|
429
|
-
jsonResponse.source =
|
|
433
|
+
|
|
434
|
+
if (userAgent) {
|
|
435
|
+
jsonResponse.source = userAgent;
|
|
430
436
|
}
|
|
431
|
-
|
|
437
|
+
|
|
432
438
|
return jsonResponse;
|
|
433
439
|
}
|
|
434
440
|
|
|
@@ -454,12 +460,15 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
454
460
|
"error-type": "api_error",
|
|
455
461
|
"error-message": result.error.message,
|
|
456
462
|
};
|
|
457
|
-
|
|
458
|
-
if (
|
|
459
|
-
analyticsProps
|
|
463
|
+
|
|
464
|
+
if (userAgent) {
|
|
465
|
+
analyticsProps["user-agent"] = userAgent;
|
|
460
466
|
}
|
|
461
|
-
|
|
462
|
-
await analytics.capture(
|
|
467
|
+
|
|
468
|
+
await analytics.capture(
|
|
469
|
+
"create_db:database_creation_failed",
|
|
470
|
+
analyticsProps
|
|
471
|
+
);
|
|
463
472
|
} catch (error) {}
|
|
464
473
|
process.exit(1);
|
|
465
474
|
}
|
|
@@ -516,15 +525,15 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
516
525
|
async function main() {
|
|
517
526
|
try {
|
|
518
527
|
const rawArgs = process.argv.slice(2);
|
|
519
|
-
|
|
528
|
+
|
|
520
529
|
const { flags } = await parseArgs();
|
|
521
|
-
|
|
522
|
-
let
|
|
530
|
+
|
|
531
|
+
let userAgent;
|
|
523
532
|
const userEnvVars = readUserEnvFile();
|
|
524
533
|
if (userEnvVars.PRISMA_ACTOR_NAME && userEnvVars.PRISMA_ACTOR_PROJECT) {
|
|
525
|
-
|
|
534
|
+
userAgent = `${userEnvVars.PRISMA_ACTOR_NAME}/${userEnvVars.PRISMA_ACTOR_PROJECT}`;
|
|
526
535
|
}
|
|
527
|
-
|
|
536
|
+
|
|
528
537
|
try {
|
|
529
538
|
const analyticsProps = {
|
|
530
539
|
command: CLI_NAME,
|
|
@@ -536,16 +545,16 @@ async function main() {
|
|
|
536
545
|
"has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
|
|
537
546
|
"has-list-regions-flag": rawArgs.includes("--list-regions"),
|
|
538
547
|
"has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
|
|
539
|
-
"has-source-from-env": !!
|
|
548
|
+
"has-source-from-env": !!userAgent,
|
|
540
549
|
"node-version": process.version,
|
|
541
550
|
platform: process.platform,
|
|
542
551
|
arch: process.arch,
|
|
543
552
|
};
|
|
544
|
-
|
|
545
|
-
if (
|
|
546
|
-
analyticsProps
|
|
553
|
+
|
|
554
|
+
if (userAgent) {
|
|
555
|
+
analyticsProps["user-agent"] = userAgent;
|
|
547
556
|
}
|
|
548
|
-
|
|
557
|
+
|
|
549
558
|
await analytics.capture("create_db:cli_command_ran", analyticsProps);
|
|
550
559
|
} catch (error) {
|
|
551
560
|
console.error("Error:", error.message);
|
|
@@ -577,17 +586,15 @@ async function main() {
|
|
|
577
586
|
region: region,
|
|
578
587
|
"selection-method": "flag",
|
|
579
588
|
};
|
|
580
|
-
|
|
581
|
-
if (
|
|
582
|
-
analyticsProps
|
|
589
|
+
|
|
590
|
+
if (userAgent) {
|
|
591
|
+
analyticsProps["user-agent"] = userAgent;
|
|
583
592
|
}
|
|
584
|
-
|
|
593
|
+
|
|
585
594
|
await analytics.capture("create_db:region_selected", analyticsProps);
|
|
586
595
|
} catch (error) {}
|
|
587
596
|
}
|
|
588
597
|
|
|
589
|
-
|
|
590
|
-
|
|
591
598
|
if (flags.interactive) {
|
|
592
599
|
chooseRegionPrompt = true;
|
|
593
600
|
}
|
|
@@ -595,11 +602,11 @@ async function main() {
|
|
|
595
602
|
if (flags.json) {
|
|
596
603
|
try {
|
|
597
604
|
if (chooseRegionPrompt) {
|
|
598
|
-
region = await promptForRegion(region,
|
|
605
|
+
region = await promptForRegion(region, userAgent);
|
|
599
606
|
} else {
|
|
600
607
|
await validateRegion(region, true);
|
|
601
608
|
}
|
|
602
|
-
const result = await createDatabase(name, region,
|
|
609
|
+
const result = await createDatabase(name, region, userAgent, true);
|
|
603
610
|
console.log(JSON.stringify(result, null, 2));
|
|
604
611
|
process.exit(0);
|
|
605
612
|
} catch (e) {
|
|
@@ -624,12 +631,12 @@ async function main() {
|
|
|
624
631
|
)
|
|
625
632
|
);
|
|
626
633
|
if (chooseRegionPrompt) {
|
|
627
|
-
region = await promptForRegion(region,
|
|
634
|
+
region = await promptForRegion(region, userAgent);
|
|
628
635
|
}
|
|
629
636
|
|
|
630
637
|
region = await validateRegion(region);
|
|
631
638
|
|
|
632
|
-
await createDatabase(name, region,
|
|
639
|
+
await createDatabase(name, region, userAgent);
|
|
633
640
|
|
|
634
641
|
outro("");
|
|
635
642
|
} catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-db",
|
|
3
|
-
"version": "1.0.3-pr45-DC-4829-source-flag-
|
|
3
|
+
"version": "1.0.3-pr45-DC-4829-source-flag-17164722487.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": "",
|