@event4u/agent-switch 2.2.0 → 2.3.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.
package/dist/shellenv.js CHANGED
@@ -34,6 +34,7 @@
34
34
  * A side effect worth having: a genuinely broken agent-switch now surfaces its
35
35
  * error instead of silently degrading the shell onto the default config dir.
36
36
  */
37
+ import { spawnSync } from "node:child_process";
37
38
  import * as fs from "node:fs";
38
39
  import * as path from "node:path";
39
40
  import { allProviders } from "./providers.js";
@@ -205,15 +206,36 @@ export const SHELLENV_LINE = {
205
206
  powershell: "agent-switch shellenv --shell powershell | Out-String | Invoke-Expression",
206
207
  };
207
208
  /** The startup file `shellenv install` writes to. macOS opens login shells,
208
- * which read ~/.bash_profile rather than ~/.bashrc. */
209
- export function rcFileFor(shell, home, platform = process.platform) {
209
+ * which read ~/.bash_profile rather than ~/.bashrc. For PowerShell only the
210
+ * shell knows its profile — Windows PowerShell 5.1 and a Documents folder
211
+ * redirected to OneDrive both live elsewhere — so it is asked first. */
212
+ export function rcFileFor(shell, home, platform = process.platform, askProfile = powershellProfile) {
210
213
  if (shell === "zsh")
211
214
  return path.join(home, ".zshrc");
212
215
  if (shell === "bash")
213
216
  return path.join(home, platform === "darwin" ? ".bash_profile" : ".bashrc");
214
217
  if (shell === "fish")
215
218
  return path.join(home, ".config", "fish", "config.fish");
216
- return path.join(home, "Documents", "PowerShell", "Microsoft.PowerShell_profile.ps1");
219
+ return askProfile() ?? path.join(home, "Documents", "PowerShell", "Microsoft.PowerShell_profile.ps1");
220
+ }
221
+ /** Which PowerShell to ask first: the one this command runs under. Windows
222
+ * PowerShell 5.1 puts only `WindowsPowerShell` paths on PSModulePath; pwsh adds
223
+ * its own `PowerShell\7` (or `Documents\PowerShell`) ones. */
224
+ export function powershellOrder(env = process.env) {
225
+ const p = env.PSModulePath ?? "";
226
+ const pwsh = /[\\/]PowerShell[\\/](7|Modules)/i.test(p);
227
+ return !pwsh && /WindowsPowerShell/i.test(p) ? ["powershell", "pwsh"] : ["pwsh", "powershell"];
228
+ }
229
+ /** `$PROFILE` from the PowerShell the user runs, then the other one; null when
230
+ * neither answers. */
231
+ export function powershellProfile() {
232
+ for (const exe of powershellOrder()) {
233
+ const r = spawnSync(exe, ["-NoProfile", "-NonInteractive", "-Command", "$PROFILE"], { encoding: "utf8", timeout: 5000, windowsHide: true });
234
+ const out = r.status === 0 ? r.stdout.trim() : "";
235
+ if (out)
236
+ return out;
237
+ }
238
+ return null;
217
239
  }
218
240
  /** Append the loader line to the shell's startup file, once. `present` says
219
241
  * whether any known startup file already loads it (the doctor check). */
@@ -152,3 +152,14 @@ export function freshAccessToken(configDir, deps = defaultFreshenDeps()) {
152
152
  const creds = deps.readCredential(configDir);
153
153
  return creds ? accessTokenOf(creds) : null;
154
154
  }
155
+ /**
156
+ * The token for a usage read. A profile that shares its login with the default
157
+ * install (seeded by `import`) is never refreshed: whichever side refreshes
158
+ * first invalidates the other, so its stored token is used only while valid.
159
+ */
160
+ export function usageToken(configDir, deps, sharesDefaultLogin) {
161
+ if (!sharesDefaultLogin)
162
+ return freshAccessToken(configDir, deps);
163
+ const creds = deps.readCredential(configDir);
164
+ return creds && !needsFreshen(creds, deps.now(), deps.bufferMs) ? accessTokenOf(creds) : null;
165
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@event4u/agent-switch",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "Switch between multiple Claude Code accounts on macOS, Linux, and Windows via isolated CLAUDE_CONFIG_DIR profiles",
5
5
  "repository": {
6
6
  "type": "git",