codepen-demokit 1.0.0 → 1.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/README.md +13 -7
- package/bin/cli.mjs +2 -1
- package/lib/blocks.mjs +1067 -0
- package/lib/build-json.mjs +84 -16
- package/lib/build-site.mjs +67 -2
- package/lib/config.mjs +10 -1
- package/lib/init.mjs +551 -118
- package/lib/serve.mjs +141 -3
- package/lib/sync-codepen.mjs +66 -17
- package/package.json +6 -1
- package/shell/index.html +26 -5
- package/shell/scripts.js +78 -11
- package/shell/styles.css +204 -40
- package/templates/AGENTS.md +8 -7
- package/templates/README.md +1 -0
package/lib/init.mjs
CHANGED
|
@@ -2,7 +2,8 @@ import { spawnSync } from 'node:child_process';
|
|
|
2
2
|
import fs from 'node:fs/promises';
|
|
3
3
|
import { existsSync } from 'node:fs';
|
|
4
4
|
import path from 'node:path';
|
|
5
|
-
import readline from 'node:readline
|
|
5
|
+
import readline from 'node:readline';
|
|
6
|
+
import readlinePromises from 'node:readline/promises';
|
|
6
7
|
import { PACKAGE_DIR } from './build-site.mjs';
|
|
7
8
|
import {
|
|
8
9
|
DEFAULT_SITE_CONFIG,
|
|
@@ -19,6 +20,71 @@ const VISIBILITY_BY_LOWER = new Map(
|
|
|
19
20
|
VISIBILITY_OPTIONS.map((option) => [option.toLowerCase(), option]),
|
|
20
21
|
);
|
|
21
22
|
|
|
23
|
+
const VISIBILITY_DESCRIPTIONS = Object.freeze({
|
|
24
|
+
Private: 'unlisted; anyone with the link can view it',
|
|
25
|
+
Public: 'visible to everyone',
|
|
26
|
+
Protected: 'private and password-protected',
|
|
27
|
+
Collaborators: 'private; visible only to you and collaborators',
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const VISIBILITY_CHOICES = Object.freeze(
|
|
31
|
+
VISIBILITY_OPTIONS.map((option) => ({
|
|
32
|
+
value: option,
|
|
33
|
+
label: option,
|
|
34
|
+
description: VISIBILITY_DESCRIPTIONS[option] || '',
|
|
35
|
+
})),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
function createFormatter(output = process.stdout) {
|
|
39
|
+
const colorsEnabled = () => {
|
|
40
|
+
if (process.env.NO_COLOR !== undefined && process.env.NO_COLOR !== '') {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
if (process.env.FORCE_COLOR !== undefined && process.env.FORCE_COLOR !== '0') {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
return Boolean(output.isTTY);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const wrap = (code, text) => {
|
|
50
|
+
if (!colorsEnabled()) return text;
|
|
51
|
+
return `\x1b[${code}m${text}\x1b[0m`;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
bold: (s) => wrap('1', s),
|
|
56
|
+
dim: (s) => wrap('2', s),
|
|
57
|
+
red: (s) => wrap('31', s),
|
|
58
|
+
green: (s) => wrap('32', s),
|
|
59
|
+
yellow: (s) => wrap('33', s),
|
|
60
|
+
blue: (s) => wrap('34', s),
|
|
61
|
+
magenta: (s) => wrap('35', s),
|
|
62
|
+
cyan: (s) => wrap('36', s),
|
|
63
|
+
gray: (s) => wrap('90', s),
|
|
64
|
+
boldGreen: (s) => wrap('1;32', s),
|
|
65
|
+
boldYellow: (s) => wrap('1;33', s),
|
|
66
|
+
boldCyan: (s) => wrap('1;36', s),
|
|
67
|
+
boldMagenta: (s) => wrap('1;35', s),
|
|
68
|
+
boldRed: (s) => wrap('1;31', s),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function stripAnsi(text) {
|
|
73
|
+
return String(text).replace(/\x1b\[[0-9;]*m/g, '');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function countTerminalRows(lines, columns) {
|
|
77
|
+
const safeCols = Math.max(1, columns);
|
|
78
|
+
let total = 0;
|
|
79
|
+
for (const rawLine of lines) {
|
|
80
|
+
for (const subLine of rawLine.split('\n')) {
|
|
81
|
+
const visibleLength = stripAnsi(subLine).length;
|
|
82
|
+
total += visibleLength === 0 ? 1 : Math.ceil(visibleLength / safeCols);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return total;
|
|
86
|
+
}
|
|
87
|
+
|
|
22
88
|
export class InitCancelledError extends Error {
|
|
23
89
|
constructor() {
|
|
24
90
|
super('Init cancelled.');
|
|
@@ -162,60 +228,399 @@ async function resolveTargetDirectory(rawInput, baseCwd) {
|
|
|
162
228
|
};
|
|
163
229
|
}
|
|
164
230
|
|
|
165
|
-
function
|
|
166
|
-
|
|
231
|
+
function promptSelectInteractive({ question, options, defaultIndex, input, output, fmt }) {
|
|
232
|
+
readline.emitKeypressEvents(input);
|
|
233
|
+
|
|
234
|
+
let selectedIndex = defaultIndex;
|
|
235
|
+
let windowStart = 0;
|
|
236
|
+
let lastRenderedRows = 0;
|
|
237
|
+
let settled = false;
|
|
238
|
+
|
|
239
|
+
const getColumns = () => Math.max(20, output.columns || 80);
|
|
240
|
+
const getMaxVisible = () => {
|
|
241
|
+
const termRows = output.rows || 24;
|
|
242
|
+
const available = Math.max(3, termRows - 5);
|
|
243
|
+
return Math.min(options.length, available, 12);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const syncWindow = (maxVisible) => {
|
|
247
|
+
if (options.length <= maxVisible) {
|
|
248
|
+
windowStart = 0;
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (selectedIndex < windowStart) {
|
|
252
|
+
windowStart = selectedIndex;
|
|
253
|
+
} else if (selectedIndex >= windowStart + maxVisible) {
|
|
254
|
+
windowStart = selectedIndex - maxVisible + 1;
|
|
255
|
+
}
|
|
256
|
+
const maxStart = Math.max(0, options.length - maxVisible);
|
|
257
|
+
if (windowStart > maxStart) {
|
|
258
|
+
windowStart = maxStart;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
const erasePrevious = () => {
|
|
263
|
+
if (lastRenderedRows <= 0) return '';
|
|
264
|
+
return `\x1b[${lastRenderedRows}A\r\x1b[J`;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const renderActive = (hideCursor) => {
|
|
268
|
+
const columns = getColumns();
|
|
269
|
+
const maxVisible = getMaxVisible();
|
|
270
|
+
syncWindow(maxVisible);
|
|
271
|
+
|
|
272
|
+
const lines = [
|
|
273
|
+
`${fmt.cyan('◆')} ${fmt.bold(question)} ${fmt.dim('(↑/↓, Enter)')}`,
|
|
274
|
+
];
|
|
275
|
+
|
|
276
|
+
if (windowStart > 0) {
|
|
277
|
+
lines.push(` ${fmt.dim(`↑ ${windowStart} more`)}`);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const windowEnd = Math.min(options.length, windowStart + maxVisible);
|
|
281
|
+
for (let i = windowStart; i < windowEnd; i++) {
|
|
282
|
+
const opt = options[i];
|
|
283
|
+
const isSelected = i === selectedIndex;
|
|
284
|
+
if (isSelected) {
|
|
285
|
+
const desc = opt.description ? ` ${fmt.cyan(`— ${opt.description}`)}` : '';
|
|
286
|
+
lines.push(` ${fmt.boldCyan('→')} ${fmt.boldCyan(opt.label)}${desc}`);
|
|
287
|
+
} else {
|
|
288
|
+
const desc = opt.description ? ` ${fmt.dim(`— ${opt.description}`)}` : '';
|
|
289
|
+
lines.push(` ${opt.label}${desc}`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const hiddenBelow = options.length - windowEnd;
|
|
294
|
+
if (hiddenBelow > 0) {
|
|
295
|
+
lines.push(` ${fmt.dim(`↓ ${hiddenBelow} more`)}`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const prefix = `${hideCursor ? '\x1b[?25l' : ''}${erasePrevious()}`;
|
|
299
|
+
output.write(`${prefix}${lines.join('\n')}\n`);
|
|
300
|
+
lastRenderedRows = countTerminalRows(lines, columns);
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
const renderFinal = (lines) => {
|
|
304
|
+
const prefix = erasePrevious();
|
|
305
|
+
output.write(`${prefix}${lines.join('\n')}\n\x1b[?25h`);
|
|
306
|
+
lastRenderedRows = 0;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const wasRaw = Boolean(input.isRaw);
|
|
310
|
+
input.setRawMode?.(true);
|
|
311
|
+
input.resume();
|
|
312
|
+
|
|
313
|
+
return new Promise((resolve, reject) => {
|
|
314
|
+
const cleanup = () => {
|
|
315
|
+
input.removeListener('keypress', onKeypress);
|
|
316
|
+
input.removeListener('end', onEnd);
|
|
317
|
+
output.removeListener('resize', onResize);
|
|
318
|
+
process.removeListener('SIGINT', onSigint);
|
|
319
|
+
try {
|
|
320
|
+
input.setRawMode?.(wasRaw);
|
|
321
|
+
} catch {
|
|
322
|
+
// Ignore if stream already closed
|
|
323
|
+
}
|
|
324
|
+
input.pause();
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const cancel = () => {
|
|
328
|
+
if (settled) return;
|
|
329
|
+
settled = true;
|
|
330
|
+
cleanup();
|
|
331
|
+
renderFinal([
|
|
332
|
+
`${fmt.cyan('◆')} ${fmt.bold(question)}`,
|
|
333
|
+
` ${fmt.red('×')} ${fmt.dim('Cancelled')}`,
|
|
334
|
+
]);
|
|
335
|
+
reject(new InitCancelledError());
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const confirm = () => {
|
|
339
|
+
if (settled) return;
|
|
340
|
+
settled = true;
|
|
341
|
+
cleanup();
|
|
342
|
+
const chosen = options[selectedIndex];
|
|
343
|
+
const desc = chosen.description ? ` ${fmt.dim(`— ${chosen.description}`)}` : '';
|
|
344
|
+
renderFinal([
|
|
345
|
+
`${fmt.cyan('◆')} ${fmt.bold(question)}`,
|
|
346
|
+
` ${fmt.boldGreen('→')} ${fmt.boldGreen(chosen.label)}${desc}`,
|
|
347
|
+
]);
|
|
348
|
+
resolve(chosen.value);
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
const onResize = () => {
|
|
352
|
+
if (settled) return;
|
|
353
|
+
renderActive(false);
|
|
354
|
+
};
|
|
167
355
|
|
|
168
|
-
if (input.isTTY) {
|
|
169
|
-
const rl = readline.createInterface({ input, output });
|
|
170
|
-
const abortController = new AbortController();
|
|
171
356
|
const onSigint = () => {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
357
|
+
cancel();
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const onEnd = () => {
|
|
361
|
+
cancel();
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const onKeypress = (str, key) => {
|
|
365
|
+
if (settled) return;
|
|
366
|
+
|
|
367
|
+
if (str === '\u0003' || (key?.ctrl && key.name === 'c') || key?.name === 'escape') {
|
|
368
|
+
cancel();
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (key?.name === 'return' || key?.name === 'enter') {
|
|
373
|
+
confirm();
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (key?.name === 'up' || (!key?.ctrl && !key?.meta && key?.name === 'k')) {
|
|
378
|
+
selectedIndex = (selectedIndex - 1 + options.length) % options.length;
|
|
379
|
+
renderActive(false);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (key?.name === 'down' || (!key?.ctrl && !key?.meta && key?.name === 'j')) {
|
|
384
|
+
selectedIndex = (selectedIndex + 1) % options.length;
|
|
385
|
+
renderActive(false);
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (key?.name === 'home' || key?.name === 'pageup') {
|
|
390
|
+
if (selectedIndex !== 0) {
|
|
391
|
+
selectedIndex = 0;
|
|
392
|
+
renderActive(false);
|
|
393
|
+
}
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (key?.name === 'end' || key?.name === 'pagedown') {
|
|
398
|
+
if (selectedIndex !== options.length - 1) {
|
|
399
|
+
selectedIndex = options.length - 1;
|
|
400
|
+
renderActive(false);
|
|
401
|
+
}
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (str && !key?.ctrl && !key?.meta) {
|
|
406
|
+
if (/^[1-9]$/.test(str)) {
|
|
407
|
+
const idx = Number.parseInt(str, 10) - 1;
|
|
408
|
+
if (idx >= 0 && idx < options.length && idx !== selectedIndex) {
|
|
409
|
+
selectedIndex = idx;
|
|
410
|
+
renderActive(false);
|
|
411
|
+
}
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const lower = str.toLowerCase();
|
|
416
|
+
const shortcutIdx = options.findIndex(
|
|
417
|
+
(opt) => opt.shortcut && opt.shortcut.toLowerCase() === lower,
|
|
418
|
+
);
|
|
419
|
+
if (shortcutIdx !== -1 && shortcutIdx !== selectedIndex) {
|
|
420
|
+
selectedIndex = shortcutIdx;
|
|
421
|
+
renderActive(false);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
input.on('keypress', onKeypress);
|
|
427
|
+
input.once('end', onEnd);
|
|
428
|
+
output.on('resize', onResize);
|
|
429
|
+
process.on('SIGINT', onSigint);
|
|
430
|
+
renderActive(true);
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function createPrompter({
|
|
435
|
+
input = process.stdin,
|
|
436
|
+
output = process.stdout,
|
|
437
|
+
fmt = createFormatter(output),
|
|
438
|
+
term = process.env.TERM,
|
|
439
|
+
} = {}) {
|
|
440
|
+
const canUseInteractive =
|
|
441
|
+
Boolean(input.isTTY) &&
|
|
442
|
+
Boolean(output.isTTY) &&
|
|
443
|
+
typeof input.setRawMode === 'function' &&
|
|
444
|
+
term !== 'dumb';
|
|
445
|
+
|
|
446
|
+
let closed = false;
|
|
447
|
+
let activeRl = null;
|
|
448
|
+
|
|
449
|
+
const formatQuestionHeader = (question, defaultValue) => {
|
|
450
|
+
const hint = defaultValue ? ` ${fmt.dim(`(${defaultValue})`)}` : '';
|
|
451
|
+
return `${fmt.cyan('◆')} ${fmt.bold(question)}${hint}`;
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
if (canUseInteractive) {
|
|
455
|
+
const readLineInteractive = async (promptText) => {
|
|
456
|
+
const rl = readlinePromises.createInterface({ input, output });
|
|
457
|
+
activeRl = rl;
|
|
458
|
+
const abortController = new AbortController();
|
|
459
|
+
|
|
460
|
+
const onSigint = () => {
|
|
461
|
+
abortController.abort();
|
|
462
|
+
rl.close();
|
|
463
|
+
};
|
|
464
|
+
const onClose = () => {
|
|
465
|
+
abortController.abort();
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
rl.on('SIGINT', onSigint);
|
|
469
|
+
rl.once('close', onClose);
|
|
470
|
+
|
|
471
|
+
try {
|
|
472
|
+
return await rl.question(promptText, { signal: abortController.signal });
|
|
473
|
+
} catch (err) {
|
|
474
|
+
if (
|
|
475
|
+
abortController.signal.aborted ||
|
|
476
|
+
err?.name === 'AbortError' ||
|
|
477
|
+
err?.code === 'ABORT_ERR' ||
|
|
478
|
+
err?.code === 'ERR_USE_AFTER_CLOSE'
|
|
479
|
+
) {
|
|
480
|
+
output.write(`\r\x1b[K ${fmt.red('×')} ${fmt.dim('Cancelled')}\n`);
|
|
481
|
+
throw new InitCancelledError();
|
|
482
|
+
}
|
|
483
|
+
throw err;
|
|
484
|
+
} finally {
|
|
485
|
+
rl.off('SIGINT', onSigint);
|
|
486
|
+
rl.off('close', onClose);
|
|
487
|
+
if (activeRl === rl) {
|
|
488
|
+
activeRl = null;
|
|
489
|
+
}
|
|
175
490
|
rl.close();
|
|
176
491
|
}
|
|
177
492
|
};
|
|
178
|
-
rl.on('SIGINT', onSigint);
|
|
179
493
|
|
|
180
494
|
return {
|
|
181
|
-
async
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
throw new InitCancelledError();
|
|
495
|
+
async askText({ question, defaultValue = '', validate }) {
|
|
496
|
+
output.write(`${formatQuestionHeader(question, defaultValue)}\n`);
|
|
497
|
+
const promptText = ` ${fmt.boldCyan('→')} `;
|
|
498
|
+
const columns = () => Math.max(20, output.columns || 80);
|
|
499
|
+
|
|
500
|
+
while (true) {
|
|
501
|
+
const raw = await readLineInteractive(promptText);
|
|
502
|
+
const result = await validate(raw);
|
|
503
|
+
if (!result.ok) {
|
|
504
|
+
output.write(` ${fmt.yellow(result.error)}\n`);
|
|
505
|
+
continue;
|
|
193
506
|
}
|
|
194
|
-
|
|
507
|
+
|
|
508
|
+
const displayText = result.displayValue ?? String(result.value);
|
|
509
|
+
const enteredRows = countTerminalRows([` → ${raw}`], columns());
|
|
510
|
+
output.write(
|
|
511
|
+
`\x1b[${enteredRows}A\r\x1b[J ${fmt.boldGreen('→')} ${fmt.boldGreen(displayText)}\n`,
|
|
512
|
+
);
|
|
513
|
+
return result.value;
|
|
514
|
+
}
|
|
515
|
+
},
|
|
516
|
+
|
|
517
|
+
async select({ question, options, defaultIndex = 0 }) {
|
|
518
|
+
if (!Array.isArray(options) || options.length === 0) {
|
|
519
|
+
throw new Error('select requires at least one option.');
|
|
195
520
|
}
|
|
521
|
+
const clampedDefault = Math.min(Math.max(0, Math.trunc(defaultIndex)), options.length - 1);
|
|
522
|
+
return promptSelectInteractive({
|
|
523
|
+
question,
|
|
524
|
+
options,
|
|
525
|
+
defaultIndex: clampedDefault,
|
|
526
|
+
input,
|
|
527
|
+
output,
|
|
528
|
+
fmt,
|
|
529
|
+
});
|
|
196
530
|
},
|
|
531
|
+
|
|
197
532
|
close() {
|
|
198
533
|
if (closed) return;
|
|
199
534
|
closed = true;
|
|
200
|
-
|
|
201
|
-
|
|
535
|
+
if (activeRl) {
|
|
536
|
+
activeRl.close();
|
|
537
|
+
activeRl = null;
|
|
538
|
+
}
|
|
202
539
|
},
|
|
203
540
|
};
|
|
204
541
|
}
|
|
205
542
|
|
|
206
|
-
|
|
543
|
+
// Non-interactive / piped / dumb terminal fallback: keep a single shared readline iterator
|
|
544
|
+
// so buffered lines from piped stdin are never dropped between prompts.
|
|
545
|
+
const rl = readlinePromises.createInterface({ input, crlfDelay: Infinity });
|
|
207
546
|
const iterator = rl[Symbol.asyncIterator]();
|
|
208
547
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
548
|
+
const readFallbackLine = async (promptText) => {
|
|
549
|
+
output.write(promptText);
|
|
550
|
+
const { value, done } = await iterator.next();
|
|
551
|
+
if (done) {
|
|
213
552
|
output.write('\n');
|
|
214
|
-
|
|
215
|
-
|
|
553
|
+
throw new InitCancelledError();
|
|
554
|
+
}
|
|
555
|
+
return value;
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
return {
|
|
559
|
+
async askText({ question, defaultValue = '', validate }) {
|
|
560
|
+
output.write(`${formatQuestionHeader(question, defaultValue)}\n`);
|
|
561
|
+
while (true) {
|
|
562
|
+
const raw = await readFallbackLine(` ${fmt.boldCyan('→')} `);
|
|
563
|
+
const result = await validate(raw);
|
|
564
|
+
if (!result.ok) {
|
|
565
|
+
output.write(`${raw}\n ${fmt.yellow(result.error)}\n`);
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
const displayText = result.displayValue ?? String(result.value);
|
|
569
|
+
output.write(`${fmt.boldGreen(displayText)}\n`);
|
|
570
|
+
return result.value;
|
|
216
571
|
}
|
|
217
|
-
return value;
|
|
218
572
|
},
|
|
573
|
+
|
|
574
|
+
async select({ question, options, defaultIndex = 0 }) {
|
|
575
|
+
if (!Array.isArray(options) || options.length === 0) {
|
|
576
|
+
throw new Error('select requires at least one option.');
|
|
577
|
+
}
|
|
578
|
+
const clampedDefault = Math.min(Math.max(0, Math.trunc(defaultIndex)), options.length - 1);
|
|
579
|
+
output.write(`${formatQuestionHeader(question)}\n`);
|
|
580
|
+
options.forEach((opt, idx) => {
|
|
581
|
+
const num = fmt.cyan(`${idx + 1})`);
|
|
582
|
+
const desc = opt.description ? ` ${fmt.dim(`— ${opt.description}`)}` : '';
|
|
583
|
+
output.write(` ${num} ${opt.label}${desc}\n`);
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
while (true) {
|
|
587
|
+
const raw = await readFallbackLine(
|
|
588
|
+
` ${fmt.dim(`Choose [1-${options.length}] (default ${clampedDefault + 1}):`)} `,
|
|
589
|
+
);
|
|
590
|
+
const trimmed = raw.trim();
|
|
591
|
+
output.write('\n');
|
|
592
|
+
|
|
593
|
+
let matched = null;
|
|
594
|
+
if (!trimmed) {
|
|
595
|
+
matched = options[clampedDefault];
|
|
596
|
+
} else {
|
|
597
|
+
const num = Number.parseInt(trimmed, 10);
|
|
598
|
+
if (!Number.isNaN(num) && num >= 1 && num <= options.length && String(num) === trimmed) {
|
|
599
|
+
matched = options[num - 1];
|
|
600
|
+
} else {
|
|
601
|
+
const lower = trimmed.toLowerCase();
|
|
602
|
+
matched =
|
|
603
|
+
options.find(
|
|
604
|
+
(opt) =>
|
|
605
|
+
opt.label.toLowerCase() === lower ||
|
|
606
|
+
String(opt.value).toLowerCase() === lower ||
|
|
607
|
+
(opt.shortcut && opt.shortcut.toLowerCase() === lower),
|
|
608
|
+
) ?? null;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (matched) {
|
|
613
|
+
const desc = matched.description ? ` ${fmt.dim(`— ${matched.description}`)}` : '';
|
|
614
|
+
output.write(` ${fmt.boldGreen('→')} ${fmt.boldGreen(matched.label)}${desc}\n`);
|
|
615
|
+
return matched.value;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
output.write(
|
|
619
|
+
` ${fmt.yellow(`Please choose a number between 1 and ${options.length} (or an option name).`)}\n`,
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
},
|
|
623
|
+
|
|
219
624
|
close() {
|
|
220
625
|
if (closed) return;
|
|
221
626
|
closed = true;
|
|
@@ -224,15 +629,17 @@ function createPrompter({ input = process.stdin, output = process.stdout } = {})
|
|
|
224
629
|
};
|
|
225
630
|
}
|
|
226
631
|
|
|
227
|
-
async function promptYesNo(prompter,
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
632
|
+
async function promptYesNo(prompter, question, defaultYes = true) {
|
|
633
|
+
const options = defaultYes
|
|
634
|
+
? [
|
|
635
|
+
{ value: true, label: 'Yes', shortcut: 'y' },
|
|
636
|
+
{ value: false, label: 'No', shortcut: 'n' },
|
|
637
|
+
]
|
|
638
|
+
: [
|
|
639
|
+
{ value: false, label: 'No', shortcut: 'n' },
|
|
640
|
+
{ value: true, label: 'Yes', shortcut: 'y' },
|
|
641
|
+
];
|
|
642
|
+
return prompter.select({ question, options, defaultIndex: 0 });
|
|
236
643
|
}
|
|
237
644
|
|
|
238
645
|
export function checkCodePenProfileStatus(username, projectDir = process.cwd()) {
|
|
@@ -291,73 +698,89 @@ async function promptInitOptions({
|
|
|
291
698
|
cwd = process.cwd(),
|
|
292
699
|
output = process.stdout,
|
|
293
700
|
prompter,
|
|
701
|
+
fmt = createFormatter(output),
|
|
294
702
|
}) {
|
|
295
703
|
const baseCwd = path.resolve(cwd);
|
|
296
704
|
|
|
297
|
-
output.write('
|
|
705
|
+
output.write('\n');
|
|
706
|
+
output.write(
|
|
707
|
+
`${fmt.boldGreen('✦')} ${fmt.bold('Welcome to')} ${fmt.boldCyan('codepen-demokit')}${fmt.bold('!')}\n`,
|
|
708
|
+
);
|
|
709
|
+
output.write(
|
|
710
|
+
` ${fmt.dim("Let's get your new self-contained web demos repository set up.")}\n`,
|
|
711
|
+
);
|
|
298
712
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
713
|
+
output.write('\n');
|
|
714
|
+
const projectName = await prompter.askText({
|
|
715
|
+
question: 'Project name',
|
|
716
|
+
defaultValue: DEFAULT_SITE_CONFIG.title,
|
|
717
|
+
validate(raw) {
|
|
718
|
+
const trimmed = raw.trim();
|
|
719
|
+
if (CONTROL_CHAR_PATTERN.test(trimmed)) {
|
|
720
|
+
return { ok: false, error: 'Project name contains invalid control characters.' };
|
|
721
|
+
}
|
|
722
|
+
const value = trimmed || DEFAULT_SITE_CONFIG.title;
|
|
723
|
+
return { ok: true, value, displayValue: value };
|
|
724
|
+
},
|
|
725
|
+
});
|
|
310
726
|
|
|
727
|
+
output.write('\n');
|
|
311
728
|
const defaultDir = slugifyDirectoryName(projectName);
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
break;
|
|
325
|
-
}
|
|
729
|
+
const { resolvedDir: targetDir, isCurrentDir } = await prompter.askText({
|
|
730
|
+
question: 'Directory',
|
|
731
|
+
defaultValue: defaultDir,
|
|
732
|
+
async validate(raw) {
|
|
733
|
+
const candidate = raw.trim() || defaultDir;
|
|
734
|
+
const result = await resolveTargetDirectory(candidate, baseCwd);
|
|
735
|
+
if (!result.ok) {
|
|
736
|
+
return { ok: false, error: result.error };
|
|
737
|
+
}
|
|
738
|
+
return { ok: true, value: result, displayValue: candidate };
|
|
739
|
+
},
|
|
740
|
+
});
|
|
326
741
|
|
|
327
|
-
output.write('\
|
|
742
|
+
output.write('\n');
|
|
743
|
+
const setupCodePenNow = await promptYesNo(
|
|
744
|
+
prompter,
|
|
745
|
+
'Do you want to set up your CodePen details right now?',
|
|
746
|
+
true,
|
|
747
|
+
);
|
|
328
748
|
|
|
329
|
-
let codepenUser;
|
|
330
|
-
|
|
331
|
-
const raw = await prompter.ask('CodePen username (optional): ');
|
|
332
|
-
const trimmed = raw.trim();
|
|
333
|
-
if (!trimmed) {
|
|
334
|
-
codepenUser = '';
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
if (!PROFILE_NAME_PATTERN.test(trimmed)) {
|
|
338
|
-
output.write(
|
|
339
|
-
` Invalid CodePen username "${trimmed}". Use letters, numbers, ".", "_", or "-" (without "@").\n`,
|
|
340
|
-
);
|
|
341
|
-
continue;
|
|
342
|
-
}
|
|
343
|
-
codepenUser = trimmed;
|
|
344
|
-
break;
|
|
345
|
-
}
|
|
749
|
+
let codepenUser = '';
|
|
750
|
+
let defaultVisibility = DEFAULT_VISIBILITY;
|
|
346
751
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
752
|
+
if (setupCodePenNow) {
|
|
753
|
+
output.write('\n');
|
|
754
|
+
codepenUser = await prompter.askText({
|
|
755
|
+
question: 'CodePen username',
|
|
756
|
+
validate(raw) {
|
|
757
|
+
const trimmed = raw.trim();
|
|
758
|
+
if (!trimmed) {
|
|
759
|
+
return {
|
|
760
|
+
ok: false,
|
|
761
|
+
error: 'Please enter a CodePen username (letters, numbers, ".", "_", or "-").',
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
if (!PROFILE_NAME_PATTERN.test(trimmed)) {
|
|
765
|
+
return {
|
|
766
|
+
ok: false,
|
|
767
|
+
error: `Invalid CodePen username "${trimmed}". Use letters, numbers, ".", "_", or "-" (without "@").`,
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
return { ok: true, value: trimmed, displayValue: trimmed };
|
|
771
|
+
},
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
output.write('\n');
|
|
775
|
+
const defaultVisIdx = Math.max(
|
|
776
|
+
0,
|
|
777
|
+
VISIBILITY_CHOICES.findIndex((choice) => choice.value === DEFAULT_VISIBILITY),
|
|
351
778
|
);
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
continue;
|
|
358
|
-
}
|
|
359
|
-
defaultVisibility = normalized;
|
|
360
|
-
break;
|
|
779
|
+
defaultVisibility = await prompter.select({
|
|
780
|
+
question: 'Default visibility for new Pens',
|
|
781
|
+
options: VISIBILITY_CHOICES,
|
|
782
|
+
defaultIndex: defaultVisIdx,
|
|
783
|
+
});
|
|
361
784
|
}
|
|
362
785
|
|
|
363
786
|
return {
|
|
@@ -402,28 +825,31 @@ function printNextSteps({
|
|
|
402
825
|
targetDir,
|
|
403
826
|
codepenUser = '',
|
|
404
827
|
needsAuthLogin = true,
|
|
828
|
+
fmt = createFormatter(output),
|
|
405
829
|
}) {
|
|
406
830
|
const writeLine = (line = '') => output.write(`${line}\n`);
|
|
407
831
|
const relDir = path.relative(path.resolve(cwd), path.resolve(targetDir));
|
|
408
832
|
const steps = [];
|
|
409
833
|
if (relDir && relDir !== '.') {
|
|
410
|
-
steps.push(`cd ${relDir}`);
|
|
834
|
+
steps.push(fmt.cyan(`cd ${relDir}`));
|
|
411
835
|
}
|
|
412
|
-
steps.push('npm install');
|
|
836
|
+
steps.push(fmt.cyan('npm install'));
|
|
413
837
|
if (codepenUser) {
|
|
414
838
|
if (needsAuthLogin) {
|
|
415
|
-
steps.push(`npx cdpn auth login ${codepenUser}`);
|
|
839
|
+
steps.push(fmt.cyan(`npx cdpn auth login ${codepenUser}`));
|
|
416
840
|
}
|
|
417
841
|
} else {
|
|
418
|
-
steps.push(
|
|
419
|
-
|
|
842
|
+
steps.push(
|
|
843
|
+
`Edit ${fmt.magenta('config.json')} to set your CodePen profile ${fmt.dim('("codepen.user")')}`,
|
|
844
|
+
);
|
|
845
|
+
steps.push(fmt.cyan('npx cdpn auth login <your-codepen-username>'));
|
|
420
846
|
}
|
|
421
|
-
steps.push('npm start');
|
|
847
|
+
steps.push(fmt.cyan('npm start'));
|
|
422
848
|
|
|
423
849
|
writeLine();
|
|
424
|
-
writeLine('Next steps:');
|
|
850
|
+
writeLine(fmt.boldYellow('Next steps:'));
|
|
425
851
|
steps.forEach((step, idx) => {
|
|
426
|
-
writeLine(` ${idx + 1}
|
|
852
|
+
writeLine(` ${fmt.dim(`${idx + 1}.`)} ${step}`);
|
|
427
853
|
});
|
|
428
854
|
}
|
|
429
855
|
|
|
@@ -438,6 +864,7 @@ export async function scaffoldProject({
|
|
|
438
864
|
showNextSteps = true,
|
|
439
865
|
needsAuthLogin = Boolean(codepenUser),
|
|
440
866
|
}) {
|
|
867
|
+
const fmt = createFormatter(output);
|
|
441
868
|
const resolvedDir = path.resolve(targetDir);
|
|
442
869
|
await fs.mkdir(resolvedDir, { recursive: true });
|
|
443
870
|
|
|
@@ -467,6 +894,7 @@ export async function scaffoldProject({
|
|
|
467
894
|
'cdpn:push': 'codepen-demokit cdpn push',
|
|
468
895
|
'cdpn:delete': 'codepen-demokit cdpn delete',
|
|
469
896
|
'cdpn:clone': 'codepen-demokit cdpn clone',
|
|
897
|
+
'cdpn:new': 'codepen-demokit cdpn new',
|
|
470
898
|
cdpn: 'cdpn',
|
|
471
899
|
},
|
|
472
900
|
engines: {
|
|
@@ -542,16 +970,20 @@ export async function scaffoldProject({
|
|
|
542
970
|
const writeLine = (line = '') => output.write(`${line}\n`);
|
|
543
971
|
|
|
544
972
|
writeLine();
|
|
545
|
-
writeLine(
|
|
973
|
+
writeLine(
|
|
974
|
+
`${fmt.boldGreen('✓')} ${fmt.bold('Initialized')} ${fmt.boldCyan('codepen-demokit')} ${fmt.bold('project in')} ${fmt.magenta(resolvedDir)}`,
|
|
975
|
+
);
|
|
546
976
|
writeLine();
|
|
547
977
|
for (const item of results) {
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
978
|
+
if (item.status === 'created') {
|
|
979
|
+
writeLine(` ${fmt.boldGreen('+')} ${fmt.green('[CREATED]')} ${item.path}`);
|
|
980
|
+
} else {
|
|
981
|
+
writeLine(` ${fmt.yellow('-')} ${fmt.dim('[SKIPPED]')} ${fmt.dim(item.path)}`);
|
|
982
|
+
}
|
|
551
983
|
}
|
|
552
984
|
|
|
553
985
|
if (showNextSteps) {
|
|
554
|
-
printNextSteps({ output, cwd, targetDir: resolvedDir, codepenUser, needsAuthLogin });
|
|
986
|
+
printNextSteps({ output, cwd, targetDir: resolvedDir, codepenUser, needsAuthLogin, fmt });
|
|
555
987
|
}
|
|
556
988
|
|
|
557
989
|
return {
|
|
@@ -568,10 +1000,11 @@ export async function initProject({
|
|
|
568
1000
|
input = process.stdin,
|
|
569
1001
|
output = process.stdout,
|
|
570
1002
|
} = {}) {
|
|
571
|
-
const
|
|
1003
|
+
const fmt = createFormatter(output);
|
|
1004
|
+
const prompter = createPrompter({ input, output, fmt });
|
|
572
1005
|
|
|
573
1006
|
try {
|
|
574
|
-
const options = await promptInitOptions({ cwd, output, prompter });
|
|
1007
|
+
const options = await promptInitOptions({ cwd, output, prompter, fmt });
|
|
575
1008
|
const scaffoldResult = await scaffoldProject({
|
|
576
1009
|
...options,
|
|
577
1010
|
cwd,
|
|
@@ -588,19 +1021,18 @@ export async function initProject({
|
|
|
588
1021
|
|
|
589
1022
|
if (status === 'configured') {
|
|
590
1023
|
output.write(
|
|
591
|
-
`\
|
|
1024
|
+
`\n${fmt.boldGreen('✓')} CodePen authentication for ${fmt.boldCyan(options.codepenUser)} is already set up.\n`,
|
|
592
1025
|
);
|
|
593
1026
|
needsAuthLogin = false;
|
|
594
1027
|
} else if (status === 'missing' && cdpnBin) {
|
|
595
1028
|
output.write(
|
|
596
|
-
`\
|
|
1029
|
+
`\n${fmt.boldYellow('!')} It looks like you haven't authenticated with CodePen as ${fmt.boldCyan(options.codepenUser)} yet.\n\n`,
|
|
597
1030
|
);
|
|
598
1031
|
let shouldLogin = false;
|
|
599
1032
|
try {
|
|
600
1033
|
shouldLogin = await promptYesNo(
|
|
601
1034
|
prompter,
|
|
602
|
-
|
|
603
|
-
`Run \`cdpn auth login ${options.codepenUser}\` now? (Y/n): `,
|
|
1035
|
+
'Would you like to set up CodePen authentication now?',
|
|
604
1036
|
true,
|
|
605
1037
|
);
|
|
606
1038
|
} catch (err) {
|
|
@@ -639,6 +1071,7 @@ export async function initProject({
|
|
|
639
1071
|
targetDir: scaffoldResult.targetDir,
|
|
640
1072
|
codepenUser: options.codepenUser,
|
|
641
1073
|
needsAuthLogin,
|
|
1074
|
+
fmt,
|
|
642
1075
|
});
|
|
643
1076
|
|
|
644
1077
|
return {
|