@envmanager-cli/cli 0.1.3 → 0.1.5

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.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/bin/envmanager.ts
4
- import { Command as Command14 } from "commander";
4
+ import { Command as Command15 } from "commander";
5
5
 
6
6
  // src/commands/login.ts
7
7
  import { Command } from "commander";
@@ -88,7 +88,7 @@ function escapeHtml(str) {
88
88
  return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
89
89
  }
90
90
  async function startCallbackServer(expectedState, port = 8976) {
91
- return new Promise((resolve8, reject) => {
91
+ return new Promise((resolve9, reject) => {
92
92
  const timeout = setTimeout(() => {
93
93
  server.close();
94
94
  reject(new Error("Authentication timed out after 5 minutes"));
@@ -161,9 +161,9 @@ async function startCallbackServer(expectedState, port = 8976) {
161
161
  clearTimeout(timeout);
162
162
  server.close();
163
163
  if (apiKey) {
164
- resolve8({ apiKey });
164
+ resolve9({ apiKey });
165
165
  } else {
166
- resolve8({
166
+ resolve9({
167
167
  accessToken,
168
168
  refreshToken,
169
169
  expiresIn: parseInt(expiresIn || "3600", 10)
@@ -419,7 +419,7 @@ var whoamiCommand = new Command3("whoami").description("Show current user and te
419
419
  console.error(chalk3.red(userError?.message || "Unknown error"));
420
420
  return;
421
421
  }
422
- const { data: memberships, error: memberError } = await client.from("organization_members").select("role, organizations(name)");
422
+ const { data: memberships, error: memberError } = await client.from("organization_members").select("role, organizations(name)").eq("user_id", user.id);
423
423
  if (memberError) {
424
424
  spinner.fail("Failed to fetch memberships");
425
425
  console.error(chalk3.red(memberError.message));
@@ -532,7 +532,9 @@ async function resolveProjectId(input, client, organizationId) {
532
532
  return data.id;
533
533
  }
534
534
  async function resolveOrganizationId(input, client, projectHint) {
535
- const { data: memberships, error: memberError } = await client.from("organization_members").select("organization_id, organizations(id, name)");
535
+ const { data: { user } } = await client.auth.getUser();
536
+ if (!user) throw new Error("Not authenticated");
537
+ const { data: memberships, error: memberError } = await client.from("organization_members").select("organization_id, organizations(id, name)").eq("user_id", user.id);
536
538
  if (memberError || !memberships || memberships.length === 0) {
537
539
  throw new Error("No organizations found. Create one at envmanager.dev");
538
540
  }
@@ -1294,7 +1296,9 @@ var listCommand = new Command7("list").description("List projects, environments,
1294
1296
  case "projects":
1295
1297
  case "project": {
1296
1298
  spinner.text = "Fetching projects...";
1297
- const { data: memberships, error: memberError } = await client.from("organization_members").select("organization_id");
1299
+ const { data: { user: currentUser } } = await client.auth.getUser();
1300
+ if (!currentUser) throw new Error("Not authenticated");
1301
+ const { data: memberships, error: memberError } = await client.from("organization_members").select("organization_id").eq("user_id", currentUser.id);
1298
1302
  if (memberError) throw new Error(memberError.message);
1299
1303
  const orgIds = memberships?.map((m) => m.organization_id) || [];
1300
1304
  if (orgIds.length === 0) {
@@ -1507,11 +1511,11 @@ async function subscribeToVariableChanges(environmentId, onEvent, onStatus) {
1507
1511
  onStatus?.("connected");
1508
1512
  }
1509
1513
  });
1510
- const subscription = await new Promise((resolve8, reject) => {
1514
+ const subscription = await new Promise((resolve9, reject) => {
1511
1515
  channel.subscribe(async (status, err) => {
1512
1516
  if (status === "SUBSCRIBED") {
1513
1517
  onStatus?.("connected");
1514
- resolve8({
1518
+ resolve9({
1515
1519
  channel,
1516
1520
  environmentId,
1517
1521
  unsubscribe: async () => {
@@ -2900,7 +2904,9 @@ async function getProjectCompletions(prefix) {
2900
2904
  try {
2901
2905
  if (!getCredentials()) return [];
2902
2906
  const client = await createClient();
2903
- const { data: memberships } = await client.from("organization_members").select("organization_id");
2907
+ const { data: { user } } = await client.auth.getUser();
2908
+ if (!user) return [];
2909
+ const { data: memberships } = await client.from("organization_members").select("organization_id").eq("user_id", user.id);
2904
2910
  if (!memberships || memberships.length === 0) return [];
2905
2911
  const orgIds = memberships.map((m) => m.organization_id);
2906
2912
  const { data: projects } = await client.from("projects").select("name").in("organization_id", orgIds).order("name");
@@ -2972,8 +2978,109 @@ var completionCommand = new Command13("completion").description("Generate shell
2972
2978
  }
2973
2979
  });
2974
2980
 
2981
+ // src/commands/debug.ts
2982
+ import { Command as Command14 } from "commander";
2983
+ import chalk14 from "chalk";
2984
+ import { readFileSync as readFileSync9 } from "fs";
2985
+ import { fileURLToPath } from "url";
2986
+ import { dirname as dirname2, resolve as resolve8 } from "path";
2987
+ function getCliVersion() {
2988
+ try {
2989
+ const __dirname2 = dirname2(fileURLToPath(import.meta.url));
2990
+ const pkg = JSON.parse(readFileSync9(resolve8(__dirname2, "../../package.json"), "utf-8"));
2991
+ return pkg.version || "unknown";
2992
+ } catch {
2993
+ return "unknown";
2994
+ }
2995
+ }
2996
+ var debugCommand = new Command14("debug").description("Collect diagnostic info for troubleshooting").action(async () => {
2997
+ const lines = [];
2998
+ const log = (msg) => lines.push(msg);
2999
+ log("=== EnvManager CLI Debug Report ===");
3000
+ log(`Timestamp: ${(/* @__PURE__ */ new Date()).toISOString()}`);
3001
+ log(`CLI Version: ${getCliVersion()}`);
3002
+ log(`Node: ${process.version}`);
3003
+ log(`Platform: ${process.platform} ${process.arch}`);
3004
+ log("");
3005
+ log("--- Config ---");
3006
+ const configPath = getConfigPath();
3007
+ const config = loadConfig();
3008
+ log(`Config file: ${configPath || "not found"}`);
3009
+ if (config) {
3010
+ log(` project_id: ${config.project_id || "not set"}`);
3011
+ log(` project_name: ${config.project_name || "not set"}`);
3012
+ log(` environment: ${config.environment || "not set"}`);
3013
+ log(` organization_id: ${config.organization_id || "not set"}`);
3014
+ log(` api_url: ${config.api_url || "not set"}`);
3015
+ }
3016
+ log("");
3017
+ log("--- Authentication ---");
3018
+ const envApiKey = getApiKeyFromEnv();
3019
+ const storedApiKey = getStoredApiKey();
3020
+ const storedApiUrl = getStoredApiUrl();
3021
+ const creds = getCredentials();
3022
+ if (envApiKey) {
3023
+ log(`Auth method: Environment API key (${envApiKey.substring(0, 11)}...)`);
3024
+ } else if (storedApiKey) {
3025
+ log(`Auth method: CLI session key (${storedApiKey.substring(0, 11)}...)`);
3026
+ } else if (creds?.accessToken) {
3027
+ log("Auth method: Legacy session tokens");
3028
+ if (creds.expiresAt) {
3029
+ const remaining = Math.round((creds.expiresAt - Date.now()) / 1e3);
3030
+ log(` Token expires in: ${remaining > 0 ? `${remaining}s` : "EXPIRED"}`);
3031
+ }
3032
+ } else {
3033
+ log("Auth method: Not authenticated");
3034
+ }
3035
+ log(`API URL: ${storedApiUrl || process.env.ENVMANAGER_API_URL || "default (production)"}`);
3036
+ log("");
3037
+ log("--- API Connection ---");
3038
+ try {
3039
+ const start = Date.now();
3040
+ const client = await createClient();
3041
+ const authTime = Date.now() - start;
3042
+ log(`Auth exchange: ${authTime}ms`);
3043
+ const userStart = Date.now();
3044
+ const { data: { user }, error: userError } = await client.auth.getUser();
3045
+ const userTime = Date.now() - userStart;
3046
+ log(`getUser: ${userTime}ms`);
3047
+ if (userError) {
3048
+ log(` ERROR: ${userError.message}`);
3049
+ } else if (user) {
3050
+ log(` User ID: ${user.id}`);
3051
+ log(` Email: ${user.email}`);
3052
+ }
3053
+ if (user) {
3054
+ const orgStart = Date.now();
3055
+ const { data: memberships, error: orgError } = await client.from("organization_members").select("organization_id, role, organizations(id, name)").eq("user_id", user.id);
3056
+ const orgTime = Date.now() - orgStart;
3057
+ log(`Org query: ${orgTime}ms`);
3058
+ if (orgError) {
3059
+ log(` ERROR: ${orgError.message}`);
3060
+ } else if (memberships) {
3061
+ log(` Organizations (${memberships.length}):`);
3062
+ memberships.forEach((m) => {
3063
+ const org = m.organizations;
3064
+ const orgName = Array.isArray(org) ? org[0]?.name : org?.name || "Unknown";
3065
+ const orgId = Array.isArray(org) ? org[0]?.id : org?.id || "?";
3066
+ log(` - ${orgName} (${m.role}) [${orgId.substring(0, 8)}...]`);
3067
+ });
3068
+ }
3069
+ }
3070
+ } catch (error) {
3071
+ log(` ERROR: ${error instanceof Error ? error.message : "Unknown error"}`);
3072
+ }
3073
+ log("");
3074
+ log("=== End Debug Report ===");
3075
+ const output = lines.join("\n");
3076
+ console.log(output);
3077
+ console.log("");
3078
+ console.log(chalk14.gray("Copy the output above and share it for troubleshooting."));
3079
+ console.log(chalk14.gray("Note: API keys are partially redacted. No secrets are exposed."));
3080
+ });
3081
+
2975
3082
  // src/bin/envmanager.ts
2976
- var program = new Command14();
3083
+ var program = new Command15();
2977
3084
  program.name("envmanager").description("CLI for EnvManager - secure environment variable management").version("0.1.0");
2978
3085
  program.addCommand(loginCommand);
2979
3086
  program.addCommand(logoutCommand);
@@ -2989,5 +3096,6 @@ program.addCommand(templateCommand);
2989
3096
  program.addCommand(validateCommand);
2990
3097
  program.addCommand(completionCommand);
2991
3098
  program.addCommand(completeCommand);
3099
+ program.addCommand(debugCommand);
2992
3100
  program.parse();
2993
3101
  //# sourceMappingURL=envmanager.js.map