codepen-commander 0.0.0-stage → 1.0.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 +179 -2
- package/api/client.js +364 -0
- package/api/errors.js +49 -0
- package/cli.js +125 -0
- package/commands/auth-context.js +139 -0
- package/commands/auth.js +593 -0
- package/commands/clone.js +371 -0
- package/commands/create-pen.js +249 -0
- package/commands/delete.js +98 -0
- package/commands/deploy.js +176 -0
- package/commands/domains.js +197 -0
- package/commands/fork.js +342 -0
- package/commands/list.js +103 -0
- package/commands/meta.js +164 -0
- package/commands/new.js +294 -0
- package/commands/open.js +174 -0
- package/commands/privacy.js +234 -0
- package/commands/pull.js +128 -0
- package/commands/push.js +294 -0
- package/commands/scaffold.js +127 -0
- package/commands/status.js +131 -0
- package/commands/unowned-pen.js +153 -0
- package/commands/versions.js +172 -0
- package/config/auth.js +470 -0
- package/config/project.js +365 -0
- package/package.json +21 -4
- package/sync/diff.js +105 -0
- package/sync/fs.js +253 -0
- package/sync/materialize.js +81 -0
- package/types.js +1 -0
- package/utils/browser.js +47 -0
- package/utils/format.js +107 -0
- package/utils/html.js +51 -0
- package/utils/json.js +122 -0
- package/utils/prompts.js +316 -0
package/utils/prompts.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import readline from 'node:readline';
|
|
2
|
+
import readlinePromises from 'node:readline/promises';
|
|
3
|
+
import { stdin as defaultInput, stdout as defaultOutput } from 'node:process';
|
|
4
|
+
import { fmt, stripAnsi } from './format.js';
|
|
5
|
+
export function isInteractive() {
|
|
6
|
+
return Boolean(defaultInput.isTTY && defaultOutput.isTTY);
|
|
7
|
+
}
|
|
8
|
+
export async function promptText(question, defaultValue) {
|
|
9
|
+
const rl = readlinePromises.createInterface({ input: defaultInput, output: defaultOutput });
|
|
10
|
+
try {
|
|
11
|
+
const suffix = defaultValue ? ` ${fmt.dim(`(${defaultValue})`)}` : '';
|
|
12
|
+
const answer = await rl.question(`${question}${suffix}: `);
|
|
13
|
+
const trimmed = answer.trim();
|
|
14
|
+
return trimmed || defaultValue || '';
|
|
15
|
+
}
|
|
16
|
+
finally {
|
|
17
|
+
rl.close();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export async function promptConfirm(question, defaultYes = false) {
|
|
21
|
+
const rl = readlinePromises.createInterface({ input: defaultInput, output: defaultOutput });
|
|
22
|
+
try {
|
|
23
|
+
const hint = defaultYes ? 'Y/n' : 'y/N';
|
|
24
|
+
const answer = (await rl.question(`${question} ${fmt.dim(`[${hint}]`)} `)).trim().toLowerCase();
|
|
25
|
+
if (!answer)
|
|
26
|
+
return defaultYes;
|
|
27
|
+
return answer === 'y' || answer === 'yes';
|
|
28
|
+
}
|
|
29
|
+
finally {
|
|
30
|
+
rl.close();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function countTerminalRows(lines, columns) {
|
|
34
|
+
const safeCols = Math.max(1, columns);
|
|
35
|
+
let total = 0;
|
|
36
|
+
for (const rawLine of lines) {
|
|
37
|
+
for (const subLine of rawLine.split('\n')) {
|
|
38
|
+
const visibleLength = stripAnsi(subLine).length;
|
|
39
|
+
total += visibleLength === 0 ? 1 : Math.ceil(visibleLength / safeCols);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return total;
|
|
43
|
+
}
|
|
44
|
+
async function promptSelectFallback(question, options, defaultIndex, input, output) {
|
|
45
|
+
output.write(`${question}\n`);
|
|
46
|
+
options.forEach((opt, idx) => {
|
|
47
|
+
const num = fmt.cyan(`${idx + 1})`);
|
|
48
|
+
const desc = opt.description ? ` ${fmt.dim(`— ${opt.description}`)}` : '';
|
|
49
|
+
output.write(` ${num} ${opt.label}${desc}\n`);
|
|
50
|
+
});
|
|
51
|
+
const rl = readlinePromises.createInterface({ input, output });
|
|
52
|
+
try {
|
|
53
|
+
for (;;) {
|
|
54
|
+
const answer = (await rl.question(`Choose [1-${options.length}] ${fmt.dim(`(default ${defaultIndex + 1})`)}: `)).trim();
|
|
55
|
+
if (!answer) {
|
|
56
|
+
return options[defaultIndex].value;
|
|
57
|
+
}
|
|
58
|
+
const num = Number.parseInt(answer, 10);
|
|
59
|
+
if (!Number.isNaN(num) && num >= 1 && num <= options.length && String(num) === answer) {
|
|
60
|
+
return options[num - 1].value;
|
|
61
|
+
}
|
|
62
|
+
const byValue = options.find((opt) => opt.value.toLowerCase() === answer.toLowerCase());
|
|
63
|
+
if (byValue) {
|
|
64
|
+
return byValue.value;
|
|
65
|
+
}
|
|
66
|
+
output.write(`${fmt.yellow(`Please enter a number between 1 and ${options.length} (or an option name).`)}\n`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
rl.close();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function promptSelectInteractive(question, options, defaultIndex, input, output) {
|
|
74
|
+
readline.emitKeypressEvents(input);
|
|
75
|
+
let selectedIndex = defaultIndex;
|
|
76
|
+
let windowStart = 0;
|
|
77
|
+
let lastRenderedRows = 0;
|
|
78
|
+
let settled = false;
|
|
79
|
+
const getColumns = () => Math.max(20, output.columns || 80);
|
|
80
|
+
const getMaxVisible = () => {
|
|
81
|
+
const termRows = output.rows || 24;
|
|
82
|
+
const available = Math.max(3, termRows - 5);
|
|
83
|
+
return Math.min(options.length, available, 12);
|
|
84
|
+
};
|
|
85
|
+
const syncWindow = (maxVisible) => {
|
|
86
|
+
if (options.length <= maxVisible) {
|
|
87
|
+
windowStart = 0;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (selectedIndex < windowStart) {
|
|
91
|
+
windowStart = selectedIndex;
|
|
92
|
+
}
|
|
93
|
+
else if (selectedIndex >= windowStart + maxVisible) {
|
|
94
|
+
windowStart = selectedIndex - maxVisible + 1;
|
|
95
|
+
}
|
|
96
|
+
const maxStart = Math.max(0, options.length - maxVisible);
|
|
97
|
+
if (windowStart > maxStart) {
|
|
98
|
+
windowStart = maxStart;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const erasePrevious = () => {
|
|
102
|
+
if (lastRenderedRows <= 0)
|
|
103
|
+
return '';
|
|
104
|
+
return `\x1b[${lastRenderedRows}A\r\x1b[J`;
|
|
105
|
+
};
|
|
106
|
+
const renderActive = (hideCursor) => {
|
|
107
|
+
const columns = getColumns();
|
|
108
|
+
const maxVisible = getMaxVisible();
|
|
109
|
+
syncWindow(maxVisible);
|
|
110
|
+
const lines = [`${fmt.bold(question)} ${fmt.dim('(↑/↓, Enter)')}`];
|
|
111
|
+
if (windowStart > 0) {
|
|
112
|
+
lines.push(` ${fmt.dim(`↑ ${windowStart} more`)}`);
|
|
113
|
+
}
|
|
114
|
+
const windowEnd = Math.min(options.length, windowStart + maxVisible);
|
|
115
|
+
for (let i = windowStart; i < windowEnd; i++) {
|
|
116
|
+
const opt = options[i];
|
|
117
|
+
const isSelected = i === selectedIndex;
|
|
118
|
+
if (isSelected) {
|
|
119
|
+
const desc = opt.description ? ` ${fmt.cyan(`— ${opt.description}`)}` : '';
|
|
120
|
+
lines.push(` ${fmt.bold(fmt.cyan('→'))} ${fmt.bold(fmt.cyan(opt.label))}${desc}`);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
const desc = opt.description ? ` ${fmt.dim(`— ${opt.description}`)}` : '';
|
|
124
|
+
lines.push(` ${opt.label}${desc}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const hiddenBelow = options.length - windowEnd;
|
|
128
|
+
if (hiddenBelow > 0) {
|
|
129
|
+
lines.push(` ${fmt.dim(`↓ ${hiddenBelow} more`)}`);
|
|
130
|
+
}
|
|
131
|
+
const prefix = `${hideCursor ? '\x1b[?25l' : ''}${erasePrevious()}`;
|
|
132
|
+
output.write(`${prefix}${lines.join('\n')}\n`);
|
|
133
|
+
lastRenderedRows = countTerminalRows(lines, columns);
|
|
134
|
+
};
|
|
135
|
+
const renderFinal = (lines) => {
|
|
136
|
+
const prefix = erasePrevious();
|
|
137
|
+
output.write(`${prefix}${lines.join('\n')}\n\x1b[?25h`);
|
|
138
|
+
lastRenderedRows = 0;
|
|
139
|
+
};
|
|
140
|
+
const wasRaw = Boolean(input.isRaw);
|
|
141
|
+
input.setRawMode?.(true);
|
|
142
|
+
input.resume();
|
|
143
|
+
return new Promise((resolve, reject) => {
|
|
144
|
+
const cleanup = () => {
|
|
145
|
+
input.removeListener('keypress', onKeypress);
|
|
146
|
+
input.removeListener('end', onEnd);
|
|
147
|
+
output.removeListener('resize', onResize);
|
|
148
|
+
process.removeListener('SIGINT', onSigint);
|
|
149
|
+
try {
|
|
150
|
+
input.setRawMode?.(wasRaw);
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// Ignore if stream already closed
|
|
154
|
+
}
|
|
155
|
+
input.pause();
|
|
156
|
+
};
|
|
157
|
+
const cancel = () => {
|
|
158
|
+
if (settled)
|
|
159
|
+
return;
|
|
160
|
+
settled = true;
|
|
161
|
+
cleanup();
|
|
162
|
+
renderFinal([fmt.bold(question), ` ${fmt.red('×')} ${fmt.dim('Canceled')}`]);
|
|
163
|
+
reject(new Error('Prompt canceled by user.'));
|
|
164
|
+
};
|
|
165
|
+
const confirm = () => {
|
|
166
|
+
if (settled)
|
|
167
|
+
return;
|
|
168
|
+
settled = true;
|
|
169
|
+
cleanup();
|
|
170
|
+
const chosen = options[selectedIndex];
|
|
171
|
+
const desc = chosen.description ? ` ${fmt.dim(`— ${chosen.description}`)}` : '';
|
|
172
|
+
renderFinal([
|
|
173
|
+
fmt.bold(question),
|
|
174
|
+
` ${fmt.bold(fmt.green('→'))} ${fmt.bold(fmt.green(chosen.label))}${desc}`,
|
|
175
|
+
]);
|
|
176
|
+
resolve(chosen.value);
|
|
177
|
+
};
|
|
178
|
+
const onResize = () => {
|
|
179
|
+
if (settled)
|
|
180
|
+
return;
|
|
181
|
+
renderActive(false);
|
|
182
|
+
};
|
|
183
|
+
const onSigint = () => {
|
|
184
|
+
cancel();
|
|
185
|
+
};
|
|
186
|
+
const onEnd = () => {
|
|
187
|
+
cancel();
|
|
188
|
+
};
|
|
189
|
+
const onKeypress = (str, key) => {
|
|
190
|
+
if (settled)
|
|
191
|
+
return;
|
|
192
|
+
if (str === '\u0003' || (key?.ctrl && key.name === 'c') || key?.name === 'escape') {
|
|
193
|
+
cancel();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (key?.name === 'return' || key?.name === 'enter') {
|
|
197
|
+
confirm();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (key?.name === 'up' || (!key?.ctrl && !key?.meta && key?.name === 'k')) {
|
|
201
|
+
selectedIndex = (selectedIndex - 1 + options.length) % options.length;
|
|
202
|
+
renderActive(false);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
if (key?.name === 'down' || (!key?.ctrl && !key?.meta && key?.name === 'j')) {
|
|
206
|
+
selectedIndex = (selectedIndex + 1) % options.length;
|
|
207
|
+
renderActive(false);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (key?.name === 'home' || key?.name === 'pageup') {
|
|
211
|
+
if (selectedIndex !== 0) {
|
|
212
|
+
selectedIndex = 0;
|
|
213
|
+
renderActive(false);
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (key?.name === 'end' || key?.name === 'pagedown') {
|
|
218
|
+
if (selectedIndex !== options.length - 1) {
|
|
219
|
+
selectedIndex = options.length - 1;
|
|
220
|
+
renderActive(false);
|
|
221
|
+
}
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (str && /^[1-9]$/.test(str) && !key?.ctrl && !key?.meta) {
|
|
225
|
+
const idx = Number.parseInt(str, 10) - 1;
|
|
226
|
+
if (idx >= 0 && idx < options.length && idx !== selectedIndex) {
|
|
227
|
+
selectedIndex = idx;
|
|
228
|
+
renderActive(false);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
input.on('keypress', onKeypress);
|
|
233
|
+
input.once('end', onEnd);
|
|
234
|
+
output.on('resize', onResize);
|
|
235
|
+
process.on('SIGINT', onSigint);
|
|
236
|
+
renderActive(true);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
export async function promptSelectWithIO(question, options, defaultIndex = 0, io = {}) {
|
|
240
|
+
if (options.length === 0) {
|
|
241
|
+
throw new Error('promptSelect requires at least one option.');
|
|
242
|
+
}
|
|
243
|
+
const clampedDefault = Math.min(Math.max(0, Math.trunc(defaultIndex)), options.length - 1);
|
|
244
|
+
const input = io.input ?? defaultInput;
|
|
245
|
+
const output = io.output ?? defaultOutput;
|
|
246
|
+
const term = io.term ?? process.env.TERM;
|
|
247
|
+
const canUseInteractive = Boolean(input.isTTY) &&
|
|
248
|
+
Boolean(output.isTTY) &&
|
|
249
|
+
typeof input.setRawMode === 'function' &&
|
|
250
|
+
term !== 'dumb';
|
|
251
|
+
if (canUseInteractive) {
|
|
252
|
+
return promptSelectInteractive(question, options, clampedDefault, input, output);
|
|
253
|
+
}
|
|
254
|
+
return promptSelectFallback(question, options, clampedDefault, input, output);
|
|
255
|
+
}
|
|
256
|
+
export async function promptSelect(question, options, defaultIndex = 0) {
|
|
257
|
+
return promptSelectWithIO(question, options, defaultIndex);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Prompts for a secret (such as an API key) without echoing the raw characters to the terminal.
|
|
261
|
+
*/
|
|
262
|
+
export async function promptSecret(question) {
|
|
263
|
+
if (!defaultInput.isTTY) {
|
|
264
|
+
const rl = readlinePromises.createInterface({ input: defaultInput, output: defaultOutput });
|
|
265
|
+
try {
|
|
266
|
+
return (await rl.question(`${question}: `)).trim();
|
|
267
|
+
}
|
|
268
|
+
finally {
|
|
269
|
+
rl.close();
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return new Promise((resolve, reject) => {
|
|
273
|
+
defaultOutput.write(`${question}: `);
|
|
274
|
+
let value = '';
|
|
275
|
+
const wasRaw = defaultInput.isRaw;
|
|
276
|
+
defaultInput.setRawMode(true);
|
|
277
|
+
defaultInput.resume();
|
|
278
|
+
defaultInput.setEncoding('utf8');
|
|
279
|
+
const cleanup = () => {
|
|
280
|
+
defaultInput.removeListener('data', onData);
|
|
281
|
+
defaultInput.setRawMode(Boolean(wasRaw));
|
|
282
|
+
defaultInput.pause();
|
|
283
|
+
};
|
|
284
|
+
const onData = (chunk) => {
|
|
285
|
+
for (const ch of chunk) {
|
|
286
|
+
if (ch === '\r' || ch === '\n') {
|
|
287
|
+
cleanup();
|
|
288
|
+
defaultOutput.write('\n');
|
|
289
|
+
resolve(value.trim());
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
if (ch === '\u0003') {
|
|
293
|
+
// Ctrl+C
|
|
294
|
+
cleanup();
|
|
295
|
+
defaultOutput.write('\n');
|
|
296
|
+
reject(new Error('Prompt canceled by user.'));
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (ch === '\u007f' || ch === '\b') {
|
|
300
|
+
if (value.length > 0) {
|
|
301
|
+
value = value.slice(0, -1);
|
|
302
|
+
defaultOutput.write('\b \b');
|
|
303
|
+
}
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
// Ignore non-printable escape sequences
|
|
307
|
+
if (ch.charCodeAt(0) < 32) {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
value += ch;
|
|
311
|
+
defaultOutput.write('•');
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
defaultInput.on('data', onData);
|
|
315
|
+
});
|
|
316
|
+
}
|