codepen-commander 1.1.0 → 1.3.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 +20 -5
- package/commands/clone.js +2 -0
- package/commands/create-pen.js +53 -23
- package/commands/delete.js +68 -20
- package/commands/deploy.js +22 -7
- 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 +223 -99
- package/commands/status.js +156 -97
- package/commands/versions.js +2 -0
- package/config/project.js +94 -16
- 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,19 +1,28 @@
|
|
|
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';
|
|
6
|
+
function createUnlinkedProjectError(rootDir) {
|
|
7
|
+
return new CliError(404, `${PROJECT_CONFIG_FILENAME} in ${rootDir} only contains Pen info and is not linked to an online Pen yet.\n` +
|
|
8
|
+
'Run `cdpn push` to push this folder as a new Pen on CodePen.');
|
|
9
|
+
}
|
|
5
10
|
/**
|
|
6
|
-
* Walks upward from `startDir` looking for `.cdpn.json` (
|
|
11
|
+
* Walks upward from `startDir` looking for the nearest `.cdpn.json` (linked or unlinked info-only).
|
|
12
|
+
* Stops at the first `.cdpn.json` encountered so nested unlinked folders never leak to a parent Pen.
|
|
7
13
|
*/
|
|
8
|
-
export async function
|
|
14
|
+
export async function findNearestConfig(startDir = process.cwd()) {
|
|
9
15
|
let current = path.resolve(startDir);
|
|
10
16
|
for (;;) {
|
|
11
17
|
const configPath = path.join(current, PROJECT_CONFIG_FILENAME);
|
|
12
18
|
try {
|
|
13
19
|
const stat = await fs.lstat(configPath);
|
|
14
20
|
if (stat.isFile()) {
|
|
15
|
-
const
|
|
16
|
-
|
|
21
|
+
const parsed = await readProjectConfigFile(current);
|
|
22
|
+
if (parsed.linked) {
|
|
23
|
+
return { linked: true, rootDir: current, configPath, config: parsed.config };
|
|
24
|
+
}
|
|
25
|
+
return { linked: false, rootDir: current, configPath, config: parsed.config };
|
|
17
26
|
}
|
|
18
27
|
}
|
|
19
28
|
catch (error) {
|
|
@@ -28,15 +37,37 @@ export async function findProject(startDir = process.cwd()) {
|
|
|
28
37
|
current = parent;
|
|
29
38
|
}
|
|
30
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Walks upward from `startDir` looking for a linked `.cdpn.json` (similar to how git locates `.git`).
|
|
42
|
+
* Returns `null` if no `.cdpn.json` is found or if the nearest `.cdpn.json` is unlinked (info-only).
|
|
43
|
+
*/
|
|
44
|
+
export async function findProject(startDir = process.cwd()) {
|
|
45
|
+
const nearest = await findNearestConfig(startDir);
|
|
46
|
+
if (!nearest || !nearest.linked) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
rootDir: nearest.rootDir,
|
|
51
|
+
configPath: nearest.configPath,
|
|
52
|
+
config: nearest.config,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
31
55
|
export async function requireProject(startDir = process.cwd()) {
|
|
32
|
-
const
|
|
33
|
-
if (!
|
|
56
|
+
const nearest = await findNearestConfig(startDir);
|
|
57
|
+
if (!nearest) {
|
|
34
58
|
throw new CliError(404, `No ${PROJECT_CONFIG_FILENAME} found in this directory or any parent directory.\nRun \`cdpn clone <url>\` to clone a Pen or \`cdpn new\` to create one.`);
|
|
35
59
|
}
|
|
36
|
-
|
|
60
|
+
if (!nearest.linked) {
|
|
61
|
+
throw createUnlinkedProjectError(nearest.rootDir);
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
rootDir: nearest.rootDir,
|
|
65
|
+
configPath: nearest.configPath,
|
|
66
|
+
config: nearest.config,
|
|
67
|
+
};
|
|
37
68
|
}
|
|
38
69
|
/**
|
|
39
|
-
* Requires `.cdpn.json` to exist directly inside `dir` (does NOT walk upward to parent directories).
|
|
70
|
+
* Requires a linked `.cdpn.json` to exist directly inside `dir` (does NOT walk upward to parent directories).
|
|
40
71
|
*/
|
|
41
72
|
export async function requireProjectInDir(dir = process.cwd()) {
|
|
42
73
|
const resolvedDir = path.resolve(dir);
|
|
@@ -71,8 +102,13 @@ export async function hasProjectConfigInDir(dir) {
|
|
|
71
102
|
throw error;
|
|
72
103
|
}
|
|
73
104
|
}
|
|
74
|
-
|
|
75
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Reads and validates `.cdpn.json` from `rootDir`, supporting both linked Pen configs
|
|
107
|
+
* and unlinked info-only configs (e.g. retained via `cdpn delete --keep-info`).
|
|
108
|
+
*/
|
|
109
|
+
export async function readProjectConfigFile(rootDir) {
|
|
110
|
+
const resolvedDir = path.resolve(rootDir);
|
|
111
|
+
const configPath = path.join(resolvedDir, PROJECT_CONFIG_FILENAME);
|
|
76
112
|
const raw = await fs.readFile(configPath, 'utf8');
|
|
77
113
|
let parsed;
|
|
78
114
|
try {
|
|
@@ -85,6 +121,17 @@ export async function readProjectConfig(rootDir) {
|
|
|
85
121
|
throw new CliError(400, `Invalid ${PROJECT_CONFIG_FILENAME}: expected a JSON object.`);
|
|
86
122
|
}
|
|
87
123
|
const record = parsed;
|
|
124
|
+
// Unlinked info-only `.cdpn.json`: no pen_id or url, only `info`
|
|
125
|
+
if (record.pen_id === undefined && record.url === undefined && record.info !== undefined) {
|
|
126
|
+
if (record.info === null) {
|
|
127
|
+
throw new CliError(400, `Invalid ${PROJECT_CONFIG_FILENAME}: "info" must be a JSON object.`);
|
|
128
|
+
}
|
|
129
|
+
const info = parsePenInfoInput(record.info, PROJECT_CONFIG_FILENAME);
|
|
130
|
+
return {
|
|
131
|
+
linked: false,
|
|
132
|
+
config: { info },
|
|
133
|
+
};
|
|
134
|
+
}
|
|
88
135
|
if (typeof record.pen_id !== 'string' || !record.pen_id.trim()) {
|
|
89
136
|
throw new CliError(400, `Invalid ${PROJECT_CONFIG_FILENAME}: missing required "pen_id" string.`);
|
|
90
137
|
}
|
|
@@ -97,7 +144,11 @@ export async function readProjectConfig(rootDir) {
|
|
|
97
144
|
const lastSynced = record.last_synced_version_id === null || typeof record.last_synced_version_id === 'string'
|
|
98
145
|
? record.last_synced_version_id
|
|
99
146
|
: null;
|
|
147
|
+
const lastSyncedInfoHash = typeof record.last_synced_info_hash === 'string' && record.last_synced_info_hash.trim()
|
|
148
|
+
? record.last_synced_info_hash.trim()
|
|
149
|
+
: undefined;
|
|
100
150
|
const editor = record.editor === 'V2' || record.editor === 'Classic' ? record.editor : undefined;
|
|
151
|
+
const info = parsePenInfoInput(record.info, PROJECT_CONFIG_FILENAME);
|
|
101
152
|
let files;
|
|
102
153
|
if (record.files && typeof record.files === 'object' && !Array.isArray(record.files)) {
|
|
103
154
|
files = {};
|
|
@@ -118,28 +169,55 @@ export async function readProjectConfig(rootDir) {
|
|
|
118
169
|
}
|
|
119
170
|
}
|
|
120
171
|
return {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
172
|
+
linked: true,
|
|
173
|
+
config: {
|
|
174
|
+
url: record.url.trim(),
|
|
175
|
+
pen_id: record.pen_id.trim(),
|
|
176
|
+
...(profile ? { profile } : {}),
|
|
177
|
+
last_synced_version_id: lastSynced,
|
|
178
|
+
...(lastSyncedInfoHash ? { last_synced_info_hash: lastSyncedInfoHash } : {}),
|
|
179
|
+
...(editor ? { editor } : {}),
|
|
180
|
+
...(info !== undefined ? { info } : {}),
|
|
181
|
+
...(files ? { files } : {}),
|
|
182
|
+
},
|
|
127
183
|
};
|
|
128
184
|
}
|
|
185
|
+
export async function readProjectConfig(rootDir) {
|
|
186
|
+
const parsed = await readProjectConfigFile(rootDir);
|
|
187
|
+
if (!parsed.linked) {
|
|
188
|
+
throw createUnlinkedProjectError(path.resolve(rootDir));
|
|
189
|
+
}
|
|
190
|
+
return parsed.config;
|
|
191
|
+
}
|
|
129
192
|
export async function writeProjectConfig(rootDir, config) {
|
|
130
193
|
const configPath = path.join(path.resolve(rootDir), PROJECT_CONFIG_FILENAME);
|
|
131
194
|
const trimmedProfile = config.profile?.trim();
|
|
195
|
+
const trimmedInfoHash = config.last_synced_info_hash?.trim();
|
|
196
|
+
const normalizedInfo = config.info !== undefined ? normalizePartialPenInfo(config.info) : undefined;
|
|
132
197
|
const ordered = {
|
|
133
198
|
url: config.url,
|
|
134
199
|
pen_id: config.pen_id,
|
|
135
200
|
...(trimmedProfile ? { profile: trimmedProfile } : {}),
|
|
136
201
|
last_synced_version_id: config.last_synced_version_id,
|
|
202
|
+
...(trimmedInfoHash ? { last_synced_info_hash: trimmedInfoHash } : {}),
|
|
137
203
|
...(config.editor ? { editor: config.editor } : {}),
|
|
204
|
+
...(normalizedInfo !== undefined ? { info: normalizedInfo } : {}),
|
|
138
205
|
...(config.files ? { files: sortRecordKeys(config.files) } : {}),
|
|
139
206
|
};
|
|
140
207
|
await fs.writeFile(configPath, `${JSON.stringify(ordered, null, 2)}\n`, 'utf8');
|
|
141
208
|
return configPath;
|
|
142
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Writes an unlinked `.cdpn.json` containing only the normalized `info` section
|
|
212
|
+
* (used by `cdpn delete --keep-info` so a later first-time push reuses the Pen metadata).
|
|
213
|
+
*/
|
|
214
|
+
export async function writeUnlinkedProjectInfo(rootDir, info) {
|
|
215
|
+
const configPath = path.join(path.resolve(rootDir), PROJECT_CONFIG_FILENAME);
|
|
216
|
+
const normalizedInfo = normalizePartialPenInfo(info);
|
|
217
|
+
const payload = { info: normalizedInfo };
|
|
218
|
+
await fs.writeFile(configPath, `${JSON.stringify(payload, null, 2)}\n`, 'utf8');
|
|
219
|
+
return configPath;
|
|
220
|
+
}
|
|
143
221
|
function sortRecordKeys(record) {
|
|
144
222
|
const sorted = {};
|
|
145
223
|
for (const key of Object.keys(record).sort()) {
|