@praeviso/code-env-switch 0.1.1 → 0.1.3

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 (75) hide show
  1. package/.github/workflows/npm-publish.yml +25 -0
  2. package/AGENTS.md +32 -0
  3. package/PLAN.md +33 -0
  4. package/README.md +24 -0
  5. package/README_zh.md +24 -0
  6. package/bin/cli/args.js +303 -0
  7. package/bin/cli/help.js +77 -0
  8. package/bin/cli/index.js +13 -0
  9. package/bin/commands/add.js +81 -0
  10. package/bin/commands/index.js +21 -0
  11. package/bin/commands/launch.js +330 -0
  12. package/bin/commands/list.js +57 -0
  13. package/bin/commands/show.js +10 -0
  14. package/bin/commands/statusline.js +12 -0
  15. package/bin/commands/unset.js +20 -0
  16. package/bin/commands/use.js +92 -0
  17. package/bin/config/defaults.js +85 -0
  18. package/bin/config/index.js +20 -0
  19. package/bin/config/io.js +72 -0
  20. package/bin/constants.js +27 -0
  21. package/bin/index.js +279 -0
  22. package/bin/profile/display.js +78 -0
  23. package/bin/profile/index.js +26 -0
  24. package/bin/profile/match.js +40 -0
  25. package/bin/profile/resolve.js +79 -0
  26. package/bin/profile/type.js +90 -0
  27. package/bin/shell/detect.js +40 -0
  28. package/bin/shell/index.js +18 -0
  29. package/bin/shell/snippet.js +92 -0
  30. package/bin/shell/utils.js +35 -0
  31. package/bin/statusline/claude.js +153 -0
  32. package/bin/statusline/codex.js +356 -0
  33. package/bin/statusline/index.js +631 -0
  34. package/bin/types.js +5 -0
  35. package/bin/ui/index.js +16 -0
  36. package/bin/ui/interactive.js +189 -0
  37. package/bin/ui/readline.js +76 -0
  38. package/bin/usage/index.js +832 -0
  39. package/code-env.example.json +11 -0
  40. package/package.json +2 -2
  41. package/src/cli/args.ts +318 -0
  42. package/src/cli/help.ts +75 -0
  43. package/src/cli/index.ts +5 -0
  44. package/src/commands/add.ts +91 -0
  45. package/src/commands/index.ts +10 -0
  46. package/src/commands/launch.ts +395 -0
  47. package/src/commands/list.ts +91 -0
  48. package/src/commands/show.ts +12 -0
  49. package/src/commands/statusline.ts +18 -0
  50. package/src/commands/unset.ts +19 -0
  51. package/src/commands/use.ts +121 -0
  52. package/src/config/defaults.ts +88 -0
  53. package/src/config/index.ts +19 -0
  54. package/src/config/io.ts +69 -0
  55. package/src/constants.ts +28 -0
  56. package/src/index.ts +359 -0
  57. package/src/profile/display.ts +77 -0
  58. package/src/profile/index.ts +12 -0
  59. package/src/profile/match.ts +41 -0
  60. package/src/profile/resolve.ts +84 -0
  61. package/src/profile/type.ts +83 -0
  62. package/src/shell/detect.ts +30 -0
  63. package/src/shell/index.ts +6 -0
  64. package/src/shell/snippet.ts +92 -0
  65. package/src/shell/utils.ts +30 -0
  66. package/src/statusline/claude.ts +172 -0
  67. package/src/statusline/codex.ts +393 -0
  68. package/src/statusline/index.ts +920 -0
  69. package/src/types.ts +95 -0
  70. package/src/ui/index.ts +5 -0
  71. package/src/ui/interactive.ts +220 -0
  72. package/src/ui/readline.ts +85 -0
  73. package/src/usage/index.ts +979 -0
  74. package/bin/codenv.js +0 -1316
  75. package/src/codenv.ts +0 -1478
