create-db 1.0.3-pr44-DC-4828-json-flag-17127092542.0 → 1.0.3-pr45-DC-4829-source-flag-17135144702.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 (2) hide show
  1. package/index.js +138 -23
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import dotenv from "dotenv";
4
+ import fs from "fs";
5
+ import path from "path";
4
6
  dotenv.config();
5
7
 
6
8
  import { select, spinner, intro, outro, log, cancel } from "@clack/prompts";
@@ -57,6 +59,31 @@ function getCommandName() {
57
59
 
58
60
  const CLI_NAME = getCommandName();
59
61
 
62
+ function readUserEnvFile() {
63
+ const userCwd = process.cwd();
64
+ const envPath = path.join(userCwd, '.env');
65
+
66
+ if (!fs.existsSync(envPath)) {
67
+ return {};
68
+ }
69
+
70
+ const envContent = fs.readFileSync(envPath, 'utf8');
71
+ const envVars = {};
72
+
73
+ envContent.split('\n').forEach(line => {
74
+ const trimmed = line.trim();
75
+ if (trimmed && !trimmed.startsWith('#')) {
76
+ const [key, ...valueParts] = trimmed.split('=');
77
+ if (key && valueParts.length > 0) {
78
+ const value = valueParts.join('=').replace(/^["']|["']$/g, '');
79
+ envVars[key.trim()] = value.trim();
80
+ }
81
+ }
82
+ });
83
+
84
+ return envVars;
85
+ }
86
+
60
87
  async function showHelp() {
61
88
  let regionExamples = "us-east-1, eu-west-1";
62
89
  try {
@@ -99,12 +126,14 @@ async function parseArgs() {
99
126
  "list-regions",
100
127
  "interactive",
101
128
  "json",
129
+ "source",
102
130
  ];
103
131
  const shorthandMap = {
104
132
  r: "region",
105
133
  i: "interactive",
106
134
  h: "help",
107
135
  j: "json",
136
+ s: "source",
108
137
  };
109
138
 
110
139
  const exitWithError = (message) => {
@@ -127,6 +156,8 @@ async function parseArgs() {
127
156
  exitWithError("Missing value for --region flag.");
128
157
  flags.region = region;
129
158
  i++;
159
+ } else if (flag === "source") {
160
+ flags.source = true;
130
161
  } else {
131
162
  flags[flag] = true;
132
163
  }
@@ -145,6 +176,8 @@ async function parseArgs() {
145
176
  exitWithError("Missing value for -r flag.");
146
177
  flags.region = region;
147
178
  i++;
179
+ } else if (mappedFlag === "source") {
180
+ flags.source = true;
148
181
  } else {
149
182
  flags[mappedFlag] = true;
150
183
  }
@@ -164,6 +197,8 @@ async function parseArgs() {
164
197
  exitWithError("Missing value for -r flag.");
165
198
  flags.region = region;
166
199
  i++;
200
+ } else if (mappedFlag === "source") {
201
+ flags.source = true;
167
202
  } else {
168
203
  flags[mappedFlag] = true;
169
204
  }
@@ -239,7 +274,7 @@ function handleError(message, extra = "") {
239
274
  process.exit(1);
240
275
  }
241
276
 
242
- async function promptForRegion(defaultRegion) {
277
+ async function promptForRegion(defaultRegion, source) {
243
278
  let regions;
244
279
  try {
245
280
  regions = await getRegions();
@@ -264,17 +299,23 @@ async function promptForRegion(defaultRegion) {
264
299
  }
265
300
 
266
301
  try {
267
- await analytics.capture("create_db:region_selected", {
302
+ const analyticsProps = {
268
303
  command: CLI_NAME,
269
304
  region: region,
270
305
  "selection-method": "interactive",
271
- });
306
+ };
307
+
308
+ if (source) {
309
+ analyticsProps.source = source;
310
+ }
311
+
312
+ await analytics.capture("create_db:region_selected", analyticsProps);
272
313
  } catch (error) {}
273
314
 
274
315
  return region;
275
316
  }
276
317
 
277
- async function createDatabase(name, region, returnJson = false) {
318
+ async function createDatabase(name, region, source, returnJson = false ) {
278
319
  let s;
279
320
  if (!returnJson) {
280
321
  s = spinner();
@@ -284,7 +325,7 @@ async function createDatabase(name, region, returnJson = false) {
284
325
  const resp = await fetch(`${CREATE_DB_WORKER_URL}/create`, {
285
326
  method: "POST",
286
327
  headers: { "Content-Type": "application/json" },
287
- body: JSON.stringify({ region, name, utm_source: CLI_NAME }),
328
+ body: JSON.stringify({ region, name, utm_source: source }),
288
329
  });
289
330
 
290
331
  if (resp.status === 429) {
@@ -304,12 +345,18 @@ async function createDatabase(name, region, returnJson = false) {
304
345
  }
305
346
 
306
347
  try {
307
- await analytics.capture("create_db:database_creation_failed", {
348
+ const analyticsProps = {
308
349
  command: CLI_NAME,
309
350
  region: region,
310
351
  "error-type": "rate_limit",
311
352
  "status-code": 429,
312
- });
353
+ };
354
+
355
+ if (source) {
356
+ analyticsProps.source = source;
357
+ }
358
+
359
+ await analytics.capture("create_db:database_creation_failed", analyticsProps);
313
360
  } catch (error) {}
314
361
 
315
362
  process.exit(1);
@@ -333,12 +380,18 @@ async function createDatabase(name, region, returnJson = false) {
333
380
  s.stop("Unexpected response from create service.");
334
381
  }
335
382
  try {
336
- await analytics.capture("create_db:database_creation_failed", {
383
+ const analyticsProps = {
337
384
  command: CLI_NAME,
338
385
  region,
339
386
  "error-type": "invalid_json",
340
387
  "status-code": resp.status,
341
- });
388
+ };
389
+
390
+ if (source) {
391
+ analyticsProps.source = source;
392
+ }
393
+
394
+ await analytics.capture("create_db:database_creation_failed", analyticsProps);
342
395
  } catch {}
343
396
  process.exit(1);
344
397
  }
@@ -366,7 +419,7 @@ async function createDatabase(name, region, returnJson = false) {
366
419
  ? `postgresql://${directUser}:${directPass}@${directHost}${directPort}/${directDbName}`
367
420
  : null;
368
421
 
369
- const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${CLI_NAME}&utm_medium=cli`;
422
+ const claimUrl = `${CLAIM_DB_WORKER_URL}?projectID=${projectId}&utm_source=${source}&utm_medium=cli`;
370
423
  const expiryDate = new Date(Date.now() + 24 * 60 * 60 * 1000);
371
424
 
372
425
  if (returnJson && !result.error) {
@@ -378,6 +431,7 @@ async function createDatabase(name, region, returnJson = false) {
378
431
  region: database?.region?.id || region,
379
432
  name: database?.name,
380
433
  projectId: projectId,
434
+ source: source || null,
381
435
  };
382
436
  }
383
437
 
@@ -397,12 +451,18 @@ async function createDatabase(name, region, returnJson = false) {
397
451
  }
398
452
 
399
453
  try {
400
- await analytics.capture("create_db:database_creation_failed", {
454
+ const analyticsProps = {
401
455
  command: CLI_NAME,
402
456
  region: region,
403
457
  "error-type": "api_error",
404
458
  "error-message": result.error.message,
405
- });
459
+ };
460
+
461
+ if (source) {
462
+ analyticsProps.source = source;
463
+ }
464
+
465
+ await analytics.capture("create_db:database_creation_failed", analyticsProps);
406
466
  } catch (error) {}
407
467
  process.exit(1);
408
468
  }
@@ -459,8 +519,29 @@ async function createDatabase(name, region, returnJson = false) {
459
519
  async function main() {
460
520
  try {
461
521
  const rawArgs = process.argv.slice(2);
522
+
523
+ const { flags } = await parseArgs();
524
+
525
+ let source;
526
+ if (flags.source) {
527
+ const userEnvVars = readUserEnvFile();
528
+ const userCwd = process.cwd();
529
+ const envPath = path.join(userCwd, '.env');
530
+
531
+ if (fs.existsSync(envPath)) {
532
+ const ctaVars = [];
533
+ if (userEnvVars.CTA_VERSION) ctaVars.push(`v${userEnvVars.CTA_VERSION}`);
534
+ if (userEnvVars.CTA_FRAMEWORK) ctaVars.push(userEnvVars.CTA_FRAMEWORK);
535
+ if (userEnvVars.CTA_FRAMEWORK_VERSION) ctaVars.push(`fv${userEnvVars.CTA_FRAMEWORK_VERSION}`);
536
+
537
+ if (ctaVars.length > 0) {
538
+ source = ctaVars.join('-');
539
+ }
540
+ }
541
+ }
542
+
462
543
  try {
463
- await analytics.capture("create_db:cli_command_ran", {
544
+ const analyticsProps = {
464
545
  command: CLI_NAME,
465
546
  "full-command": `${CLI_NAME} ${rawArgs.join(" ")}`.trim(),
466
547
  "has-region-flag":
@@ -470,13 +551,20 @@ async function main() {
470
551
  "has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
471
552
  "has-list-regions-flag": rawArgs.includes("--list-regions"),
472
553
  "has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
554
+ "has-source-flag": rawArgs.includes("--source") || rawArgs.includes("-s"),
473
555
  "node-version": process.version,
474
556
  platform: process.platform,
475
557
  arch: process.arch,
476
- });
477
- } catch (error) {}
478
-
479
- const { flags } = await parseArgs();
558
+ };
559
+
560
+ if (source) {
561
+ analyticsProps.source = source;
562
+ }
563
+
564
+ await analytics.capture("create_db:cli_command_ran", analyticsProps);
565
+ } catch (error) {
566
+ console.error("Error:", error.message);
567
+ }
480
568
 
481
569
  if (!flags.help && !flags.json) {
482
570
  await isOffline();
@@ -499,14 +587,41 @@ async function main() {
499
587
  region = flags.region;
500
588
 
501
589
  try {
502
- await analytics.capture("create_db:region_selected", {
590
+ const analyticsProps = {
503
591
  command: CLI_NAME,
504
592
  region: region,
505
593
  "selection-method": "flag",
506
- });
594
+ };
595
+
596
+ if (source) {
597
+ analyticsProps.source = source;
598
+ }
599
+
600
+ await analytics.capture("create_db:region_selected", analyticsProps);
507
601
  } catch (error) {}
508
602
  }
509
603
 
604
+ if (flags.source) {
605
+ const userCwd = process.cwd();
606
+ const envPath = path.join(userCwd, '.env');
607
+
608
+ if (!fs.existsSync(envPath)) {
609
+ console.error(chalk.red("Error: Source not configured correctly."));
610
+ process.exit(1);
611
+ }
612
+
613
+ const userEnvVars = readUserEnvFile();
614
+ const ctaVars = [];
615
+ if (userEnvVars.CTA_VERSION) ctaVars.push(`v${userEnvVars.CTA_VERSION}`);
616
+ if (userEnvVars.CTA_FRAMEWORK) ctaVars.push(userEnvVars.CTA_FRAMEWORK);
617
+ if (userEnvVars.CTA_FRAMEWORK_VERSION) ctaVars.push(`fv${userEnvVars.CTA_FRAMEWORK_VERSION}`);
618
+
619
+ if (ctaVars.length === 0) {
620
+ console.error(chalk.red("Error: Source not configured correctly."));
621
+ process.exit(1);
622
+ }
623
+ }
624
+
510
625
  if (flags.interactive) {
511
626
  chooseRegionPrompt = true;
512
627
  }
@@ -514,11 +629,11 @@ async function main() {
514
629
  if (flags.json) {
515
630
  try {
516
631
  if (chooseRegionPrompt) {
517
- region = await promptForRegion(region);
632
+ region = await promptForRegion(region, source);
518
633
  } else {
519
634
  await validateRegion(region, true);
520
635
  }
521
- const result = await createDatabase(name, region, true);
636
+ const result = await createDatabase(name, region, source, true);
522
637
  console.log(JSON.stringify(result, null, 2));
523
638
  process.exit(0);
524
639
  } catch (e) {
@@ -543,12 +658,12 @@ async function main() {
543
658
  )
544
659
  );
545
660
  if (chooseRegionPrompt) {
546
- region = await promptForRegion(region);
661
+ region = await promptForRegion(region, source);
547
662
  }
548
663
 
549
664
  region = await validateRegion(region);
550
665
 
551
- await createDatabase(name, region);
666
+ await createDatabase(name, region, source);
552
667
 
553
668
  outro("");
554
669
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "1.0.3-pr44-DC-4828-json-flag-17127092542.0",
3
+ "version": "1.0.3-pr45-DC-4829-source-flag-17135144702.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": "",