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
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { buildSyncedFileMap, writeProjectConfig, } from '../config/project.js';
|
|
2
|
+
import { PROJECT_CONFIG_FILENAME } from '../constants.js';
|
|
3
|
+
import { CliError } from '../errors.js';
|
|
4
|
+
import { computeSyncDiff, detectLocalChangesFromBaseline } from '../sync/diff.js';
|
|
5
|
+
import { scanLocalFiles } from '../sync/fs.js';
|
|
6
|
+
import { analyzeInfoSync, buildPenInfoConfigFields, formatInfoValue, hasAnyPenInfoFields, isLocalInfoDirty, } from '../sync/info.js';
|
|
7
|
+
import { syncRemoteFilesToDisk } from '../sync/materialize.js';
|
|
8
|
+
import { fmt, shortId } from '../utils/format.js';
|
|
9
|
+
import { defaultOutput } from '../utils/output.js';
|
|
10
|
+
import { promptConfirm } from '../utils/prompts/index.js';
|
|
11
|
+
import { fetchLinkedPenOrHandleMissing } from './missing-pen.js';
|
|
12
|
+
/**
|
|
13
|
+
* Executes a pull of remote files and Pen metadata from CodePen into an existing linked
|
|
14
|
+
* Pen directory, with full support for dirty-state guards and `--dry-run` preview.
|
|
15
|
+
* Returns `null` if the user cancels interactively (either on dirty files or missing Pen).
|
|
16
|
+
*/
|
|
17
|
+
export async function executePull(client, project, auth, options = {}) {
|
|
18
|
+
const output = options.output ?? defaultOutput;
|
|
19
|
+
const silent = Boolean(options.silent);
|
|
20
|
+
const dryRun = Boolean(options.dryRun);
|
|
21
|
+
const force = Boolean(options.force);
|
|
22
|
+
const interactive = Boolean(options.interactive) && !silent;
|
|
23
|
+
const confirmImpl = options.prompts?.confirm ?? promptConfirm;
|
|
24
|
+
const { rootDir, config } = project;
|
|
25
|
+
if (!silent) {
|
|
26
|
+
output.log(dryRun
|
|
27
|
+
? `Checking remote changes for ${fmt.cyan(config.pen_id)} (dry run)…`
|
|
28
|
+
: `Pulling latest changes for ${fmt.cyan(config.pen_id)}…`);
|
|
29
|
+
}
|
|
30
|
+
const localFiles = options.prefetchedLocalFiles ?? (await scanLocalFiles(rootDir));
|
|
31
|
+
const localDirty = detectLocalChangesFromBaseline(localFiles, config.files);
|
|
32
|
+
const dirtyCount = localDirty.added.length + localDirty.modified.length + localDirty.deleted.length;
|
|
33
|
+
const localInfoDirty = isLocalInfoDirty(config.info, config.last_synced_info_hash);
|
|
34
|
+
const hasLocalChanges = dirtyCount > 0 || localInfoDirty;
|
|
35
|
+
if (hasLocalChanges && !force && !dryRun) {
|
|
36
|
+
if (!silent) {
|
|
37
|
+
output.log(fmt.yellow('\nWarning: You have uncommitted local changes that will be overwritten:'));
|
|
38
|
+
for (const p of localDirty.added) {
|
|
39
|
+
output.log(` ${fmt.green('+')} ${p} ${fmt.dim('(will be removed)')}`);
|
|
40
|
+
}
|
|
41
|
+
for (const p of localDirty.modified) {
|
|
42
|
+
output.log(` ${fmt.yellow('~')} ${p} ${fmt.dim('(will be overwritten)')}`);
|
|
43
|
+
}
|
|
44
|
+
for (const p of localDirty.deleted) {
|
|
45
|
+
output.log(` ${fmt.red('-')} ${p} ${fmt.dim('(will be restored)')}`);
|
|
46
|
+
}
|
|
47
|
+
if (localInfoDirty) {
|
|
48
|
+
output.log(` ${fmt.yellow('~')} ${PROJECT_CONFIG_FILENAME} (info) ${fmt.dim('(will be overwritten)')}`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (!interactive) {
|
|
52
|
+
throw new CliError(409, 'Local changes detected. Commit/push them first, or re-run with --force.', {
|
|
53
|
+
dirty_files: localDirty,
|
|
54
|
+
...(localInfoDirty ? { dirty_info: true } : {}),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const ok = await confirmImpl('\nOverwrite local changes with remote version?', false);
|
|
58
|
+
if (!ok) {
|
|
59
|
+
output.log('Aborted.');
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let pen = options.prefetchedPen;
|
|
64
|
+
if (!pen) {
|
|
65
|
+
if (options.handleMissingPen !== false) {
|
|
66
|
+
const penLookup = await fetchLinkedPenOrHandleMissing({
|
|
67
|
+
client,
|
|
68
|
+
project,
|
|
69
|
+
auth,
|
|
70
|
+
commandName: 'pull',
|
|
71
|
+
interactive,
|
|
72
|
+
json: options.json,
|
|
73
|
+
output,
|
|
74
|
+
prompts: options.prompts,
|
|
75
|
+
});
|
|
76
|
+
if (!penLookup.found) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
pen = penLookup.pen;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
pen = await client.getPen(config.pen_id);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const remoteFiles = options.prefetchedRemoteFiles ?? (await client.getPenFiles(config.pen_id));
|
|
86
|
+
const hadInfoBefore = hasAnyPenInfoFields(config.info);
|
|
87
|
+
const infoAnalysis = analyzeInfoSync(config.info, config.last_synced_info_hash, pen);
|
|
88
|
+
if (dryRun) {
|
|
89
|
+
const diff = await computeSyncDiff({
|
|
90
|
+
localFiles,
|
|
91
|
+
remoteFiles,
|
|
92
|
+
baseline: config.files,
|
|
93
|
+
downloadFile: (url) => client.downloadFile(url),
|
|
94
|
+
});
|
|
95
|
+
// From pull's perspective:
|
|
96
|
+
// - diff.deleted = exists on remote, missing locally -> would add/write locally
|
|
97
|
+
// - diff.modified = differs -> would overwrite locally
|
|
98
|
+
// - diff.added = exists locally only -> would remove locally
|
|
99
|
+
const addedLocally = diff.deleted.map((item) => item.path).sort();
|
|
100
|
+
const updatedLocally = diff.modified.map((item) => item.path).sort();
|
|
101
|
+
const removedLocally = diff.added.map((item) => item.path).sort();
|
|
102
|
+
const wouldWritePaths = [...addedLocally, ...updatedLocally].sort();
|
|
103
|
+
const remoteHead = pen.saved_version?.id ?? null;
|
|
104
|
+
const versionChanged = Boolean(remoteHead && config.last_synced_version_id && remoteHead !== config.last_synced_version_id);
|
|
105
|
+
const hasAnyPullEffect = wouldWritePaths.length > 0 ||
|
|
106
|
+
removedLocally.length > 0 ||
|
|
107
|
+
infoAnalysis.changes.length > 0 ||
|
|
108
|
+
!hadInfoBefore ||
|
|
109
|
+
versionChanged;
|
|
110
|
+
if (!silent) {
|
|
111
|
+
if (hasLocalChanges && !force) {
|
|
112
|
+
output.log(fmt.yellow('\nNote: Uncommitted local changes exist (running without --dry-run will prompt or require --force).'));
|
|
113
|
+
}
|
|
114
|
+
if (!hasAnyPullEffect) {
|
|
115
|
+
output.log(`\n${fmt.green('✓')} Everything is up to date — nothing to pull.`);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
output.log(`\nDry-run pulling ${fmt.bold(pen.title || 'Untitled')} ${fmt.dim(`(${pen.id})`)}:`);
|
|
119
|
+
for (const p of addedLocally)
|
|
120
|
+
output.log(` ${fmt.green('+')} ${p}`);
|
|
121
|
+
for (const p of updatedLocally)
|
|
122
|
+
output.log(` ${fmt.yellow('~')} ${p}`);
|
|
123
|
+
for (const p of removedLocally)
|
|
124
|
+
output.log(` ${fmt.red('-')} ${p}`);
|
|
125
|
+
for (const change of infoAnalysis.changes) {
|
|
126
|
+
output.log(` ${fmt.yellow('~')} info.${change.field}: ${formatInfoValue(change.field, change.local)} → ${formatInfoValue(change.field, change.remote)}`);
|
|
127
|
+
}
|
|
128
|
+
if (!hadInfoBefore && infoAnalysis.changes.length === 0) {
|
|
129
|
+
output.log(` ${fmt.cyan('~')} ${PROJECT_CONFIG_FILENAME} (add Pen info)`);
|
|
130
|
+
}
|
|
131
|
+
if (versionChanged && wouldWritePaths.length === 0 && removedLocally.length === 0) {
|
|
132
|
+
output.log(` ${fmt.cyan('~')} version: ${shortId(config.last_synced_version_id)} → ${shortId(remoteHead)}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
output.log(`\n${fmt.cyan('ℹ')} Dry run complete — no local files were modified.`);
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
pen,
|
|
139
|
+
writtenPaths: wouldWritePaths,
|
|
140
|
+
deletedPaths: removedLocally,
|
|
141
|
+
infoChanges: infoAnalysis.changes,
|
|
142
|
+
hadInfoBefore,
|
|
143
|
+
hasLocalChanges,
|
|
144
|
+
dryRun: true,
|
|
145
|
+
auth,
|
|
146
|
+
project,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const result = await syncRemoteFilesToDisk({
|
|
150
|
+
client,
|
|
151
|
+
rootDir,
|
|
152
|
+
remoteFiles,
|
|
153
|
+
existingLocalPaths: localFiles.keys(),
|
|
154
|
+
pruneRemoved: true,
|
|
155
|
+
});
|
|
156
|
+
const syncedFiles = buildSyncedFileMap(remoteFiles, result.localSha256ByPath);
|
|
157
|
+
const profileToPersist = options.persistProfile === false
|
|
158
|
+
? config.profile
|
|
159
|
+
: (config.profile ?? auth.profile ?? undefined);
|
|
160
|
+
const nextConfig = {
|
|
161
|
+
url: config.url || pen.urls.editor,
|
|
162
|
+
pen_id: pen.id,
|
|
163
|
+
...(profileToPersist ? { profile: profileToPersist } : {}),
|
|
164
|
+
last_synced_version_id: pen.saved_version?.id ?? null,
|
|
165
|
+
editor: pen.editor,
|
|
166
|
+
...buildPenInfoConfigFields(pen),
|
|
167
|
+
files: syncedFiles,
|
|
168
|
+
};
|
|
169
|
+
const configPath = await writeProjectConfig(rootDir, nextConfig);
|
|
170
|
+
if (!silent) {
|
|
171
|
+
output.log(`\n${fmt.green('✓')} Synced ${fmt.bold(pen.title || 'Untitled')} ${fmt.dim(`(${pen.id})`)}`);
|
|
172
|
+
if (auth.profile) {
|
|
173
|
+
output.log(` Profile: ${fmt.cyan(auth.profile)}`);
|
|
174
|
+
}
|
|
175
|
+
output.log(` Updated: ${result.writtenPaths.length} file(s)` +
|
|
176
|
+
(result.deletedPaths.length > 0
|
|
177
|
+
? `, removed ${result.deletedPaths.length} deleted file(s)`
|
|
178
|
+
: ''));
|
|
179
|
+
if (!hadInfoBefore) {
|
|
180
|
+
output.log(` Info: ${fmt.dim(`added to ${PROJECT_CONFIG_FILENAME}`)}`);
|
|
181
|
+
}
|
|
182
|
+
else if (infoAnalysis.changes.length > 0) {
|
|
183
|
+
output.log(` Info: ${fmt.dim(`updated ${infoAnalysis.changes.map((c) => c.field).join(', ')} in ${PROJECT_CONFIG_FILENAME}`)}`);
|
|
184
|
+
}
|
|
185
|
+
if (pen.saved_version) {
|
|
186
|
+
output.log(` Version: ${fmt.dim(shortId(pen.saved_version.id))}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
pen,
|
|
191
|
+
writtenPaths: result.writtenPaths,
|
|
192
|
+
deletedPaths: result.deletedPaths,
|
|
193
|
+
infoChanges: infoAnalysis.changes,
|
|
194
|
+
hadInfoBefore,
|
|
195
|
+
hasLocalChanges,
|
|
196
|
+
dryRun: false,
|
|
197
|
+
auth,
|
|
198
|
+
project: {
|
|
199
|
+
rootDir,
|
|
200
|
+
configPath,
|
|
201
|
+
config: nextConfig,
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { buildSyncedFileMap, writeProjectConfig, } from '../config/project.js';
|
|
2
|
+
import { PROJECT_CONFIG_FILENAME } from '../constants.js';
|
|
3
|
+
import { CliError } from '../errors.js';
|
|
4
|
+
import { computeSyncDiff, hasDiffChanges } from '../sync/diff.js';
|
|
5
|
+
import { scanLocalFiles } from '../sync/fs.js';
|
|
6
|
+
import { analyzeInfoSync, buildInfoUpdatePatch, buildPenInfoConfigFields, formatInfoValue, hashPenInfo, isCompletePenInfo, } from '../sync/info.js';
|
|
7
|
+
import { applyCompilerPatchesToDisk } from '../sync/materialize.js';
|
|
8
|
+
import { fmt, printBuildSummary, shortId } from '../utils/format.js';
|
|
9
|
+
import { defaultOutput } from '../utils/output.js';
|
|
10
|
+
import { handleMissingRemotePen, isRemotePenNotFoundError } from './missing-pen.js';
|
|
11
|
+
/**
|
|
12
|
+
* Executes a push of local file and `.cdpn.json` metadata changes to an already-linked
|
|
13
|
+
* CodePen 2.0 Pen, handling build convergence, compiler patch sync-back, and conflict detection.
|
|
14
|
+
*/
|
|
15
|
+
export async function executePush(client, project, options = {}) {
|
|
16
|
+
const output = options.output ?? defaultOutput;
|
|
17
|
+
const silent = Boolean(options.silent);
|
|
18
|
+
let pen = options.prefetchedPen;
|
|
19
|
+
if (!pen) {
|
|
20
|
+
try {
|
|
21
|
+
pen = await client.getPen(project.config.pen_id);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (isRemotePenNotFoundError(error)) {
|
|
25
|
+
await handleMissingRemotePen({
|
|
26
|
+
client,
|
|
27
|
+
project,
|
|
28
|
+
auth: { profile: project.config.profile ?? null },
|
|
29
|
+
commandName: 'push',
|
|
30
|
+
interactive: false,
|
|
31
|
+
json: options.json,
|
|
32
|
+
output,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const [remoteFiles, localFiles] = await Promise.all([
|
|
39
|
+
client.getPenFiles(project.config.pen_id),
|
|
40
|
+
scanLocalFiles(project.rootDir),
|
|
41
|
+
]);
|
|
42
|
+
if (pen.editor === 'Classic') {
|
|
43
|
+
throw new CliError(403, 'This is a Classic Pen, which is read-only in CodePen API v2. Only CodePen 2.0 Pens can be pushed.');
|
|
44
|
+
}
|
|
45
|
+
const localHead = project.config.last_synced_version_id;
|
|
46
|
+
const remoteHead = pen.saved_version?.id ?? null;
|
|
47
|
+
if (remoteHead && localHead && remoteHead !== localHead && !options.force) {
|
|
48
|
+
throw new CliError(409, `Remote Pen head (${shortId(remoteHead)}) has moved ahead of your last synced version (${shortId(localHead)}).\n` +
|
|
49
|
+
'Run `cdpn status` to inspect differences, `cdpn pull` to sync remote changes, or `cdpn push --force` to overwrite.', {
|
|
50
|
+
local_head: localHead,
|
|
51
|
+
remote_head: remoteHead,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const diff = await computeSyncDiff({
|
|
55
|
+
localFiles,
|
|
56
|
+
remoteFiles,
|
|
57
|
+
baseline: project.config.files,
|
|
58
|
+
downloadFile: (url) => client.downloadFile(url),
|
|
59
|
+
});
|
|
60
|
+
const infoAnalysis = analyzeInfoSync(project.config.info, project.config.last_synced_info_hash, pen);
|
|
61
|
+
if (infoAnalysis.localChanged && infoAnalysis.remoteChanged && !options.force) {
|
|
62
|
+
const fields = infoAnalysis.changes.map((c) => c.field).join(', ');
|
|
63
|
+
throw new CliError(409, `Remote Pen info has changed since your last sync, and you also have local info edits in ${PROJECT_CONFIG_FILENAME} (${fields}).\n` +
|
|
64
|
+
'Run `cdpn status` to inspect differences, `cdpn pull` to sync remote changes, or `cdpn push --force` to overwrite.', {
|
|
65
|
+
info_changes: infoAnalysis.changes,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const hasFileChanges = hasDiffChanges(diff);
|
|
69
|
+
const shouldPushInfo = infoAnalysis.changes.length > 0 && (infoAnalysis.localChanged || Boolean(options.force));
|
|
70
|
+
const infoChangesToPush = shouldPushInfo ? infoAnalysis.changes : [];
|
|
71
|
+
// Validate info patch constraints up-front before making any write API calls
|
|
72
|
+
const infoPatch = shouldPushInfo ? buildInfoUpdatePatch(infoChangesToPush) : null;
|
|
73
|
+
if (!hasFileChanges && !shouldPushInfo) {
|
|
74
|
+
let backfilledLegacyInfo = false;
|
|
75
|
+
if (!options.dryRun && infoAnalysis.changes.length === 0) {
|
|
76
|
+
const remoteHash = hashPenInfo(infoAnalysis.remoteInfo);
|
|
77
|
+
const needsInfoUpgrade = !infoAnalysis.tracked ||
|
|
78
|
+
!isCompletePenInfo(project.config.info) ||
|
|
79
|
+
project.config.last_synced_info_hash !== remoteHash;
|
|
80
|
+
if (needsInfoUpgrade) {
|
|
81
|
+
backfilledLegacyInfo = !infoAnalysis.tracked;
|
|
82
|
+
const nextConfig = {
|
|
83
|
+
...project.config,
|
|
84
|
+
...buildPenInfoConfigFields(pen),
|
|
85
|
+
};
|
|
86
|
+
await writeProjectConfig(project.rootDir, nextConfig);
|
|
87
|
+
project.config = nextConfig;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (!silent) {
|
|
91
|
+
if (infoAnalysis.remoteChanged) {
|
|
92
|
+
output.log(`${fmt.cyan('ℹ')} No local changes to push. Remote Pen info has changed on CodePen — run \`cdpn pull\` to update ${PROJECT_CONFIG_FILENAME}, or \`cdpn push --force\` to overwrite.`);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
output.log(`${fmt.green('✓')} Everything is up to date — nothing to push.`);
|
|
96
|
+
if (backfilledLegacyInfo) {
|
|
97
|
+
output.log(fmt.dim(` Added Pen info to ${PROJECT_CONFIG_FILENAME}.`));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
pushed: false,
|
|
103
|
+
dryRun: Boolean(options.dryRun),
|
|
104
|
+
pen,
|
|
105
|
+
versionId: remoteHead,
|
|
106
|
+
build: pen.saved_version?.build ?? null,
|
|
107
|
+
changes: { added: [], modified: [], deleted: [] },
|
|
108
|
+
infoChanges: [],
|
|
109
|
+
appliedPatches: [],
|
|
110
|
+
remoteInfoChanged: infoAnalysis.remoteChanged,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const changes = {
|
|
114
|
+
added: diff.added.map((item) => item.path),
|
|
115
|
+
modified: diff.modified.map((item) => item.path),
|
|
116
|
+
deleted: diff.deleted.map((item) => item.path),
|
|
117
|
+
};
|
|
118
|
+
if (!silent) {
|
|
119
|
+
output.log(`${options.dryRun ? 'Dry-run pushing' : 'Pushing'} changes to ${fmt.bold(pen.title || 'Untitled')} ${fmt.dim(`(${pen.id})`)}:`);
|
|
120
|
+
for (const item of diff.added)
|
|
121
|
+
output.log(` ${fmt.green('+')} ${item.path}`);
|
|
122
|
+
for (const item of diff.modified)
|
|
123
|
+
output.log(` ${fmt.yellow('~')} ${item.path}`);
|
|
124
|
+
for (const item of diff.deleted)
|
|
125
|
+
output.log(` ${fmt.red('-')} ${item.path}`);
|
|
126
|
+
for (const change of infoChangesToPush) {
|
|
127
|
+
output.log(` ${fmt.yellow('~')} info.${change.field}: ${formatInfoValue(change.field, change.remote)} → ${formatInfoValue(change.field, change.local)}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
let finalBuild = pen.saved_version?.build ?? null;
|
|
131
|
+
let finalVersionId = remoteHead;
|
|
132
|
+
let appliedPatches = [];
|
|
133
|
+
let updatedPen = pen;
|
|
134
|
+
let finalRemoteFiles;
|
|
135
|
+
let localShaByPath;
|
|
136
|
+
if (hasFileChanges) {
|
|
137
|
+
const fileChanges = [];
|
|
138
|
+
const binaryUploads = [];
|
|
139
|
+
for (const item of [...diff.added, ...diff.modified]) {
|
|
140
|
+
const local = item.local;
|
|
141
|
+
// Note: CodePen API returns 400 on empty multipart parts, so 0-byte files always go as text JSON
|
|
142
|
+
if (local.isBinary && local.buffer.length > 0) {
|
|
143
|
+
binaryUploads.push({ path: item.path, content: local.buffer });
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
fileChanges.push({
|
|
147
|
+
path: item.path,
|
|
148
|
+
content: local.textContent ?? '',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const item of diff.deleted) {
|
|
153
|
+
fileChanges.push({
|
|
154
|
+
path: item.path,
|
|
155
|
+
delete: true,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const expectedVersionId = options.force ? undefined : (localHead ?? remoteHead ?? undefined);
|
|
159
|
+
const shouldWait = options.wait !== false;
|
|
160
|
+
if (!silent) {
|
|
161
|
+
output.log(shouldWait ? '\nUploading and building on CodePen…' : '\nUploading to CodePen…');
|
|
162
|
+
}
|
|
163
|
+
const writeResult = await client.writePenFiles(pen.id, {
|
|
164
|
+
files: fileChanges,
|
|
165
|
+
...(options.label ? { label: options.label } : {}),
|
|
166
|
+
...(expectedVersionId ? { expected_version_id: expectedVersionId } : {}),
|
|
167
|
+
...(options.dryRun ? { dry_run: true } : {}),
|
|
168
|
+
wait: shouldWait ? 45 : 0,
|
|
169
|
+
}, binaryUploads);
|
|
170
|
+
finalBuild = writeResult.build;
|
|
171
|
+
if (finalBuild && shouldWait) {
|
|
172
|
+
let lastStatus = `${finalBuild.status}:${finalBuild.id}`;
|
|
173
|
+
finalBuild = await client.settleBuild(finalBuild, {
|
|
174
|
+
onUpdate: (b) => {
|
|
175
|
+
if (silent)
|
|
176
|
+
return;
|
|
177
|
+
const key = `${b.status}:${b.id}`;
|
|
178
|
+
if (key !== lastStatus) {
|
|
179
|
+
lastStatus = key;
|
|
180
|
+
if (b.status === 'Running' || b.status === 'Queued') {
|
|
181
|
+
output.log(fmt.dim(` Build ${shortId(b.id)}: ${b.status}…`));
|
|
182
|
+
}
|
|
183
|
+
else if (b.status === 'Canceled' && b.next_id) {
|
|
184
|
+
output.log(fmt.dim(' Compiler requested patches — waiting for converged rebuild…'));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
if (finalBuild && !silent) {
|
|
191
|
+
printBuildSummary(finalBuild, output);
|
|
192
|
+
}
|
|
193
|
+
finalVersionId = finalBuild?.version_id ?? writeResult.version?.id ?? null;
|
|
194
|
+
appliedPatches = finalBuild?.applied_patches ?? [];
|
|
195
|
+
updatedPen = writeResult.pen ?? pen;
|
|
196
|
+
if (!options.dryRun) {
|
|
197
|
+
// Re-read the converged version's source files so we write back any compiler patches
|
|
198
|
+
let convergedFiles = writeResult.version?.files;
|
|
199
|
+
if (finalVersionId && (appliedPatches.length > 0 || !convergedFiles)) {
|
|
200
|
+
const version = await client.getVersion(finalVersionId);
|
|
201
|
+
convergedFiles = version.files;
|
|
202
|
+
}
|
|
203
|
+
if (finalBuild && appliedPatches.length > 0) {
|
|
204
|
+
await applyCompilerPatchesToDisk({
|
|
205
|
+
client,
|
|
206
|
+
rootDir: project.rootDir,
|
|
207
|
+
patches: appliedPatches,
|
|
208
|
+
convergedFiles,
|
|
209
|
+
});
|
|
210
|
+
if (!silent) {
|
|
211
|
+
output.log(fmt.cyan('✓ Synced compiler patches back to local folder.'));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Re-scan local hashes so .cdpn.json reflects exact post-patch local state
|
|
215
|
+
const postPushLocalFiles = await scanLocalFiles(project.rootDir);
|
|
216
|
+
localShaByPath = new Map();
|
|
217
|
+
for (const [p, lf] of postPushLocalFiles) {
|
|
218
|
+
localShaByPath.set(p, lf.sha256);
|
|
219
|
+
}
|
|
220
|
+
finalRemoteFiles = convergedFiles ?? (await client.getPenFiles(pen.id));
|
|
221
|
+
// If an info PATCH is still pending, persist the synced file manifest now while preserving
|
|
222
|
+
// unpushed local `info` so a subsequent info validation error never leaves version metadata stale.
|
|
223
|
+
if (shouldPushInfo) {
|
|
224
|
+
const intermediateConfig = {
|
|
225
|
+
url: project.config.url || updatedPen.urls.editor,
|
|
226
|
+
pen_id: updatedPen.id,
|
|
227
|
+
...(project.config.profile ? { profile: project.config.profile } : {}),
|
|
228
|
+
last_synced_version_id: finalVersionId,
|
|
229
|
+
...(project.config.last_synced_info_hash
|
|
230
|
+
? { last_synced_info_hash: project.config.last_synced_info_hash }
|
|
231
|
+
: {}),
|
|
232
|
+
editor: updatedPen.editor,
|
|
233
|
+
...(project.config.info !== undefined ? { info: project.config.info } : {}),
|
|
234
|
+
files: buildSyncedFileMap(finalRemoteFiles, localShaByPath),
|
|
235
|
+
};
|
|
236
|
+
await writeProjectConfig(project.rootDir, intermediateConfig);
|
|
237
|
+
project.config = intermediateConfig;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (options.dryRun) {
|
|
242
|
+
const dryRunBuildFailed = hasFileChanges && finalBuild?.status === 'Failed';
|
|
243
|
+
if (!silent) {
|
|
244
|
+
output.log(`\n${fmt.cyan('ℹ')} Dry run complete — no changes were saved to CodePen` +
|
|
245
|
+
(hasFileChanges && finalVersionId
|
|
246
|
+
? ` ${fmt.dim(`(ephemeral version ${shortId(finalVersionId)})`)}.`
|
|
247
|
+
: '.'));
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
pushed: false,
|
|
251
|
+
dryRun: true,
|
|
252
|
+
pen: updatedPen,
|
|
253
|
+
versionId: hasFileChanges ? finalVersionId : remoteHead,
|
|
254
|
+
build: hasFileChanges ? finalBuild : null,
|
|
255
|
+
changes,
|
|
256
|
+
infoChanges: infoChangesToPush,
|
|
257
|
+
appliedPatches,
|
|
258
|
+
...(dryRunBuildFailed ? { exitCode: 1 } : {}),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
if (shouldPushInfo && infoPatch) {
|
|
262
|
+
if (!silent) {
|
|
263
|
+
output.log('\nUpdating Pen info on CodePen…');
|
|
264
|
+
}
|
|
265
|
+
updatedPen = await client.updatePen(pen.id, infoPatch);
|
|
266
|
+
}
|
|
267
|
+
const nextConfig = {
|
|
268
|
+
url: project.config.url || updatedPen.urls.editor,
|
|
269
|
+
pen_id: updatedPen.id,
|
|
270
|
+
...(project.config.profile ? { profile: project.config.profile } : {}),
|
|
271
|
+
last_synced_version_id: hasFileChanges ? finalVersionId : project.config.last_synced_version_id,
|
|
272
|
+
editor: updatedPen.editor,
|
|
273
|
+
...buildPenInfoConfigFields(updatedPen),
|
|
274
|
+
...(hasFileChanges && finalRemoteFiles && localShaByPath
|
|
275
|
+
? { files: buildSyncedFileMap(finalRemoteFiles, localShaByPath) }
|
|
276
|
+
: project.config.files
|
|
277
|
+
? { files: project.config.files }
|
|
278
|
+
: {}),
|
|
279
|
+
};
|
|
280
|
+
await writeProjectConfig(project.rootDir, nextConfig);
|
|
281
|
+
project.config = nextConfig;
|
|
282
|
+
if (!silent) {
|
|
283
|
+
if (hasFileChanges && !shouldPushInfo && infoAnalysis.remoteChanged) {
|
|
284
|
+
output.log(fmt.cyan(`ℹ Synced remote Pen info into ${PROJECT_CONFIG_FILENAME}.`));
|
|
285
|
+
}
|
|
286
|
+
else if (hasFileChanges && !infoAnalysis.tracked) {
|
|
287
|
+
output.log(fmt.dim(`Added Pen info to ${PROJECT_CONFIG_FILENAME}.`));
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
const buildFailed = hasFileChanges && finalBuild?.status === 'Failed';
|
|
291
|
+
if (buildFailed) {
|
|
292
|
+
if (!silent) {
|
|
293
|
+
output.log(`\n${fmt.yellow('!')} Files were saved as version ${fmt.dim(shortId(finalVersionId))}, but the build failed.`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
else if (!silent) {
|
|
297
|
+
if (hasFileChanges) {
|
|
298
|
+
output.log(`\n${fmt.green('✓')} Pushed successfully ${fmt.dim(`(version ${shortId(finalVersionId)})`)}`);
|
|
299
|
+
output.log(` Preview: ${fmt.cyan(updatedPen.urls.preview)}`);
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
output.log(`\n${fmt.green('✓')} Updated Pen info successfully.`);
|
|
303
|
+
output.log(` Editor: ${fmt.cyan(updatedPen.urls.editor)}`);
|
|
304
|
+
output.log(` Preview: ${fmt.cyan(updatedPen.urls.preview)}`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return {
|
|
308
|
+
pushed: true,
|
|
309
|
+
dryRun: false,
|
|
310
|
+
pen: updatedPen,
|
|
311
|
+
versionId: finalVersionId,
|
|
312
|
+
build: finalBuild,
|
|
313
|
+
changes,
|
|
314
|
+
infoChanges: infoChangesToPush,
|
|
315
|
+
appliedPatches,
|
|
316
|
+
...(buildFailed ? { exitCode: 1 } : {}),
|
|
317
|
+
};
|
|
318
|
+
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
2
|
import { isCodePenApiError } from '../api/errors.js';
|
|
4
|
-
import { hasProjectConfigInDir
|
|
3
|
+
import { hasProjectConfigInDir } from '../config/project.js';
|
|
4
|
+
import { PROJECT_CONFIG_FILENAME } from '../constants.js';
|
|
5
|
+
import { CliError } from '../errors.js';
|
|
5
6
|
import { isDirectoryNonEmpty, normalizePenPath } from '../sync/fs.js';
|
|
6
|
-
import {
|
|
7
|
+
import { normalizePenReference } from '../utils/codepen-url.js';
|
|
7
8
|
/**
|
|
8
9
|
* Ensures a local target directory either does not exist yet or is completely empty
|
|
9
10
|
* (and does not already contain `.cdpn.json`).
|