@@ -0,0 +1,189 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runInteractiveAdd = runInteractiveAdd;
4
+ exports.runInteractiveUse = runInteractiveUse;
5
+ /**
6
+ * Interactive UI components
7
+ */
8
+ const readline = require("readline");
9
+ const config_1 = require("../config");
10
+ const resolve_1 = require("../profile/resolve");
11
+ const type_1 = require("../profile/type");
12
+ const display_1 = require("../profile/display");
13
+ const readline_1 = require("./readline");
14
+ async function runInteractiveAdd(configPath) {
15
+ const config = (0, config_1.readConfigIfExists)(configPath);
16
+ const rl = (0, readline_1.createReadline)();
17
+ try {
18
+ const type = await (0, readline_1.askType)(rl);
19
+ const defaultName = "default";
20
+ const profileInfo = await (0, readline_1.askProfileName)(rl, config, defaultName, type);
21
+ const profileKey = profileInfo.key || (0, resolve_1.generateProfileKey)(config);
22
+ const baseUrl = await (0, readline_1.askRequired)(rl, "Base URL (required): ");
23
+ const apiKey = await (0, readline_1.askRequired)(rl, "API key (required): ");
24
+ if (!config.profiles || typeof config.profiles !== "object") {
25
+ config.profiles = {};
26
+ }
27
+ if (!config.profiles[profileKey]) {
28
+ config.profiles[profileKey] = {};
29
+ }
30
+ const profile = config.profiles[profileKey];
31
+ profile.name = profileInfo.name;
32
+ profile.type = type;
33
+ if (!profile.env || typeof profile.env !== "object") {
34
+ profile.env = {};
35
+ }
36
+ if (type === "codex") {
37
+ profile.env.OPENAI_BASE_URL = baseUrl;
38
+ profile.env.OPENAI_API_KEY = apiKey;
39
+ }
40
+ else {
41
+ profile.env.ANTHROPIC_BASE_URL = baseUrl;
42
+ profile.env.ANTHROPIC_API_KEY = apiKey;
43
+ console.log("Note: ANTHROPIC_AUTH_TOKEN will be set to the same value when applying.");
44
+ }
45
+ (0, config_1.writeConfig)(configPath, config);
46
+ console.log(`Updated config: ${configPath}`);
47
+ }
48
+ finally {
49
+ rl.close();
50
+ }
51
+ }
52
+ async function runInteractiveUse(config, printUse) {
53
+ if (!process.stdin.isTTY || !process.stderr.isTTY) {
54
+ throw new Error("Interactive selection requires a TTY. Provide a profile name.");
55
+ }
56
+ const rows = (0, display_1.buildListRows)(config, config_1.getResolvedDefaultProfileKeys);
57
+ if (rows.length === 0) {
58
+ throw new Error("No profiles found.");
59
+ }
60
+ const nameTypeCounts = new Map();
61
+ for (const row of rows) {
62
+ const key = `${row.name}||${row.type}`;
63
+ nameTypeCounts.set(key, (nameTypeCounts.get(key) || 0) + 1);
64
+ }
65
+ const displayRows = rows.map((row) => {
66
+ const key = `${row.name}||${row.type}`;
67
+ const displayName = (nameTypeCounts.get(key) || 0) > 1 ? `${row.name} [${row.key}]` : row.name;
68
+ const noteText = row.note;
69
+ const profile = config.profiles && config.profiles[row.key];
70
+ const inferredType = (0, type_1.inferProfileType)(row.key, profile, null);
71
+ const resolvedType = inferredType || (0, type_1.normalizeType)(row.type) || null;
72
+ return { ...row, displayName, noteText, resolvedType };
73
+ });
74
+ const headerName = "PROFILE";
75
+ const headerType = "TYPE";
76
+ const headerNote = "NOTE";
77
+ const nameWidth = Math.max(headerName.length, ...displayRows.map((row) => row.displayName.length));
78
+ const typeWidth = Math.max(headerType.length, ...displayRows.map((row) => row.type.length));
79
+ const noteWidth = Math.max(headerNote.length, ...displayRows.map((row) => row.noteText.length));
80
+ const formatRow = (name, type, note) => `${name.padEnd(nameWidth)} ${type.padEnd(typeWidth)} ${note.padEnd(noteWidth)}`;
81
+ const activeKeys = new Set();
82
+ const keyToType = new Map();
83
+ for (const row of displayRows) {
84
+ keyToType.set(row.key, row.resolvedType || null);
85
+ if (row.active)
86
+ activeKeys.add(row.key);
87
+ }
88
+ let index = displayRows.findIndex((row) => row.active);
89
+ if (index < 0)
90
+ index = 0;
91
+ const ANSI_CLEAR = "\x1b[2J\x1b[H";
92
+ const ANSI_HIDE_CURSOR = "\x1b[?25l";
93
+ const ANSI_SHOW_CURSOR = "\x1b[?25h";
94
+ const ANSI_INVERT = "\x1b[7m";
95
+ const ANSI_GREEN = "\x1b[32m";
96
+ const ANSI_RESET = "\x1b[0m";
97
+ const render = () => {
98
+ const lines = [];
99
+ lines.push("Select profile (up/down, Enter to apply, q to exit)");
100
+ lines.push(formatRow(headerName, headerType, headerNote));
101
+ lines.push(formatRow("-".repeat(nameWidth), "-".repeat(typeWidth), "-".repeat(noteWidth)));
102
+ for (let i = 0; i < displayRows.length; i++) {
103
+ const row = displayRows[i];
104
+ const isActive = activeKeys.has(row.key);
105
+ const line = ` ${formatRow(row.displayName, row.type, row.noteText)}`;
106
+ if (i === index) {
107
+ const prefix = isActive ? `${ANSI_INVERT}${ANSI_GREEN}` : ANSI_INVERT;
108
+ lines.push(`${prefix}${line}${ANSI_RESET}`);
109
+ }
110
+ else {
111
+ if (isActive) {
112
+ lines.push(`${ANSI_GREEN}${line}${ANSI_RESET}`);
113
+ }
114
+ else {
115
+ lines.push(line);
116
+ }
117
+ }
118
+ }
119
+ process.stderr.write(`${ANSI_CLEAR}${ANSI_HIDE_CURSOR}${lines.join("\n")}\n`);
120
+ };
121
+ return await new Promise((resolve) => {
122
+ readline.emitKeypressEvents(process.stdin);
123
+ const stdin = process.stdin;
124
+ const wasRaw = !!stdin.isRaw;
125
+ stdin.setRawMode(true);
126
+ stdin.resume();
127
+ const cleanup = () => {
128
+ stdin.removeListener("keypress", onKeypress);
129
+ if (!wasRaw)
130
+ stdin.setRawMode(false);
131
+ stdin.pause();
132
+ process.stderr.write(`${ANSI_RESET}${ANSI_SHOW_CURSOR}`);
133
+ };
134
+ const finish = () => {
135
+ cleanup();
136
+ resolve();
137
+ };
138
+ const onKeypress = (str, key) => {
139
+ if (key && key.ctrl && key.name === "c") {
140
+ finish();
141
+ return;
142
+ }
143
+ if (key && key.name === "up") {
144
+ index = (index - 1 + displayRows.length) % displayRows.length;
145
+ render();
146
+ return;
147
+ }
148
+ if (key && key.name === "down") {
149
+ index = (index + 1) % displayRows.length;
150
+ render();
151
+ return;
152
+ }
153
+ if (key && key.name === "home") {
154
+ index = 0;
155
+ render();
156
+ return;
157
+ }
158
+ if (key && key.name === "end") {
159
+ index = displayRows.length - 1;
160
+ render();
161
+ return;
162
+ }
163
+ if (key && (key.name === "return" || key.name === "enter")) {
164
+ const selectedKey = displayRows[index].key;
165
+ const selectedType = keyToType.get(selectedKey) || null;
166
+ if (selectedType) {
167
+ for (const activeKey of Array.from(activeKeys)) {
168
+ if (keyToType.get(activeKey) === selectedType) {
169
+ activeKeys.delete(activeKey);
170
+ }
171
+ }
172
+ }
173
+ activeKeys.add(selectedKey);
174
+ printUse(config, selectedKey, null);
175
+ render();
176
+ return;
177
+ }
178
+ if (key && key.name === "escape") {
179
+ finish();
180
+ return;
181
+ }
182
+ if (str === "q" || str === "Q") {
183
+ finish();
184
+ }
185
+ };
186
+ stdin.on("keypress", onKeypress);
187
+ render();
188
+ });
189
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createReadline = createReadline;
4
+ exports.ask = ask;
5
+ exports.askRequired = askRequired;
6
+ exports.askConfirm = askConfirm;
7
+ exports.askType = askType;
8
+ exports.askProfileName = askProfileName;
9
+ /**
10
+ * Readline utilities
11
+ */
12
+ const readline = require("readline");
13
+ const match_1 = require("../profile/match");
14
+ function createReadline() {
15
+ return readline.createInterface({
16
+ input: process.stdin,
17
+ output: process.stdout,
18
+ });
19
+ }
20
+ function ask(rl, question) {
21
+ return new Promise((resolve) => {
22
+ rl.question(question, (answer) => resolve(answer));
23
+ });
24
+ }
25
+ async function askRequired(rl, question) {
26
+ while (true) {
27
+ const answer = String(await ask(rl, question)).trim();
28
+ if (answer)
29
+ return answer;
30
+ }
31
+ }
32
+ async function askConfirm(rl, question) {
33
+ const answer = String(await ask(rl, question)).trim().toLowerCase();
34
+ return answer === "y" || answer === "yes";
35
+ }
36
+ async function askType(rl) {
37
+ while (true) {
38
+ const answer = String(await ask(rl, "Select type (1=codex, 2=claude): "))
39
+ .trim()
40
+ .toLowerCase();
41
+ if (answer === "1")
42
+ return "codex";
43
+ if (answer === "2")
44
+ return "claude";
45
+ const normalized = answer.replace(/[\s-]+/g, "");
46
+ if (normalized === "codex")
47
+ return "codex";
48
+ if (normalized === "claude" ||
49
+ normalized === "claudecode" ||
50
+ normalized === "cc")
51
+ return "claude";
52
+ }
53
+ }
54
+ async function askProfileName(rl, config, defaultName, type) {
55
+ while (true) {
56
+ const answer = String(await ask(rl, `Profile name (default: ${defaultName}): `)).trim();
57
+ const baseName = answer || defaultName;
58
+ if (!baseName)
59
+ continue;
60
+ const matches = (0, match_1.findProfileKeysByName)(config, baseName, type);
61
+ if (matches.length === 0) {
62
+ return { name: baseName, key: null };
63
+ }
64
+ if (matches.length === 1) {
65
+ const overwrite = String(await ask(rl, `Profile "${baseName}" exists. Overwrite? (y/N): `))
66
+ .trim()
67
+ .toLowerCase();
68
+ if (overwrite === "y" || overwrite === "yes") {
69
+ return { name: baseName, key: matches[0] };
70
+ }
71
+ continue;
72
+ }
73
+ console.log(`Multiple profiles named "${baseName}" for type "${type}". ` +
74
+ `Use a unique name or update by key in config.`);
75
+ }
76
+ }