codepen-demokit 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/README.md +229 -2
- package/bin/cli.mjs +154 -0
- package/lib/build-json.mjs +439 -0
- package/lib/build-site.mjs +193 -0
- package/lib/config.mjs +245 -0
- package/lib/init.mjs +651 -0
- package/lib/serve.mjs +369 -0
- package/lib/sync-codepen.mjs +1326 -0
- package/package.json +35 -4
- package/shell/index.html +48 -0
- package/shell/scripts.js +201 -0
- package/shell/styles.css +299 -0
- package/templates/AGENTS.md +76 -0
- package/templates/README.md +62 -0
- package/templates/config.json.example +10 -0
- package/templates/gitignore +27 -0
package/lib/init.mjs
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import { existsSync } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import readline from 'node:readline/promises';
|
|
6
|
+
import { PACKAGE_DIR } from './build-site.mjs';
|
|
7
|
+
import {
|
|
8
|
+
DEFAULT_SITE_CONFIG,
|
|
9
|
+
DEFAULT_VISIBILITY,
|
|
10
|
+
PROFILE_NAME_PATTERN,
|
|
11
|
+
VISIBILITY_OPTIONS,
|
|
12
|
+
} from './config.mjs';
|
|
13
|
+
import { locateCdpnBinary } from './sync-codepen.mjs';
|
|
14
|
+
|
|
15
|
+
const TEMPLATES_DIR = path.join(PACKAGE_DIR, 'templates');
|
|
16
|
+
const CONTROL_CHAR_PATTERN = /[\x00-\x1f\x7f]/;
|
|
17
|
+
|
|
18
|
+
const VISIBILITY_BY_LOWER = new Map(
|
|
19
|
+
VISIBILITY_OPTIONS.map((option) => [option.toLowerCase(), option]),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
export class InitCancelledError extends Error {
|
|
23
|
+
constructor() {
|
|
24
|
+
super('Init cancelled.');
|
|
25
|
+
this.name = 'InitCancelledError';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function slugifyDirectoryName(name) {
|
|
30
|
+
const slug = String(name ?? '')
|
|
31
|
+
.normalize('NFKD')
|
|
32
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
33
|
+
.toLowerCase()
|
|
34
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
35
|
+
.replace(/^-+|-+$/g, '');
|
|
36
|
+
return slug || 'my-codepen-demos';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function toValidPackageName(dirName) {
|
|
40
|
+
const cleaned = dirName
|
|
41
|
+
.toLowerCase()
|
|
42
|
+
.replace(/[^a-z0-9._-]+/g, '-')
|
|
43
|
+
.replace(/^[._-]+/, '')
|
|
44
|
+
.replace(/-+$/, '');
|
|
45
|
+
return cleaned || 'my-codepen-demos';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function normalizeVisibility(rawInput) {
|
|
49
|
+
const trimmed = String(rawInput ?? '').trim();
|
|
50
|
+
if (!trimmed) return DEFAULT_VISIBILITY;
|
|
51
|
+
return VISIBILITY_BY_LOWER.get(trimmed.toLowerCase()) ?? null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function isWithinRoot(candidatePath, rootPath) {
|
|
55
|
+
return candidatePath === rootPath || candidatePath.startsWith(rootPath + path.sep);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function resolveTargetDirectory(rawInput, baseCwd) {
|
|
59
|
+
const trimmed = String(rawInput ?? '').trim();
|
|
60
|
+
if (!trimmed) {
|
|
61
|
+
return { ok: false, error: 'Directory cannot be empty.' };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (CONTROL_CHAR_PATTERN.test(trimmed) || trimmed.includes('\\')) {
|
|
65
|
+
return {
|
|
66
|
+
ok: false,
|
|
67
|
+
error: 'Directory contains invalid characters. Use a relative path with forward slashes.',
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (path.isAbsolute(trimmed)) {
|
|
72
|
+
return {
|
|
73
|
+
ok: false,
|
|
74
|
+
error: 'Please enter a relative directory path (e.g., "my-codepen-demos" or ".").',
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let resolvedDir;
|
|
79
|
+
if (trimmed === '.' || trimmed === './') {
|
|
80
|
+
resolvedDir = baseCwd;
|
|
81
|
+
} else {
|
|
82
|
+
let normalized = trimmed;
|
|
83
|
+
while (normalized.startsWith('./')) {
|
|
84
|
+
normalized = normalized.slice(2);
|
|
85
|
+
}
|
|
86
|
+
normalized = normalized.replace(/\/+$/, '');
|
|
87
|
+
|
|
88
|
+
if (!normalized || normalized === '.') {
|
|
89
|
+
resolvedDir = baseCwd;
|
|
90
|
+
} else {
|
|
91
|
+
const segments = normalized.split('/').filter(Boolean);
|
|
92
|
+
for (const segment of segments) {
|
|
93
|
+
if (segment === '.' || segment === '..' || segment.startsWith('.')) {
|
|
94
|
+
return {
|
|
95
|
+
ok: false,
|
|
96
|
+
error: 'Directory cannot contain "..", ".", or hidden folder segments.',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
resolvedDir = path.resolve(baseCwd, ...segments);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!isWithinRoot(resolvedDir, baseCwd)) {
|
|
105
|
+
return {
|
|
106
|
+
ok: false,
|
|
107
|
+
error: 'Directory must resolve inside the current working directory.',
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const baseRealpath = await fs.realpath(baseCwd);
|
|
112
|
+
|
|
113
|
+
// Check nearest existing path to guard against symlinks or file collisions
|
|
114
|
+
let probe = resolvedDir;
|
|
115
|
+
while (true) {
|
|
116
|
+
try {
|
|
117
|
+
const stat = await fs.lstat(probe);
|
|
118
|
+
if (probe === resolvedDir) {
|
|
119
|
+
if (stat.isSymbolicLink()) {
|
|
120
|
+
return {
|
|
121
|
+
ok: false,
|
|
122
|
+
error: `"${trimmed}" is a symbolic link. Please choose a regular directory.`,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
if (!stat.isDirectory()) {
|
|
126
|
+
return {
|
|
127
|
+
ok: false,
|
|
128
|
+
error: `"${trimmed}" already exists and is not a directory.`,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
} else if (!stat.isDirectory()) {
|
|
132
|
+
return {
|
|
133
|
+
ok: false,
|
|
134
|
+
error: `Cannot create "${trimmed}" because an ancestor path is a file.`,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const realProbe = await fs.realpath(probe);
|
|
139
|
+
if (!isWithinRoot(realProbe, baseRealpath)) {
|
|
140
|
+
return {
|
|
141
|
+
ok: false,
|
|
142
|
+
error: `"${trimmed}" resolves outside the current working directory.`,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
} catch (err) {
|
|
147
|
+
if (err && err.code === 'ENOENT') {
|
|
148
|
+
if (probe === baseCwd) break;
|
|
149
|
+
const parent = path.dirname(probe);
|
|
150
|
+
if (parent === probe) break;
|
|
151
|
+
probe = parent;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
throw err;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
ok: true,
|
|
160
|
+
resolvedDir,
|
|
161
|
+
isCurrentDir: resolvedDir === baseCwd,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function createPrompter({ input = process.stdin, output = process.stdout } = {}) {
|
|
166
|
+
let closed = false;
|
|
167
|
+
|
|
168
|
+
if (input.isTTY) {
|
|
169
|
+
const rl = readline.createInterface({ input, output });
|
|
170
|
+
const abortController = new AbortController();
|
|
171
|
+
const onSigint = () => {
|
|
172
|
+
abortController.abort();
|
|
173
|
+
if (!closed) {
|
|
174
|
+
closed = true;
|
|
175
|
+
rl.close();
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
rl.on('SIGINT', onSigint);
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
async ask(promptText) {
|
|
182
|
+
try {
|
|
183
|
+
return await rl.question(promptText, { signal: abortController.signal });
|
|
184
|
+
} catch (err) {
|
|
185
|
+
if (
|
|
186
|
+
abortController.signal.aborted ||
|
|
187
|
+
err?.name === 'AbortError' ||
|
|
188
|
+
err?.code === 'ABORT_ERR' ||
|
|
189
|
+
err?.code === 'ERR_USE_AFTER_CLOSE'
|
|
190
|
+
) {
|
|
191
|
+
output.write('\n');
|
|
192
|
+
throw new InitCancelledError();
|
|
193
|
+
}
|
|
194
|
+
throw err;
|
|
195
|
+
}
|
|
196
|
+
},
|
|
197
|
+
close() {
|
|
198
|
+
if (closed) return;
|
|
199
|
+
closed = true;
|
|
200
|
+
rl.off('SIGINT', onSigint);
|
|
201
|
+
rl.close();
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const rl = readline.createInterface({ input, crlfDelay: Infinity });
|
|
207
|
+
const iterator = rl[Symbol.asyncIterator]();
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
async ask(promptText) {
|
|
211
|
+
output.write(promptText);
|
|
212
|
+
const { value, done } = await iterator.next();
|
|
213
|
+
output.write('\n');
|
|
214
|
+
if (done) {
|
|
215
|
+
throw new InitCancelledError();
|
|
216
|
+
}
|
|
217
|
+
return value;
|
|
218
|
+
},
|
|
219
|
+
close() {
|
|
220
|
+
if (closed) return;
|
|
221
|
+
closed = true;
|
|
222
|
+
rl.close();
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function promptYesNo(prompter, output, promptText, defaultValue = true) {
|
|
228
|
+
while (true) {
|
|
229
|
+
const raw = await prompter.ask(promptText);
|
|
230
|
+
const trimmed = raw.trim().toLowerCase();
|
|
231
|
+
if (!trimmed) return defaultValue;
|
|
232
|
+
if (trimmed === 'y' || trimmed === 'yes') return true;
|
|
233
|
+
if (trimmed === 'n' || trimmed === 'no') return false;
|
|
234
|
+
output.write(' Please answer "y" or "n".\n');
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function checkCodePenProfileStatus(username, projectDir = process.cwd()) {
|
|
239
|
+
const trimmed = String(username ?? '').trim();
|
|
240
|
+
if (!trimmed || !PROFILE_NAME_PATTERN.test(trimmed)) {
|
|
241
|
+
return { status: 'unknown', cdpnBin: null };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const located = locateCdpnBinary(projectDir);
|
|
245
|
+
if (!located) {
|
|
246
|
+
return { status: 'unknown', cdpnBin: null };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const res = spawnSync(
|
|
250
|
+
located.bin.command,
|
|
251
|
+
[...located.bin.prefixArgs, '--profile', trimmed, 'auth', 'status', '--offline', '--json'],
|
|
252
|
+
{
|
|
253
|
+
cwd: projectDir,
|
|
254
|
+
encoding: 'utf8',
|
|
255
|
+
shell: located.bin.shell,
|
|
256
|
+
},
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
if (res.error) {
|
|
260
|
+
return { status: 'unknown', cdpnBin: located.bin };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let parsed = null;
|
|
264
|
+
const rawStdout = (res.stdout || '').trim();
|
|
265
|
+
if (rawStdout) {
|
|
266
|
+
try {
|
|
267
|
+
parsed = JSON.parse(rawStdout);
|
|
268
|
+
} catch {
|
|
269
|
+
const firstBrace = rawStdout.indexOf('{');
|
|
270
|
+
if (firstBrace !== -1) {
|
|
271
|
+
try {
|
|
272
|
+
parsed = JSON.parse(rawStdout.slice(firstBrace));
|
|
273
|
+
} catch {
|
|
274
|
+
parsed = null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const statusCode = parsed?.status?.code ?? null;
|
|
281
|
+
if (res.status === 0 && statusCode === 200) {
|
|
282
|
+
return { status: 'configured', cdpnBin: located.bin };
|
|
283
|
+
}
|
|
284
|
+
if (statusCode === 404) {
|
|
285
|
+
return { status: 'missing', cdpnBin: located.bin };
|
|
286
|
+
}
|
|
287
|
+
return { status: 'unknown', cdpnBin: located.bin };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async function promptInitOptions({
|
|
291
|
+
cwd = process.cwd(),
|
|
292
|
+
output = process.stdout,
|
|
293
|
+
prompter,
|
|
294
|
+
}) {
|
|
295
|
+
const baseCwd = path.resolve(cwd);
|
|
296
|
+
|
|
297
|
+
output.write('Create a new codepen-demokit project\n\n');
|
|
298
|
+
|
|
299
|
+
let projectName;
|
|
300
|
+
while (true) {
|
|
301
|
+
const raw = await prompter.ask(`Project name (${DEFAULT_SITE_CONFIG.title}): `);
|
|
302
|
+
const trimmed = raw.trim();
|
|
303
|
+
if (CONTROL_CHAR_PATTERN.test(trimmed)) {
|
|
304
|
+
output.write(' Project name contains invalid control characters.\n');
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
projectName = trimmed || DEFAULT_SITE_CONFIG.title;
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const defaultDir = slugifyDirectoryName(projectName);
|
|
312
|
+
let targetDir;
|
|
313
|
+
let isCurrentDir;
|
|
314
|
+
while (true) {
|
|
315
|
+
const raw = await prompter.ask(`Directory (${defaultDir}): `);
|
|
316
|
+
const candidate = raw.trim() || defaultDir;
|
|
317
|
+
const result = await resolveTargetDirectory(candidate, baseCwd);
|
|
318
|
+
if (!result.ok) {
|
|
319
|
+
output.write(` ${result.error}\n`);
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
targetDir = result.resolvedDir;
|
|
323
|
+
isCurrentDir = result.isCurrentDir;
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
output.write('\nCodePen details:\n');
|
|
328
|
+
|
|
329
|
+
let codepenUser;
|
|
330
|
+
while (true) {
|
|
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
|
+
}
|
|
346
|
+
|
|
347
|
+
let defaultVisibility;
|
|
348
|
+
while (true) {
|
|
349
|
+
const raw = await prompter.ask(
|
|
350
|
+
`Default visibility [${VISIBILITY_OPTIONS.join(', ')}] (${DEFAULT_VISIBILITY}): `,
|
|
351
|
+
);
|
|
352
|
+
const normalized = normalizeVisibility(raw);
|
|
353
|
+
if (!normalized) {
|
|
354
|
+
output.write(
|
|
355
|
+
` Invalid visibility "${raw.trim()}". Choose one of: ${VISIBILITY_OPTIONS.join(', ')}.\n`,
|
|
356
|
+
);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
defaultVisibility = normalized;
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
return {
|
|
364
|
+
projectName,
|
|
365
|
+
targetDir,
|
|
366
|
+
isCurrentDir,
|
|
367
|
+
codepenUser,
|
|
368
|
+
defaultVisibility,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async function readFrameworkMeta() {
|
|
373
|
+
const pkgJsonPath = path.join(PACKAGE_DIR, 'package.json');
|
|
374
|
+
const pkg = JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'));
|
|
375
|
+
return {
|
|
376
|
+
version: pkg.version || '1.0.0',
|
|
377
|
+
codepenCommanderRange: pkg.dependencies?.['codepen-commander'] || '^1.3.0',
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async function writeIfMissing(destPath, content) {
|
|
382
|
+
if (existsSync(destPath)) {
|
|
383
|
+
return 'skipped';
|
|
384
|
+
}
|
|
385
|
+
await fs.mkdir(path.dirname(destPath), { recursive: true });
|
|
386
|
+
await fs.writeFile(destPath, content, 'utf8');
|
|
387
|
+
return 'created';
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
async function copyIfMissing(srcPath, destPath) {
|
|
391
|
+
if (existsSync(destPath)) {
|
|
392
|
+
return 'skipped';
|
|
393
|
+
}
|
|
394
|
+
await fs.mkdir(path.dirname(destPath), { recursive: true });
|
|
395
|
+
await fs.copyFile(srcPath, destPath);
|
|
396
|
+
return 'created';
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function printNextSteps({
|
|
400
|
+
output = process.stdout,
|
|
401
|
+
cwd = process.cwd(),
|
|
402
|
+
targetDir,
|
|
403
|
+
codepenUser = '',
|
|
404
|
+
needsAuthLogin = true,
|
|
405
|
+
}) {
|
|
406
|
+
const writeLine = (line = '') => output.write(`${line}\n`);
|
|
407
|
+
const relDir = path.relative(path.resolve(cwd), path.resolve(targetDir));
|
|
408
|
+
const steps = [];
|
|
409
|
+
if (relDir && relDir !== '.') {
|
|
410
|
+
steps.push(`cd ${relDir}`);
|
|
411
|
+
}
|
|
412
|
+
steps.push('npm install');
|
|
413
|
+
if (codepenUser) {
|
|
414
|
+
if (needsAuthLogin) {
|
|
415
|
+
steps.push(`npx cdpn auth login ${codepenUser}`);
|
|
416
|
+
}
|
|
417
|
+
} else {
|
|
418
|
+
steps.push('Edit config.json to set your CodePen profile ("codepen.user")');
|
|
419
|
+
steps.push('npx cdpn auth login <your-codepen-username>');
|
|
420
|
+
}
|
|
421
|
+
steps.push('npm start');
|
|
422
|
+
|
|
423
|
+
writeLine();
|
|
424
|
+
writeLine('Next steps:');
|
|
425
|
+
steps.forEach((step, idx) => {
|
|
426
|
+
writeLine(` ${idx + 1}. ${step}`);
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export async function scaffoldProject({
|
|
431
|
+
targetDir,
|
|
432
|
+
projectName = DEFAULT_SITE_CONFIG.title,
|
|
433
|
+
isCurrentDir = false,
|
|
434
|
+
codepenUser = '',
|
|
435
|
+
defaultVisibility = DEFAULT_VISIBILITY,
|
|
436
|
+
cwd = process.cwd(),
|
|
437
|
+
output = process.stdout,
|
|
438
|
+
showNextSteps = true,
|
|
439
|
+
needsAuthLogin = Boolean(codepenUser),
|
|
440
|
+
}) {
|
|
441
|
+
const resolvedDir = path.resolve(targetDir);
|
|
442
|
+
await fs.mkdir(resolvedDir, { recursive: true });
|
|
443
|
+
|
|
444
|
+
const { version, codepenCommanderRange } = await readFrameworkMeta();
|
|
445
|
+
const packageName = isCurrentDir
|
|
446
|
+
? toValidPackageName(slugifyDirectoryName(projectName))
|
|
447
|
+
: toValidPackageName(path.basename(resolvedDir));
|
|
448
|
+
const results = [];
|
|
449
|
+
|
|
450
|
+
const record = (relPath, status) => {
|
|
451
|
+
results.push({ path: relPath, status });
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
const consumerPackageJson = {
|
|
455
|
+
name: packageName,
|
|
456
|
+
version: '1.0.0',
|
|
457
|
+
private: true,
|
|
458
|
+
description: 'Self-contained web demos synced with CodePen.',
|
|
459
|
+
type: 'module',
|
|
460
|
+
scripts: {
|
|
461
|
+
start: 'codepen-demokit start',
|
|
462
|
+
dev: 'codepen-demokit start',
|
|
463
|
+
build: 'codepen-demokit build',
|
|
464
|
+
'build:watch': 'codepen-demokit build --watch',
|
|
465
|
+
'cdpn:sync': 'codepen-demokit cdpn sync',
|
|
466
|
+
'cdpn:pull': 'codepen-demokit cdpn pull',
|
|
467
|
+
'cdpn:push': 'codepen-demokit cdpn push',
|
|
468
|
+
'cdpn:delete': 'codepen-demokit cdpn delete',
|
|
469
|
+
'cdpn:clone': 'codepen-demokit cdpn clone',
|
|
470
|
+
cdpn: 'cdpn',
|
|
471
|
+
},
|
|
472
|
+
engines: {
|
|
473
|
+
node: '>=20',
|
|
474
|
+
},
|
|
475
|
+
devDependencies: {
|
|
476
|
+
'codepen-commander': codepenCommanderRange,
|
|
477
|
+
'codepen-demokit': `^${version}`,
|
|
478
|
+
},
|
|
479
|
+
};
|
|
480
|
+
|
|
481
|
+
record(
|
|
482
|
+
'package.json',
|
|
483
|
+
await writeIfMissing(
|
|
484
|
+
path.join(resolvedDir, 'package.json'),
|
|
485
|
+
`${JSON.stringify(consumerPackageJson, null, 2)}\n`,
|
|
486
|
+
),
|
|
487
|
+
);
|
|
488
|
+
|
|
489
|
+
// Copy template files (note: gitignore is named `gitignore` inside templates/ so npm pack always includes it!)
|
|
490
|
+
const templateCopies = [
|
|
491
|
+
['gitignore', '.gitignore'],
|
|
492
|
+
['config.json.example', 'config.json.example'],
|
|
493
|
+
];
|
|
494
|
+
|
|
495
|
+
for (const [srcName, destName] of templateCopies) {
|
|
496
|
+
const status = await copyIfMissing(
|
|
497
|
+
path.join(TEMPLATES_DIR, srcName),
|
|
498
|
+
path.join(resolvedDir, destName),
|
|
499
|
+
);
|
|
500
|
+
record(destName, status);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const consumerConfig = {
|
|
504
|
+
site: {
|
|
505
|
+
title: projectName,
|
|
506
|
+
subtitle: DEFAULT_SITE_CONFIG.subtitle,
|
|
507
|
+
},
|
|
508
|
+
codepen: {
|
|
509
|
+
user: codepenUser,
|
|
510
|
+
defaultVisibility,
|
|
511
|
+
},
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
record(
|
|
515
|
+
'config.json',
|
|
516
|
+
await writeIfMissing(
|
|
517
|
+
path.join(resolvedDir, 'config.json'),
|
|
518
|
+
`${JSON.stringify(consumerConfig, null, 2)}\n`,
|
|
519
|
+
),
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
record(
|
|
523
|
+
'AGENTS.md',
|
|
524
|
+
await copyIfMissing(
|
|
525
|
+
path.join(TEMPLATES_DIR, 'AGENTS.md'),
|
|
526
|
+
path.join(resolvedDir, 'AGENTS.md'),
|
|
527
|
+
),
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
const readmeTemplate = await fs.readFile(path.join(TEMPLATES_DIR, 'README.md'), 'utf8');
|
|
531
|
+
const renderedReadme = readmeTemplate.replace(/^# .+/, () => `# ${projectName}`);
|
|
532
|
+
record(
|
|
533
|
+
'README.md',
|
|
534
|
+
await writeIfMissing(path.join(resolvedDir, 'README.md'), renderedReadme),
|
|
535
|
+
);
|
|
536
|
+
|
|
537
|
+
record(
|
|
538
|
+
'src/.gitkeep',
|
|
539
|
+
await writeIfMissing(path.join(resolvedDir, 'src', '.gitkeep'), ''),
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
const writeLine = (line = '') => output.write(`${line}\n`);
|
|
543
|
+
|
|
544
|
+
writeLine();
|
|
545
|
+
writeLine(`Initialized codepen-demokit project in ${resolvedDir}`);
|
|
546
|
+
writeLine();
|
|
547
|
+
for (const item of results) {
|
|
548
|
+
const icon = item.status === 'created' ? '+' : '-';
|
|
549
|
+
const label = item.status === 'created' ? 'CREATED' : 'SKIPPED';
|
|
550
|
+
writeLine(` ${icon} [${label}] ${item.path}`);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (showNextSteps) {
|
|
554
|
+
printNextSteps({ output, cwd, targetDir: resolvedDir, codepenUser, needsAuthLogin });
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
return {
|
|
558
|
+
targetDir: resolvedDir,
|
|
559
|
+
projectName,
|
|
560
|
+
codepenUser,
|
|
561
|
+
defaultVisibility,
|
|
562
|
+
results,
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export async function initProject({
|
|
567
|
+
cwd = process.cwd(),
|
|
568
|
+
input = process.stdin,
|
|
569
|
+
output = process.stdout,
|
|
570
|
+
} = {}) {
|
|
571
|
+
const prompter = createPrompter({ input, output });
|
|
572
|
+
|
|
573
|
+
try {
|
|
574
|
+
const options = await promptInitOptions({ cwd, output, prompter });
|
|
575
|
+
const scaffoldResult = await scaffoldProject({
|
|
576
|
+
...options,
|
|
577
|
+
cwd,
|
|
578
|
+
output,
|
|
579
|
+
showNextSteps: false,
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
let needsAuthLogin = Boolean(options.codepenUser);
|
|
583
|
+
if (options.codepenUser) {
|
|
584
|
+
const { status, cdpnBin } = checkCodePenProfileStatus(
|
|
585
|
+
options.codepenUser,
|
|
586
|
+
scaffoldResult.targetDir,
|
|
587
|
+
);
|
|
588
|
+
|
|
589
|
+
if (status === 'configured') {
|
|
590
|
+
output.write(
|
|
591
|
+
`\nCodePen Commander profile "${options.codepenUser}" is already configured.\n`,
|
|
592
|
+
);
|
|
593
|
+
needsAuthLogin = false;
|
|
594
|
+
} else if (status === 'missing' && cdpnBin) {
|
|
595
|
+
output.write(
|
|
596
|
+
`\nNo CodePen Commander configuration found for "${options.codepenUser}".\n`,
|
|
597
|
+
);
|
|
598
|
+
let shouldLogin = false;
|
|
599
|
+
try {
|
|
600
|
+
shouldLogin = await promptYesNo(
|
|
601
|
+
prompter,
|
|
602
|
+
output,
|
|
603
|
+
`Run \`cdpn auth login ${options.codepenUser}\` now? (Y/n): `,
|
|
604
|
+
true,
|
|
605
|
+
);
|
|
606
|
+
} catch (err) {
|
|
607
|
+
if (err instanceof InitCancelledError) {
|
|
608
|
+
shouldLogin = false;
|
|
609
|
+
} else {
|
|
610
|
+
throw err;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// Release stdin cleanly before handing terminal control to `cdpn auth login`
|
|
615
|
+
prompter.close();
|
|
616
|
+
|
|
617
|
+
if (shouldLogin) {
|
|
618
|
+
output.write('\n');
|
|
619
|
+
const loginRes = spawnSync(
|
|
620
|
+
cdpnBin.command,
|
|
621
|
+
[...cdpnBin.prefixArgs, 'auth', 'login', options.codepenUser],
|
|
622
|
+
{
|
|
623
|
+
cwd: scaffoldResult.targetDir,
|
|
624
|
+
stdio: 'inherit',
|
|
625
|
+
shell: cdpnBin.shell,
|
|
626
|
+
},
|
|
627
|
+
);
|
|
628
|
+
if (!loginRes.error && loginRes.status === 0) {
|
|
629
|
+
needsAuthLogin = false;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
prompter.close();
|
|
636
|
+
printNextSteps({
|
|
637
|
+
output,
|
|
638
|
+
cwd,
|
|
639
|
+
targetDir: scaffoldResult.targetDir,
|
|
640
|
+
codepenUser: options.codepenUser,
|
|
641
|
+
needsAuthLogin,
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
return {
|
|
645
|
+
...scaffoldResult,
|
|
646
|
+
authConfigured: Boolean(options.codepenUser) && !needsAuthLogin,
|
|
647
|
+
};
|
|
648
|
+
} finally {
|
|
649
|
+
prompter.close();
|
|
650
|
+
}
|
|
651
|
+
}
|