@commonlyai/cli 0.1.40 → 0.1.41

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commonlyai/cli",
3
- "version": "0.1.40",
3
+ "version": "0.1.41",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -8,7 +8,7 @@
8
8
  import { createInterface } from 'readline';
9
9
  import { hostname } from 'os';
10
10
  import { createClient, login as apiLogin } from '../lib/api.js';
11
- import { saveInstance } from '../lib/config.js';
11
+ import { DEFAULT_URL, resolveInstance, saveInstance } from '../lib/config.js';
12
12
  import {
13
13
  DeviceLoginCancelledError,
14
14
  DeviceLoginDeniedError,
@@ -42,16 +42,40 @@ const promptSecret = (question) => new Promise((resolve) => {
42
42
  stdin.on('data', onData);
43
43
  });
44
44
 
45
+ /**
46
+ * Resolve the login target before making the request or writing config.
47
+ *
48
+ * `--instance` accepts either a URL or a saved profile key. The API client
49
+ * resolves keys internally, but persisting the raw argument would write a key
50
+ * (for example, "default") into the URL field and break every later command.
51
+ */
52
+ export const resolveLoginTarget = ({ instanceArg, keyArg }) => {
53
+ // A missing --instance means "the active profile", just like the other
54
+ // config helpers. Resolve it before choosing the key so a login without
55
+ // flags refreshes that profile instead of silently forking `default`.
56
+ const resolved = resolveInstance(instanceArg);
57
+ const isUrl = /^https?:\/\//i.test(instanceArg || '');
58
+ const instanceUrl = (resolved?.url || (isUrl ? instanceArg : DEFAULT_URL)).replace(/\/$/, '');
59
+ const isLocal = instanceUrl.includes('localhost') || instanceUrl.includes('127.0.0.1');
60
+ const isKey = instanceArg && !isUrl;
61
+ // A URL may resolve back to an existing named profile too. Prefer that key
62
+ // regardless of input shape, or a URL login can fork a second profile onto
63
+ // the same endpoint and leave the original token stale.
64
+ const configKey = keyArg || resolved?.key || (isKey ? instanceArg : (isLocal ? 'local' : 'default'));
65
+
66
+ return { instanceUrl, configKey };
67
+ };
68
+
45
69
  export const registerLogin = (program) => {
46
70
  program
47
71
  .command('login')
48
72
  .description('Authenticate to a Commonly instance')
49
- .option('--instance <url>', 'Instance URL (default: https://api.commonly.me)')
50
- .option('--key <name>', 'Config key to save as (default: "default" or "local")')
73
+ .option('--instance <url-or-key>', 'Instance URL or saved profile key (default: active profile; production on first run)')
74
+ .option('--key <name>', 'Config key to save as (default: active profile key; otherwise derived from the instance)')
51
75
  .option('--password', 'Use the legacy email/password prompt instead of device authorization')
52
76
  .addHelpText('after', `
53
77
  Examples:
54
- $ commonly login # production (default key)
78
+ $ commonly login # active profile (production on first run)
55
79
  $ commonly login --instance https://api.commonly.me --key dev # named profile
56
80
  $ commonly login --instance http://localhost:5000 # saved as "local"
57
81
 
@@ -59,12 +83,10 @@ Tokens are stored in ~/.commonly/config.json. Other commands take
59
83
  --instance <url-or-key> to target the right profile.
60
84
  `)
61
85
  .action(async (opts) => {
62
- const instanceUrl = opts.instance
63
- ? opts.instance.replace(/\/$/, '')
64
- : 'https://api.commonly.me';
65
-
66
- const isLocal = instanceUrl.includes('localhost') || instanceUrl.includes('127.0.0.1');
67
- const configKey = opts.key || (isLocal ? 'local' : 'default');
86
+ const { instanceUrl, configKey } = resolveLoginTarget({
87
+ instanceArg: opts.instance,
88
+ keyArg: opts.key,
89
+ });
68
90
 
69
91
  try {
70
92
  if (!opts.password) {