create-db 1.0.3-pr44-DC-4828-json-flag-17109631462.0 → 1.0.3-pr45-DC-4829-source-flag-17134940199.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 +137 -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) {
@@ -397,12 +450,18 @@ async function createDatabase(name, region, returnJson = false) {
397
450
  }
398
451
 
399
452
  try {
400
- await analytics.capture("create_db:database_creation_failed", {
453
+ const analyticsProps = {
401
454
  command: CLI_NAME,
402
455
  region: region,
403
456
  "error-type": "api_error",
404
457
  "error-message": result.error.message,
405
- });
458
+ };
459
+
460
+ if (source) {
461
+ analyticsProps.source = source;
462
+ }
463
+
464
+ await analytics.capture("create_db:database_creation_failed", analyticsProps);
406
465
  } catch (error) {}
407
466
  process.exit(1);
408
467
  }
@@ -459,8 +518,29 @@ async function createDatabase(name, region, returnJson = false) {
459
518
  async function main() {
460
519
  try {
461
520
  const rawArgs = process.argv.slice(2);
521
+
522
+ const { flags } = await parseArgs();
523
+
524
+ let source;
525
+ if (flags.source) {
526
+ const userEnvVars = readUserEnvFile();
527
+ const userCwd = process.cwd();
528
+ const envPath = path.join(userCwd, '.env');
529
+
530
+ if (fs.existsSync(envPath)) {
531
+ const ctaVars = [];
532
+ if (userEnvVars.CTA_VERSION) ctaVars.push(`v${userEnvVars.CTA_VERSION}`);
533
+ if (userEnvVars.CTA_FRAMEWORK) ctaVars.push(userEnvVars.CTA_FRAMEWORK);
534
+ if (userEnvVars.CTA_FRAMEWORK_VERSION) ctaVars.push(`fv${userEnvVars.CTA_FRAMEWORK_VERSION}`);
535
+
536
+ if (ctaVars.length > 0) {
537
+ source = ctaVars.join('-');
538
+ }
539
+ }
540
+ }
541
+
462
542
  try {
463
- await analytics.capture("create_db:cli_command_ran", {
543
+ const analyticsProps = {
464
544
  command: CLI_NAME,
465
545
  "full-command": `${CLI_NAME} ${rawArgs.join(" ")}`.trim(),
466
546
  "has-region-flag":
@@ -470,13 +550,20 @@ async function main() {
470
550
  "has-help-flag": rawArgs.includes("--help") || rawArgs.includes("-h"),
471
551
  "has-list-regions-flag": rawArgs.includes("--list-regions"),
472
552
  "has-json-flag": rawArgs.includes("--json") || rawArgs.includes("-j"),
553
+ "has-source-flag": rawArgs.includes("--source") || rawArgs.includes("-s"),
473
554
  "node-version": process.version,
474
555
  platform: process.platform,
475
556
  arch: process.arch,
476
- });
477
- } catch (error) {}
478
-
479
- const { flags } = await parseArgs();
557
+ };
558
+
559
+ if (source) {
560
+ analyticsProps.source = source;
561
+ }
562
+
563
+ await analytics.capture("create_db:cli_command_ran", analyticsProps);
564
+ } catch (error) {
565
+ console.error("Error:", error.message);
566
+ }
480
567
 
481
568
  if (!flags.help && !flags.json) {
482
569
  await isOffline();
@@ -499,14 +586,41 @@ async function main() {
499
586
  region = flags.region;
500
587
 
501
588
  try {
502
- await analytics.capture("create_db:region_selected", {
589
+ const analyticsProps = {
503
590
  command: CLI_NAME,
504
591
  region: region,
505
592
  "selection-method": "flag",
506
- });
593
+ };
594
+
595
+ if (source) {
596
+ analyticsProps.source = source;
597
+ }
598
+
599
+ await analytics.capture("create_db:region_selected", analyticsProps);
507
600
  } catch (error) {}
508
601
  }
509
602
 
603
+ if (flags.source) {
604
+ const userCwd = process.cwd();
605
+ const envPath = path.join(userCwd, '.env');
606
+
607
+ if (!fs.existsSync(envPath)) {
608
+ console.error(chalk.red("Error: Source not configured correctly."));
609
+ process.exit(1);
610
+ }
611
+
612
+ const userEnvVars = readUserEnvFile();
613
+ const ctaVars = [];
614
+ if (userEnvVars.CTA_VERSION) ctaVars.push(`v${userEnvVars.CTA_VERSION}`);
615
+ if (userEnvVars.CTA_FRAMEWORK) ctaVars.push(userEnvVars.CTA_FRAMEWORK);
616
+ if (userEnvVars.CTA_FRAMEWORK_VERSION) ctaVars.push(`fv${userEnvVars.CTA_FRAMEWORK_VERSION}`);
617
+
618
+ if (ctaVars.length === 0) {
619
+ console.error(chalk.red("Error: Source not configured correctly."));
620
+ process.exit(1);
621
+ }
622
+ }
623
+
510
624
  if (flags.interactive) {
511
625
  chooseRegionPrompt = true;
512
626
  }
@@ -514,11 +628,11 @@ async function main() {
514
628
  if (flags.json) {
515
629
  try {
516
630
  if (chooseRegionPrompt) {
517
- region = await promptForRegion(region);
631
+ region = await promptForRegion(region, source);
518
632
  } else {
519
633
  await validateRegion(region, true);
520
634
  }
521
- const result = await createDatabase(name, region, true);
635
+ const result = await createDatabase(name, region, source, true);
522
636
  console.log(JSON.stringify(result, null, 2));
523
637
  process.exit(0);
524
638
  } catch (e) {
@@ -543,12 +657,12 @@ async function main() {
543
657
  )
544
658
  );
545
659
  if (chooseRegionPrompt) {
546
- region = await promptForRegion(region);
660
+ region = await promptForRegion(region, source);
547
661
  }
548
662
 
549
663
  region = await validateRegion(region);
550
664
 
551
- await createDatabase(name, region);
665
+ await createDatabase(name, region, source);
552
666
 
553
667
  outro("");
554
668
  } 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-17109631462.0",
3
+ "version": "1.0.3-pr45-DC-4829-source-flag-17134940199.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": "",