codex-account-switcher 0.1.0 → 0.1.1

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 (3) hide show
  1. package/README.md +2 -0
  2. package/bin/cdx.js +67 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -61,6 +61,8 @@ cdx list
61
61
  cdx current
62
62
  cdx use work
63
63
  cdx switch
64
+ cdx swap
65
+ cdx remove work
64
66
  ```
65
67
 
66
68
  ## How It Works
package/bin/cdx.js CHANGED
@@ -22,6 +22,8 @@ function usage() {
22
22
  cdx save <name> Save current ~/.codex/auth.json as a named account
23
23
  cdx use <name> Activate a named account
24
24
  cdx switch Switch to next configured account
25
+ cdx swap Alias for 'switch'
26
+ cdx remove <name> Remove a configured account
25
27
  cdx current Print active account name
26
28
  cdx list List configured accounts
27
29
  cdx help Show this help
@@ -64,6 +66,12 @@ function setActive(name) {
64
66
  fs.writeFileSync(ACTIVE_FILE, `${name}\n`, "utf8");
65
67
  }
66
68
 
69
+ function clearActive() {
70
+ if (fs.existsSync(ACTIVE_FILE)) {
71
+ fs.rmSync(ACTIVE_FILE, { force: true });
72
+ }
73
+ }
74
+
67
75
  function upsertAccount(accounts, name, accountPath) {
68
76
  let found = false;
69
77
  const next = accounts.map((entry) => {
@@ -83,8 +91,22 @@ function findAccount(accounts, name) {
83
91
  return accounts.find((entry) => entry.name === name);
84
92
  }
85
93
 
94
+ function isRegularFile(filePath) {
95
+ try {
96
+ return fs.existsSync(filePath) && fs.statSync(filePath).isFile();
97
+ } catch (_) {
98
+ return false;
99
+ }
100
+ }
101
+
102
+ function isManagedSnapshot(filePath) {
103
+ const snapshotRoot = path.resolve(CDX_DIR, "auth");
104
+ const resolvedPath = path.resolve(filePath);
105
+ return resolvedPath.startsWith(`${snapshotRoot}${path.sep}`);
106
+ }
107
+
86
108
  function applyAuthFile(sourceAuth) {
87
- if (!fs.existsSync(sourceAuth) || !fs.statSync(sourceAuth).isFile()) {
109
+ if (!isRegularFile(sourceAuth)) {
88
110
  die(`auth file does not exist: ${sourceAuth}`);
89
111
  }
90
112
 
@@ -106,7 +128,7 @@ function cmdAdd(args) {
106
128
  }
107
129
  const [name, rawPath] = args;
108
130
  const fullPath = path.resolve(rawPath);
109
- if (!fs.existsSync(fullPath) || !fs.statSync(fullPath).isFile()) {
131
+ if (!isRegularFile(fullPath)) {
110
132
  die(`auth file not found: ${fullPath}`);
111
133
  }
112
134
 
@@ -124,7 +146,7 @@ function cmdSave(args) {
124
146
  }
125
147
  const [name] = args;
126
148
 
127
- if (!fs.existsSync(TARGET_AUTH) || !fs.statSync(TARGET_AUTH).isFile()) {
149
+ if (!isRegularFile(TARGET_AUTH)) {
128
150
  die(`no current auth found at ${TARGET_AUTH}. Run 'codex login' first.`);
129
151
  }
130
152
 
@@ -219,6 +241,42 @@ function cmdSwitch(args) {
219
241
  process.stdout.write(`Switched to account '${next.name}'\n`);
220
242
  }
221
243
 
244
+ function cmdRemove(args) {
245
+ if (args.length !== 1) {
246
+ die("remove requires <name>");
247
+ }
248
+ const [name] = args;
249
+ const accounts = readAccounts();
250
+ const removed = findAccount(accounts, name);
251
+ if (!removed) {
252
+ die(`unknown account: ${name}`);
253
+ }
254
+
255
+ const remaining = accounts.filter((entry) => entry.name !== name);
256
+ writeAccounts(remaining);
257
+
258
+ if (isManagedSnapshot(removed.path) && isRegularFile(removed.path)) {
259
+ fs.rmSync(removed.path, { force: true });
260
+ }
261
+
262
+ const active = getActive();
263
+ if (active !== name) {
264
+ process.stdout.write(`Removed account '${name}'\n`);
265
+ return;
266
+ }
267
+
268
+ const next = remaining.find((entry) => isRegularFile(entry.path));
269
+ if (!next) {
270
+ clearActive();
271
+ process.stdout.write(`Removed account '${name}'. No active account remains.\n`);
272
+ return;
273
+ }
274
+
275
+ applyAuthFile(next.path);
276
+ setActive(next.name);
277
+ process.stdout.write(`Removed account '${name}'. Switched to '${next.name}'.\n`);
278
+ }
279
+
222
280
  function main() {
223
281
  const args = process.argv.slice(2);
224
282
  const command = args[0] || "help";
@@ -244,6 +302,12 @@ function main() {
244
302
  case "switch":
245
303
  cmdSwitch(rest);
246
304
  break;
305
+ case "swap":
306
+ cmdSwitch(rest);
307
+ break;
308
+ case "remove":
309
+ cmdRemove(rest);
310
+ break;
247
311
  case "current":
248
312
  cmdCurrent(rest);
249
313
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codex-account-switcher",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Switch between multiple Codex CLI auth accounts with cdx switch",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",