codepen-commander 1.3.0 → 1.4.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 +103 -11
- package/api/client.js +129 -106
- package/cli.js +34 -29
- package/commands/auth/default.js +40 -0
- package/commands/auth/index.js +83 -0
- package/commands/auth/list.js +78 -0
- package/commands/auth/login.js +136 -0
- package/commands/auth/logout.js +186 -0
- package/commands/auth/status.js +212 -0
- package/commands/build.js +256 -0
- package/commands/clone.js +13 -581
- package/commands/delete.js +548 -80
- package/commands/deploy.js +164 -169
- package/commands/domains.js +83 -39
- package/commands/fork.js +13 -317
- package/commands/info.js +152 -0
- package/commands/init.js +375 -0
- package/commands/list.js +95 -78
- package/commands/new.js +155 -83
- package/commands/open.js +29 -19
- package/commands/privacy.js +52 -36
- package/commands/pull.js +123 -110
- package/commands/push.js +221 -376
- package/commands/restore.js +251 -0
- package/commands/rollback.js +141 -0
- package/commands/serve.js +119 -0
- package/commands/settings.js +126 -0
- package/commands/shell.js +305 -0
- package/commands/status.js +133 -39
- package/commands/sync.js +330 -0
- package/commands/undeploy.js +102 -0
- package/commands/unlink.js +191 -0
- package/commands/versions.js +100 -163
- package/config/auth.js +4 -374
- package/config/pen-blocks.js +166 -0
- package/config/project.js +103 -231
- package/config/user-store.js +364 -0
- package/constants.js +42 -0
- package/errors.js +17 -0
- package/package.json +1 -1
- package/shell/index.html +79 -0
- package/shell/scripts.js +276 -0
- package/shell/styles.css +492 -0
- package/site/blocks/classic.js +270 -0
- package/site/blocks/context.js +63 -0
- package/site/blocks/index.js +4 -0
- package/site/blocks/packages.js +242 -0
- package/site/blocks/unsupported-page.js +328 -0
- package/site/builder.js +475 -0
- package/site/detect.js +65 -0
- package/site/ignore-files.js +90 -0
- package/site/manifest.js +227 -0
- package/site/server.js +483 -0
- package/site/shell.js +363 -0
- package/sync/fs.js +19 -17
- package/sync/info.js +6 -81
- package/utils/browser.js +17 -10
- package/utils/cli-options.js +79 -0
- package/utils/codepen-url.js +183 -0
- package/utils/format.js +39 -30
- package/utils/fs-safe.js +95 -0
- package/utils/html.js +40 -1
- package/utils/json.js +4 -19
- package/utils/output.js +37 -0
- package/utils/package-paths.js +51 -0
- package/utils/prompts/index.js +5 -0
- package/utils/prompts/multi-select.js +185 -0
- package/utils/prompts/secret.js +65 -0
- package/utils/prompts/select.js +141 -0
- package/utils/prompts/text.js +99 -0
- package/utils/prompts/tty-list.js +165 -0
- package/utils/prompts/types.js +35 -0
- package/utils/strings.js +98 -0
- package/utils/watcher.js +82 -0
- package/{commands → workflows}/auth-context.js +13 -7
- package/workflows/batch-pens.js +1647 -0
- package/workflows/batch-target.js +184 -0
- package/workflows/classic-convert.js +241 -0
- package/workflows/clone-pen.js +381 -0
- package/{commands → workflows}/create-pen.js +59 -84
- package/workflows/discover-pens.js +91 -0
- package/workflows/fork-pen.js +314 -0
- package/workflows/missing-pen.js +212 -0
- package/workflows/pull-pen.js +204 -0
- package/workflows/push-pen.js +318 -0
- package/{commands → workflows}/scaffold.js +4 -3
- package/workflows/sync-pen.js +227 -0
- package/{commands → workflows}/unowned-pen.js +16 -12
- package/commands/auth.js +0 -593
- package/commands/meta.js +0 -184
- package/utils/prompts.js +0 -316
- /package/{types.js → api/types.js} +0 -0
package/commands/init.js
ADDED
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { INIT_BLOCK_CHOICES, buildPenConfigObject, buildStarterClassicConfig, normalizePenBlocksList, parseBlocksOption, } from '../config/pen-blocks.js';
|
|
4
|
+
import { hasProjectConfigInDir, readProjectConfigFile, writeUnlinkedProjectInfo, } from '../config/project.js';
|
|
5
|
+
import { CLASSIC_CONFIG_FILENAME, CODEPEN_DIR, PEN_ACCESS_CHOICES, PEN_CONFIG_FILENAME, PROJECT_CONFIG_FILENAME, } from '../constants.js';
|
|
6
|
+
import { CliError } from '../errors.js';
|
|
7
|
+
import { readPenBlocks } from '../site/blocks/index.js';
|
|
8
|
+
import { assertValidTagCount, parseTagsOption, parseTemplateCliFlag, resolveAccessOrVisibility, } from '../utils/cli-options.js';
|
|
9
|
+
import { fmt } from '../utils/format.js';
|
|
10
|
+
import { fileExistsRegular, isWithinRoot } from '../utils/fs-safe.js';
|
|
11
|
+
import { extractHtmlTitle } from '../utils/html.js';
|
|
12
|
+
import { printJsonResponse } from '../utils/json.js';
|
|
13
|
+
import { defaultOutput } from '../utils/output.js';
|
|
14
|
+
import { isInteractive, promptConfirm, promptMultiSelect, promptSelect, promptValidatedTextWithIO, } from '../utils/prompts/index.js';
|
|
15
|
+
import { formatFolderTitle, hasControlChars, isPlainObject } from '../utils/strings.js';
|
|
16
|
+
import { getGlobalProfileOption } from '../workflows/auth-context.js';
|
|
17
|
+
import { createPenFromDirectory } from '../workflows/create-pen.js';
|
|
18
|
+
export async function runInitCommand(options = {}) {
|
|
19
|
+
const output = options.output ?? defaultOutput;
|
|
20
|
+
const jsonMode = Boolean(options.json);
|
|
21
|
+
const interactive = !jsonMode && (options.interactive ?? isInteractive());
|
|
22
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
23
|
+
const dirDisplay = options.dirArg?.trim() || '.';
|
|
24
|
+
if (hasControlChars(dirDisplay)) {
|
|
25
|
+
throw new CliError(400, 'Directory path cannot contain control characters.');
|
|
26
|
+
}
|
|
27
|
+
// Pre-validate CLI flags before touching disk or prompting
|
|
28
|
+
const explicitAccess = resolveAccessOrVisibility(options.access, options.visibility);
|
|
29
|
+
let explicitBlocks;
|
|
30
|
+
if (options.blocks !== undefined) {
|
|
31
|
+
explicitBlocks =
|
|
32
|
+
typeof options.blocks === 'string'
|
|
33
|
+
? parseBlocksOption(options.blocks)
|
|
34
|
+
: normalizePenBlocksList(options.blocks);
|
|
35
|
+
}
|
|
36
|
+
assertValidTagCount(options.tags);
|
|
37
|
+
if (options.title !== undefined && hasControlChars(options.title)) {
|
|
38
|
+
throw new CliError(400, 'Pen title cannot contain control characters.');
|
|
39
|
+
}
|
|
40
|
+
const targetDir = path.resolve(cwd, dirDisplay);
|
|
41
|
+
try {
|
|
42
|
+
const stat = await fs.lstat(targetDir);
|
|
43
|
+
if (stat.isSymbolicLink()) {
|
|
44
|
+
throw new CliError(400, `"${dirDisplay}" is a symbolic link. Please choose a regular directory.`);
|
|
45
|
+
}
|
|
46
|
+
if (!stat.isDirectory()) {
|
|
47
|
+
throw new CliError(400, `"${dirDisplay}" already exists and is not a directory.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
if (error.code === 'ENOENT') {
|
|
52
|
+
await fs.mkdir(targetDir, { recursive: true });
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const realTargetDir = await fs.realpath(targetDir);
|
|
59
|
+
const configPath = path.join(realTargetDir, PROJECT_CONFIG_FILENAME);
|
|
60
|
+
const codepenDir = path.join(realTargetDir, CODEPEN_DIR);
|
|
61
|
+
const penConfigPath = path.join(codepenDir, PEN_CONFIG_FILENAME);
|
|
62
|
+
const classicConfigPath = path.join(codepenDir, CLASSIC_CONFIG_FILENAME);
|
|
63
|
+
const autoForce = Boolean(options.force || options.yes);
|
|
64
|
+
const confirmImpl = options.prompts?.confirm ?? promptConfirm;
|
|
65
|
+
const selectImpl = options.prompts?.select ?? promptSelect;
|
|
66
|
+
const multiSelectImpl = options.prompts?.multiSelect ?? promptMultiSelect;
|
|
67
|
+
const textImpl = options.prompts?.text ??
|
|
68
|
+
((question, defaultValue) => promptValidatedTextWithIO(question, {
|
|
69
|
+
defaultValue,
|
|
70
|
+
validate(raw) {
|
|
71
|
+
const trimmed = raw.trim();
|
|
72
|
+
if (hasControlChars(trimmed)) {
|
|
73
|
+
return { ok: false, error: 'Pen title cannot contain control characters.' };
|
|
74
|
+
}
|
|
75
|
+
const value = trimmed || defaultValue || 'Untitled';
|
|
76
|
+
return { ok: true, value, displayValue: value };
|
|
77
|
+
},
|
|
78
|
+
}));
|
|
79
|
+
// Check for existing .cdpn.json
|
|
80
|
+
let existingInfo;
|
|
81
|
+
if (await hasProjectConfigInDir(realTargetDir)) {
|
|
82
|
+
let isLinked = false;
|
|
83
|
+
try {
|
|
84
|
+
const existing = await readProjectConfigFile(realTargetDir);
|
|
85
|
+
isLinked = existing.linked;
|
|
86
|
+
existingInfo = existing.config.info;
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
// Malformed existing .cdpn.json — treat as unlinked overwrite candidate
|
|
90
|
+
}
|
|
91
|
+
if (isLinked && !autoForce) {
|
|
92
|
+
throw new CliError(409, `${realTargetDir} is already linked to a CodePen Pen (${PROJECT_CONFIG_FILENAME}).\n` +
|
|
93
|
+
'Use `cdpn info` to edit metadata, or pass --force to re-initialize locally.');
|
|
94
|
+
}
|
|
95
|
+
if (!isLinked && !autoForce) {
|
|
96
|
+
if (!interactive) {
|
|
97
|
+
throw new CliError(409, `${PROJECT_CONFIG_FILENAME} already exists in ${realTargetDir}. Pass --force (or --yes) to overwrite it.`);
|
|
98
|
+
}
|
|
99
|
+
const overwrite = await confirmImpl(`${PROJECT_CONFIG_FILENAME} already exists in this folder. Overwrite it?`, false);
|
|
100
|
+
if (!overwrite) {
|
|
101
|
+
output.log(fmt.dim('Init aborted.'));
|
|
102
|
+
return {
|
|
103
|
+
initialized: false,
|
|
104
|
+
aborted: true,
|
|
105
|
+
pushed: false,
|
|
106
|
+
pushResult: null,
|
|
107
|
+
rootDir: realTargetDir,
|
|
108
|
+
configPath,
|
|
109
|
+
penConfigPath,
|
|
110
|
+
classicConfigPath: null,
|
|
111
|
+
info: {
|
|
112
|
+
title: existingInfo?.title ?? '',
|
|
113
|
+
description: existingInfo?.description ?? '',
|
|
114
|
+
tags: existingInfo?.tags ?? [],
|
|
115
|
+
access: existingInfo?.access ?? 'Public',
|
|
116
|
+
template: existingInfo?.template ?? false,
|
|
117
|
+
},
|
|
118
|
+
blocks: [],
|
|
119
|
+
writtenFiles: [],
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Detect default title from existing .cdpn.json, index.html <title>, or folder name
|
|
125
|
+
let htmlTitle = null;
|
|
126
|
+
const indexPath = path.join(realTargetDir, 'index.html');
|
|
127
|
+
if (await fileExistsRegular(indexPath)) {
|
|
128
|
+
try {
|
|
129
|
+
const rawHtml = await fs.readFile(indexPath, 'utf8');
|
|
130
|
+
htmlTitle = extractHtmlTitle(rawHtml);
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
htmlTitle = null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const defaultTitle = existingInfo?.title?.trim() ||
|
|
137
|
+
htmlTitle ||
|
|
138
|
+
formatFolderTitle(path.basename(realTargetDir)) ||
|
|
139
|
+
'Untitled';
|
|
140
|
+
// 1. Pen Title (prompted interactively unless --title supplied)
|
|
141
|
+
let title = options.title?.trim();
|
|
142
|
+
if (!title) {
|
|
143
|
+
if (interactive) {
|
|
144
|
+
output.log('');
|
|
145
|
+
title = (await textImpl('Pen title', defaultTitle)).trim() || defaultTitle;
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
title = defaultTitle;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// 2. Pen Visibility (prompted interactively unless --access / --visibility supplied)
|
|
152
|
+
let access;
|
|
153
|
+
if (explicitAccess) {
|
|
154
|
+
access = explicitAccess;
|
|
155
|
+
}
|
|
156
|
+
else if (interactive) {
|
|
157
|
+
const visibilityOptions = PEN_ACCESS_CHOICES.map((c) => ({
|
|
158
|
+
value: c.value,
|
|
159
|
+
label: c.label,
|
|
160
|
+
description: c.value === 'Protected'
|
|
161
|
+
? 'private and password-protected (set password on push)'
|
|
162
|
+
: c.description,
|
|
163
|
+
}));
|
|
164
|
+
const defaultVisIndex = existingInfo?.access
|
|
165
|
+
? Math.max(0, visibilityOptions.findIndex((o) => o.value === existingInfo?.access))
|
|
166
|
+
: 0;
|
|
167
|
+
output.log('');
|
|
168
|
+
access = await selectImpl('Pen visibility', visibilityOptions, defaultVisIndex);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
access = existingInfo?.access ?? 'Public';
|
|
172
|
+
}
|
|
173
|
+
// 3. Blocks multi-select (validate & fingerprint are locked and cannot be unselected)
|
|
174
|
+
let selectedBlocks;
|
|
175
|
+
if (explicitBlocks !== undefined) {
|
|
176
|
+
selectedBlocks = explicitBlocks;
|
|
177
|
+
}
|
|
178
|
+
else if (interactive) {
|
|
179
|
+
const [existingPenBlocks, hasPackageJson, hasPenHtml] = await Promise.all([
|
|
180
|
+
readPenBlocks(realTargetDir, realTargetDir),
|
|
181
|
+
fileExistsRegular(path.join(realTargetDir, 'package.json')),
|
|
182
|
+
fileExistsRegular(path.join(realTargetDir, 'index.pen.html')),
|
|
183
|
+
]);
|
|
184
|
+
const blockOptions = INIT_BLOCK_CHOICES.map((choice) => ({
|
|
185
|
+
value: choice.blockName,
|
|
186
|
+
label: choice.label,
|
|
187
|
+
description: choice.description,
|
|
188
|
+
locked: choice.locked,
|
|
189
|
+
checked: choice.locked ||
|
|
190
|
+
(choice.id === 'packages' && (Boolean(existingPenBlocks?.hasPackages) || hasPackageJson)) ||
|
|
191
|
+
(choice.id === 'classic' && (Boolean(existingPenBlocks?.hasClassic) || hasPenHtml)),
|
|
192
|
+
}));
|
|
193
|
+
output.log('');
|
|
194
|
+
const picked = await multiSelectImpl('Blocks to add', blockOptions);
|
|
195
|
+
selectedBlocks = normalizePenBlocksList(picked);
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
selectedBlocks = normalizePenBlocksList([]);
|
|
199
|
+
}
|
|
200
|
+
// Extra CLI-only metadata fields (not asked in UI)
|
|
201
|
+
const description = options.description !== undefined ? options.description : (existingInfo?.description ?? '');
|
|
202
|
+
const tags = options.tags !== undefined ? options.tags : (existingInfo?.tags ?? []);
|
|
203
|
+
const template = options.template !== undefined ? options.template : (existingInfo?.template ?? false);
|
|
204
|
+
const info = {
|
|
205
|
+
title,
|
|
206
|
+
description,
|
|
207
|
+
tags,
|
|
208
|
+
access,
|
|
209
|
+
template,
|
|
210
|
+
};
|
|
211
|
+
// Verify `.codepen` and config targets are not symlinks / escaping realTargetDir
|
|
212
|
+
try {
|
|
213
|
+
const codepenStat = await fs.lstat(codepenDir);
|
|
214
|
+
if (codepenStat.isSymbolicLink() || !codepenStat.isDirectory()) {
|
|
215
|
+
throw new CliError(400, `${CODEPEN_DIR} must be a regular directory (symlinks are not allowed).`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
if (error.code === 'ENOENT') {
|
|
220
|
+
await fs.mkdir(codepenDir, { recursive: true });
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
throw error;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const realCodepenDir = await fs.realpath(codepenDir);
|
|
227
|
+
if (!isWithinRoot(realCodepenDir, realTargetDir)) {
|
|
228
|
+
throw new CliError(400, `${CODEPEN_DIR} resolves outside the target directory.`);
|
|
229
|
+
}
|
|
230
|
+
let existingPenConfig = null;
|
|
231
|
+
try {
|
|
232
|
+
const penConfigStat = await fs.lstat(penConfigPath);
|
|
233
|
+
if (penConfigStat.isSymbolicLink() || !penConfigStat.isFile()) {
|
|
234
|
+
throw new CliError(400, `${CODEPEN_DIR}/${PEN_CONFIG_FILENAME} must be a regular file.`);
|
|
235
|
+
}
|
|
236
|
+
const rawExisting = await fs.readFile(penConfigPath, 'utf8');
|
|
237
|
+
const parsed = JSON.parse(rawExisting);
|
|
238
|
+
if (isPlainObject(parsed)) {
|
|
239
|
+
existingPenConfig = parsed;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
if (error.code !== 'ENOENT' && error instanceof CliError) {
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
let classicConfigExists = false;
|
|
248
|
+
try {
|
|
249
|
+
const classicStat = await fs.lstat(classicConfigPath);
|
|
250
|
+
if (classicStat.isSymbolicLink() || !classicStat.isFile()) {
|
|
251
|
+
throw new CliError(400, `${CODEPEN_DIR}/${CLASSIC_CONFIG_FILENAME} must be a regular file.`);
|
|
252
|
+
}
|
|
253
|
+
classicConfigExists = true;
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
if (error.code !== 'ENOENT') {
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
// Write `.cdpn.json` (unlinked info-only) and `.codepen/pen.config.json`
|
|
261
|
+
await writeUnlinkedProjectInfo(realTargetDir, info);
|
|
262
|
+
const writtenFiles = [PROJECT_CONFIG_FILENAME];
|
|
263
|
+
const penConfigPayload = buildPenConfigObject(selectedBlocks, { existing: existingPenConfig });
|
|
264
|
+
await fs.writeFile(penConfigPath, `${JSON.stringify(penConfigPayload, null, 2)}\n`, 'utf8');
|
|
265
|
+
writtenFiles.push(`${CODEPEN_DIR}/${PEN_CONFIG_FILENAME}`);
|
|
266
|
+
let createdClassicConfigPath = null;
|
|
267
|
+
if (selectedBlocks.includes('classic-1') && !classicConfigExists) {
|
|
268
|
+
const classicConfig = await buildStarterClassicConfig(realTargetDir);
|
|
269
|
+
await fs.writeFile(classicConfigPath, `${JSON.stringify(classicConfig, null, 2)}\n`, 'utf8');
|
|
270
|
+
createdClassicConfigPath = classicConfigPath;
|
|
271
|
+
writtenFiles.push(`${CODEPEN_DIR}/${CLASSIC_CONFIG_FILENAME}`);
|
|
272
|
+
}
|
|
273
|
+
const result = {
|
|
274
|
+
initialized: true,
|
|
275
|
+
aborted: false,
|
|
276
|
+
pushed: false,
|
|
277
|
+
pushResult: null,
|
|
278
|
+
rootDir: realTargetDir,
|
|
279
|
+
configPath,
|
|
280
|
+
penConfigPath,
|
|
281
|
+
classicConfigPath: createdClassicConfigPath,
|
|
282
|
+
info,
|
|
283
|
+
blocks: selectedBlocks,
|
|
284
|
+
writtenFiles,
|
|
285
|
+
};
|
|
286
|
+
if (jsonMode) {
|
|
287
|
+
printJsonResponse(201, `Initialized CodePen demo "${info.title}" in ${dirDisplay}.`, {
|
|
288
|
+
root_dir: result.rootDir,
|
|
289
|
+
config_path: result.configPath,
|
|
290
|
+
pen_config_path: result.penConfigPath,
|
|
291
|
+
classic_config_path: result.classicConfigPath,
|
|
292
|
+
info: result.info,
|
|
293
|
+
blocks: result.blocks,
|
|
294
|
+
written_files: result.writtenFiles,
|
|
295
|
+
}, output);
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
298
|
+
output.log('');
|
|
299
|
+
output.log(`${fmt.boldGreen('✓')} ${fmt.bold('Initialized CodePen demo')} ${fmt.boldCyan(info.title)} ${fmt.dim(`(${info.access})`)}`);
|
|
300
|
+
output.log(` ${fmt.dim('Folder:')} ${fmt.magenta(realTargetDir)}`);
|
|
301
|
+
output.log(` ${fmt.dim('Blocks:')} ${fmt.cyan(selectedBlocks.join(', '))}`);
|
|
302
|
+
output.log('');
|
|
303
|
+
for (const rel of writtenFiles) {
|
|
304
|
+
output.log(` ${fmt.boldGreen('+')} ${rel}`);
|
|
305
|
+
}
|
|
306
|
+
if (interactive) {
|
|
307
|
+
output.log('');
|
|
308
|
+
const shouldPush = await confirmImpl('Do you want to push this to CodePen right now?', false);
|
|
309
|
+
if (shouldPush) {
|
|
310
|
+
const pushResult = await createPenFromDirectory({
|
|
311
|
+
targetDir: realTargetDir,
|
|
312
|
+
folderDisplay: dirDisplay,
|
|
313
|
+
explicitProfile: options.explicitProfile,
|
|
314
|
+
interactive: true,
|
|
315
|
+
output,
|
|
316
|
+
baseUrl: options.baseUrl,
|
|
317
|
+
env: options.env,
|
|
318
|
+
homeDir: options.homeDir,
|
|
319
|
+
prompts: {
|
|
320
|
+
confirm: confirmImpl,
|
|
321
|
+
text: options.prompts?.text,
|
|
322
|
+
select: selectImpl,
|
|
323
|
+
secret: options.prompts?.secret,
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
result.pushed = Boolean(pushResult);
|
|
327
|
+
result.pushResult = pushResult;
|
|
328
|
+
if (pushResult?.exitCode !== undefined) {
|
|
329
|
+
result.exitCode = pushResult.exitCode;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
output.log(fmt.dim('\nNext steps: run `cdpn serve` to preview locally or `cdpn push` to upload to CodePen.'));
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
output.log(fmt.dim('\nNext steps: run `cdpn serve` to preview locally or `cdpn push` to upload to CodePen.'));
|
|
338
|
+
}
|
|
339
|
+
return result;
|
|
340
|
+
}
|
|
341
|
+
export function registerInitCommand(program) {
|
|
342
|
+
program
|
|
343
|
+
.command('init [dir]')
|
|
344
|
+
.description('Initialize a local folder as a CodePen demo (.cdpn.json + .codepen/pen.config.json)')
|
|
345
|
+
.option('-t, --title <title>', 'Pen title')
|
|
346
|
+
.option('--access <level>', 'Pen visibility: Public, Private, Protected, or Collaborators')
|
|
347
|
+
.option('--visibility <level>', 'Alias for --access (Public, Private, Protected, or Collaborators)')
|
|
348
|
+
.option('--blocks <blocks>', 'Comma-separated CodePen blocks to enable (e.g. packages,classic; validate and fingerprint are always included)')
|
|
349
|
+
.option('-d, --description <text>', 'Pen description (CLI flag only)')
|
|
350
|
+
.option('--tags <tags>', 'Comma-separated tags, up to 5 (CLI flag only)')
|
|
351
|
+
.option('--template [bool]', 'Mark Pen as a CodePen Template: true or false (CLI flag only)')
|
|
352
|
+
.option('--as-template', 'Alias for --template true (CLI flag only)')
|
|
353
|
+
.option('-f, --force', 'Overwrite existing .cdpn.json without confirmation')
|
|
354
|
+
.option('-y, --yes', 'Alias for --force')
|
|
355
|
+
.option('--json', 'Output initialization result as JSON')
|
|
356
|
+
.action(async (dirArg, options, command) => {
|
|
357
|
+
const result = await runInitCommand({
|
|
358
|
+
dirArg,
|
|
359
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
360
|
+
title: options.title,
|
|
361
|
+
access: options.access,
|
|
362
|
+
visibility: options.visibility,
|
|
363
|
+
blocks: options.blocks,
|
|
364
|
+
description: options.description,
|
|
365
|
+
tags: parseTagsOption(options.tags),
|
|
366
|
+
template: parseTemplateCliFlag(options.template, options.asTemplate),
|
|
367
|
+
force: options.force,
|
|
368
|
+
yes: options.yes,
|
|
369
|
+
json: options.json,
|
|
370
|
+
});
|
|
371
|
+
if (result.exitCode !== undefined) {
|
|
372
|
+
process.exitCode = result.exitCode;
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
}
|
package/commands/list.js
CHANGED
|
@@ -1,9 +1,100 @@
|
|
|
1
1
|
import { CodePenClient } from '../api/client.js';
|
|
2
|
+
import { CliError } from '../errors.js';
|
|
2
3
|
import { fmt, formatDate, formatTable } from '../utils/format.js';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { printJsonResponse } from '../utils/json.js';
|
|
5
|
+
import { defaultOutput } from '../utils/output.js';
|
|
6
|
+
import { getGlobalProfileOption, resolveCommandAuth } from '../workflows/auth-context.js';
|
|
5
7
|
const VALID_SORTS = new Set(['created', 'updated', 'popularity', 'title']);
|
|
6
8
|
const VALID_DIRECTIONS = new Set(['asc', 'desc']);
|
|
9
|
+
export async function runListCommand(options = {}) {
|
|
10
|
+
const output = options.output ?? defaultOutput;
|
|
11
|
+
if (options.deployed && options.notDeployed) {
|
|
12
|
+
throw new CliError(400, 'Cannot combine --deployed and --not-deployed.');
|
|
13
|
+
}
|
|
14
|
+
const limit = typeof options.limit === 'number'
|
|
15
|
+
? options.limit
|
|
16
|
+
: Number.parseInt(options.limit ?? '20', 10);
|
|
17
|
+
if (!Number.isInteger(limit) || limit < 1) {
|
|
18
|
+
throw new CliError(400, '--limit must be a positive integer.');
|
|
19
|
+
}
|
|
20
|
+
const sort = (options.sort ?? 'updated').toLowerCase();
|
|
21
|
+
if (!VALID_SORTS.has(sort)) {
|
|
22
|
+
throw new CliError(400, `Invalid --sort value "${options.sort}". Use: created, updated, popularity, or title.`);
|
|
23
|
+
}
|
|
24
|
+
const direction = (options.direction ?? 'desc').toLowerCase();
|
|
25
|
+
if (!VALID_DIRECTIONS.has(direction)) {
|
|
26
|
+
throw new CliError(400, `Invalid --direction value "${options.direction}". Use: asc or desc.`);
|
|
27
|
+
}
|
|
28
|
+
let editor;
|
|
29
|
+
if (options.editor) {
|
|
30
|
+
const lower = options.editor.trim().toLowerCase();
|
|
31
|
+
if (lower === 'v2')
|
|
32
|
+
editor = 'V2';
|
|
33
|
+
else if (lower === 'classic')
|
|
34
|
+
editor = 'Classic';
|
|
35
|
+
else
|
|
36
|
+
throw new CliError(400, `Invalid --editor value "${options.editor}". Use: V2 or Classic.`);
|
|
37
|
+
}
|
|
38
|
+
const deployed = options.deployed ? true : options.notDeployed ? false : undefined;
|
|
39
|
+
const template = options.templates ? true : undefined;
|
|
40
|
+
const auth = await resolveCommandAuth({
|
|
41
|
+
explicitProfile: options.explicitProfile,
|
|
42
|
+
interactive: options.json ? false : options.interactive,
|
|
43
|
+
promptMessage: 'Which CodePen profile would you like to list Pens for?',
|
|
44
|
+
output,
|
|
45
|
+
env: options.env,
|
|
46
|
+
homeDir: options.homeDir,
|
|
47
|
+
selectProfile: options.selectProfile,
|
|
48
|
+
});
|
|
49
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
50
|
+
const pens = [];
|
|
51
|
+
let pageResult = await client.listPens({
|
|
52
|
+
per_page: Math.min(limit, 100),
|
|
53
|
+
sort: sort,
|
|
54
|
+
direction: direction,
|
|
55
|
+
search_term: options.search,
|
|
56
|
+
access: options.access,
|
|
57
|
+
deployed,
|
|
58
|
+
template,
|
|
59
|
+
tags: options.tags,
|
|
60
|
+
editor,
|
|
61
|
+
});
|
|
62
|
+
pens.push(...pageResult.items);
|
|
63
|
+
while (pens.length < limit && pageResult.links.next) {
|
|
64
|
+
pageResult = await client.listPensPageByUrl(pageResult.links.next);
|
|
65
|
+
pens.push(...pageResult.items);
|
|
66
|
+
}
|
|
67
|
+
const sliced = pens.slice(0, limit);
|
|
68
|
+
const hasMore = Boolean(pageResult.links.next) || pens.length > limit;
|
|
69
|
+
if (options.json) {
|
|
70
|
+
const message = sliced.length === 0 ? 'No matching Pens found.' : `Found ${sliced.length} Pen(s).`;
|
|
71
|
+
printJsonResponse(200, message, sliced, output);
|
|
72
|
+
return { pens: sliced, hasMore };
|
|
73
|
+
}
|
|
74
|
+
if (sliced.length === 0) {
|
|
75
|
+
output.log(fmt.dim('No matching Pens found.'));
|
|
76
|
+
return { pens: sliced, hasMore };
|
|
77
|
+
}
|
|
78
|
+
const rows = sliced.map((pen) => {
|
|
79
|
+
const editorBadge = pen.editor === 'V2' ? fmt.green('V2') : fmt.dim('Classic');
|
|
80
|
+
const deployBadge = pen.deployment ? fmt.cyan('live') : fmt.dim('—');
|
|
81
|
+
const accessLabel = pen.template ? `${pen.access} ${fmt.dim('(template)')}` : pen.access;
|
|
82
|
+
const title = (pen.title || 'Untitled').slice(0, 36);
|
|
83
|
+
return [
|
|
84
|
+
fmt.bold(title),
|
|
85
|
+
editorBadge,
|
|
86
|
+
accessLabel,
|
|
87
|
+
deployBadge,
|
|
88
|
+
fmt.dim(formatDate(pen.updated_at)),
|
|
89
|
+
fmt.cyan(pen.urls.editor),
|
|
90
|
+
];
|
|
91
|
+
});
|
|
92
|
+
output.log(formatTable(['Title', 'Editor', 'Access', 'Live', 'Updated', 'URL'], rows));
|
|
93
|
+
if (hasMore) {
|
|
94
|
+
output.log(fmt.dim(`\nShowing ${sliced.length} Pen(s). Pass --limit <n> to see more.`));
|
|
95
|
+
}
|
|
96
|
+
return { pens: sliced, hasMore };
|
|
97
|
+
}
|
|
7
98
|
export function registerListCommand(program) {
|
|
8
99
|
program
|
|
9
100
|
.command('list')
|
|
@@ -21,83 +112,9 @@ export function registerListCommand(program) {
|
|
|
21
112
|
.option('-n, --limit <count>', 'Maximum number of Pens to show', '20')
|
|
22
113
|
.option('--json', 'Output Pens as JSON')
|
|
23
114
|
.action(async (options, command) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
const limit = Number.parseInt(options.limit ?? '20', 10);
|
|
28
|
-
if (Number.isNaN(limit) || limit < 1) {
|
|
29
|
-
throw new CliError(400, '--limit must be a positive integer.');
|
|
30
|
-
}
|
|
31
|
-
const sort = (options.sort ?? 'updated').toLowerCase();
|
|
32
|
-
if (!VALID_SORTS.has(sort)) {
|
|
33
|
-
throw new CliError(400, `Invalid --sort value "${options.sort}". Use: created, updated, popularity, or title.`);
|
|
34
|
-
}
|
|
35
|
-
const direction = (options.direction ?? 'desc').toLowerCase();
|
|
36
|
-
if (!VALID_DIRECTIONS.has(direction)) {
|
|
37
|
-
throw new CliError(400, `Invalid --direction value "${options.direction}". Use: asc or desc.`);
|
|
38
|
-
}
|
|
39
|
-
let editor;
|
|
40
|
-
if (options.editor) {
|
|
41
|
-
const lower = options.editor.trim().toLowerCase();
|
|
42
|
-
if (lower === 'v2')
|
|
43
|
-
editor = 'V2';
|
|
44
|
-
else if (lower === 'classic')
|
|
45
|
-
editor = 'Classic';
|
|
46
|
-
else
|
|
47
|
-
throw new CliError(400, `Invalid --editor value "${options.editor}". Use: V2 or Classic.`);
|
|
48
|
-
}
|
|
49
|
-
const deployed = options.deployed ? true : options.notDeployed ? false : undefined;
|
|
50
|
-
const template = options.templates ? true : undefined;
|
|
51
|
-
const auth = await resolveCommandAuth({
|
|
115
|
+
await runListCommand({
|
|
116
|
+
...options,
|
|
52
117
|
explicitProfile: getGlobalProfileOption(command),
|
|
53
|
-
interactive: options.json ? false : undefined,
|
|
54
|
-
promptMessage: 'Which CodePen profile would you like to list Pens for?',
|
|
55
|
-
});
|
|
56
|
-
const client = new CodePenClient({ apiKey: auth.apiKey });
|
|
57
|
-
const pens = [];
|
|
58
|
-
let pageResult = await client.listPens({
|
|
59
|
-
per_page: Math.min(limit, 100),
|
|
60
|
-
sort: sort,
|
|
61
|
-
direction: direction,
|
|
62
|
-
search_term: options.search,
|
|
63
|
-
access: options.access,
|
|
64
|
-
deployed,
|
|
65
|
-
template,
|
|
66
|
-
tags: options.tags,
|
|
67
|
-
editor,
|
|
68
|
-
});
|
|
69
|
-
pens.push(...pageResult.items);
|
|
70
|
-
while (pens.length < limit && pageResult.links.next) {
|
|
71
|
-
pageResult = await client.listPensPageByUrl(pageResult.links.next);
|
|
72
|
-
pens.push(...pageResult.items);
|
|
73
|
-
}
|
|
74
|
-
const sliced = pens.slice(0, limit);
|
|
75
|
-
if (options.json) {
|
|
76
|
-
const message = sliced.length === 0 ? 'No matching Pens found.' : `Found ${sliced.length} Pen(s).`;
|
|
77
|
-
printJsonResponse(200, message, sliced);
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
if (sliced.length === 0) {
|
|
81
|
-
console.log(fmt.dim('No matching Pens found.'));
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const rows = sliced.map((pen) => {
|
|
85
|
-
const editorBadge = pen.editor === 'V2' ? fmt.green('V2') : fmt.dim('Classic');
|
|
86
|
-
const deployBadge = pen.deployment ? fmt.cyan('live') : fmt.dim('—');
|
|
87
|
-
const accessLabel = pen.template ? `${pen.access} ${fmt.dim('(template)')}` : pen.access;
|
|
88
|
-
const title = (pen.title || 'Untitled').slice(0, 36);
|
|
89
|
-
return [
|
|
90
|
-
fmt.bold(title),
|
|
91
|
-
editorBadge,
|
|
92
|
-
accessLabel,
|
|
93
|
-
deployBadge,
|
|
94
|
-
fmt.dim(formatDate(pen.updated_at)),
|
|
95
|
-
fmt.cyan(pen.urls.editor),
|
|
96
|
-
];
|
|
97
118
|
});
|
|
98
|
-
console.log(formatTable(['Title', 'Editor', 'Access', 'Live', 'Updated', 'URL'], rows));
|
|
99
|
-
if (pageResult.links.next || pens.length > limit) {
|
|
100
|
-
console.log(fmt.dim(`\nShowing ${sliced.length} Pen(s). Pass --limit <n> to see more.`));
|
|
101
|
-
}
|
|
102
119
|
});
|
|
103
120
|
}
|