codepen-commander 1.2.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 +104 -10
- 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 +553 -37
- package/commands/deploy.js +164 -154
- 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 +222 -365
- 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 +179 -240
- 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/workflows/create-pen.js +254 -0
- 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/create-pen.js +0 -251
- package/commands/meta.js +0 -184
- package/utils/prompts.js +0 -316
- /package/{types.js → api/types.js} +0 -0
package/commands/sync.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { CodePenClient } from '../api/client.js';
|
|
3
|
+
import { findNearestConfig } from '../config/project.js';
|
|
4
|
+
import { MAX_PEN_TAGS, PROJECT_CONFIG_FILENAME } from '../constants.js';
|
|
5
|
+
import { CliError } from '../errors.js';
|
|
6
|
+
import { scanLocalFiles } from '../sync/fs.js';
|
|
7
|
+
import { assertValidProtectedPassword, assertValidVersionLabel, normalizeAccess, resolveAccessOrVisibility, } from '../utils/cli-options.js';
|
|
8
|
+
import { fmt, shortId } from '../utils/format.js';
|
|
9
|
+
import { extractHtmlTitle } from '../utils/html.js';
|
|
10
|
+
import { printJsonResponse } from '../utils/json.js';
|
|
11
|
+
import { defaultOutput } from '../utils/output.js';
|
|
12
|
+
import { isInteractive } from '../utils/prompts/index.js';
|
|
13
|
+
import { getGlobalProfileOption, resolveCommandAuth } from '../workflows/auth-context.js';
|
|
14
|
+
import { runBatchSync } from '../workflows/batch-pens.js';
|
|
15
|
+
import { offerBatchAllFallback, resolveBatchCommandTarget, } from '../workflows/batch-target.js';
|
|
16
|
+
import { createPenFromDirectory } from '../workflows/create-pen.js';
|
|
17
|
+
import { fetchLinkedPenOrHandleMissing } from '../workflows/missing-pen.js';
|
|
18
|
+
import { executeSync } from '../workflows/sync-pen.js';
|
|
19
|
+
/**
|
|
20
|
+
* Bidirectional sync command:
|
|
21
|
+
* - Inside a Pen folder (or explicit `[target]` directory): creates unlinked Pens,
|
|
22
|
+
* pushes when local is ahead, pulls when remote is ahead, leaves up-to-date Pens untouched,
|
|
23
|
+
* and refuses with `409 Conflict` when both sides have diverged.
|
|
24
|
+
* - With `[target]` set to `"all"`: walks subdirectories to discover all Pens (`.cdpn.json`)
|
|
25
|
+
* and syncs them in batch, printing a summary report.
|
|
26
|
+
*/
|
|
27
|
+
export async function runSyncCommand(options = {}) {
|
|
28
|
+
const output = options.output ?? defaultOutput;
|
|
29
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
30
|
+
const jsonMode = Boolean(options.json);
|
|
31
|
+
const dryRun = Boolean(options.dryRun);
|
|
32
|
+
const interactive = !jsonMode && (options.interactive ?? isInteractive());
|
|
33
|
+
assertValidVersionLabel(options.label);
|
|
34
|
+
const resolvedAccess = resolveAccessOrVisibility(options.access, options.visibility);
|
|
35
|
+
const executeBatch = async (rootDir, discovered) => {
|
|
36
|
+
const batch = await runBatchSync({
|
|
37
|
+
rootDir,
|
|
38
|
+
discovered,
|
|
39
|
+
label: options.label,
|
|
40
|
+
force: options.force,
|
|
41
|
+
dryRun,
|
|
42
|
+
wait: options.wait,
|
|
43
|
+
access: resolvedAccess,
|
|
44
|
+
password: options.password,
|
|
45
|
+
json: jsonMode,
|
|
46
|
+
interactive,
|
|
47
|
+
explicitProfile: options.explicitProfile,
|
|
48
|
+
baseUrl: options.baseUrl,
|
|
49
|
+
env: options.env,
|
|
50
|
+
homeDir: options.homeDir,
|
|
51
|
+
output,
|
|
52
|
+
prompts: options.prompts,
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
mode: 'batch',
|
|
56
|
+
dryRun,
|
|
57
|
+
batch,
|
|
58
|
+
...(batch.exitCode !== undefined ? { exitCode: batch.exitCode } : {}),
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const resolvedTarget = await resolveBatchCommandTarget({
|
|
62
|
+
commandName: 'sync',
|
|
63
|
+
cwd,
|
|
64
|
+
target: options.target,
|
|
65
|
+
interactive,
|
|
66
|
+
output,
|
|
67
|
+
prompts: options.prompts,
|
|
68
|
+
});
|
|
69
|
+
if (resolvedTarget.mode === 'batch') {
|
|
70
|
+
return executeBatch(resolvedTarget.rootDir, resolvedTarget.discovered);
|
|
71
|
+
}
|
|
72
|
+
const nearest = await findNearestConfig(resolvedTarget.targetDir);
|
|
73
|
+
if (!nearest) {
|
|
74
|
+
const fallback = await offerBatchAllFallback({
|
|
75
|
+
commandName: 'sync',
|
|
76
|
+
targetDir: resolvedTarget.targetDir,
|
|
77
|
+
fromExplicitDir: resolvedTarget.fromExplicitDir,
|
|
78
|
+
displayDir: resolvedTarget.displayDir,
|
|
79
|
+
dryRun,
|
|
80
|
+
interactive,
|
|
81
|
+
output,
|
|
82
|
+
prompts: options.prompts,
|
|
83
|
+
});
|
|
84
|
+
if (fallback.action === 'batch') {
|
|
85
|
+
return executeBatch(resolvedTarget.targetDir, fallback.discovered);
|
|
86
|
+
}
|
|
87
|
+
if (fallback.action === 'canceled') {
|
|
88
|
+
return { mode: 'single', dryRun, aborted: true };
|
|
89
|
+
}
|
|
90
|
+
throw new CliError(404, `No ${PROJECT_CONFIG_FILENAME} found in this directory or any parent directory.\n` +
|
|
91
|
+
'Run `cdpn clone <url>` to clone a Pen, `cdpn init` to initialize this folder, or `cdpn new` to create one.');
|
|
92
|
+
}
|
|
93
|
+
// Single-Pen unlinked info-only `.cdpn.json` -> create new Pen (or preview in --dry-run)
|
|
94
|
+
if (!nearest.linked) {
|
|
95
|
+
const targetDir = nearest.rootDir;
|
|
96
|
+
const folderDisplay = path.relative(cwd, targetDir) || '.';
|
|
97
|
+
const retainedInfo = nearest.config.info;
|
|
98
|
+
if (dryRun) {
|
|
99
|
+
const effectiveAccess = resolvedAccess ?? normalizeAccess(retainedInfo.access) ?? 'Public';
|
|
100
|
+
if (retainedInfo.tags && retainedInfo.tags.length > MAX_PEN_TAGS) {
|
|
101
|
+
throw new CliError(400, `CodePen allows at most ${MAX_PEN_TAGS} tags per Pen (found ${retainedInfo.tags.length} in ${PROJECT_CONFIG_FILENAME}).`);
|
|
102
|
+
}
|
|
103
|
+
assertValidProtectedPassword(effectiveAccess, options.password, false);
|
|
104
|
+
const scanned = await scanLocalFiles(targetDir);
|
|
105
|
+
if (scanned.size === 0) {
|
|
106
|
+
throw new CliError(400, `No syncable files found in ${targetDir} (only an unlinked ${PROJECT_CONFIG_FILENAME} is present).`);
|
|
107
|
+
}
|
|
108
|
+
const indexFile = scanned.get('/index.html');
|
|
109
|
+
if (!indexFile && !options.force) {
|
|
110
|
+
throw new CliError(400, `No index.html found in ${targetDir}. Pass --force to push this folder without an index.html.`);
|
|
111
|
+
}
|
|
112
|
+
const htmlTitle = indexFile && !indexFile.isBinary && indexFile.textContent
|
|
113
|
+
? extractHtmlTitle(indexFile.textContent)
|
|
114
|
+
: null;
|
|
115
|
+
const title = retainedInfo.title?.trim() || htmlTitle || path.basename(targetDir) || 'Untitled';
|
|
116
|
+
if (jsonMode) {
|
|
117
|
+
printJsonResponse(200, `Dry run complete — would create new ${effectiveAccess} Pen "${title}" (${scanned.size} file(s)).`, {
|
|
118
|
+
batch: false,
|
|
119
|
+
action: 'created',
|
|
120
|
+
state: 'unlinked',
|
|
121
|
+
dry_run: true,
|
|
122
|
+
title,
|
|
123
|
+
access: effectiveAccess,
|
|
124
|
+
folder: folderDisplay,
|
|
125
|
+
root_dir: targetDir,
|
|
126
|
+
file_count: scanned.size,
|
|
127
|
+
}, output);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
output.log(`${fmt.green('+ [WOULD CREATE]')} New ${effectiveAccess} Pen ${fmt.bold(title)} from ${scanned.size} file(s) in ${fmt.cyan(folderDisplay)}.`);
|
|
131
|
+
output.log(`\n${fmt.cyan('ℹ')} Dry run complete — no changes were saved to CodePen.`);
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
mode: 'single',
|
|
135
|
+
action: 'created',
|
|
136
|
+
state: 'unlinked',
|
|
137
|
+
dryRun: true,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
if (!jsonMode) {
|
|
141
|
+
output.log(fmt.dim(`Found unlinked ${PROJECT_CONFIG_FILENAME} with saved Pen info — creating a new CodePen 2.0 Pen.`));
|
|
142
|
+
}
|
|
143
|
+
const created = await createPenFromDirectory({
|
|
144
|
+
targetDir,
|
|
145
|
+
folderDisplay,
|
|
146
|
+
explicitProfile: options.explicitProfile,
|
|
147
|
+
access: resolvedAccess,
|
|
148
|
+
password: options.password,
|
|
149
|
+
force: options.force,
|
|
150
|
+
wait: options.wait,
|
|
151
|
+
silent: jsonMode,
|
|
152
|
+
output,
|
|
153
|
+
baseUrl: options.baseUrl,
|
|
154
|
+
interactive,
|
|
155
|
+
env: options.env,
|
|
156
|
+
homeDir: options.homeDir,
|
|
157
|
+
prompts: options.prompts,
|
|
158
|
+
});
|
|
159
|
+
if (!created) {
|
|
160
|
+
return { mode: 'single', dryRun: false, aborted: true };
|
|
161
|
+
}
|
|
162
|
+
const buildFailed = created.build?.status === 'Failed';
|
|
163
|
+
if (jsonMode) {
|
|
164
|
+
printJsonResponse(buildFailed ? 422 : 201, buildFailed
|
|
165
|
+
? `Created Pen ${created.pen.title || 'Untitled'} (${created.pen.id}), but the initial build failed.`
|
|
166
|
+
: `Created Pen ${created.pen.title || 'Untitled'} (${created.pen.id}).`, {
|
|
167
|
+
batch: false,
|
|
168
|
+
action: 'created',
|
|
169
|
+
state: 'unlinked',
|
|
170
|
+
dry_run: false,
|
|
171
|
+
pen: created.pen,
|
|
172
|
+
profile: created.auth.profile,
|
|
173
|
+
folder: folderDisplay,
|
|
174
|
+
root_dir: created.project.rootDir,
|
|
175
|
+
config_path: created.project.configPath,
|
|
176
|
+
version_id: created.versionId,
|
|
177
|
+
build: created.build,
|
|
178
|
+
}, output);
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
mode: 'single',
|
|
182
|
+
action: 'created',
|
|
183
|
+
state: 'unlinked',
|
|
184
|
+
dryRun: false,
|
|
185
|
+
pen: created.pen,
|
|
186
|
+
versionId: created.versionId,
|
|
187
|
+
build: created.build,
|
|
188
|
+
...(created.exitCode !== undefined ? { exitCode: created.exitCode } : {}),
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
// Single-Pen linked `.cdpn.json`
|
|
192
|
+
const project = {
|
|
193
|
+
rootDir: nearest.rootDir,
|
|
194
|
+
configPath: nearest.configPath,
|
|
195
|
+
config: nearest.config,
|
|
196
|
+
};
|
|
197
|
+
const auth = await resolveCommandAuth({
|
|
198
|
+
explicitProfile: options.explicitProfile,
|
|
199
|
+
project,
|
|
200
|
+
interactive,
|
|
201
|
+
persistProfileToProject: !jsonMode,
|
|
202
|
+
output,
|
|
203
|
+
env: options.env,
|
|
204
|
+
homeDir: options.homeDir,
|
|
205
|
+
selectProfile: options.prompts?.select,
|
|
206
|
+
});
|
|
207
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
208
|
+
const penLookup = await fetchLinkedPenOrHandleMissing({
|
|
209
|
+
client,
|
|
210
|
+
project,
|
|
211
|
+
auth,
|
|
212
|
+
commandName: 'sync',
|
|
213
|
+
interactive,
|
|
214
|
+
json: jsonMode,
|
|
215
|
+
output,
|
|
216
|
+
prompts: options.prompts,
|
|
217
|
+
});
|
|
218
|
+
if (!penLookup.found) {
|
|
219
|
+
return { mode: 'single', dryRun, aborted: true };
|
|
220
|
+
}
|
|
221
|
+
const syncRes = await executeSync(client, project, auth, {
|
|
222
|
+
label: options.label,
|
|
223
|
+
dryRun,
|
|
224
|
+
wait: options.wait,
|
|
225
|
+
silent: jsonMode,
|
|
226
|
+
json: jsonMode,
|
|
227
|
+
output,
|
|
228
|
+
prefetchedPen: penLookup.pen,
|
|
229
|
+
});
|
|
230
|
+
if (jsonMode) {
|
|
231
|
+
if (syncRes.action === 'up-to-date') {
|
|
232
|
+
printJsonResponse(200, 'Everything is up to date — nothing to sync.', {
|
|
233
|
+
batch: false,
|
|
234
|
+
action: 'up-to-date',
|
|
235
|
+
state: syncRes.state,
|
|
236
|
+
dry_run: dryRun,
|
|
237
|
+
pen: syncRes.pen,
|
|
238
|
+
profile: auth.profile,
|
|
239
|
+
version_id: syncRes.inspection.remoteHead,
|
|
240
|
+
}, output);
|
|
241
|
+
}
|
|
242
|
+
else if (syncRes.action === 'pushed' && syncRes.pushResult) {
|
|
243
|
+
const pr = syncRes.pushResult;
|
|
244
|
+
const hasFileChanges = pr.changes.added.length > 0 ||
|
|
245
|
+
pr.changes.modified.length > 0 ||
|
|
246
|
+
pr.changes.deleted.length > 0;
|
|
247
|
+
const buildFailed = hasFileChanges && pr.build?.status === 'Failed';
|
|
248
|
+
const statusCode = buildFailed ? 422 : 200;
|
|
249
|
+
const message = dryRun
|
|
250
|
+
? buildFailed
|
|
251
|
+
? 'Dry run completed, but the build failed.'
|
|
252
|
+
: 'Dry run complete — would push local changes to CodePen.'
|
|
253
|
+
: buildFailed
|
|
254
|
+
? `Files were saved as version ${shortId(pr.versionId)}, but the build failed.`
|
|
255
|
+
: `Synced by pushing local changes (version ${shortId(pr.versionId)}).`;
|
|
256
|
+
printJsonResponse(statusCode, message, {
|
|
257
|
+
batch: false,
|
|
258
|
+
action: 'pushed',
|
|
259
|
+
state: syncRes.state,
|
|
260
|
+
dry_run: dryRun,
|
|
261
|
+
pen: pr.pen,
|
|
262
|
+
profile: auth.profile,
|
|
263
|
+
version_id: pr.versionId,
|
|
264
|
+
build: pr.build,
|
|
265
|
+
changes: pr.changes,
|
|
266
|
+
info_changes: pr.infoChanges,
|
|
267
|
+
applied_patches: pr.appliedPatches,
|
|
268
|
+
}, output);
|
|
269
|
+
}
|
|
270
|
+
else if (syncRes.action === 'pulled' && syncRes.pullResult) {
|
|
271
|
+
const lr = syncRes.pullResult;
|
|
272
|
+
printJsonResponse(200, dryRun
|
|
273
|
+
? `Dry run complete — would pull remote changes for ${lr.pen.title || 'Untitled'}.`
|
|
274
|
+
: `Synced by pulling remote changes for ${lr.pen.title || 'Untitled'}.`, {
|
|
275
|
+
batch: false,
|
|
276
|
+
action: 'pulled',
|
|
277
|
+
state: syncRes.state,
|
|
278
|
+
dry_run: dryRun,
|
|
279
|
+
pen: lr.pen,
|
|
280
|
+
profile: auth.profile,
|
|
281
|
+
version_id: lr.pen.saved_version?.id ?? null,
|
|
282
|
+
written_paths: lr.writtenPaths,
|
|
283
|
+
deleted_paths: lr.deletedPaths,
|
|
284
|
+
info_changes: lr.infoChanges,
|
|
285
|
+
}, output);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
mode: 'single',
|
|
290
|
+
action: syncRes.action,
|
|
291
|
+
state: syncRes.state,
|
|
292
|
+
dryRun,
|
|
293
|
+
pen: syncRes.pen,
|
|
294
|
+
versionId: syncRes.pushResult?.versionId ??
|
|
295
|
+
syncRes.pullResult?.pen.saved_version?.id ??
|
|
296
|
+
syncRes.inspection.remoteHead,
|
|
297
|
+
build: syncRes.pushResult?.build,
|
|
298
|
+
changes: syncRes.pushResult?.changes,
|
|
299
|
+
writtenPaths: syncRes.pullResult?.writtenPaths,
|
|
300
|
+
deletedPaths: syncRes.pullResult?.deletedPaths,
|
|
301
|
+
infoChanges: syncRes.pushResult?.infoChanges ?? syncRes.pullResult?.infoChanges,
|
|
302
|
+
appliedPatches: syncRes.pushResult?.appliedPatches,
|
|
303
|
+
...(syncRes.exitCode !== undefined ? { exitCode: syncRes.exitCode } : {}),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
export function registerSyncCommand(program) {
|
|
307
|
+
program
|
|
308
|
+
.command('sync')
|
|
309
|
+
.argument('[target]', 'Directory path to sync, or "all" to sync all Pens in subdirectories')
|
|
310
|
+
.allowExcessArguments(false)
|
|
311
|
+
.description('Bidirectionally sync a Pen with CodePen (or pass "all" to walk subdirectories)')
|
|
312
|
+
.option('-m, --label <label>', 'Label for the saved version when pushing (max 80 chars)')
|
|
313
|
+
.option('-f, --force', 'Skip missing index.html prompt when creating a Pen from an unlinked folder')
|
|
314
|
+
.option('--dry-run', 'Preview sync actions without saving changes or modifying local files')
|
|
315
|
+
.option('--no-wait', 'Return as soon as files are saved when pushing without waiting for the build')
|
|
316
|
+
.option('--access <level>', 'Privacy for a new Pen: Public, Private, Protected, or Collaborators')
|
|
317
|
+
.option('--visibility <level>', 'Alias for --access')
|
|
318
|
+
.option('--password <password>', 'Password for a new Protected Pen (min 8 chars)')
|
|
319
|
+
.option('--json', 'Output sync result as JSON')
|
|
320
|
+
.action(async (target, options, command) => {
|
|
321
|
+
const result = await runSyncCommand({
|
|
322
|
+
...options,
|
|
323
|
+
target,
|
|
324
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
325
|
+
});
|
|
326
|
+
if (result.exitCode !== undefined) {
|
|
327
|
+
process.exitCode = result.exitCode;
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { CodePenClient } from '../api/client.js';
|
|
2
|
+
import { requireProject } from '../config/project.js';
|
|
3
|
+
import { CliError } from '../errors.js';
|
|
4
|
+
import { fmt } from '../utils/format.js';
|
|
5
|
+
import { printJsonResponse } from '../utils/json.js';
|
|
6
|
+
import { defaultOutput } from '../utils/output.js';
|
|
7
|
+
import { isInteractive, promptConfirm } from '../utils/prompts/index.js';
|
|
8
|
+
import { getGlobalProfileOption, resolveCommandAuth } from '../workflows/auth-context.js';
|
|
9
|
+
import { fetchLinkedPenOrHandleMissing } from '../workflows/missing-pen.js';
|
|
10
|
+
export async function runUndeployCommand(options = {}) {
|
|
11
|
+
const output = options.output ?? defaultOutput;
|
|
12
|
+
const jsonMode = Boolean(options.json);
|
|
13
|
+
const interactive = !jsonMode && (options.interactive ?? isInteractive());
|
|
14
|
+
const confirmImpl = options.prompts?.confirm ?? promptConfirm;
|
|
15
|
+
const project = await requireProject(options.cwd ?? process.cwd());
|
|
16
|
+
const auth = await resolveCommandAuth({
|
|
17
|
+
explicitProfile: options.explicitProfile,
|
|
18
|
+
project,
|
|
19
|
+
interactive,
|
|
20
|
+
persistProfileToProject: !jsonMode,
|
|
21
|
+
output,
|
|
22
|
+
env: options.env,
|
|
23
|
+
homeDir: options.homeDir,
|
|
24
|
+
selectProfile: options.prompts?.selectProfile,
|
|
25
|
+
});
|
|
26
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
27
|
+
const penLookup = await fetchLinkedPenOrHandleMissing({
|
|
28
|
+
client,
|
|
29
|
+
project,
|
|
30
|
+
auth,
|
|
31
|
+
commandName: 'undeploy',
|
|
32
|
+
interactive,
|
|
33
|
+
json: jsonMode,
|
|
34
|
+
output,
|
|
35
|
+
prompts: options.prompts,
|
|
36
|
+
});
|
|
37
|
+
if (!penLookup.found) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const pen = penLookup.pen;
|
|
41
|
+
if (!pen.deployment) {
|
|
42
|
+
if (jsonMode) {
|
|
43
|
+
printJsonResponse(200, 'This Pen is not currently deployed.', {
|
|
44
|
+
pen_id: project.config.pen_id,
|
|
45
|
+
profile: auth.profile,
|
|
46
|
+
undeployed: false,
|
|
47
|
+
previous_deployment: null,
|
|
48
|
+
}, output);
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
output.log(fmt.dim('This Pen is not currently deployed.'));
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
penId: project.config.pen_id,
|
|
55
|
+
profile: auth.profile,
|
|
56
|
+
undeployed: false,
|
|
57
|
+
previousDeployment: null,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (!options.yes) {
|
|
61
|
+
if (!interactive) {
|
|
62
|
+
throw new CliError(400, 'Refusing to undeploy in non-interactive mode without --yes.');
|
|
63
|
+
}
|
|
64
|
+
const ok = await confirmImpl(`Take down live deployment at ${fmt.cyan(pen.deployment.url)}?`, false);
|
|
65
|
+
if (!ok) {
|
|
66
|
+
output.log('Aborted.');
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
await client.undeployPen(project.config.pen_id);
|
|
71
|
+
if (jsonMode) {
|
|
72
|
+
printJsonResponse(200, 'Deployment taken down. Your Pen and saved versions remain intact.', {
|
|
73
|
+
pen_id: project.config.pen_id,
|
|
74
|
+
profile: auth.profile,
|
|
75
|
+
undeployed: true,
|
|
76
|
+
previous_deployment: pen.deployment,
|
|
77
|
+
}, output);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
output.log(`${fmt.green('✓')} Deployment taken down. Your Pen and saved versions remain intact.`);
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
penId: project.config.pen_id,
|
|
84
|
+
profile: auth.profile,
|
|
85
|
+
undeployed: true,
|
|
86
|
+
previousDeployment: pen.deployment,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export function registerUndeployCommand(program) {
|
|
90
|
+
program
|
|
91
|
+
.command('undeploy')
|
|
92
|
+
.description('Take down the Pen’s live deployment (Pen and versions stay intact)')
|
|
93
|
+
.option('-y, --yes', 'Skip confirmation prompt')
|
|
94
|
+
.option('--json', 'Output undeploy result as JSON')
|
|
95
|
+
.action(async (options, command) => {
|
|
96
|
+
await runUndeployCommand({
|
|
97
|
+
yes: options.yes,
|
|
98
|
+
json: options.json,
|
|
99
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { findProject, hasProjectConfigInDir, requireProjectInDir, } from '../config/project.js';
|
|
3
|
+
import { CODEPEN_DIR, PROJECT_CONFIG_FILENAME } from '../constants.js';
|
|
4
|
+
import { CliError } from '../errors.js';
|
|
5
|
+
import { fmt } from '../utils/format.js';
|
|
6
|
+
import { printJsonResponse } from '../utils/json.js';
|
|
7
|
+
import { defaultOutput } from '../utils/output.js';
|
|
8
|
+
import { isInteractive, promptConfirm } from '../utils/prompts/index.js';
|
|
9
|
+
import { runBatchUnlink } from '../workflows/batch-pens.js';
|
|
10
|
+
import { offerBatchAllFallback, resolveBatchCommandTarget, } from '../workflows/batch-target.js';
|
|
11
|
+
import { unlinkLinkedProject } from '../workflows/missing-pen.js';
|
|
12
|
+
/**
|
|
13
|
+
* Locally unlinks the current folder (or explicit directory `[target]`) from its remote CodePen Pen
|
|
14
|
+
* by clearing all remote link fields from `.cdpn.json` while keeping `info`, `.codepen/`, and all
|
|
15
|
+
* local source files untouched.
|
|
16
|
+
*
|
|
17
|
+
* Pass `[target]` = `"all"` to walk subdirectories and unlink all discovered Pens in batch after
|
|
18
|
+
* an upfront two-step confirmation (unless `--yes` or `--dry-run` is passed).
|
|
19
|
+
*
|
|
20
|
+
* Does NOT modify or delete online Pens on CodePen.
|
|
21
|
+
* Returns `null` if the user cancels at a confirmation prompt.
|
|
22
|
+
*/
|
|
23
|
+
export async function runUnlinkCommand(options = {}) {
|
|
24
|
+
const output = options.output ?? defaultOutput;
|
|
25
|
+
const cwd = path.resolve(options.cwd ?? process.cwd());
|
|
26
|
+
const dryRun = Boolean(options.dryRun);
|
|
27
|
+
const interactive = (options.interactive ?? isInteractive()) && !options.json;
|
|
28
|
+
const confirmImpl = options.prompts?.confirm ?? promptConfirm;
|
|
29
|
+
const executeBatch = async (rootDir, discovered) => {
|
|
30
|
+
const batch = await runBatchUnlink({
|
|
31
|
+
rootDir,
|
|
32
|
+
discovered,
|
|
33
|
+
yes: options.yes,
|
|
34
|
+
dryRun,
|
|
35
|
+
json: options.json,
|
|
36
|
+
interactive,
|
|
37
|
+
output,
|
|
38
|
+
prompts: options.prompts,
|
|
39
|
+
});
|
|
40
|
+
if (!batch) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
unlinked: !dryRun && batch.counts.unlinked > 0,
|
|
45
|
+
dryRun,
|
|
46
|
+
rootDir,
|
|
47
|
+
batch,
|
|
48
|
+
...(batch.exitCode !== undefined ? { exitCode: batch.exitCode } : {}),
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const resolvedTarget = await resolveBatchCommandTarget({
|
|
52
|
+
commandName: 'unlink',
|
|
53
|
+
cwd,
|
|
54
|
+
target: options.target,
|
|
55
|
+
interactive,
|
|
56
|
+
output,
|
|
57
|
+
prompts: options.prompts,
|
|
58
|
+
});
|
|
59
|
+
if (resolvedTarget.mode === 'batch') {
|
|
60
|
+
return executeBatch(resolvedTarget.rootDir, resolvedTarget.discovered);
|
|
61
|
+
}
|
|
62
|
+
const targetDir = resolvedTarget.targetDir;
|
|
63
|
+
// Outside a Pen directory: preserve parent safety, then offer `cdpn unlink all` if child Pens exist
|
|
64
|
+
if (!(await hasProjectConfigInDir(targetDir))) {
|
|
65
|
+
const parentDir = path.dirname(targetDir);
|
|
66
|
+
const hasParentProject = parentDir !== targetDir && Boolean(await findProject(parentDir));
|
|
67
|
+
if (!hasParentProject) {
|
|
68
|
+
const fallback = await offerBatchAllFallback({
|
|
69
|
+
commandName: 'unlink',
|
|
70
|
+
targetDir,
|
|
71
|
+
fromExplicitDir: resolvedTarget.fromExplicitDir,
|
|
72
|
+
displayDir: resolvedTarget.displayDir,
|
|
73
|
+
dryRun,
|
|
74
|
+
interactive,
|
|
75
|
+
output,
|
|
76
|
+
prompts: options.prompts,
|
|
77
|
+
});
|
|
78
|
+
if (fallback.action === 'batch') {
|
|
79
|
+
return executeBatch(targetDir, fallback.discovered);
|
|
80
|
+
}
|
|
81
|
+
if (fallback.action === 'canceled') {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
// Strictly require a linked `.cdpn.json` directly inside the target folder
|
|
87
|
+
const project = await requireProjectInDir(targetDir);
|
|
88
|
+
const displayTitle = project.config.info?.title?.trim() || 'Untitled';
|
|
89
|
+
const previousPenId = project.config.pen_id;
|
|
90
|
+
const previousUrl = project.config.url;
|
|
91
|
+
const previousProfile = project.config.profile ?? null;
|
|
92
|
+
if (dryRun) {
|
|
93
|
+
const previewInfo = project.config.info ?? {};
|
|
94
|
+
if (options.json) {
|
|
95
|
+
printJsonResponse(200, `Dry run complete — would unlink local folder from ${displayTitle} (${previousPenId}); online Pen left untouched.`, {
|
|
96
|
+
unlinked: false,
|
|
97
|
+
dry_run: true,
|
|
98
|
+
would_unlink: true,
|
|
99
|
+
previous_pen_id: previousPenId,
|
|
100
|
+
previous_url: previousUrl,
|
|
101
|
+
previous_profile: previousProfile,
|
|
102
|
+
root_dir: project.rootDir,
|
|
103
|
+
config_path: project.configPath,
|
|
104
|
+
info: previewInfo,
|
|
105
|
+
}, output);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
output.log(`\n${fmt.yellow('× [WOULD UNLINK]')} ${fmt.bold(displayTitle)} ${fmt.dim(`(${previousPenId})`)}`);
|
|
109
|
+
output.log(` ${fmt.dim('Config:')} ${fmt.cyan(PROJECT_CONFIG_FILENAME)} ${fmt.dim(`(would clear remote link; keep ${CODEPEN_DIR}/ and Pen info)`)}`);
|
|
110
|
+
output.log(` ${fmt.dim('Online:')} ${fmt.cyan(previousUrl)} ${fmt.dim('(would remain untouched on CodePen)')}`);
|
|
111
|
+
output.log(`\n${fmt.cyan('ℹ')} Dry run complete — no local files were modified.`);
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
unlinked: false,
|
|
115
|
+
dryRun: true,
|
|
116
|
+
previousPenId,
|
|
117
|
+
previousUrl,
|
|
118
|
+
previousProfile,
|
|
119
|
+
rootDir: project.rootDir,
|
|
120
|
+
configPath: project.configPath,
|
|
121
|
+
retainedInfo: previewInfo,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
if (!options.yes) {
|
|
125
|
+
if (!interactive) {
|
|
126
|
+
throw new CliError(400, 'Refusing to unlink Pen in non-interactive mode without --yes.');
|
|
127
|
+
}
|
|
128
|
+
output.log(`\n${fmt.yellow('About to unlink local folder from CodePen Pen:')}`);
|
|
129
|
+
output.log(` Title: ${fmt.bold(displayTitle)} ${fmt.dim(`(${previousPenId})`)}`);
|
|
130
|
+
if (previousProfile) {
|
|
131
|
+
output.log(` Profile: ${fmt.cyan(previousProfile)}`);
|
|
132
|
+
}
|
|
133
|
+
output.log(` Editor: ${fmt.cyan(previousUrl)}`);
|
|
134
|
+
output.log(fmt.dim(`\nThe online Pen on CodePen, ${CODEPEN_DIR}/, and all local source files will stay untouched.\n` +
|
|
135
|
+
`Only the remote link fields in ${PROJECT_CONFIG_FILENAME} will be cleared (keeping "info").`));
|
|
136
|
+
const ok = await confirmImpl(`Unlink this local folder from the online Pen (keeps ${CODEPEN_DIR}/ and "info" in ${PROJECT_CONFIG_FILENAME})?`, false);
|
|
137
|
+
if (!ok) {
|
|
138
|
+
output.log('Aborted.');
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const { configPath, info: retainedInfo } = await unlinkLinkedProject(project);
|
|
143
|
+
if (options.json) {
|
|
144
|
+
printJsonResponse(200, `Unlinked local folder from ${displayTitle} (${previousPenId}); online Pen left untouched.`, {
|
|
145
|
+
unlinked: true,
|
|
146
|
+
previous_pen_id: previousPenId,
|
|
147
|
+
previous_url: previousUrl,
|
|
148
|
+
previous_profile: previousProfile,
|
|
149
|
+
root_dir: project.rootDir,
|
|
150
|
+
config_path: configPath,
|
|
151
|
+
info: retainedInfo,
|
|
152
|
+
}, output);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
output.log(`\n${fmt.boldGreen('✓')} Unlinked local folder from ${fmt.bold(displayTitle)} ${fmt.dim(`(${previousPenId})`)}`);
|
|
156
|
+
output.log(` ${fmt.dim('Updated:')} ${fmt.cyan(PROJECT_CONFIG_FILENAME)} ${fmt.dim(`(cleared remote link; kept ${CODEPEN_DIR}/ and Pen info)`)}`);
|
|
157
|
+
output.log(` ${fmt.dim('Online:')} ${fmt.cyan(previousUrl)} ${fmt.dim('(left untouched on CodePen)')}`);
|
|
158
|
+
output.log(` ${fmt.dim('Tip:')} Run \`cdpn push\` anytime to publish this folder as a new Pen.`);
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
unlinked: true,
|
|
162
|
+
dryRun: false,
|
|
163
|
+
previousPenId,
|
|
164
|
+
previousUrl,
|
|
165
|
+
previousProfile,
|
|
166
|
+
rootDir: project.rootDir,
|
|
167
|
+
configPath,
|
|
168
|
+
retainedInfo,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
export function registerUnlinkCommand(program) {
|
|
172
|
+
program
|
|
173
|
+
.command('unlink')
|
|
174
|
+
.argument('[target]', 'Directory path to unlink, or "all" to unlink all Pens in subdirectories')
|
|
175
|
+
.allowExcessArguments(false)
|
|
176
|
+
.description(`Unlink a local folder from its online Pen (pass "all" to walk subdirectories; keeps online Pens, ${CODEPEN_DIR}/, and Pen info)`)
|
|
177
|
+
.option('--dry-run', 'Preview what would be unlinked without modifying local files')
|
|
178
|
+
.option('-y, --yes', 'Skip confirmation prompt')
|
|
179
|
+
.option('--json', 'Output unlink result as JSON')
|
|
180
|
+
.action(async (target, options) => {
|
|
181
|
+
const result = await runUnlinkCommand({
|
|
182
|
+
target,
|
|
183
|
+
yes: options.yes,
|
|
184
|
+
dryRun: options.dryRun,
|
|
185
|
+
json: options.json,
|
|
186
|
+
});
|
|
187
|
+
if (result?.exitCode !== undefined) {
|
|
188
|
+
process.exitCode = result.exitCode;
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|