codepen-commander 1.0.0 → 1.2.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 +18 -5
- package/commands/clone.js +241 -12
- package/commands/create-pen.js +2 -0
- package/commands/fork.js +2 -0
- package/commands/meta.js +66 -46
- package/commands/new.js +2 -0
- package/commands/privacy.js +5 -1
- package/commands/pull.js +22 -4
- package/commands/push.js +202 -90
- package/commands/status.js +156 -97
- package/commands/versions.js +2 -0
- package/config/project.js +11 -0
- package/package.json +1 -1
- package/sync/info.js +339 -0
package/README.md
CHANGED
|
@@ -80,10 +80,23 @@ Every synced Pen folder contains a `.cdpn.json` file at its root:
|
|
|
80
80
|
"url": "https://codepen.io/username/pen/abcde",
|
|
81
81
|
"pen_id": "01a08738-73f0-7847-b6a8-5789d289c158",
|
|
82
82
|
"profile": "username",
|
|
83
|
-
"last_synced_version_id": "01a08738-73f0-7848-b8bf-e8e7e91b1ae2"
|
|
83
|
+
"last_synced_version_id": "01a08738-73f0-7848-b8bf-e8e7e91b1ae2",
|
|
84
|
+
"last_synced_info_hash": "a1b2c3d4...",
|
|
85
|
+
"editor": "V2",
|
|
86
|
+
"info": {
|
|
87
|
+
"title": "My Awesome Pen",
|
|
88
|
+
"description": "A short description of the Pen",
|
|
89
|
+
"tags": ["css", "demo"],
|
|
90
|
+
"access": "Public",
|
|
91
|
+
"template": false
|
|
92
|
+
}
|
|
84
93
|
}
|
|
85
94
|
```
|
|
86
95
|
|
|
96
|
+
The `info` section tracks the Pen’s editable metadata (`title`, `description`, `tags`, `access`, and `template`) and syncs automatically with `cdpn status`, `cdpn push`, `cdpn pull`, `cdpn info`, and `cdpn privacy`. Edit `info.title`, `info.tags`, etc. directly in `.cdpn.json` and run `cdpn push` to update them on CodePen (passwords for `Protected` Pens are never stored in `.cdpn.json`; use `cdpn privacy protected` or `cdpn info --access Protected --password ...` instead).
|
|
97
|
+
|
|
98
|
+
Older `.cdpn.json` files created without an `info` section remain fully supported: `cdpn` will never overwrite remote metadata when `info` is absent, and will seamlessly populate `info` on your next `cdpn pull` or `cdpn push`.
|
|
99
|
+
|
|
87
100
|
> **Why `.cdpn.json` instead of `.codepen`?**
|
|
88
101
|
> CodePen 2.0 Pens already contain a `.codepen/` directory (housing `.codepen/pen.config.json`). Using `.cdpn.json` avoids filesystem collisions while allowing `.codepen/pen.config.json` to sync cleanly with CodePen.
|
|
89
102
|
|
|
@@ -123,10 +136,10 @@ All commands accept the global `--profile <name>` flag to override the active pr
|
|
|
123
136
|
| `cdpn auth logout [profile] [--all] [--json]` | Remove a specific profile or delete `~/.cdpn` completely |
|
|
124
137
|
| `cdpn new [folder] [--template <id\|url\|title>] [--as-template] [--json]` | Scaffold a brand-new 2.0 Pen from a CodePen Template (or CodePen default starter) into a new or empty folder |
|
|
125
138
|
| `cdpn fork <url> [folder] [--title ...] [--access ...] [--json]` | Fork one of your own 2.0 Pens into a local folder (for other users' Pens, offers to open in browser to fork and clone back) |
|
|
126
|
-
| `cdpn clone <url> [folder] [--force] [--json]` | Clone a CodePen Pen into a local folder (auto-matches stored profile by URL owner; offers browser fork-and-clone fallback for unowned Pens) |
|
|
127
|
-
| `cdpn pull [--force] [--json]` | Pull latest remote changes into an existing cloned folder (requires `.cdpn.json`) |
|
|
128
|
-
| `cdpn status [--json]` | Show active profile, file diffs
|
|
129
|
-
| `cdpn push [-m <label>] [--force] [--dry-run] [--no-wait] [--json]` | Push local changes (text & binary + deletions), or push an unlinked local demo folder as a new Pen (checks `index.html`, asks account, title, and visibility) |
|
|
139
|
+
| `cdpn clone <url> [folder] [--force] [--json]` | Clone a CodePen Pen into a local folder (auto-matches stored profile by URL owner; offers to convert Classic Pens or clone them unlinked; offers browser fork-and-clone fallback for unowned Pens) |
|
|
140
|
+
| `cdpn pull [--force] [--json]` | Pull latest remote file and Pen info changes into an existing cloned folder (requires `.cdpn.json`) |
|
|
141
|
+
| `cdpn status [--json]` | Show active profile, file and Pen info diffs, remote head, build & deployment status |
|
|
142
|
+
| `cdpn push [-m <label>] [--force] [--dry-run] [--no-wait] [--json]` | Push local file and `.cdpn.json` Pen info changes (text & binary + deletions), or push an unlinked local demo folder as a new Pen (checks `index.html`, asks account, title, and visibility) |
|
|
130
143
|
| `cdpn deploy [--no-push] [--version <id>] [--force] [--json]` | Push local changes (creating a Pen first if unlinked) and deploy to CodePen Hosting |
|
|
131
144
|
| `cdpn undeploy [--yes] [--json]` | Take down the live deployment (Pen & versions remain intact) |
|
|
132
145
|
| `cdpn domains [list] [--json]` | Show the default deployed URL, custom domains, verification status, and required DNS records |
|
package/commands/clone.js
CHANGED
|
@@ -5,13 +5,54 @@ import { CodePenApiError } from '../api/errors.js';
|
|
|
5
5
|
import { DEFAULT_PROFILE_NAME, findMatchingProfile } from '../config/auth.js';
|
|
6
6
|
import { buildSyncedFileMap, extractCodePenOwnerFromUrl, hasProjectConfigInDir, normalizePenReference, PROJECT_CONFIG_FILENAME, suggestFolderName, writeProjectConfig, } from '../config/project.js';
|
|
7
7
|
import { isDirectoryNonEmpty } from '../sync/fs.js';
|
|
8
|
+
import { buildPenInfoConfigFields } from '../sync/info.js';
|
|
8
9
|
import { syncRemoteFilesToDisk } from '../sync/materialize.js';
|
|
10
|
+
import { openBrowser, validateBrowserUrl } from '../utils/browser.js';
|
|
9
11
|
import { fmt, shortId } from '../utils/format.js';
|
|
10
12
|
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
11
13
|
import { isInteractive, promptConfirm, promptSecret, promptSelect, promptText, } from '../utils/prompts.js';
|
|
12
14
|
import { getGlobalProfileOption, resolveCommandAuth } from './auth-context.js';
|
|
13
15
|
import { forkPenCommand } from './fork.js';
|
|
14
16
|
import { findUnmatchedStoredOwner, handlePenNotFoundOnFetch, handleUnownedPenAccess, } from './unowned-pen.js';
|
|
17
|
+
function validateEditorBrowserUrl(rawUrl) {
|
|
18
|
+
let candidate = rawUrl.trim();
|
|
19
|
+
try {
|
|
20
|
+
const parsed = new URL(candidate);
|
|
21
|
+
if (parsed.protocol === 'http:') {
|
|
22
|
+
parsed.protocol = 'https:';
|
|
23
|
+
}
|
|
24
|
+
candidate = parsed.toString();
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// Let validateBrowserUrl throw a clean validation error
|
|
28
|
+
}
|
|
29
|
+
const safeUrl = validateBrowserUrl(candidate);
|
|
30
|
+
const host = new URL(safeUrl).hostname.toLowerCase();
|
|
31
|
+
const isCodePenHost = host === 'codepen.io' || host.endsWith('.codepen.io');
|
|
32
|
+
if (!isCodePenHost) {
|
|
33
|
+
throw new CliError(400, `Refusing to open non-CodePen URL in browser: ${host}`);
|
|
34
|
+
}
|
|
35
|
+
return safeUrl;
|
|
36
|
+
}
|
|
37
|
+
async function promptClassicRetryChoice(selectImpl) {
|
|
38
|
+
return selectImpl('How would you like to proceed?', [
|
|
39
|
+
{
|
|
40
|
+
value: 'retry',
|
|
41
|
+
label: 'Check again / paste 2.0 Pen URL',
|
|
42
|
+
description: 're-fetch after converting or enter the new URL',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
value: 'clone-unlinked',
|
|
46
|
+
label: 'Just clone without linking',
|
|
47
|
+
description: `download files without ${PROJECT_CONFIG_FILENAME}`,
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
value: 'cancel',
|
|
51
|
+
label: 'Cancel',
|
|
52
|
+
description: 'abort clone',
|
|
53
|
+
},
|
|
54
|
+
], 0);
|
|
55
|
+
}
|
|
15
56
|
async function resolveCurrentUsername(client, auth, penOwner) {
|
|
16
57
|
// Fast path: named profile already matches the Pen owner
|
|
17
58
|
if (auth.profile &&
|
|
@@ -38,6 +79,162 @@ async function resolveCurrentUsername(client, auth, penOwner) {
|
|
|
38
79
|
}
|
|
39
80
|
return null;
|
|
40
81
|
}
|
|
82
|
+
async function handleClassicPenClone(params) {
|
|
83
|
+
const { pen, apiIdentifier, effectiveUrl, options, selectImpl, textImpl } = params;
|
|
84
|
+
let currentAuth = params.auth;
|
|
85
|
+
let currentClient = params.client;
|
|
86
|
+
let fallbackPen = pen;
|
|
87
|
+
const makeFallbackResult = (action) => ({
|
|
88
|
+
action,
|
|
89
|
+
pen: fallbackPen,
|
|
90
|
+
effectiveUrl,
|
|
91
|
+
auth: currentAuth,
|
|
92
|
+
client: currentClient,
|
|
93
|
+
isOwnedByAnotherUser: false,
|
|
94
|
+
penOwner: null,
|
|
95
|
+
currentUsername: null,
|
|
96
|
+
});
|
|
97
|
+
console.log(`\n${fmt.yellow('This is a Classic (v1) Pen, which is read-only through CodePen API v2 and cannot be synced.')}`);
|
|
98
|
+
console.log('You can convert it to a CodePen 2.0 Pen in the browser using the dropdown on the Save button.\n');
|
|
99
|
+
const initialChoice = await selectImpl('How would you like to proceed?', [
|
|
100
|
+
{
|
|
101
|
+
value: 'convert',
|
|
102
|
+
label: 'Open the Pen to convert it first',
|
|
103
|
+
description: 'open in browser, then clone linked',
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
value: 'clone-unlinked',
|
|
107
|
+
label: 'Just clone without linking',
|
|
108
|
+
description: `download files without ${PROJECT_CONFIG_FILENAME}`,
|
|
109
|
+
},
|
|
110
|
+
], 0);
|
|
111
|
+
if (initialChoice === 'clone-unlinked') {
|
|
112
|
+
return makeFallbackResult('clone-unlinked');
|
|
113
|
+
}
|
|
114
|
+
const editorUrl = validateEditorBrowserUrl(pen.urls.editor || effectiveUrl);
|
|
115
|
+
const openImpl = options.openBrowser ?? openBrowser;
|
|
116
|
+
const opened = await openImpl(editorUrl);
|
|
117
|
+
if (opened) {
|
|
118
|
+
console.log(`\n${fmt.green('✓')} Opened ${fmt.cyan(editorUrl)} in your browser.`);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
console.log(`\n${fmt.yellow('!')} Could not launch a browser automatically. Open this URL manually:\n ${fmt.cyan(editorUrl)}`);
|
|
122
|
+
}
|
|
123
|
+
console.log(fmt.dim('In CodePen, use the dropdown on the Save button to convert this Pen to CodePen 2.0.'));
|
|
124
|
+
for (;;) {
|
|
125
|
+
console.log('');
|
|
126
|
+
const pasted = (await textImpl('Press Enter once converted to re-fetch & clone (or paste the new 2.0 Pen URL)')).trim();
|
|
127
|
+
let targetApiId = apiIdentifier;
|
|
128
|
+
let pastedOriginalUrl;
|
|
129
|
+
if (pasted) {
|
|
130
|
+
try {
|
|
131
|
+
const normalized = normalizePenReference(pasted);
|
|
132
|
+
targetApiId = normalized.apiIdentifier;
|
|
133
|
+
pastedOriginalUrl = normalized.originalUrl;
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
137
|
+
console.log(`\n${fmt.yellow('!')} ${msg}\n`);
|
|
138
|
+
const nextAction = await promptClassicRetryChoice(selectImpl);
|
|
139
|
+
if (nextAction === 'retry')
|
|
140
|
+
continue;
|
|
141
|
+
if (nextAction === 'clone-unlinked')
|
|
142
|
+
return makeFallbackResult('clone-unlinked');
|
|
143
|
+
console.log('Aborted.');
|
|
144
|
+
return makeFallbackResult('cancel');
|
|
145
|
+
}
|
|
146
|
+
if (!options.explicitProfile && currentAuth.source === 'file' && pastedOriginalUrl) {
|
|
147
|
+
const matchedProfile = findMatchingProfile(currentAuth.availableProfiles, pastedOriginalUrl);
|
|
148
|
+
if (matchedProfile && matchedProfile !== currentAuth.profile) {
|
|
149
|
+
currentAuth = await resolveCommandAuth({
|
|
150
|
+
explicitProfile: matchedProfile,
|
|
151
|
+
interactive: true,
|
|
152
|
+
env: options.env,
|
|
153
|
+
homeDir: options.homeDir,
|
|
154
|
+
});
|
|
155
|
+
currentClient = new CodePenClient({ apiKey: currentAuth.apiKey, baseUrl: options.baseUrl });
|
|
156
|
+
console.log(fmt.dim(`Using profile "${matchedProfile}" (matches Pen owner).`));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
console.log(`Fetching Pen ${fmt.cyan(targetApiId)}…`);
|
|
161
|
+
let refetched;
|
|
162
|
+
try {
|
|
163
|
+
refetched = await currentClient.getPen(targetApiId);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
const msg = error instanceof CodePenApiError
|
|
167
|
+
? error.formatForUser()
|
|
168
|
+
: error instanceof Error
|
|
169
|
+
? error.message
|
|
170
|
+
: String(error);
|
|
171
|
+
console.log(`\n${fmt.yellow('!')} Could not fetch Pen ${targetApiId}: ${msg}\n`);
|
|
172
|
+
const nextAction = await promptClassicRetryChoice(selectImpl);
|
|
173
|
+
if (nextAction === 'retry')
|
|
174
|
+
continue;
|
|
175
|
+
if (nextAction === 'clone-unlinked')
|
|
176
|
+
return makeFallbackResult('clone-unlinked');
|
|
177
|
+
console.log('Aborted.');
|
|
178
|
+
return makeFallbackResult('cancel');
|
|
179
|
+
}
|
|
180
|
+
const refetchedOwner = extractCodePenOwnerFromUrl(refetched.urls.editor) ??
|
|
181
|
+
(pastedOriginalUrl ? extractCodePenOwnerFromUrl(pastedOriginalUrl) : null);
|
|
182
|
+
if (refetchedOwner &&
|
|
183
|
+
!options.explicitProfile &&
|
|
184
|
+
currentAuth.source === 'file' &&
|
|
185
|
+
currentAuth.profile?.toLowerCase() !== refetchedOwner.toLowerCase()) {
|
|
186
|
+
const matchedStoredProfile = findMatchingProfile(currentAuth.availableProfiles, refetched.urls.editor);
|
|
187
|
+
if (matchedStoredProfile && matchedStoredProfile !== currentAuth.profile) {
|
|
188
|
+
currentAuth = await resolveCommandAuth({
|
|
189
|
+
explicitProfile: matchedStoredProfile,
|
|
190
|
+
interactive: true,
|
|
191
|
+
env: options.env,
|
|
192
|
+
homeDir: options.homeDir,
|
|
193
|
+
});
|
|
194
|
+
currentClient = new CodePenClient({ apiKey: currentAuth.apiKey, baseUrl: options.baseUrl });
|
|
195
|
+
console.log(fmt.dim(`Using profile "${matchedStoredProfile}" (matches Pen owner).`));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (refetched.editor !== 'Classic') {
|
|
199
|
+
const detectedUsername = refetchedOwner
|
|
200
|
+
? await resolveCurrentUsername(currentClient, currentAuth, refetchedOwner)
|
|
201
|
+
: null;
|
|
202
|
+
const ownedByOther = Boolean(refetchedOwner &&
|
|
203
|
+
detectedUsername &&
|
|
204
|
+
refetchedOwner.toLowerCase() !== detectedUsername.toLowerCase());
|
|
205
|
+
const resolvedUrl = pasted
|
|
206
|
+
? (pastedOriginalUrl ?? refetched.urls.editor)
|
|
207
|
+
: (refetched.urls.editor || effectiveUrl);
|
|
208
|
+
if (!ownedByOther) {
|
|
209
|
+
console.log(`\n${fmt.green('✓')} Pen is now CodePen 2.0 ${fmt.dim(`(${refetched.id})`)} — continuing linked clone.`);
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
action: ownedByOther ? 'clone-unlinked' : 'clone-linked',
|
|
213
|
+
pen: refetched,
|
|
214
|
+
effectiveUrl: resolvedUrl,
|
|
215
|
+
auth: currentAuth,
|
|
216
|
+
client: currentClient,
|
|
217
|
+
isOwnedByAnotherUser: ownedByOther,
|
|
218
|
+
penOwner: refetchedOwner,
|
|
219
|
+
currentUsername: detectedUsername,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (!pasted) {
|
|
223
|
+
fallbackPen = refetched;
|
|
224
|
+
}
|
|
225
|
+
console.log(`\n${fmt.yellow('!')} ${fmt.bold(refetched.title || 'Untitled')} is still a Classic (v1) Pen on CodePen.`);
|
|
226
|
+
console.log(fmt.dim('Make sure you converted it to 2.0 via the Save button dropdown (or paste the new 2.0 Pen URL if CodePen opened a new one).\n'));
|
|
227
|
+
const nextAction = await promptClassicRetryChoice(selectImpl);
|
|
228
|
+
if (nextAction === 'retry') {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if (nextAction === 'clone-unlinked') {
|
|
232
|
+
return makeFallbackResult('clone-unlinked');
|
|
233
|
+
}
|
|
234
|
+
console.log('Aborted.');
|
|
235
|
+
return makeFallbackResult('cancel');
|
|
236
|
+
}
|
|
237
|
+
}
|
|
41
238
|
async function ensureCloneTargetReady(targetDir, folderDisplay, options) {
|
|
42
239
|
try {
|
|
43
240
|
const stat = await fs.lstat(targetDir);
|
|
@@ -74,6 +271,10 @@ async function ensureCloneTargetReady(targetDir, folderDisplay, options) {
|
|
|
74
271
|
* instead (interactively). If not forked, downloads files locally without writing `.cdpn.json`
|
|
75
272
|
* so the local folder is not linked to an online Pen owned by another user.
|
|
76
273
|
*
|
|
274
|
+
* When the Pen is a Classic (v1) Pen (read-only through CodePen API v2), interactively offers
|
|
275
|
+
* a choice to open it in the browser to convert to CodePen 2.0 first (waiting to re-fetch or
|
|
276
|
+
* accept the new 2.0 URL for a linked clone) or to clone it locally without writing `.cdpn.json`.
|
|
277
|
+
*
|
|
77
278
|
* Returns `null` if the user cancels interactively.
|
|
78
279
|
*/
|
|
79
280
|
export async function clonePenCommand(options) {
|
|
@@ -181,7 +382,7 @@ export async function clonePenCommand(options) {
|
|
|
181
382
|
throw error;
|
|
182
383
|
}
|
|
183
384
|
// Check whether this Pen belongs to a stored profile (handles Pen IDs / preview URLs without an owner in the input)
|
|
184
|
-
|
|
385
|
+
let penOwner = extractCodePenOwnerFromUrl(pen.urls.editor) ??
|
|
185
386
|
(originalUrl ? extractCodePenOwnerFromUrl(originalUrl) : null);
|
|
186
387
|
if (penOwner &&
|
|
187
388
|
!options.explicitProfile &&
|
|
@@ -201,8 +402,9 @@ export async function clonePenCommand(options) {
|
|
|
201
402
|
}
|
|
202
403
|
}
|
|
203
404
|
}
|
|
204
|
-
|
|
205
|
-
|
|
405
|
+
let currentUsername = penOwner ? await resolveCurrentUsername(client, auth, penOwner) : null;
|
|
406
|
+
let isOwnedByAnotherUser = Boolean(penOwner && currentUsername && penOwner.toLowerCase() !== currentUsername.toLowerCase());
|
|
407
|
+
let effectiveUrl = originalUrl ?? pen.urls.editor;
|
|
206
408
|
if (isOwnedByAnotherUser && penOwner && currentUsername) {
|
|
207
409
|
if (!jsonMode) {
|
|
208
410
|
console.log(fmt.yellow(`\nThis Pen belongs to ${fmt.bold(penOwner)} (your current account is ${fmt.bold(currentUsername)}).`));
|
|
@@ -245,6 +447,28 @@ export async function clonePenCommand(options) {
|
|
|
245
447
|
}
|
|
246
448
|
}
|
|
247
449
|
}
|
|
450
|
+
if (pen.editor === 'Classic' && !isOwnedByAnotherUser && interactive) {
|
|
451
|
+
const classicResolution = await handleClassicPenClone({
|
|
452
|
+
pen,
|
|
453
|
+
apiIdentifier,
|
|
454
|
+
effectiveUrl,
|
|
455
|
+
auth,
|
|
456
|
+
client,
|
|
457
|
+
options,
|
|
458
|
+
selectImpl,
|
|
459
|
+
textImpl,
|
|
460
|
+
});
|
|
461
|
+
if (classicResolution.action === 'cancel') {
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
pen = classicResolution.pen;
|
|
465
|
+
effectiveUrl = classicResolution.effectiveUrl;
|
|
466
|
+
auth = classicResolution.auth;
|
|
467
|
+
client = classicResolution.client;
|
|
468
|
+
isOwnedByAnotherUser = classicResolution.isOwnedByAnotherUser;
|
|
469
|
+
penOwner = classicResolution.penOwner;
|
|
470
|
+
currentUsername = classicResolution.currentUsername;
|
|
471
|
+
}
|
|
248
472
|
if (!targetDir) {
|
|
249
473
|
const defaultFolder = suggestFolderName(pen);
|
|
250
474
|
if (interactive) {
|
|
@@ -271,7 +495,8 @@ export async function clonePenCommand(options) {
|
|
|
271
495
|
remoteFiles,
|
|
272
496
|
pruneRemoved: false,
|
|
273
497
|
});
|
|
274
|
-
|
|
498
|
+
const isUnlinked = isOwnedByAnotherUser || pen.editor === 'Classic';
|
|
499
|
+
if (isUnlinked) {
|
|
275
500
|
if (jsonMode) {
|
|
276
501
|
printJsonResponse(200, `Cloned ${pen.title || 'Untitled'} into ${targetFolderDisplay} (unlinked local copy).`, {
|
|
277
502
|
pen,
|
|
@@ -287,10 +512,16 @@ export async function clonePenCommand(options) {
|
|
|
287
512
|
}
|
|
288
513
|
else {
|
|
289
514
|
console.log(`\n${fmt.green('✓')} Cloned ${fmt.bold(pen.title || 'Untitled')} ${fmt.dim(`(${pen.id})`)} into ${fmt.cyan(targetFolderDisplay)} ${fmt.dim('(unlinked local copy)')}`);
|
|
290
|
-
console.log(` Source: ${fmt.dim(
|
|
515
|
+
console.log(` Source: ${fmt.dim(effectiveUrl)}`);
|
|
291
516
|
console.log(` Files: ${result.writtenPaths.length} downloaded`);
|
|
292
|
-
|
|
293
|
-
|
|
517
|
+
if (isOwnedByAnotherUser) {
|
|
518
|
+
console.log(fmt.yellow(`\nNote: No ${PROJECT_CONFIG_FILENAME} was created because this Pen belongs to ${penOwner}, not ${currentUsername}.\n` +
|
|
519
|
+
`Run \`cdpn push\` inside ${targetFolderDisplay} later to publish it as a new Pen under your account, or \`cdpn fork ${effectiveUrl}\` to fork it on CodePen.`));
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
console.log(fmt.yellow(`\nNote: No ${PROJECT_CONFIG_FILENAME} was created because this is a Classic (v1) Pen (read-only through CodePen API v2).\n` +
|
|
523
|
+
`Run \`cdpn push\` inside ${targetFolderDisplay} later to publish it as a new CodePen 2.0 Pen, or convert it to 2.0 on CodePen via the Save button dropdown and re-clone.`));
|
|
524
|
+
}
|
|
294
525
|
}
|
|
295
526
|
return {
|
|
296
527
|
pen,
|
|
@@ -304,11 +535,12 @@ export async function clonePenCommand(options) {
|
|
|
304
535
|
}
|
|
305
536
|
const syncedFiles = buildSyncedFileMap(remoteFiles, result.localSha256ByPath);
|
|
306
537
|
const config = {
|
|
307
|
-
url:
|
|
538
|
+
url: effectiveUrl,
|
|
308
539
|
pen_id: pen.id,
|
|
309
540
|
...(auth.profile ? { profile: auth.profile } : {}),
|
|
310
541
|
last_synced_version_id: pen.saved_version?.id ?? null,
|
|
311
542
|
editor: pen.editor,
|
|
543
|
+
...buildPenInfoConfigFields(pen),
|
|
312
544
|
files: syncedFiles,
|
|
313
545
|
};
|
|
314
546
|
const configPath = await writeProjectConfig(targetDir, config);
|
|
@@ -335,9 +567,6 @@ export async function clonePenCommand(options) {
|
|
|
335
567
|
console.log(` Version: ${fmt.dim(shortId(pen.saved_version.id))}`);
|
|
336
568
|
}
|
|
337
569
|
console.log(` Config: ${fmt.dim(path.join(targetFolderDisplay, PROJECT_CONFIG_FILENAME))}`);
|
|
338
|
-
if (pen.editor === 'Classic') {
|
|
339
|
-
console.log(fmt.yellow('\nNote: This is a Classic Pen (read-only through CodePen API v2). You can clone and pull files, but pushing and deploying require a CodePen 2.0 Pen.'));
|
|
340
|
-
}
|
|
341
570
|
}
|
|
342
571
|
return {
|
|
343
572
|
pen,
|
|
@@ -356,7 +585,7 @@ export async function clonePenCommand(options) {
|
|
|
356
585
|
export function registerCloneCommand(program) {
|
|
357
586
|
program
|
|
358
587
|
.command('clone <url> [folder]')
|
|
359
|
-
.description('Clone a CodePen Pen into a local folder (offers to
|
|
588
|
+
.description('Clone a CodePen Pen into a local folder (offers to convert Classic Pens or fork unowned Pens)')
|
|
360
589
|
.option('-f, --force', 'Overwrite files if the target directory already exists and is not empty')
|
|
361
590
|
.option('--json', 'Output clone result as JSON')
|
|
362
591
|
.action(async (urlArg, folderArg, options, command) => {
|
package/commands/create-pen.js
CHANGED
|
@@ -3,6 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import { CodePenClient } from '../api/client.js';
|
|
4
4
|
import { buildSyncedFileMap, hasProjectConfigInDir, PROJECT_CONFIG_FILENAME, writeProjectConfig, } from '../config/project.js';
|
|
5
5
|
import { scanLocalFiles } from '../sync/fs.js';
|
|
6
|
+
import { buildPenInfoConfigFields } from '../sync/info.js';
|
|
6
7
|
import { syncRemoteFilesToDisk } from '../sync/materialize.js';
|
|
7
8
|
import { fmt, printBuildSummary, shortId } from '../utils/format.js';
|
|
8
9
|
import { extractHtmlTitle } from '../utils/html.js';
|
|
@@ -213,6 +214,7 @@ export async function createPenFromDirectory(options) {
|
|
|
213
214
|
...(auth.profile ? { profile: auth.profile } : {}),
|
|
214
215
|
last_synced_version_id: finalVersionId,
|
|
215
216
|
editor: created.editor,
|
|
217
|
+
...buildPenInfoConfigFields(created),
|
|
216
218
|
files: buildSyncedFileMap(remoteFiles, materialized.localSha256ByPath),
|
|
217
219
|
};
|
|
218
220
|
const configPath = await writeProjectConfig(targetDir, config);
|
package/commands/fork.js
CHANGED
|
@@ -4,6 +4,7 @@ import { CodePenClient } from '../api/client.js';
|
|
|
4
4
|
import { CodePenApiError } from '../api/errors.js';
|
|
5
5
|
import { buildSyncedFileMap, hasProjectConfigInDir, normalizePenReference, slugifyTitle, suggestFolderName, writeProjectConfig, } from '../config/project.js';
|
|
6
6
|
import { isDirectoryNonEmpty } from '../sync/fs.js';
|
|
7
|
+
import { buildPenInfoConfigFields } from '../sync/info.js';
|
|
7
8
|
import { syncRemoteFilesToDisk } from '../sync/materialize.js';
|
|
8
9
|
import { fmt, printBuildSummary, shortId } from '../utils/format.js';
|
|
9
10
|
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
@@ -252,6 +253,7 @@ export async function forkPenCommand(options) {
|
|
|
252
253
|
...(auth.profile ? { profile: auth.profile } : {}),
|
|
253
254
|
last_synced_version_id: finalVersionId,
|
|
254
255
|
editor: forked.editor,
|
|
256
|
+
...buildPenInfoConfigFields(forked),
|
|
255
257
|
files: buildSyncedFileMap(remoteFiles, materialized.localSha256ByPath),
|
|
256
258
|
};
|
|
257
259
|
const configPath = await writeProjectConfig(targetDir, config);
|
package/commands/meta.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CodePenClient } from '../api/client.js';
|
|
2
|
-
import { requireProject } from '../config/project.js';
|
|
2
|
+
import { requireProject, writeProjectConfig } from '../config/project.js';
|
|
3
|
+
import { mergeUpdatedPenInfoIntoConfig } from '../sync/info.js';
|
|
3
4
|
import { fmt, formatDate, shortId } from '../utils/format.js';
|
|
4
5
|
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
5
6
|
import { getGlobalProfileOption, resolveCommandAuth } from './auth-context.js';
|
|
@@ -46,6 +47,68 @@ function renderPenSettings(settings) {
|
|
|
46
47
|
console.log(`indent_with: ${fmt.cyan(settings.indent_with)}`);
|
|
47
48
|
console.log(`tab_size: ${fmt.cyan(String(settings.tab_size))}`);
|
|
48
49
|
}
|
|
50
|
+
export async function infoPenCommand(options = {}) {
|
|
51
|
+
const project = await requireProject(options.cwd ?? process.cwd());
|
|
52
|
+
const auth = await resolveCommandAuth({
|
|
53
|
+
explicitProfile: options.explicitProfile,
|
|
54
|
+
project,
|
|
55
|
+
interactive: options.json ? false : options.interactive,
|
|
56
|
+
persistProfileToProject: !options.json,
|
|
57
|
+
env: options.env,
|
|
58
|
+
homeDir: options.homeDir,
|
|
59
|
+
selectProfile: options.selectProfile,
|
|
60
|
+
});
|
|
61
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
62
|
+
const patch = {};
|
|
63
|
+
if (options.title !== undefined)
|
|
64
|
+
patch.title = options.title;
|
|
65
|
+
if (options.description !== undefined)
|
|
66
|
+
patch.description = options.description;
|
|
67
|
+
if (options.tags !== undefined) {
|
|
68
|
+
const parsedTags = options.tags
|
|
69
|
+
.split(',')
|
|
70
|
+
.map((t) => t.trim())
|
|
71
|
+
.filter(Boolean);
|
|
72
|
+
if (parsedTags.length > 5) {
|
|
73
|
+
throw new CliError(400, 'CodePen allows at most 5 tags per Pen.');
|
|
74
|
+
}
|
|
75
|
+
patch.tags = parsedTags;
|
|
76
|
+
}
|
|
77
|
+
if (options.access !== undefined) {
|
|
78
|
+
const lower = options.access.trim().toLowerCase();
|
|
79
|
+
if (!VALID_ACCESS.has(lower)) {
|
|
80
|
+
throw new CliError(400, `Invalid --access "${options.access}". Use: Public, Private, Protected, or Collaborators.`);
|
|
81
|
+
}
|
|
82
|
+
patch.access = (lower[0].toUpperCase() + lower.slice(1));
|
|
83
|
+
}
|
|
84
|
+
if (options.password !== undefined) {
|
|
85
|
+
if (options.password.length < 8) {
|
|
86
|
+
throw new CliError(400, 'Password must be at least 8 characters.');
|
|
87
|
+
}
|
|
88
|
+
patch.password = options.password;
|
|
89
|
+
}
|
|
90
|
+
if (options.template !== undefined) {
|
|
91
|
+
patch.template = parseBooleanOption(options.template, '--template');
|
|
92
|
+
}
|
|
93
|
+
const hasUpdates = Object.keys(patch).length > 0;
|
|
94
|
+
const pen = hasUpdates
|
|
95
|
+
? await client.updatePen(project.config.pen_id, patch)
|
|
96
|
+
: await client.getPen(project.config.pen_id);
|
|
97
|
+
if (hasUpdates) {
|
|
98
|
+
const nextConfig = mergeUpdatedPenInfoIntoConfig(project.config, pen, patch);
|
|
99
|
+
await writeProjectConfig(project.rootDir, nextConfig);
|
|
100
|
+
project.config = nextConfig;
|
|
101
|
+
}
|
|
102
|
+
if (options.json) {
|
|
103
|
+
printJsonResponse(200, hasUpdates ? 'Updated Pen metadata.' : 'Fetched Pen metadata.', pen);
|
|
104
|
+
return { pen, updated: hasUpdates, auth, project };
|
|
105
|
+
}
|
|
106
|
+
if (hasUpdates) {
|
|
107
|
+
console.log(`${fmt.green('✓')} Updated Pen metadata.\n`);
|
|
108
|
+
}
|
|
109
|
+
renderPenInfo(pen, auth.profile);
|
|
110
|
+
return { pen, updated: hasUpdates, auth, project };
|
|
111
|
+
}
|
|
49
112
|
export function registerMetaCommands(program) {
|
|
50
113
|
program
|
|
51
114
|
.command('info')
|
|
@@ -58,53 +121,10 @@ export function registerMetaCommands(program) {
|
|
|
58
121
|
.option('--template <bool>', 'Set template flag (true/false)')
|
|
59
122
|
.option('--json', 'Output Pen metadata as JSON')
|
|
60
123
|
.action(async (options, command) => {
|
|
61
|
-
|
|
62
|
-
|
|
124
|
+
await infoPenCommand({
|
|
125
|
+
...options,
|
|
63
126
|
explicitProfile: getGlobalProfileOption(command),
|
|
64
|
-
project,
|
|
65
|
-
interactive: options.json ? false : undefined,
|
|
66
|
-
persistProfileToProject: !options.json,
|
|
67
127
|
});
|
|
68
|
-
const client = new CodePenClient({ apiKey: auth.apiKey });
|
|
69
|
-
const patch = {};
|
|
70
|
-
if (options.title !== undefined)
|
|
71
|
-
patch.title = options.title;
|
|
72
|
-
if (options.description !== undefined)
|
|
73
|
-
patch.description = options.description;
|
|
74
|
-
if (options.tags !== undefined) {
|
|
75
|
-
patch.tags = options.tags
|
|
76
|
-
.split(',')
|
|
77
|
-
.map((t) => t.trim())
|
|
78
|
-
.filter(Boolean);
|
|
79
|
-
}
|
|
80
|
-
if (options.access !== undefined) {
|
|
81
|
-
const lower = options.access.trim().toLowerCase();
|
|
82
|
-
if (!VALID_ACCESS.has(lower)) {
|
|
83
|
-
throw new CliError(400, `Invalid --access "${options.access}". Use: Public, Private, Protected, or Collaborators.`);
|
|
84
|
-
}
|
|
85
|
-
patch.access = (lower[0].toUpperCase() + lower.slice(1));
|
|
86
|
-
}
|
|
87
|
-
if (options.password !== undefined) {
|
|
88
|
-
if (options.password.length < 8) {
|
|
89
|
-
throw new CliError(400, 'Password must be at least 8 characters.');
|
|
90
|
-
}
|
|
91
|
-
patch.password = options.password;
|
|
92
|
-
}
|
|
93
|
-
if (options.template !== undefined) {
|
|
94
|
-
patch.template = parseBooleanOption(options.template, '--template');
|
|
95
|
-
}
|
|
96
|
-
const hasUpdates = Object.keys(patch).length > 0;
|
|
97
|
-
const pen = hasUpdates
|
|
98
|
-
? await client.updatePen(project.config.pen_id, patch)
|
|
99
|
-
: await client.getPen(project.config.pen_id);
|
|
100
|
-
if (options.json) {
|
|
101
|
-
printJsonResponse(200, hasUpdates ? 'Updated Pen metadata.' : 'Fetched Pen metadata.', pen);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (hasUpdates) {
|
|
105
|
-
console.log(`${fmt.green('✓')} Updated Pen metadata.\n`);
|
|
106
|
-
}
|
|
107
|
-
renderPenInfo(pen, auth.profile);
|
|
108
128
|
});
|
|
109
129
|
program
|
|
110
130
|
.command('settings')
|
package/commands/new.js
CHANGED
|
@@ -3,6 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import { CodePenClient } from '../api/client.js';
|
|
4
4
|
import { buildSyncedFileMap, slugifyTitle, writeProjectConfig, } from '../config/project.js';
|
|
5
5
|
import { isDirectoryNonEmpty } from '../sync/fs.js';
|
|
6
|
+
import { buildPenInfoConfigFields } from '../sync/info.js';
|
|
6
7
|
import { syncRemoteFilesToDisk } from '../sync/materialize.js';
|
|
7
8
|
import { fmt, formatDate, printBuildSummary, shortId } from '../utils/format.js';
|
|
8
9
|
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
@@ -202,6 +203,7 @@ export async function scaffoldNewPen(options = {}) {
|
|
|
202
203
|
...(auth.profile ? { profile: auth.profile } : {}),
|
|
203
204
|
last_synced_version_id: finalVersionId,
|
|
204
205
|
editor: created.editor,
|
|
206
|
+
...buildPenInfoConfigFields(created),
|
|
205
207
|
files: buildSyncedFileMap(remoteFiles, materialized.localSha256ByPath),
|
|
206
208
|
};
|
|
207
209
|
const configPath = await writeProjectConfig(targetDir, config);
|
package/commands/privacy.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CodePenClient } from '../api/client.js';
|
|
2
|
-
import { requireProjectInDir } from '../config/project.js';
|
|
2
|
+
import { requireProjectInDir, writeProjectConfig } from '../config/project.js';
|
|
3
|
+
import { mergeUpdatedPenInfoIntoConfig } from '../sync/info.js';
|
|
3
4
|
import { fmt } from '../utils/format.js';
|
|
4
5
|
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
5
6
|
import { isInteractive, promptConfirm, promptSecret, promptSelect, } from '../utils/prompts.js';
|
|
@@ -177,6 +178,9 @@ export async function managePenPrivacy(options = {}) {
|
|
|
177
178
|
};
|
|
178
179
|
}
|
|
179
180
|
const updatedPen = await client.updatePen(project.config.pen_id, patch);
|
|
181
|
+
const nextConfig = mergeUpdatedPenInfoIntoConfig(project.config, updatedPen, patch);
|
|
182
|
+
await writeProjectConfig(project.rootDir, nextConfig);
|
|
183
|
+
project.config = nextConfig;
|
|
180
184
|
if (options.json) {
|
|
181
185
|
const message = previousAccess === updatedPen.access && passwordUpdated
|
|
182
186
|
? 'Updated password for Protected Pen.'
|
package/commands/pull.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { CodePenClient } from '../api/client.js';
|
|
2
|
-
import { buildSyncedFileMap, requireProject, writeProjectConfig, } from '../config/project.js';
|
|
2
|
+
import { buildSyncedFileMap, PROJECT_CONFIG_FILENAME, requireProject, writeProjectConfig, } from '../config/project.js';
|
|
3
3
|
import { detectLocalChangesFromBaseline } from '../sync/diff.js';
|
|
4
4
|
import { scanLocalFiles } from '../sync/fs.js';
|
|
5
|
+
import { analyzeInfoSync, buildPenInfoConfigFields, hasAnyPenInfoFields, isLocalInfoDirty, } from '../sync/info.js';
|
|
5
6
|
import { syncRemoteFilesToDisk } from '../sync/materialize.js';
|
|
6
7
|
import { fmt, shortId } from '../utils/format.js';
|
|
7
8
|
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
8
9
|
import { isInteractive, promptConfirm, promptSelect, } from '../utils/prompts.js';
|
|
9
10
|
import { getGlobalProfileOption, resolveCommandAuth } from './auth-context.js';
|
|
10
11
|
/**
|
|
11
|
-
* Pulls the latest remote files from CodePen into an existing cloned folder
|
|
12
|
+
* Pulls the latest remote files and Pen metadata from CodePen into an existing cloned folder
|
|
13
|
+
* containing `.cdpn.json`.
|
|
12
14
|
* Returns `null` if the user cancels interactively when uncommitted local changes are present.
|
|
13
15
|
*/
|
|
14
16
|
export async function pullPenCommand(options = {}) {
|
|
@@ -36,7 +38,8 @@ export async function pullPenCommand(options = {}) {
|
|
|
36
38
|
const localFiles = await scanLocalFiles(rootDir);
|
|
37
39
|
const localDirty = detectLocalChangesFromBaseline(localFiles, config.files);
|
|
38
40
|
const dirtyCount = localDirty.added.length + localDirty.modified.length + localDirty.deleted.length;
|
|
39
|
-
|
|
41
|
+
const localInfoDirty = isLocalInfoDirty(config.info, config.last_synced_info_hash);
|
|
42
|
+
if ((dirtyCount > 0 || localInfoDirty) && !force) {
|
|
40
43
|
if (!jsonMode) {
|
|
41
44
|
console.log(fmt.yellow('\nWarning: You have uncommitted local changes that will be overwritten:'));
|
|
42
45
|
for (const p of localDirty.added)
|
|
@@ -45,9 +48,15 @@ export async function pullPenCommand(options = {}) {
|
|
|
45
48
|
console.log(` ${fmt.yellow('~')} ${p} ${fmt.dim('(will be overwritten)')}`);
|
|
46
49
|
for (const p of localDirty.deleted)
|
|
47
50
|
console.log(` ${fmt.red('-')} ${p} ${fmt.dim('(will be restored)')}`);
|
|
51
|
+
if (localInfoDirty) {
|
|
52
|
+
console.log(` ${fmt.yellow('~')} ${PROJECT_CONFIG_FILENAME} (info) ${fmt.dim('(will be overwritten)')}`);
|
|
53
|
+
}
|
|
48
54
|
}
|
|
49
55
|
if (!interactive) {
|
|
50
|
-
throw new CliError(409, 'Local changes detected. Commit/push them first, or re-run with --force.', {
|
|
56
|
+
throw new CliError(409, 'Local changes detected. Commit/push them first, or re-run with --force.', {
|
|
57
|
+
dirty_files: localDirty,
|
|
58
|
+
...(localInfoDirty ? { dirty_info: true } : {}),
|
|
59
|
+
});
|
|
51
60
|
}
|
|
52
61
|
const ok = await confirmImpl('\nOverwrite local changes with remote version?', false);
|
|
53
62
|
if (!ok) {
|
|
@@ -59,6 +68,8 @@ export async function pullPenCommand(options = {}) {
|
|
|
59
68
|
client.getPen(config.pen_id),
|
|
60
69
|
client.getPenFiles(config.pen_id),
|
|
61
70
|
]);
|
|
71
|
+
const hadInfoBefore = hasAnyPenInfoFields(config.info);
|
|
72
|
+
const infoAnalysis = analyzeInfoSync(config.info, config.last_synced_info_hash, pen);
|
|
62
73
|
const result = await syncRemoteFilesToDisk({
|
|
63
74
|
client,
|
|
64
75
|
rootDir,
|
|
@@ -74,6 +85,7 @@ export async function pullPenCommand(options = {}) {
|
|
|
74
85
|
...(profileToPersist ? { profile: profileToPersist } : {}),
|
|
75
86
|
last_synced_version_id: pen.saved_version?.id ?? null,
|
|
76
87
|
editor: pen.editor,
|
|
88
|
+
...buildPenInfoConfigFields(pen),
|
|
77
89
|
files: syncedFiles,
|
|
78
90
|
};
|
|
79
91
|
const configPath = await writeProjectConfig(rootDir, nextConfig);
|
|
@@ -95,6 +107,12 @@ export async function pullPenCommand(options = {}) {
|
|
|
95
107
|
}
|
|
96
108
|
console.log(` Updated: ${result.writtenPaths.length} file(s)` +
|
|
97
109
|
(result.deletedPaths.length > 0 ? `, removed ${result.deletedPaths.length} deleted file(s)` : ''));
|
|
110
|
+
if (!hadInfoBefore) {
|
|
111
|
+
console.log(` Info: ${fmt.dim(`added to ${PROJECT_CONFIG_FILENAME}`)}`);
|
|
112
|
+
}
|
|
113
|
+
else if (infoAnalysis.changes.length > 0) {
|
|
114
|
+
console.log(` Info: ${fmt.dim(`updated ${infoAnalysis.changes.map((c) => c.field).join(', ')} in ${PROJECT_CONFIG_FILENAME}`)}`);
|
|
115
|
+
}
|
|
98
116
|
if (pen.saved_version) {
|
|
99
117
|
console.log(` Version: ${fmt.dim(shortId(pen.saved_version.id))}`);
|
|
100
118
|
}
|