@envmanager-cli/cli 0.1.2 → 0.1.3

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.
@@ -506,12 +506,16 @@ function getConfigPath() {
506
506
 
507
507
  // src/lib/resolve.ts
508
508
  var UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
509
+ function cleanProjectInput(input) {
510
+ return input.startsWith("#") ? input.slice(1) : input;
511
+ }
509
512
  async function resolveProjectId(input, client, organizationId) {
510
- if (UUID_REGEX.test(input)) {
511
- return input;
513
+ const cleaned = cleanProjectInput(input);
514
+ if (UUID_REGEX.test(cleaned)) {
515
+ return cleaned;
512
516
  }
513
- if (/^\d+$/.test(input)) {
514
- const friendlyId = parseInt(input, 10);
517
+ if (/^\d+$/.test(cleaned)) {
518
+ const friendlyId = parseInt(cleaned, 10);
515
519
  if (friendlyId < 1) {
516
520
  throw new Error("Project ID must be 1 or greater");
517
521
  }
@@ -521,13 +525,13 @@ async function resolveProjectId(input, client, organizationId) {
521
525
  }
522
526
  return data2.id;
523
527
  }
524
- const { data, error } = await client.from("projects").select("id").eq("organization_id", organizationId).ilike("name", input).single();
528
+ const { data, error } = await client.from("projects").select("id").eq("organization_id", organizationId).ilike("name", cleaned).single();
525
529
  if (error || !data) {
526
530
  throw new Error(`Project "${input}" not found in this organization`);
527
531
  }
528
532
  return data.id;
529
533
  }
530
- async function resolveOrganizationId(input, client) {
534
+ async function resolveOrganizationId(input, client, projectHint) {
531
535
  const { data: memberships, error: memberError } = await client.from("organization_members").select("organization_id, organizations(id, name)");
532
536
  if (memberError || !memberships || memberships.length === 0) {
533
537
  throw new Error("No organizations found. Create one at envmanager.dev");
@@ -535,21 +539,42 @@ async function resolveOrganizationId(input, client) {
535
539
  if (memberships.length === 1 && !input) {
536
540
  return memberships[0].organization_id;
537
541
  }
538
- if (!input) {
539
- const orgList = memberships.map((m) => {
542
+ if (input) {
543
+ const match = memberships.find((m) => {
540
544
  const org = m.organizations;
541
- return org?.name || "Unknown";
542
- }).join(", ");
543
- throw new Error(`Multiple organizations found. Use --org to specify: ${orgList}`);
545
+ return org?.name?.toLowerCase() === input.toLowerCase();
546
+ });
547
+ if (!match) {
548
+ throw new Error(`Organization "${input}" not found or you don't have access`);
549
+ }
550
+ return match.organization_id;
544
551
  }
545
- const match = memberships.find((m) => {
552
+ if (projectHint) {
553
+ const orgId = await detectOrgFromProject(projectHint, client, memberships);
554
+ if (orgId) return orgId;
555
+ }
556
+ const orgList = memberships.map((m) => {
546
557
  const org = m.organizations;
547
- return org?.name?.toLowerCase() === input.toLowerCase();
548
- });
549
- if (!match) {
550
- throw new Error(`Organization "${input}" not found or you don't have access`);
558
+ return org?.name || "Unknown";
559
+ }).join(", ");
560
+ throw new Error(`Multiple organizations found. Use --org to specify: ${orgList}`);
561
+ }
562
+ async function detectOrgFromProject(projectInput, client, memberships) {
563
+ const cleaned = cleanProjectInput(projectInput);
564
+ const orgIds = memberships.map((m) => m.organization_id);
565
+ if (UUID_REGEX.test(cleaned)) {
566
+ const { data: data2 } = await client.from("projects").select("organization_id").eq("id", cleaned).in("organization_id", orgIds).single();
567
+ return data2?.organization_id ?? null;
568
+ }
569
+ if (/^\d+$/.test(cleaned)) {
570
+ const friendlyId = parseInt(cleaned, 10);
571
+ const { data: data2 } = await client.from("projects").select("organization_id").in("organization_id", orgIds).eq("friendly_id", friendlyId);
572
+ if (data2 && data2.length === 1) return data2[0].organization_id;
573
+ return null;
551
574
  }
552
- return match.organization_id;
575
+ const { data } = await client.from("projects").select("organization_id").in("organization_id", orgIds).ilike("name", cleaned);
576
+ if (data && data.length === 1) return data[0].organization_id;
577
+ return null;
553
578
  }
554
579
 
555
580
  // src/lib/variable-references.ts
@@ -731,7 +756,7 @@ var pullCommand = new Command4("pull").description("Pull environment variables f
731
756
  const client = await createClient();
732
757
  let organizationId;
733
758
  try {
734
- organizationId = await resolveOrganizationId(options.org, client);
759
+ organizationId = await resolveOrganizationId(options.org, client, projectInput);
735
760
  } catch (error) {
736
761
  spinner.fail(error instanceof Error ? error.message : "Failed to resolve organization");
737
762
  process.exit(1);
@@ -1026,7 +1051,7 @@ var pushCommand = new Command5("push").description("Push local .env file to EnvM
1026
1051
  const client = await createClient();
1027
1052
  let organizationId;
1028
1053
  try {
1029
- organizationId = await resolveOrganizationId(options.org, client);
1054
+ organizationId = await resolveOrganizationId(options.org, client, projectInput);
1030
1055
  } catch (error) {
1031
1056
  spinner.fail(error instanceof Error ? error.message : "Failed to resolve organization");
1032
1057
  process.exit(1);
@@ -1158,7 +1183,7 @@ var diffCommand = new Command6("diff").description("Show differences between loc
1158
1183
  const client = await createClient();
1159
1184
  let organizationId;
1160
1185
  try {
1161
- organizationId = await resolveOrganizationId(options.org, client);
1186
+ organizationId = await resolveOrganizationId(options.org, client, projectInput);
1162
1187
  } catch (error) {
1163
1188
  spinner.fail(error instanceof Error ? error.message : "Failed to resolve organization");
1164
1189
  process.exit(1);
@@ -1310,7 +1335,7 @@ var listCommand = new Command7("list").description("List projects, environments,
1310
1335
  }
1311
1336
  let organizationId;
1312
1337
  try {
1313
- organizationId = await resolveOrganizationId(options.org, client);
1338
+ organizationId = await resolveOrganizationId(options.org, client, projectInput);
1314
1339
  } catch (error) {
1315
1340
  spinner.fail(error instanceof Error ? error.message : "Failed to resolve organization");
1316
1341
  process.exit(1);
@@ -1354,7 +1379,7 @@ var listCommand = new Command7("list").description("List projects, environments,
1354
1379
  }
1355
1380
  let organizationId;
1356
1381
  try {
1357
- organizationId = await resolveOrganizationId(options.org, client);
1382
+ organizationId = await resolveOrganizationId(options.org, client, projectInput);
1358
1383
  } catch (error) {
1359
1384
  spinner.fail(error instanceof Error ? error.message : "Failed to resolve organization");
1360
1385
  process.exit(1);
@@ -1678,7 +1703,7 @@ var devCommand = new Command9("dev").description("Start real-time sync daemon -
1678
1703
  const client = await createClient();
1679
1704
  let organizationId;
1680
1705
  try {
1681
- organizationId = await resolveOrganizationId(options.org, client);
1706
+ organizationId = await resolveOrganizationId(options.org, client, projectInput);
1682
1707
  } catch (error) {
1683
1708
  spinner.fail(error instanceof Error ? error.message : "Failed to resolve organization");
1684
1709
  process.exit(1);
@@ -2657,7 +2682,7 @@ var validateCommand = new Command12("validate").description("Validate local .env
2657
2682
  const client = await createClient();
2658
2683
  let organizationId;
2659
2684
  try {
2660
- organizationId = await resolveOrganizationId(options.org, client);
2685
+ organizationId = await resolveOrganizationId(options.org, client, projectInput);
2661
2686
  } catch (error) {
2662
2687
  spinner.fail(error instanceof Error ? error.message : "Failed to resolve organization");
2663
2688
  process.exit(1);