create-db 1.0.3-pr45-DC-4829-source-flag-17136895926.0 → 1.0.3-pr45-DC-4829-source-flag-17164565333.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 +3 -1
- package/index.js +67 -55
- package/package.json +1 -1
package/analytics.js
CHANGED
|
@@ -11,7 +11,9 @@ class PosthogEventCapture {
|
|
|
11
11
|
const POSTHOG_CAPTURE_URL = process.env.POSTHOG_API_HOST
|
|
12
12
|
? process.env.POSTHOG_API_HOST + "/capture"
|
|
13
13
|
: "https://proxyhog.prisma-data.net/capture";
|
|
14
|
-
const POSTHOG_KEY =
|
|
14
|
+
const POSTHOG_KEY =
|
|
15
|
+
process.env.POSTHOG_API_KEY ||
|
|
16
|
+
"phc_cmc85avbWyuJ2JyKdGPdv7dxXli8xLdWDBPbvIXWJfs";
|
|
15
17
|
|
|
16
18
|
const payload = {
|
|
17
19
|
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,11 +417,11 @@ 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) {
|
|
418
|
-
|
|
424
|
+
const jsonResponse = {
|
|
419
425
|
connectionString: prismaConn,
|
|
420
426
|
directConnectionString: directConn,
|
|
421
427
|
claimUrl: claimUrl,
|
|
@@ -423,8 +429,13 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
423
429
|
region: database?.region?.id || region,
|
|
424
430
|
name: database?.name,
|
|
425
431
|
projectId: projectId,
|
|
426
|
-
source: source || null,
|
|
427
432
|
};
|
|
433
|
+
|
|
434
|
+
if (userAgent) {
|
|
435
|
+
jsonResponse.source = userAgent;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return jsonResponse;
|
|
428
439
|
}
|
|
429
440
|
|
|
430
441
|
if (result.error) {
|
|
@@ -449,12 +460,15 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
449
460
|
"error-type": "api_error",
|
|
450
461
|
"error-message": result.error.message,
|
|
451
462
|
};
|
|
452
|
-
|
|
453
|
-
if (
|
|
454
|
-
analyticsProps
|
|
463
|
+
|
|
464
|
+
if (userAgent) {
|
|
465
|
+
analyticsProps["user-agent"] = userAgent;
|
|
455
466
|
}
|
|
456
|
-
|
|
457
|
-
await analytics.capture(
|
|
467
|
+
|
|
468
|
+
await analytics.capture(
|
|
469
|
+
"create_db:database_creation_failed",
|
|
470
|
+
analyticsProps
|
|
471
|
+
);
|
|
458
472
|
} catch (error) {}
|
|
459
473
|
process.exit(1);
|
|
460
474
|
}
|
|
@@ -511,15 +525,15 @@ async function createDatabase(name, region, source, returnJson = false ) {
|
|
|
511
525
|
async function main() {
|
|
512
526
|
try {
|
|
513
527
|
const rawArgs = process.argv.slice(2);
|
|
514
|
-
|
|
528
|
+
|
|
515
529
|
const { flags } = await parseArgs();
|
|
516
|
-
|
|
517
|
-
let
|
|
530
|
+
|
|
531
|
+
let userAgent;
|
|
518
532
|
const userEnvVars = readUserEnvFile();
|
|
519
533
|
if (userEnvVars.PRISMA_ACTOR_NAME && userEnvVars.PRISMA_ACTOR_PROJECT) {
|
|
520
|
-
|
|
534
|
+
userAgent = `${userEnvVars.PRISMA_ACTOR_NAME}/${userEnvVars.PRISMA_ACTOR_PROJECT}`;
|
|
521
535
|
}
|
|
522
|
-
|
|
536
|
+
|
|
523
537
|
try {
|
|
524
538
|
const analyticsProps = {
|
|
525
539
|
command: CLI_NAME,
|
|
@@ -531,16 +545,16 @@ async function main() {
|
|
|
531
545
|
"has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
|
|
532
546
|
"has-list-regions-flag": rawArgs.includes("--list-regions"),
|
|
533
547
|
"has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
|
|
534
|
-
"has-source-from-env": !!
|
|
548
|
+
"has-source-from-env": !!userAgent,
|
|
535
549
|
"node-version": process.version,
|
|
536
550
|
platform: process.platform,
|
|
537
551
|
arch: process.arch,
|
|
538
552
|
};
|
|
539
|
-
|
|
540
|
-
if (
|
|
541
|
-
analyticsProps
|
|
553
|
+
|
|
554
|
+
if (userAgent) {
|
|
555
|
+
analyticsProps["user-agent"] = userAgent;
|
|
542
556
|
}
|
|
543
|
-
|
|
557
|
+
|
|
544
558
|
await analytics.capture("create_db:cli_command_ran", analyticsProps);
|
|
545
559
|
} catch (error) {
|
|
546
560
|
console.error("Error:", error.message);
|
|
@@ -572,17 +586,15 @@ async function main() {
|
|
|
572
586
|
region: region,
|
|
573
587
|
"selection-method": "flag",
|
|
574
588
|
};
|
|
575
|
-
|
|
576
|
-
if (
|
|
577
|
-
analyticsProps
|
|
589
|
+
|
|
590
|
+
if (userAgent) {
|
|
591
|
+
analyticsProps["user-agent"] = userAgent;
|
|
578
592
|
}
|
|
579
|
-
|
|
593
|
+
|
|
580
594
|
await analytics.capture("create_db:region_selected", analyticsProps);
|
|
581
595
|
} catch (error) {}
|
|
582
596
|
}
|
|
583
597
|
|
|
584
|
-
|
|
585
|
-
|
|
586
598
|
if (flags.interactive) {
|
|
587
599
|
chooseRegionPrompt = true;
|
|
588
600
|
}
|
|
@@ -590,11 +602,11 @@ async function main() {
|
|
|
590
602
|
if (flags.json) {
|
|
591
603
|
try {
|
|
592
604
|
if (chooseRegionPrompt) {
|
|
593
|
-
region = await promptForRegion(region,
|
|
605
|
+
region = await promptForRegion(region, userAgent);
|
|
594
606
|
} else {
|
|
595
607
|
await validateRegion(region, true);
|
|
596
608
|
}
|
|
597
|
-
const result = await createDatabase(name, region,
|
|
609
|
+
const result = await createDatabase(name, region, userAgent, true);
|
|
598
610
|
console.log(JSON.stringify(result, null, 2));
|
|
599
611
|
process.exit(0);
|
|
600
612
|
} catch (e) {
|
|
@@ -619,12 +631,12 @@ async function main() {
|
|
|
619
631
|
)
|
|
620
632
|
);
|
|
621
633
|
if (chooseRegionPrompt) {
|
|
622
|
-
region = await promptForRegion(region,
|
|
634
|
+
region = await promptForRegion(region, userAgent);
|
|
623
635
|
}
|
|
624
636
|
|
|
625
637
|
region = await validateRegion(region);
|
|
626
638
|
|
|
627
|
-
await createDatabase(name, region,
|
|
639
|
+
await createDatabase(name, region, userAgent);
|
|
628
640
|
|
|
629
641
|
outro("");
|
|
630
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-17164565333.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": "",
|