codepen-commander 1.1.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 +17 -4
- package/commands/clone.js +2 -0
- 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/commands/status.js
CHANGED
|
@@ -1,107 +1,131 @@
|
|
|
1
1
|
import { CodePenClient } from '../api/client.js';
|
|
2
2
|
import { API_KEY_ENV_VAR } from '../config/auth.js';
|
|
3
|
-
import { requireProject } from '../config/project.js';
|
|
4
|
-
import { computeSyncDiff, detectLocalChangesFromBaseline, hasDiffChanges } from '../sync/diff.js';
|
|
3
|
+
import { PROJECT_CONFIG_FILENAME, requireProject } from '../config/project.js';
|
|
4
|
+
import { computeSyncDiff, detectLocalChangesFromBaseline, hasDiffChanges, } from '../sync/diff.js';
|
|
5
5
|
import { scanLocalFiles } from '../sync/fs.js';
|
|
6
|
+
import { analyzeInfoSync, formatInfoValue } from '../sync/info.js';
|
|
6
7
|
import { fmt, shortId } from '../utils/format.js';
|
|
7
8
|
import { printJsonResponse } from '../utils/json.js';
|
|
8
9
|
import { getGlobalProfileOption, resolveCommandAuth } from './auth-context.js';
|
|
9
|
-
export function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
editor: pen.editor,
|
|
49
|
-
profile: auth.profile ?? project.config.profile ?? null,
|
|
50
|
-
auth_source: auth.source,
|
|
51
|
-
url: project.config.url,
|
|
52
|
-
local_version_id: localHead,
|
|
53
|
-
remote_version_id: remoteHead,
|
|
54
|
-
remote_ahead: remoteMoved,
|
|
55
|
-
up_to_date: changeCount === 0 && !remoteMoved,
|
|
56
|
-
build_status: pen.saved_version?.build?.status ?? null,
|
|
57
|
-
deployment: pen.deployment,
|
|
58
|
-
changes: {
|
|
59
|
-
added: diff.added.map((i) => i.path),
|
|
60
|
-
modified: diff.modified.map((i) => i.path),
|
|
61
|
-
deleted: diff.deleted.map((i) => i.path),
|
|
62
|
-
unchanged: diff.unchanged.map((i) => i.path),
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
return;
|
|
10
|
+
export async function statusPenCommand(options = {}) {
|
|
11
|
+
const cwd = options.cwd ?? process.cwd();
|
|
12
|
+
const project = await requireProject(cwd);
|
|
13
|
+
const auth = await resolveCommandAuth({
|
|
14
|
+
explicitProfile: options.explicitProfile,
|
|
15
|
+
project,
|
|
16
|
+
interactive: options.json ? false : options.interactive,
|
|
17
|
+
persistProfileToProject: !options.json,
|
|
18
|
+
env: options.env,
|
|
19
|
+
homeDir: options.homeDir,
|
|
20
|
+
selectProfile: options.selectProfile,
|
|
21
|
+
});
|
|
22
|
+
const client = new CodePenClient({ apiKey: auth.apiKey, baseUrl: options.baseUrl });
|
|
23
|
+
const [pen, remoteFiles, localFiles] = await Promise.all([
|
|
24
|
+
client.getPen(project.config.pen_id),
|
|
25
|
+
client.getPenFiles(project.config.pen_id),
|
|
26
|
+
scanLocalFiles(project.rootDir),
|
|
27
|
+
]);
|
|
28
|
+
const diff = await computeSyncDiff({
|
|
29
|
+
localFiles,
|
|
30
|
+
remoteFiles,
|
|
31
|
+
baseline: project.config.files,
|
|
32
|
+
downloadFile: (url) => client.downloadFile(url),
|
|
33
|
+
});
|
|
34
|
+
const infoAnalysis = analyzeInfoSync(project.config.info, project.config.last_synced_info_hash, pen);
|
|
35
|
+
const localHead = project.config.last_synced_version_id;
|
|
36
|
+
const remoteHead = pen.saved_version?.id ?? null;
|
|
37
|
+
const remoteMoved = Boolean(remoteHead && localHead && remoteHead !== localHead);
|
|
38
|
+
const localBaselineChanges = detectLocalChangesFromBaseline(localFiles, project.config.files);
|
|
39
|
+
const fileChangeCount = diff.added.length + diff.modified.length + diff.deleted.length;
|
|
40
|
+
const infoChangeCount = infoAnalysis.changes.length;
|
|
41
|
+
const totalChanges = fileChangeCount + infoChangeCount;
|
|
42
|
+
const upToDate = totalChanges === 0 && !remoteMoved;
|
|
43
|
+
if (options.json) {
|
|
44
|
+
let message;
|
|
45
|
+
if (totalChanges === 0) {
|
|
46
|
+
message = remoteMoved
|
|
47
|
+
? 'Local files match remote content, but version metadata is behind.'
|
|
48
|
+
: `Up to date with CodePen (${diff.unchanged.length} file(s)).`;
|
|
66
49
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
console.log(`URL: ${fmt.cyan(project.config.url)}`);
|
|
70
|
-
console.log(`Synced: ${fmt.dim(shortId(localHead))}`);
|
|
71
|
-
console.log(`Remote: ${fmt.dim(shortId(remoteHead))}` +
|
|
72
|
-
(remoteMoved ? ` ${fmt.yellow('(remote head has moved since last sync!)')}` : ''));
|
|
73
|
-
const buildStatus = pen.saved_version?.build?.status;
|
|
74
|
-
if (buildStatus) {
|
|
75
|
-
const colored = buildStatus === 'Success'
|
|
76
|
-
? fmt.green(buildStatus)
|
|
77
|
-
: buildStatus === 'Failed'
|
|
78
|
-
? fmt.red(buildStatus)
|
|
79
|
-
: fmt.yellow(buildStatus);
|
|
80
|
-
console.log(`Build: ${colored}`);
|
|
50
|
+
else if (fileChangeCount > 0 && infoChangeCount > 0) {
|
|
51
|
+
message = `Found ${fileChangeCount} file difference(s) and ${infoChangeCount} Pen info difference(s).`;
|
|
81
52
|
}
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
const deployNote = deployedIsHead
|
|
85
|
-
? fmt.green('up to date')
|
|
86
|
-
: fmt.yellow(`older version ${shortId(pen.deployment.version_id)}`);
|
|
87
|
-
console.log(`Deployed: ${fmt.cyan(pen.deployment.url)} ${fmt.dim(`(${deployNote})`)}`);
|
|
88
|
-
if (pen.deployment.custom_domains.length > 0) {
|
|
89
|
-
console.log(`Domains: ${pen.deployment.custom_domains.join(', ')}`);
|
|
90
|
-
}
|
|
53
|
+
else if (fileChangeCount > 0) {
|
|
54
|
+
message = `Found ${fileChangeCount} difference(s) between local folder and remote head.`;
|
|
91
55
|
}
|
|
92
56
|
else {
|
|
93
|
-
|
|
57
|
+
message = `Found ${infoChangeCount} Pen info difference(s) between ${PROJECT_CONFIG_FILENAME} and CodePen.`;
|
|
94
58
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
59
|
+
printJsonResponse(200, message, {
|
|
60
|
+
pen_id: pen.id,
|
|
61
|
+
title: pen.title,
|
|
62
|
+
editor: pen.editor,
|
|
63
|
+
profile: auth.profile ?? project.config.profile ?? null,
|
|
64
|
+
auth_source: auth.source,
|
|
65
|
+
url: project.config.url,
|
|
66
|
+
local_version_id: localHead,
|
|
67
|
+
remote_version_id: remoteHead,
|
|
68
|
+
remote_ahead: remoteMoved,
|
|
69
|
+
up_to_date: upToDate,
|
|
70
|
+
build_status: pen.saved_version?.build?.status ?? null,
|
|
71
|
+
deployment: pen.deployment,
|
|
72
|
+
changes: {
|
|
73
|
+
added: diff.added.map((i) => i.path),
|
|
74
|
+
modified: diff.modified.map((i) => i.path),
|
|
75
|
+
deleted: diff.deleted.map((i) => i.path),
|
|
76
|
+
unchanged: diff.unchanged.map((i) => i.path),
|
|
77
|
+
},
|
|
78
|
+
info_tracked: infoAnalysis.tracked,
|
|
79
|
+
info_local_changed: infoAnalysis.localChanged,
|
|
80
|
+
info_remote_changed: infoAnalysis.remoteChanged,
|
|
81
|
+
info_changes: infoAnalysis.changes,
|
|
82
|
+
});
|
|
83
|
+
return { pen, auth, project, diff, infoAnalysis, remoteMoved, upToDate };
|
|
84
|
+
}
|
|
85
|
+
console.log(`${fmt.bold(pen.title || 'Untitled')} ${fmt.dim(`(${pen.id}, ${pen.editor})`)}`);
|
|
86
|
+
console.log(`Profile: ${auth.profile ? fmt.cyan(auth.profile) : fmt.dim(`env (${API_KEY_ENV_VAR})`)}`);
|
|
87
|
+
console.log(`URL: ${fmt.cyan(project.config.url)}`);
|
|
88
|
+
console.log(`Synced: ${fmt.dim(shortId(localHead))}`);
|
|
89
|
+
console.log(`Remote: ${fmt.dim(shortId(remoteHead))}` +
|
|
90
|
+
(remoteMoved ? ` ${fmt.yellow('(remote head has moved since last sync!)')}` : ''));
|
|
91
|
+
const buildStatus = pen.saved_version?.build?.status;
|
|
92
|
+
if (buildStatus) {
|
|
93
|
+
const colored = buildStatus === 'Success'
|
|
94
|
+
? fmt.green(buildStatus)
|
|
95
|
+
: buildStatus === 'Failed'
|
|
96
|
+
? fmt.red(buildStatus)
|
|
97
|
+
: fmt.yellow(buildStatus);
|
|
98
|
+
console.log(`Build: ${colored}`);
|
|
99
|
+
}
|
|
100
|
+
if (pen.deployment) {
|
|
101
|
+
const deployedIsHead = pen.deployment.version_id === remoteHead;
|
|
102
|
+
const deployNote = deployedIsHead
|
|
103
|
+
? fmt.green('up to date')
|
|
104
|
+
: fmt.yellow(`older version ${shortId(pen.deployment.version_id)}`);
|
|
105
|
+
console.log(`Deployed: ${fmt.cyan(pen.deployment.url)} ${fmt.dim(`(${deployNote})`)}`);
|
|
106
|
+
if (pen.deployment.custom_domains.length > 0) {
|
|
107
|
+
console.log(`Domains: ${pen.deployment.custom_domains.join(', ')}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
console.log(`Deployed: ${fmt.dim('not deployed')}`);
|
|
112
|
+
}
|
|
113
|
+
console.log('');
|
|
114
|
+
const hasFileChanges = hasDiffChanges(diff);
|
|
115
|
+
const hasInfoChanges = infoAnalysis.changes.length > 0;
|
|
116
|
+
if (!hasFileChanges && !hasInfoChanges) {
|
|
117
|
+
if (remoteMoved) {
|
|
118
|
+
console.log(`${fmt.yellow('!')} Local files match remote content, but version metadata is behind. Run \`cdpn pull\` to update ${fmt.dim(PROJECT_CONFIG_FILENAME)}.`);
|
|
104
119
|
}
|
|
120
|
+
else {
|
|
121
|
+
console.log(`${fmt.green('✓')} Up to date with CodePen (${diff.unchanged.length} file(s)).`);
|
|
122
|
+
}
|
|
123
|
+
if (!infoAnalysis.tracked) {
|
|
124
|
+
console.log(fmt.dim(`Note: ${PROJECT_CONFIG_FILENAME} does not include Pen info yet. Run \`cdpn pull\` or \`cdpn push\` to add it.`));
|
|
125
|
+
}
|
|
126
|
+
return { pen, auth, project, diff, infoAnalysis, remoteMoved, upToDate };
|
|
127
|
+
}
|
|
128
|
+
if (hasFileChanges) {
|
|
105
129
|
const localModifiedSet = new Set([
|
|
106
130
|
...localBaselineChanges.added,
|
|
107
131
|
...localBaselineChanges.modified,
|
|
@@ -120,12 +144,47 @@ export function registerStatusCommand(program) {
|
|
|
120
144
|
const hint = remoteMoved && !localModifiedSet.has(item.path) ? fmt.dim(' (added remotely)') : '';
|
|
121
145
|
console.log(` ${fmt.red('-')} ${item.path}${hint}`);
|
|
122
146
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
147
|
+
}
|
|
148
|
+
if (hasInfoChanges) {
|
|
149
|
+
if (hasFileChanges) {
|
|
150
|
+
console.log('');
|
|
126
151
|
}
|
|
127
|
-
|
|
128
|
-
|
|
152
|
+
console.log(`Differences in Pen info (${PROJECT_CONFIG_FILENAME}):`);
|
|
153
|
+
for (const change of infoAnalysis.changes) {
|
|
154
|
+
const localVal = formatInfoValue(change.field, change.local);
|
|
155
|
+
const remoteVal = formatInfoValue(change.field, change.remote);
|
|
156
|
+
if (infoAnalysis.localChanged && infoAnalysis.remoteChanged) {
|
|
157
|
+
console.log(` ${fmt.yellow('~')} info.${change.field}: local ${localVal} vs remote ${remoteVal}${fmt.yellow(' (conflict)')}`);
|
|
158
|
+
}
|
|
159
|
+
else if (!infoAnalysis.localChanged && infoAnalysis.remoteChanged) {
|
|
160
|
+
console.log(` ${fmt.yellow('~')} info.${change.field}: ${localVal} → ${remoteVal}${fmt.dim(' (changed remotely)')}`);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
console.log(` ${fmt.yellow('~')} info.${change.field}: ${remoteVal} → ${localVal}`);
|
|
164
|
+
}
|
|
129
165
|
}
|
|
166
|
+
}
|
|
167
|
+
console.log('');
|
|
168
|
+
if (remoteMoved || (infoAnalysis.localChanged && infoAnalysis.remoteChanged)) {
|
|
169
|
+
console.log(fmt.yellow('Remote has newer changes. Run `cdpn pull` to sync remote changes, or `cdpn push --force` to overwrite remote with local.'));
|
|
170
|
+
}
|
|
171
|
+
else if (!hasFileChanges && !infoAnalysis.localChanged && infoAnalysis.remoteChanged) {
|
|
172
|
+
console.log(fmt.yellow(`Remote Pen info has changed. Run \`cdpn pull\` to update ${PROJECT_CONFIG_FILENAME}, or \`cdpn push --force\` to overwrite remote with local.`));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
console.log(fmt.dim('Run `cdpn push` to upload these changes, or `cdpn pull` to discard local changes.'));
|
|
176
|
+
}
|
|
177
|
+
return { pen, auth, project, diff, infoAnalysis, remoteMoved, upToDate };
|
|
178
|
+
}
|
|
179
|
+
export function registerStatusCommand(program) {
|
|
180
|
+
program
|
|
181
|
+
.command('status')
|
|
182
|
+
.description('Compare local files and Pen info with the online version on CodePen')
|
|
183
|
+
.option('--json', 'Output status as JSON')
|
|
184
|
+
.action(async (options, command) => {
|
|
185
|
+
await statusPenCommand({
|
|
186
|
+
json: options.json,
|
|
187
|
+
explicitProfile: getGlobalProfileOption(command),
|
|
188
|
+
});
|
|
130
189
|
});
|
|
131
190
|
}
|
package/commands/versions.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { CodePenClient } from '../api/client.js';
|
|
2
2
|
import { buildSyncedFileMap, requireProject, writeProjectConfig } from '../config/project.js';
|
|
3
3
|
import { scanLocalFiles } from '../sync/fs.js';
|
|
4
|
+
import { buildPenInfoConfigFields } from '../sync/info.js';
|
|
4
5
|
import { syncRemoteFilesToDisk } from '../sync/materialize.js';
|
|
5
6
|
import { fmt, formatDate, formatTable, shortId } from '../utils/format.js';
|
|
6
7
|
import { CliError, printJsonResponse } from '../utils/json.js';
|
|
@@ -150,6 +151,7 @@ export function registerVersionCommands(program) {
|
|
|
150
151
|
...(project.config.profile ? { profile: project.config.profile } : {}),
|
|
151
152
|
last_synced_version_id: newHeadId,
|
|
152
153
|
editor: updatedPen.editor,
|
|
154
|
+
...buildPenInfoConfigFields(updatedPen),
|
|
153
155
|
files: buildSyncedFileMap(remoteFiles, materialized.localSha256ByPath),
|
|
154
156
|
});
|
|
155
157
|
if (options.json) {
|
package/config/project.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
+
import { normalizePartialPenInfo, parsePenInfoInput } from '../sync/info.js';
|
|
3
4
|
import { CliError } from '../utils/json.js';
|
|
4
5
|
export const PROJECT_CONFIG_FILENAME = '.cdpn.json';
|
|
5
6
|
/**
|
|
@@ -97,7 +98,11 @@ export async function readProjectConfig(rootDir) {
|
|
|
97
98
|
const lastSynced = record.last_synced_version_id === null || typeof record.last_synced_version_id === 'string'
|
|
98
99
|
? record.last_synced_version_id
|
|
99
100
|
: null;
|
|
101
|
+
const lastSyncedInfoHash = typeof record.last_synced_info_hash === 'string' && record.last_synced_info_hash.trim()
|
|
102
|
+
? record.last_synced_info_hash.trim()
|
|
103
|
+
: undefined;
|
|
100
104
|
const editor = record.editor === 'V2' || record.editor === 'Classic' ? record.editor : undefined;
|
|
105
|
+
const info = parsePenInfoInput(record.info, PROJECT_CONFIG_FILENAME);
|
|
101
106
|
let files;
|
|
102
107
|
if (record.files && typeof record.files === 'object' && !Array.isArray(record.files)) {
|
|
103
108
|
files = {};
|
|
@@ -122,19 +127,25 @@ export async function readProjectConfig(rootDir) {
|
|
|
122
127
|
pen_id: record.pen_id.trim(),
|
|
123
128
|
...(profile ? { profile } : {}),
|
|
124
129
|
last_synced_version_id: lastSynced,
|
|
130
|
+
...(lastSyncedInfoHash ? { last_synced_info_hash: lastSyncedInfoHash } : {}),
|
|
125
131
|
...(editor ? { editor } : {}),
|
|
132
|
+
...(info !== undefined ? { info } : {}),
|
|
126
133
|
...(files ? { files } : {}),
|
|
127
134
|
};
|
|
128
135
|
}
|
|
129
136
|
export async function writeProjectConfig(rootDir, config) {
|
|
130
137
|
const configPath = path.join(path.resolve(rootDir), PROJECT_CONFIG_FILENAME);
|
|
131
138
|
const trimmedProfile = config.profile?.trim();
|
|
139
|
+
const trimmedInfoHash = config.last_synced_info_hash?.trim();
|
|
140
|
+
const normalizedInfo = config.info !== undefined ? normalizePartialPenInfo(config.info) : undefined;
|
|
132
141
|
const ordered = {
|
|
133
142
|
url: config.url,
|
|
134
143
|
pen_id: config.pen_id,
|
|
135
144
|
...(trimmedProfile ? { profile: trimmedProfile } : {}),
|
|
136
145
|
last_synced_version_id: config.last_synced_version_id,
|
|
146
|
+
...(trimmedInfoHash ? { last_synced_info_hash: trimmedInfoHash } : {}),
|
|
137
147
|
...(config.editor ? { editor: config.editor } : {}),
|
|
148
|
+
...(normalizedInfo !== undefined ? { info: normalizedInfo } : {}),
|
|
138
149
|
...(config.files ? { files: sortRecordKeys(config.files) } : {}),
|
|
139
150
|
};
|
|
140
151
|
await fs.writeFile(configPath, `${JSON.stringify(ordered, null, 2)}\n`, 'utf8');
|