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/fork.js
CHANGED
|
@@ -1,314 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { CodePenApiError } from '../api/errors.js';
|
|
5
|
-
import { buildSyncedFileMap, hasProjectConfigInDir, normalizePenReference, slugifyTitle, suggestFolderName, writeProjectConfig, } from '../config/project.js';
|
|
6
|
-
import { isDirectoryNonEmpty } from '../sync/fs.js';
|
|
7
|
-
import { buildPenInfoConfigFields } from '../sync/info.js';
|
|
8
|
-
import { syncRemoteFilesToDisk } from '../sync/materialize.js';
|
|
9
|
-
import { fmt, printBuildSummary, shortId } from '../utils/format.js';
|
|
10
|
-
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
11
|
-
import { isInteractive, promptConfirm, promptSecret, promptSelect, promptText, } from '../utils/prompts.js';
|
|
12
|
-
import { getGlobalProfileOption, resolveCommandAuth } from './auth-context.js';
|
|
13
|
-
import { normalizeAccess } from './create-pen.js';
|
|
14
|
-
import { assertEmptyTargetDirectory } from './scaffold.js';
|
|
15
|
-
import { findUnmatchedStoredOwner, handlePenNotFoundOnFetch, handleUnownedPenAccess, } from './unowned-pen.js';
|
|
16
|
-
const FORK_NON_EMPTY_HINT = 'Choose a different target folder (e.g. `cdpn fork <url> my-fork`) or remove existing files first.';
|
|
17
|
-
async function isPathOccupied(candidatePath) {
|
|
18
|
-
try {
|
|
19
|
-
const stat = await fs.lstat(candidatePath);
|
|
20
|
-
if (!stat.isDirectory())
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
catch (error) {
|
|
24
|
-
if (error.code === 'ENOENT') {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
throw error;
|
|
28
|
-
}
|
|
29
|
-
return (await hasProjectConfigInDir(candidatePath)) || (await isDirectoryNonEmpty(candidatePath));
|
|
30
|
-
}
|
|
1
|
+
import { parseTagsOption } from '../utils/cli-options.js';
|
|
2
|
+
import { getGlobalProfileOption } from '../workflows/auth-context.js';
|
|
3
|
+
import { forkPenWorkflow, } from '../workflows/fork-pen.js';
|
|
31
4
|
/**
|
|
32
|
-
*
|
|
33
|
-
* `-fork` (or `-fork-2`, etc.) if the original Pen's folder name is already occupied.
|
|
5
|
+
* Forks an existing CodePen Pen and downloads the new fork into a local linked folder.
|
|
34
6
|
*/
|
|
35
|
-
async function
|
|
36
|
-
|
|
37
|
-
return baseName;
|
|
38
|
-
}
|
|
39
|
-
const withFork = `${baseName}-fork`;
|
|
40
|
-
if (!(await isPathOccupied(path.resolve(cwd, withFork)))) {
|
|
41
|
-
return withFork;
|
|
42
|
-
}
|
|
43
|
-
for (let i = 2; i <= 100; i++) {
|
|
44
|
-
const candidate = `${baseName}-fork-${i}`;
|
|
45
|
-
if (!(await isPathOccupied(path.resolve(cwd, candidate)))) {
|
|
46
|
-
return candidate;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return `${baseName}-fork-${Date.now()}`;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Forks an existing CodePen Pen via `POST /v2/pens/{id}/fork` and downloads the new fork
|
|
53
|
-
* into a local folder linked with `.cdpn.json`.
|
|
54
|
-
*/
|
|
55
|
-
export async function forkPenCommand(options) {
|
|
56
|
-
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
57
|
-
const jsonMode = Boolean(options.json);
|
|
58
|
-
const interactive = (options.interactive ?? isInteractive()) && !jsonMode;
|
|
59
|
-
const textImpl = options.prompts?.text ?? promptText;
|
|
60
|
-
const confirmImpl = options.prompts?.confirm ?? promptConfirm;
|
|
61
|
-
const selectImpl = options.prompts?.select ?? promptSelect;
|
|
62
|
-
const secretImpl = options.prompts?.secret ?? promptSecret;
|
|
63
|
-
const { apiIdentifier, originalUrl } = normalizePenReference(options.urlArg);
|
|
64
|
-
if (options.tags && options.tags.length > 5) {
|
|
65
|
-
throw new CliError(400, 'CodePen allows at most 5 tags per Pen.');
|
|
66
|
-
}
|
|
67
|
-
const explicitAccess = normalizeAccess(options.access);
|
|
68
|
-
if (explicitAccess === 'Protected' && options.password !== undefined && options.password.length < 8) {
|
|
69
|
-
throw new CliError(400, 'Protected Pens require --password with at least 8 characters.');
|
|
70
|
-
}
|
|
71
|
-
if (explicitAccess === 'Protected' && !options.password && !interactive) {
|
|
72
|
-
throw new CliError(400, 'Protected Pens require --password with at least 8 characters.');
|
|
73
|
-
}
|
|
74
|
-
// If target folder was passed explicitly, validate before hitting the network
|
|
75
|
-
let targetDir = null;
|
|
76
|
-
let folderDisplay = options.folderArg?.trim() || '';
|
|
77
|
-
if (folderDisplay) {
|
|
78
|
-
targetDir = path.resolve(cwd, folderDisplay);
|
|
79
|
-
if (!options.allowOverwrite) {
|
|
80
|
-
await assertEmptyTargetDirectory(targetDir, folderDisplay, {
|
|
81
|
-
commandName: 'fork',
|
|
82
|
-
nonEmptyHint: FORK_NON_EMPTY_HINT,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
const clonePastedForkUrl = async (pastedUrl) => {
|
|
87
|
-
const { clonePenCommand } = await import('./clone.js');
|
|
88
|
-
const cloned = await clonePenCommand({
|
|
89
|
-
urlArg: pastedUrl,
|
|
90
|
-
folderArg: options.folderArg,
|
|
91
|
-
cwd,
|
|
92
|
-
baseUrl: options.baseUrl,
|
|
93
|
-
interactive,
|
|
94
|
-
env: options.env,
|
|
95
|
-
homeDir: options.homeDir,
|
|
96
|
-
openBrowser: options.openBrowser,
|
|
97
|
-
allowBrowserFallback: false,
|
|
98
|
-
prompts: {
|
|
99
|
-
text: textImpl,
|
|
100
|
-
confirm: confirmImpl,
|
|
101
|
-
select: selectImpl,
|
|
102
|
-
secret: secretImpl,
|
|
103
|
-
},
|
|
104
|
-
});
|
|
105
|
-
if (!cloned || !cloned.project) {
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
return {
|
|
109
|
-
sourcePen: null,
|
|
110
|
-
pen: cloned.pen,
|
|
111
|
-
versionId: cloned.pen.saved_version?.id ?? null,
|
|
112
|
-
build: null,
|
|
113
|
-
writtenPaths: cloned.writtenPaths,
|
|
114
|
-
auth: cloned.auth,
|
|
115
|
-
project: cloned.project,
|
|
116
|
-
forkedInBrowser: true,
|
|
117
|
-
};
|
|
118
|
-
};
|
|
119
|
-
if (!options.resolvedAuth && !options.sourcePen) {
|
|
120
|
-
const unmatched = await findUnmatchedStoredOwner({
|
|
121
|
-
originalUrl,
|
|
122
|
-
explicitProfile: options.explicitProfile,
|
|
123
|
-
env: options.env,
|
|
124
|
-
homeDir: options.homeDir,
|
|
125
|
-
});
|
|
126
|
-
if (unmatched && originalUrl) {
|
|
127
|
-
const pastedUrl = await handleUnownedPenAccess({
|
|
128
|
-
action: 'fork',
|
|
129
|
-
penUrl: originalUrl,
|
|
130
|
-
urlOwner: unmatched.urlOwner,
|
|
131
|
-
availableProfiles: unmatched.availableProfiles,
|
|
132
|
-
interactive,
|
|
133
|
-
allowBrowserFallback: options.allowBrowserFallback,
|
|
134
|
-
confirmImpl,
|
|
135
|
-
textImpl,
|
|
136
|
-
openBrowserImpl: options.openBrowser,
|
|
137
|
-
});
|
|
138
|
-
if (!pastedUrl) {
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
return clonePastedForkUrl(pastedUrl);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
const auth = options.resolvedAuth ??
|
|
145
|
-
(await resolveCommandAuth({
|
|
146
|
-
explicitProfile: options.explicitProfile,
|
|
147
|
-
urlHint: originalUrl ?? options.urlArg,
|
|
148
|
-
promptMessage: 'Which CodePen profile should be used to fork this Pen?',
|
|
149
|
-
interactive,
|
|
150
|
-
env: options.env,
|
|
151
|
-
homeDir: options.homeDir,
|
|
152
|
-
selectProfile: selectImpl,
|
|
153
|
-
}));
|
|
154
|
-
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
155
|
-
let sourcePen;
|
|
156
|
-
if (options.sourcePen) {
|
|
157
|
-
sourcePen = options.sourcePen;
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
if (!jsonMode) {
|
|
161
|
-
console.log(`Fetching Pen ${fmt.cyan(apiIdentifier)}…`);
|
|
162
|
-
}
|
|
163
|
-
try {
|
|
164
|
-
sourcePen = await client.getPen(apiIdentifier);
|
|
165
|
-
}
|
|
166
|
-
catch (error) {
|
|
167
|
-
if (error instanceof CodePenApiError && error.status === 404) {
|
|
168
|
-
const pastedUrl = await handlePenNotFoundOnFetch({
|
|
169
|
-
action: 'fork',
|
|
170
|
-
apiIdentifier,
|
|
171
|
-
originalUrl,
|
|
172
|
-
auth,
|
|
173
|
-
client,
|
|
174
|
-
interactive,
|
|
175
|
-
allowBrowserFallback: options.allowBrowserFallback,
|
|
176
|
-
confirmImpl,
|
|
177
|
-
textImpl,
|
|
178
|
-
openBrowserImpl: options.openBrowser,
|
|
179
|
-
});
|
|
180
|
-
if (!pastedUrl) {
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
183
|
-
return clonePastedForkUrl(pastedUrl);
|
|
184
|
-
}
|
|
185
|
-
throw error;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
// CodePen API requires a password when forking a Protected Pen (unless access is overridden)
|
|
189
|
-
const effectiveAccess = explicitAccess ?? sourcePen.access;
|
|
190
|
-
let password = options.password;
|
|
191
|
-
if (effectiveAccess === 'Protected' && (!password || password.length < 8)) {
|
|
192
|
-
if (!interactive) {
|
|
193
|
-
throw new CliError(400, 'Forking a Protected Pen requires --password with at least 8 characters (or pass --access Public/Private).');
|
|
194
|
-
}
|
|
195
|
-
for (;;) {
|
|
196
|
-
password = await secretImpl('Password for Protected fork (min 8 characters)');
|
|
197
|
-
if (password.length >= 8)
|
|
198
|
-
break;
|
|
199
|
-
console.log(fmt.yellow('Password must be at least 8 characters.'));
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
const title = options.title?.trim() || undefined;
|
|
203
|
-
// Determine target folder if omitted (asked interactively or generated cleanly) BEFORE calling fork
|
|
204
|
-
if (!targetDir) {
|
|
205
|
-
const baseName = (title ? slugifyTitle(title) : null) ?? suggestFolderName(sourcePen);
|
|
206
|
-
const defaultFolder = await suggestAvailableForkFolder(cwd, baseName);
|
|
207
|
-
if (interactive) {
|
|
208
|
-
const chosen = (await textImpl('Target folder', defaultFolder)).trim();
|
|
209
|
-
folderDisplay = chosen || defaultFolder;
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
folderDisplay = defaultFolder;
|
|
213
|
-
}
|
|
214
|
-
targetDir = path.resolve(cwd, folderDisplay);
|
|
215
|
-
await assertEmptyTargetDirectory(targetDir, folderDisplay, {
|
|
216
|
-
commandName: 'fork',
|
|
217
|
-
nonEmptyHint: FORK_NON_EMPTY_HINT,
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
const shouldWait = options.wait !== false;
|
|
221
|
-
if (!jsonMode) {
|
|
222
|
-
console.log(`\nForking ${fmt.bold(sourcePen.title || 'Untitled')} ${fmt.dim(`(${sourcePen.id})`)}…`);
|
|
223
|
-
}
|
|
224
|
-
const forked = await client.forkPen(sourcePen.id, {
|
|
225
|
-
...(title ? { title } : {}),
|
|
226
|
-
...(options.description ? { description: options.description } : {}),
|
|
227
|
-
...(options.tags && options.tags.length > 0 ? { tags: options.tags } : {}),
|
|
228
|
-
...(explicitAccess ? { access: explicitAccess } : {}),
|
|
229
|
-
...(effectiveAccess === 'Protected' && password ? { password } : {}),
|
|
230
|
-
wait: shouldWait ? 45 : 0,
|
|
231
|
-
});
|
|
232
|
-
let finalBuild = forked.build ?? null;
|
|
233
|
-
if (finalBuild && shouldWait) {
|
|
234
|
-
finalBuild = await client.settleBuild(finalBuild);
|
|
235
|
-
if (!jsonMode) {
|
|
236
|
-
printBuildSummary(finalBuild);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
await fs.mkdir(targetDir, { recursive: true });
|
|
240
|
-
const finalVersionId = finalBuild?.version_id ?? forked.saved_version?.id ?? null;
|
|
241
|
-
const remoteFiles = finalVersionId
|
|
242
|
-
? (await client.getVersion(finalVersionId)).files ?? (await client.getPenFiles(forked.id))
|
|
243
|
-
: await client.getPenFiles(forked.id);
|
|
244
|
-
const materialized = await syncRemoteFilesToDisk({
|
|
245
|
-
client,
|
|
246
|
-
rootDir: targetDir,
|
|
247
|
-
remoteFiles,
|
|
248
|
-
pruneRemoved: false,
|
|
249
|
-
});
|
|
250
|
-
const config = {
|
|
251
|
-
url: forked.urls.editor,
|
|
252
|
-
pen_id: forked.id,
|
|
253
|
-
...(auth.profile ? { profile: auth.profile } : {}),
|
|
254
|
-
last_synced_version_id: finalVersionId,
|
|
255
|
-
editor: forked.editor,
|
|
256
|
-
...buildPenInfoConfigFields(forked),
|
|
257
|
-
files: buildSyncedFileMap(remoteFiles, materialized.localSha256ByPath),
|
|
258
|
-
};
|
|
259
|
-
const configPath = await writeProjectConfig(targetDir, config);
|
|
260
|
-
const buildFailed = finalBuild?.status === 'Failed';
|
|
261
|
-
if (buildFailed) {
|
|
262
|
-
process.exitCode = 1;
|
|
263
|
-
}
|
|
264
|
-
if (jsonMode) {
|
|
265
|
-
const code = buildFailed ? 422 : 201;
|
|
266
|
-
const message = buildFailed
|
|
267
|
-
? `Forked ${forked.title || 'Untitled'}, but the initial build failed.`
|
|
268
|
-
: `Forked ${forked.title || 'Untitled'} into ${folderDisplay}.`;
|
|
269
|
-
printJsonResponse(code, message, {
|
|
270
|
-
pen: forked,
|
|
271
|
-
source_pen: sourcePen,
|
|
272
|
-
profile: auth.profile,
|
|
273
|
-
folder: folderDisplay,
|
|
274
|
-
root_dir: targetDir,
|
|
275
|
-
config_path: configPath,
|
|
276
|
-
version_id: finalVersionId,
|
|
277
|
-
build: finalBuild,
|
|
278
|
-
written_paths: materialized.writtenPaths,
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
else {
|
|
282
|
-
if (buildFailed) {
|
|
283
|
-
console.log(`\n${fmt.yellow('!')} Forked ${fmt.bold(forked.title || 'Untitled')} ${fmt.dim(`(${forked.id})`)}, but the initial build failed.`);
|
|
284
|
-
}
|
|
285
|
-
else {
|
|
286
|
-
console.log(`\n${fmt.green('✓')} Forked ${fmt.bold(forked.title || 'Untitled')} ${fmt.dim(`(${forked.id})`)}`);
|
|
287
|
-
}
|
|
288
|
-
console.log(` From: ${fmt.dim(sourcePen.urls.editor)}`);
|
|
289
|
-
if (auth.profile) {
|
|
290
|
-
console.log(` Profile: ${fmt.cyan(auth.profile)}`);
|
|
291
|
-
}
|
|
292
|
-
console.log(` Visibility: ${forked.access}`);
|
|
293
|
-
console.log(` Folder: ${fmt.cyan(folderDisplay)}`);
|
|
294
|
-
console.log(` Files: ${materialized.writtenPaths.length} downloaded`);
|
|
295
|
-
console.log(` Editor: ${fmt.cyan(forked.urls.editor)}`);
|
|
296
|
-
console.log(` Preview: ${fmt.cyan(forked.urls.preview)}`);
|
|
297
|
-
console.log(` Version: ${fmt.dim(shortId(finalVersionId))}`);
|
|
298
|
-
}
|
|
299
|
-
return {
|
|
300
|
-
sourcePen,
|
|
301
|
-
pen: forked,
|
|
302
|
-
versionId: finalVersionId,
|
|
303
|
-
build: finalBuild,
|
|
304
|
-
writtenPaths: materialized.writtenPaths,
|
|
305
|
-
auth,
|
|
306
|
-
project: {
|
|
307
|
-
rootDir: targetDir,
|
|
308
|
-
configPath,
|
|
309
|
-
config,
|
|
310
|
-
},
|
|
311
|
-
};
|
|
7
|
+
export async function runForkCommand(options) {
|
|
8
|
+
return forkPenWorkflow(options);
|
|
312
9
|
}
|
|
313
10
|
export function registerForkCommand(program) {
|
|
314
11
|
program
|
|
@@ -318,27 +15,26 @@ export function registerForkCommand(program) {
|
|
|
318
15
|
.option('-d, --description <text>', 'Description for the forked Pen')
|
|
319
16
|
.option('--tags <tags>', 'Comma-separated tags (up to 5)')
|
|
320
17
|
.option('--access <level>', 'Privacy: Public, Private, Protected, or Collaborators')
|
|
18
|
+
.option('--visibility <level>', 'Alias for --access')
|
|
321
19
|
.option('--password <password>', 'Password (min 8 chars; required for Protected forks)')
|
|
322
20
|
.option('--no-wait', 'Return as soon as files are saved without waiting for the build')
|
|
323
21
|
.option('--json', 'Output fork result as JSON')
|
|
324
22
|
.action(async (urlArg, folderArg, options, command) => {
|
|
325
|
-
const
|
|
326
|
-
? options.tags
|
|
327
|
-
.split(',')
|
|
328
|
-
.map((t) => t.trim())
|
|
329
|
-
.filter(Boolean)
|
|
330
|
-
: undefined;
|
|
331
|
-
await forkPenCommand({
|
|
23
|
+
const result = await runForkCommand({
|
|
332
24
|
urlArg,
|
|
333
25
|
folderArg,
|
|
334
26
|
explicitProfile: getGlobalProfileOption(command),
|
|
335
27
|
title: options.title,
|
|
336
28
|
description: options.description,
|
|
337
|
-
tags,
|
|
29
|
+
tags: parseTagsOption(options.tags),
|
|
338
30
|
access: options.access,
|
|
31
|
+
visibility: options.visibility,
|
|
339
32
|
password: options.password,
|
|
340
33
|
wait: options.wait,
|
|
341
34
|
json: options.json,
|
|
342
35
|
});
|
|
36
|
+
if (result?.exitCode !== undefined) {
|
|
37
|
+
process.exitCode = result.exitCode;
|
|
38
|
+
}
|
|
343
39
|
});
|
|
344
40
|
}
|
package/commands/info.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { CodePenClient } from '../api/client.js';
|
|
2
|
+
import { requireProject, writeProjectConfig } from '../config/project.js';
|
|
3
|
+
import { MIN_PROTECTED_PASSWORD_LENGTH } from '../constants.js';
|
|
4
|
+
import { CliError } from '../errors.js';
|
|
5
|
+
import { mergeUpdatedPenInfoIntoConfig } from '../sync/info.js';
|
|
6
|
+
import { assertValidTagCount, parseBooleanOption, parseTagsOption, resolveAccessOrVisibility, } from '../utils/cli-options.js';
|
|
7
|
+
import { fmt, formatDate, shortId } from '../utils/format.js';
|
|
8
|
+
import { printJsonResponse } from '../utils/json.js';
|
|
9
|
+
import { defaultOutput } from '../utils/output.js';
|
|
10
|
+
import { isInteractive } from '../utils/prompts/index.js';
|
|
11
|
+
import { getGlobalProfileOption, resolveCommandAuth } from '../workflows/auth-context.js';
|
|
12
|
+
import { fetchLinkedPenOrHandleMissing, handleMissingRemotePen, isRemotePenNotFoundError, } from '../workflows/missing-pen.js';
|
|
13
|
+
function renderPenInfo(pen, profile, output) {
|
|
14
|
+
output.log(`${fmt.bold(pen.title || 'Untitled')} ${fmt.dim(`(${pen.id})`)}`);
|
|
15
|
+
if (profile) {
|
|
16
|
+
output.log(`Profile: ${fmt.cyan(profile)}`);
|
|
17
|
+
}
|
|
18
|
+
if (pen.description) {
|
|
19
|
+
output.log(`Description: ${pen.description}`);
|
|
20
|
+
}
|
|
21
|
+
output.log(`Editor: ${pen.editor}`);
|
|
22
|
+
output.log(`Access: ${pen.access}${pen.template ? fmt.dim(' · Template') : ''}`);
|
|
23
|
+
output.log(`Tags: ${pen.tags.length > 0 ? pen.tags.join(', ') : fmt.dim('none')}`);
|
|
24
|
+
output.log(`Stats: ${pen.views_count} views · ${pen.loves_count} loves`);
|
|
25
|
+
output.log(`Editor URL: ${fmt.cyan(pen.urls.editor)}`);
|
|
26
|
+
output.log(`Preview URL: ${fmt.cyan(pen.urls.preview)}`);
|
|
27
|
+
if (pen.deployment) {
|
|
28
|
+
output.log(`Deployed: ${fmt.cyan(pen.deployment.url)} ${fmt.dim(`(v ${shortId(pen.deployment.version_id)})`)}`);
|
|
29
|
+
if (pen.deployment.custom_domains.length > 0) {
|
|
30
|
+
output.log(`Domains: ${pen.deployment.custom_domains.join(', ')}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
output.log(`Deployed: ${fmt.dim('not deployed')}`);
|
|
35
|
+
}
|
|
36
|
+
output.log(`Created: ${fmt.dim(formatDate(pen.created_at))}`);
|
|
37
|
+
output.log(`Updated: ${fmt.dim(formatDate(pen.updated_at))}`);
|
|
38
|
+
}
|
|
39
|
+
export async function runInfoCommand(options = {}) {
|
|
40
|
+
const output = options.output ?? defaultOutput;
|
|
41
|
+
const interactive = (options.interactive ?? isInteractive()) && !options.json;
|
|
42
|
+
const project = await requireProject(options.cwd ?? process.cwd());
|
|
43
|
+
const auth = await resolveCommandAuth({
|
|
44
|
+
explicitProfile: options.explicitProfile,
|
|
45
|
+
project,
|
|
46
|
+
interactive,
|
|
47
|
+
persistProfileToProject: !options.json,
|
|
48
|
+
output,
|
|
49
|
+
env: options.env,
|
|
50
|
+
homeDir: options.homeDir,
|
|
51
|
+
selectProfile: options.selectProfile,
|
|
52
|
+
});
|
|
53
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
54
|
+
const patch = {};
|
|
55
|
+
if (options.title !== undefined)
|
|
56
|
+
patch.title = options.title;
|
|
57
|
+
if (options.description !== undefined)
|
|
58
|
+
patch.description = options.description;
|
|
59
|
+
if (options.tags !== undefined) {
|
|
60
|
+
const parsedTags = parseTagsOption(options.tags) ?? [];
|
|
61
|
+
assertValidTagCount(parsedTags);
|
|
62
|
+
patch.tags = parsedTags;
|
|
63
|
+
}
|
|
64
|
+
const resolvedAccess = resolveAccessOrVisibility(options.access, options.visibility);
|
|
65
|
+
if (resolvedAccess !== undefined) {
|
|
66
|
+
patch.access = resolvedAccess;
|
|
67
|
+
}
|
|
68
|
+
if (options.password !== undefined) {
|
|
69
|
+
if (options.password.length < MIN_PROTECTED_PASSWORD_LENGTH) {
|
|
70
|
+
throw new CliError(400, `Password must be at least ${MIN_PROTECTED_PASSWORD_LENGTH} characters.`);
|
|
71
|
+
}
|
|
72
|
+
patch.password = options.password;
|
|
73
|
+
}
|
|
74
|
+
if (options.template !== undefined) {
|
|
75
|
+
patch.template = parseBooleanOption(options.template, '--template');
|
|
76
|
+
}
|
|
77
|
+
const hasUpdates = Object.keys(patch).length > 0;
|
|
78
|
+
let pen;
|
|
79
|
+
if (hasUpdates) {
|
|
80
|
+
try {
|
|
81
|
+
pen = await client.updatePen(project.config.pen_id, patch);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (!isRemotePenNotFoundError(error)) {
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
const resolution = await handleMissingRemotePen({
|
|
88
|
+
client,
|
|
89
|
+
project,
|
|
90
|
+
auth,
|
|
91
|
+
commandName: 'info',
|
|
92
|
+
interactive,
|
|
93
|
+
json: options.json,
|
|
94
|
+
output,
|
|
95
|
+
prompts: options.prompts,
|
|
96
|
+
});
|
|
97
|
+
if (resolution.action !== 'recovered') {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
pen = await client.updatePen(project.config.pen_id, patch);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
const penLookup = await fetchLinkedPenOrHandleMissing({
|
|
105
|
+
client,
|
|
106
|
+
project,
|
|
107
|
+
auth,
|
|
108
|
+
commandName: 'info',
|
|
109
|
+
interactive,
|
|
110
|
+
json: options.json,
|
|
111
|
+
output,
|
|
112
|
+
prompts: options.prompts,
|
|
113
|
+
});
|
|
114
|
+
if (!penLookup.found) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
pen = penLookup.pen;
|
|
118
|
+
}
|
|
119
|
+
if (hasUpdates) {
|
|
120
|
+
const nextConfig = mergeUpdatedPenInfoIntoConfig(project.config, pen, patch);
|
|
121
|
+
await writeProjectConfig(project.rootDir, nextConfig);
|
|
122
|
+
project.config = nextConfig;
|
|
123
|
+
}
|
|
124
|
+
if (options.json) {
|
|
125
|
+
printJsonResponse(200, hasUpdates ? 'Updated Pen metadata.' : 'Fetched Pen metadata.', pen, output);
|
|
126
|
+
return { pen, updated: hasUpdates, auth, project };
|
|
127
|
+
}
|
|
128
|
+
if (hasUpdates) {
|
|
129
|
+
output.log(`${fmt.green('✓')} Updated Pen metadata.\n`);
|
|
130
|
+
}
|
|
131
|
+
renderPenInfo(pen, auth.profile, output);
|
|
132
|
+
return { pen, updated: hasUpdates, auth, project };
|
|
133
|
+
}
|
|
134
|
+
export function registerInfoCommand(program) {
|
|
135
|
+
program
|
|
136
|
+
.command('info')
|
|
137
|
+
.description('Show or update Pen metadata (title, description, tags, access, template)')
|
|
138
|
+
.option('-t, --title <title>', 'Update Pen title')
|
|
139
|
+
.option('-d, --description <text>', 'Update Pen description')
|
|
140
|
+
.option('--tags <tags>', 'Replace tags with comma-separated list (pass "" to clear)')
|
|
141
|
+
.option('--access <level>', 'Update access: Public, Private, Protected, or Collaborators')
|
|
142
|
+
.option('--visibility <level>', 'Alias for --access')
|
|
143
|
+
.option('--password <password>', 'Set/replace password (min 8 chars; for Protected Pens)')
|
|
144
|
+
.option('--template <bool>', 'Set template flag (true/false)')
|
|
145
|
+
.option('--json', 'Output Pen metadata as JSON')
|
|
146
|
+
.action(async (options, command) => {
|
|
147
|
+
await runInfoCommand({
|
|
148
|
+
...options,
|
|
149
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|