codex-account-switcher 0.1.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/LICENSE +21 -0
- package/README.md +76 -0
- package/bin/cdx.js +258 -0
- package/package.json +28 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# cdx (Codex Account Switcher)
|
|
2
|
+
|
|
3
|
+
Switch between Codex CLI accounts quickly with:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
cdx switch
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm i -g codex-account-switcher
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
or
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bun add -g codex-account-switcher
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Commands installed:
|
|
22
|
+
|
|
23
|
+
- `cdx`
|
|
24
|
+
- `cxs` (alias of `cdx`)
|
|
25
|
+
|
|
26
|
+
## Before Setup
|
|
27
|
+
|
|
28
|
+
Make sure Codex stores auth in a file (`auth.json`):
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
codex -c 'cli_auth_credentials_store="file"' login
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick Setup
|
|
35
|
+
|
|
36
|
+
1. Login to account A and save it:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
codex login
|
|
40
|
+
cdx save personal
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
2. Login to account B and save it:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
codex logout
|
|
47
|
+
codex login
|
|
48
|
+
cdx save work
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
3. Switch:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
cdx switch
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
cdx list
|
|
61
|
+
cdx current
|
|
62
|
+
cdx use work
|
|
63
|
+
cdx switch
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## How It Works
|
|
67
|
+
|
|
68
|
+
`cdx` copies the selected account auth file into:
|
|
69
|
+
|
|
70
|
+
- `~/.codex/auth.json` (or `$CODEX_HOME/auth.json`)
|
|
71
|
+
|
|
72
|
+
State is stored in:
|
|
73
|
+
|
|
74
|
+
- `~/.cdx/accounts.json`
|
|
75
|
+
- `~/.cdx/active`
|
|
76
|
+
- `~/.cdx/auth/*.auth.json` (created by `cdx save`)
|
package/bin/cdx.js
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const os = require("node:os");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
|
|
8
|
+
const CDX_DIR = process.env.CDX_DIR || path.join(os.homedir(), ".cdx");
|
|
9
|
+
const ACCOUNTS_FILE = path.join(CDX_DIR, "accounts.json");
|
|
10
|
+
const ACTIVE_FILE = path.join(CDX_DIR, "active");
|
|
11
|
+
const CODEX_HOME_DIR = process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
|
|
12
|
+
const TARGET_AUTH = path.join(CODEX_HOME_DIR, "auth.json");
|
|
13
|
+
|
|
14
|
+
function die(message) {
|
|
15
|
+
process.stderr.write(`cdx: ${message}\n`);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function usage() {
|
|
20
|
+
process.stdout.write(`Usage:
|
|
21
|
+
cdx add <name> <auth_json_path> Register an account auth file
|
|
22
|
+
cdx save <name> Save current ~/.codex/auth.json as a named account
|
|
23
|
+
cdx use <name> Activate a named account
|
|
24
|
+
cdx switch Switch to next configured account
|
|
25
|
+
cdx current Print active account name
|
|
26
|
+
cdx list List configured accounts
|
|
27
|
+
cdx help Show this help
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function ensureState() {
|
|
32
|
+
fs.mkdirSync(CDX_DIR, { recursive: true });
|
|
33
|
+
if (!fs.existsSync(ACCOUNTS_FILE)) {
|
|
34
|
+
fs.writeFileSync(ACCOUNTS_FILE, "[]\n", "utf8");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function readAccounts() {
|
|
39
|
+
try {
|
|
40
|
+
const data = fs.readFileSync(ACCOUNTS_FILE, "utf8");
|
|
41
|
+
const parsed = JSON.parse(data);
|
|
42
|
+
if (!Array.isArray(parsed)) {
|
|
43
|
+
die(`invalid accounts file at ${ACCOUNTS_FILE}`);
|
|
44
|
+
}
|
|
45
|
+
return parsed.filter((entry) => entry && entry.name && entry.path);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
die(`failed to read accounts: ${err.message}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function writeAccounts(accounts) {
|
|
52
|
+
const json = `${JSON.stringify(accounts, null, 2)}\n`;
|
|
53
|
+
fs.writeFileSync(ACCOUNTS_FILE, json, "utf8");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getActive() {
|
|
57
|
+
if (!fs.existsSync(ACTIVE_FILE)) {
|
|
58
|
+
return "";
|
|
59
|
+
}
|
|
60
|
+
return fs.readFileSync(ACTIVE_FILE, "utf8").trim();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function setActive(name) {
|
|
64
|
+
fs.writeFileSync(ACTIVE_FILE, `${name}\n`, "utf8");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function upsertAccount(accounts, name, accountPath) {
|
|
68
|
+
let found = false;
|
|
69
|
+
const next = accounts.map((entry) => {
|
|
70
|
+
if (entry.name === name) {
|
|
71
|
+
found = true;
|
|
72
|
+
return { name, path: accountPath };
|
|
73
|
+
}
|
|
74
|
+
return entry;
|
|
75
|
+
});
|
|
76
|
+
if (!found) {
|
|
77
|
+
next.push({ name, path: accountPath });
|
|
78
|
+
}
|
|
79
|
+
return next;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function findAccount(accounts, name) {
|
|
83
|
+
return accounts.find((entry) => entry.name === name);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function applyAuthFile(sourceAuth) {
|
|
87
|
+
if (!fs.existsSync(sourceAuth) || !fs.statSync(sourceAuth).isFile()) {
|
|
88
|
+
die(`auth file does not exist: ${sourceAuth}`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fs.mkdirSync(CODEX_HOME_DIR, { recursive: true });
|
|
92
|
+
if (fs.existsSync(TARGET_AUTH)) {
|
|
93
|
+
fs.copyFileSync(TARGET_AUTH, `${TARGET_AUTH}.bak`);
|
|
94
|
+
}
|
|
95
|
+
fs.copyFileSync(sourceAuth, TARGET_AUTH);
|
|
96
|
+
try {
|
|
97
|
+
fs.chmodSync(TARGET_AUTH, 0o600);
|
|
98
|
+
} catch (_) {
|
|
99
|
+
// Ignore chmod failures on non-POSIX systems.
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function cmdAdd(args) {
|
|
104
|
+
if (args.length !== 2) {
|
|
105
|
+
die("add requires <name> <auth_json_path>");
|
|
106
|
+
}
|
|
107
|
+
const [name, rawPath] = args;
|
|
108
|
+
const fullPath = path.resolve(rawPath);
|
|
109
|
+
if (!fs.existsSync(fullPath) || !fs.statSync(fullPath).isFile()) {
|
|
110
|
+
die(`auth file not found: ${fullPath}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const accounts = readAccounts();
|
|
114
|
+
writeAccounts(upsertAccount(accounts, name, fullPath));
|
|
115
|
+
if (!getActive()) {
|
|
116
|
+
setActive(name);
|
|
117
|
+
}
|
|
118
|
+
process.stdout.write(`Registered account '${name}' -> ${fullPath}\n`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function cmdSave(args) {
|
|
122
|
+
if (args.length !== 1) {
|
|
123
|
+
die("save requires <name>");
|
|
124
|
+
}
|
|
125
|
+
const [name] = args;
|
|
126
|
+
|
|
127
|
+
if (!fs.existsSync(TARGET_AUTH) || !fs.statSync(TARGET_AUTH).isFile()) {
|
|
128
|
+
die(`no current auth found at ${TARGET_AUTH}. Run 'codex login' first.`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const snapshotDir = path.join(CDX_DIR, "auth");
|
|
132
|
+
const snapshotPath = path.join(snapshotDir, `${name}.auth.json`);
|
|
133
|
+
fs.mkdirSync(snapshotDir, { recursive: true });
|
|
134
|
+
fs.copyFileSync(TARGET_AUTH, snapshotPath);
|
|
135
|
+
try {
|
|
136
|
+
fs.chmodSync(snapshotPath, 0o600);
|
|
137
|
+
} catch (_) {
|
|
138
|
+
// Ignore chmod failures on non-POSIX systems.
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const accounts = readAccounts();
|
|
142
|
+
writeAccounts(upsertAccount(accounts, name, snapshotPath));
|
|
143
|
+
if (!getActive()) {
|
|
144
|
+
setActive(name);
|
|
145
|
+
}
|
|
146
|
+
process.stdout.write(`Saved current auth as '${name}'\n`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function cmdUse(args) {
|
|
150
|
+
if (args.length !== 1) {
|
|
151
|
+
die("use requires <name>");
|
|
152
|
+
}
|
|
153
|
+
const [name] = args;
|
|
154
|
+
const accounts = readAccounts();
|
|
155
|
+
const account = findAccount(accounts, name);
|
|
156
|
+
if (!account) {
|
|
157
|
+
die(`unknown account: ${name}`);
|
|
158
|
+
}
|
|
159
|
+
applyAuthFile(account.path);
|
|
160
|
+
setActive(name);
|
|
161
|
+
process.stdout.write(`Switched to account '${name}'\n`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function cmdCurrent(args) {
|
|
165
|
+
if (args.length !== 0) {
|
|
166
|
+
die("current takes no arguments");
|
|
167
|
+
}
|
|
168
|
+
const active = getActive();
|
|
169
|
+
if (!active) {
|
|
170
|
+
die("no active account set");
|
|
171
|
+
}
|
|
172
|
+
process.stdout.write(`${active}\n`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function cmdList(args) {
|
|
176
|
+
if (args.length !== 0) {
|
|
177
|
+
die("list takes no arguments");
|
|
178
|
+
}
|
|
179
|
+
const active = getActive();
|
|
180
|
+
const accounts = readAccounts();
|
|
181
|
+
|
|
182
|
+
if (accounts.length === 0) {
|
|
183
|
+
process.stdout.write("No accounts configured.\n");
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
for (const account of accounts) {
|
|
188
|
+
if (account.name === active) {
|
|
189
|
+
process.stdout.write(`* ${account.name}\t${account.path}\n`);
|
|
190
|
+
} else {
|
|
191
|
+
process.stdout.write(` ${account.name}\t${account.path}\n`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function cmdSwitch(args) {
|
|
197
|
+
if (args.length !== 0) {
|
|
198
|
+
die("switch takes no arguments");
|
|
199
|
+
}
|
|
200
|
+
const active = getActive();
|
|
201
|
+
const accounts = readAccounts();
|
|
202
|
+
if (accounts.length === 0) {
|
|
203
|
+
die("no accounts configured. Use 'cdx add' or 'cdx save'.");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!active) {
|
|
207
|
+
applyAuthFile(accounts[0].path);
|
|
208
|
+
setActive(accounts[0].name);
|
|
209
|
+
process.stdout.write(`Switched to account '${accounts[0].name}'\n`);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const currentIndex = accounts.findIndex((entry) => entry.name === active);
|
|
214
|
+
const nextIndex = currentIndex < 0 ? 0 : (currentIndex + 1) % accounts.length;
|
|
215
|
+
const next = accounts[nextIndex];
|
|
216
|
+
|
|
217
|
+
applyAuthFile(next.path);
|
|
218
|
+
setActive(next.name);
|
|
219
|
+
process.stdout.write(`Switched to account '${next.name}'\n`);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function main() {
|
|
223
|
+
const args = process.argv.slice(2);
|
|
224
|
+
const command = args[0] || "help";
|
|
225
|
+
|
|
226
|
+
if (command === "help" || command === "--help" || command === "-h") {
|
|
227
|
+
usage();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
ensureState();
|
|
232
|
+
|
|
233
|
+
const rest = args.slice(1);
|
|
234
|
+
switch (command) {
|
|
235
|
+
case "add":
|
|
236
|
+
cmdAdd(rest);
|
|
237
|
+
break;
|
|
238
|
+
case "save":
|
|
239
|
+
cmdSave(rest);
|
|
240
|
+
break;
|
|
241
|
+
case "use":
|
|
242
|
+
cmdUse(rest);
|
|
243
|
+
break;
|
|
244
|
+
case "switch":
|
|
245
|
+
cmdSwitch(rest);
|
|
246
|
+
break;
|
|
247
|
+
case "current":
|
|
248
|
+
cmdCurrent(rest);
|
|
249
|
+
break;
|
|
250
|
+
case "list":
|
|
251
|
+
cmdList(rest);
|
|
252
|
+
break;
|
|
253
|
+
default:
|
|
254
|
+
die(`unknown command: ${command} (run 'cdx help')`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "codex-account-switcher",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Switch between multiple Codex CLI auth accounts with cdx switch",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"bin": {
|
|
8
|
+
"cdx": "bin/cdx.js",
|
|
9
|
+
"cxs": "bin/cdx.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"bin",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test:smoke": "node bin/cdx.js help"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"codex",
|
|
23
|
+
"openai",
|
|
24
|
+
"cli",
|
|
25
|
+
"account",
|
|
26
|
+
"switcher"
|
|
27
|
+
]
|
|
28
|
+
}
|