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
package/site/manifest.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { CODEPEN_DIR, PROJECT_CONFIG_FILENAME } from '../constants.js';
|
|
4
|
+
import { isIgnoredPath, loadIgnoreMatchers } from '../sync/fs.js';
|
|
5
|
+
import { sanitizeCodePenUrl } from '../utils/codepen-url.js';
|
|
6
|
+
import { readPartialUtf8, resolveSafeDir, resolveSafeFile } from '../utils/fs-safe.js';
|
|
7
|
+
import { extractHtmlTitle, extractMetaDescription } from '../utils/html.js';
|
|
8
|
+
import { compareNatural, formatFolderTitle, isPlainObject, normalizePlainText, } from '../utils/strings.js';
|
|
9
|
+
import { createDebouncedWatcher } from '../utils/watcher.js';
|
|
10
|
+
import { readClassicHeadMetadata, readPenBlocks } from './blocks/index.js';
|
|
11
|
+
export { formatFolderTitle };
|
|
12
|
+
const MAX_SCAN_DEPTH = 4;
|
|
13
|
+
const MAX_HTML_HEAD_BYTES = 16 * 1024;
|
|
14
|
+
const MAX_CDPN_JSON_BYTES = 64 * 1024;
|
|
15
|
+
const MAX_DEMO_TAGS = 10;
|
|
16
|
+
function extractCdpnTags(rawTags) {
|
|
17
|
+
if (!Array.isArray(rawTags))
|
|
18
|
+
return [];
|
|
19
|
+
const seen = new Set();
|
|
20
|
+
const tags = [];
|
|
21
|
+
for (const rawTag of rawTags) {
|
|
22
|
+
if (typeof rawTag !== 'string')
|
|
23
|
+
continue;
|
|
24
|
+
const normalized = normalizePlainText(rawTag, 48);
|
|
25
|
+
if (!normalized)
|
|
26
|
+
continue;
|
|
27
|
+
const key = normalized.toLowerCase();
|
|
28
|
+
if (seen.has(key))
|
|
29
|
+
continue;
|
|
30
|
+
seen.add(key);
|
|
31
|
+
tags.push(normalized);
|
|
32
|
+
if (tags.length >= MAX_DEMO_TAGS)
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
return tags;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Detects whether a directory looks like a previously built collection output
|
|
39
|
+
* (contains `demos.json` + `index.html`, with neither `.cdpn.json` nor `.codepen/`).
|
|
40
|
+
*/
|
|
41
|
+
export async function isBuiltCollectionOutputDir(dirRealpath, rootRealpath) {
|
|
42
|
+
const [hasDemosJson, hasIndexHtml, hasCdpnJson, hasCodepenDir] = await Promise.all([
|
|
43
|
+
resolveSafeFile(path.join(dirRealpath, 'demos.json'), rootRealpath, dirRealpath),
|
|
44
|
+
resolveSafeFile(path.join(dirRealpath, 'index.html'), rootRealpath, dirRealpath),
|
|
45
|
+
resolveSafeFile(path.join(dirRealpath, PROJECT_CONFIG_FILENAME), rootRealpath, dirRealpath),
|
|
46
|
+
resolveSafeDir(path.join(dirRealpath, CODEPEN_DIR), rootRealpath, dirRealpath),
|
|
47
|
+
]);
|
|
48
|
+
return Boolean(hasDemosJson && hasIndexHtml && !hasCdpnJson && !hasCodepenDir);
|
|
49
|
+
}
|
|
50
|
+
async function readDemoEntry(segments, demoRealpath, rootDir, rootRealpath) {
|
|
51
|
+
const demoDir = path.resolve(rootDir, ...segments);
|
|
52
|
+
const cdpnFile = await resolveSafeFile(path.join(demoDir, PROJECT_CONFIG_FILENAME), rootRealpath, rootDir);
|
|
53
|
+
if (!cdpnFile) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
const codepenDir = await resolveSafeDir(path.join(demoDir, CODEPEN_DIR), rootRealpath, rootDir);
|
|
57
|
+
const indexFile = await resolveSafeFile(path.join(demoDir, 'index.html'), rootRealpath, rootDir);
|
|
58
|
+
let classicIndexFile = null;
|
|
59
|
+
if (!indexFile && codepenDir) {
|
|
60
|
+
const penBlocks = await readPenBlocks(demoRealpath, rootRealpath);
|
|
61
|
+
if (penBlocks?.hasClassic) {
|
|
62
|
+
classicIndexFile = await resolveSafeFile(path.join(demoDir, 'index.pen.html'), rootRealpath, rootDir);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
let htmlTitle = null;
|
|
66
|
+
let htmlDescription = null;
|
|
67
|
+
if (indexFile) {
|
|
68
|
+
try {
|
|
69
|
+
const headHtml = await readPartialUtf8(indexFile.realPath, MAX_HTML_HEAD_BYTES);
|
|
70
|
+
const rawTitle = extractHtmlTitle(headHtml);
|
|
71
|
+
htmlTitle = rawTitle ? normalizePlainText(rawTitle, 160) : null;
|
|
72
|
+
htmlDescription = extractMetaDescription(headHtml, 320);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// Fall back to .cdpn.json or folder name when HTML metadata cannot be read
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else if (classicIndexFile) {
|
|
79
|
+
try {
|
|
80
|
+
const classicHead = await readClassicHeadMetadata(demoRealpath, rootRealpath);
|
|
81
|
+
if (classicHead) {
|
|
82
|
+
const rawTitle = extractHtmlTitle(classicHead);
|
|
83
|
+
htmlTitle = rawTitle ? normalizePlainText(rawTitle, 160) : null;
|
|
84
|
+
htmlDescription = extractMetaDescription(classicHead, 320);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// Fall back to .cdpn.json or folder name when classic config metadata cannot be read
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
let codepenUrl = null;
|
|
92
|
+
let cdpnTitle = null;
|
|
93
|
+
let cdpnDescription = null;
|
|
94
|
+
let tags = [];
|
|
95
|
+
if (cdpnFile && cdpnFile.size <= MAX_CDPN_JSON_BYTES) {
|
|
96
|
+
try {
|
|
97
|
+
const rawJson = await fs.readFile(cdpnFile.realPath, 'utf8');
|
|
98
|
+
const parsed = JSON.parse(rawJson);
|
|
99
|
+
if (isPlainObject(parsed)) {
|
|
100
|
+
codepenUrl = sanitizeCodePenUrl(parsed.url);
|
|
101
|
+
const info = isPlainObject(parsed.info) ? parsed.info : null;
|
|
102
|
+
if (typeof info?.title === 'string') {
|
|
103
|
+
cdpnTitle = normalizePlainText(info.title, 160);
|
|
104
|
+
}
|
|
105
|
+
if (typeof info?.description === 'string') {
|
|
106
|
+
cdpnDescription = normalizePlainText(info.description, 320);
|
|
107
|
+
}
|
|
108
|
+
tags = extractCdpnTags(info?.tags);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
codepenUrl = null;
|
|
113
|
+
tags = [];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const lastSegment = segments.at(-1) ?? 'demo';
|
|
117
|
+
return {
|
|
118
|
+
title: cdpnTitle ?? htmlTitle ?? formatFolderTitle(lastSegment),
|
|
119
|
+
description: cdpnDescription ?? htmlDescription,
|
|
120
|
+
tags,
|
|
121
|
+
path: `${segments.join('/')}/`,
|
|
122
|
+
href: `./${segments.map(encodeURIComponent).join('/')}/`,
|
|
123
|
+
codepenUrl,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
async function scanGroupNode(dirRealpath, segments, depth, visitedRealpaths, rootDir, rootRealpath, excludeDirs, ignoreMatchers) {
|
|
127
|
+
if (depth > MAX_SCAN_DEPTH)
|
|
128
|
+
return { demos: [], groups: [] };
|
|
129
|
+
let entries;
|
|
130
|
+
try {
|
|
131
|
+
entries = await fs.readdir(dirRealpath, { withFileTypes: true });
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return { demos: [], groups: [] };
|
|
135
|
+
}
|
|
136
|
+
const demos = [];
|
|
137
|
+
const groups = [];
|
|
138
|
+
for (const entry of entries) {
|
|
139
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules')
|
|
140
|
+
continue;
|
|
141
|
+
const childSegments = [...segments, entry.name];
|
|
142
|
+
const relPosix = `/${childSegments.join('/')}`;
|
|
143
|
+
if (ignoreMatchers.length > 0 && isIgnoredPath(relPosix, ignoreMatchers))
|
|
144
|
+
continue;
|
|
145
|
+
const childCandidate = path.resolve(rootDir, ...childSegments);
|
|
146
|
+
const childRealDir = await resolveSafeDir(childCandidate, rootRealpath, rootDir);
|
|
147
|
+
if (!childRealDir || visitedRealpaths.has(childRealDir))
|
|
148
|
+
continue;
|
|
149
|
+
if (excludeDirs.has(childRealDir))
|
|
150
|
+
continue;
|
|
151
|
+
visitedRealpaths.add(childRealDir);
|
|
152
|
+
// Skip previously built collection output folders (e.g. `./build/` or `./dist/` containing `demos.json` + `index.html`)
|
|
153
|
+
if (await isBuiltCollectionOutputDir(childRealDir, rootRealpath)) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const demoEntry = await readDemoEntry(childSegments, childRealDir, rootDir, rootRealpath);
|
|
157
|
+
if (demoEntry) {
|
|
158
|
+
demos.push(demoEntry);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const subNode = await scanGroupNode(childRealDir, childSegments, depth + 1, visitedRealpaths, rootDir, rootRealpath, excludeDirs, ignoreMatchers);
|
|
162
|
+
if (subNode.demos.length > 0 || subNode.groups.length > 0) {
|
|
163
|
+
groups.push({
|
|
164
|
+
id: childSegments.join('/'),
|
|
165
|
+
title: formatFolderTitle(entry.name),
|
|
166
|
+
demos: subNode.demos,
|
|
167
|
+
groups: subNode.groups,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
demos: [...demos].sort((a, b) => compareNatural(a.path, b.path)),
|
|
173
|
+
groups: [...groups].sort((a, b) => compareNatural(a.id, b.id)),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Recursively scans `rootDir` subdirectories for Pens/demos (folders containing `.cdpn.json`).
|
|
178
|
+
*/
|
|
179
|
+
export async function scanDemos(rootDir, options = {}) {
|
|
180
|
+
const resolvedRoot = path.resolve(rootDir);
|
|
181
|
+
const rootRealpath = await fs.realpath(resolvedRoot);
|
|
182
|
+
const excludeDirs = options.excludeDirs ?? new Set();
|
|
183
|
+
const ignoreMatchers = await loadIgnoreMatchers(rootRealpath);
|
|
184
|
+
const visitedRealpaths = new Set([rootRealpath]);
|
|
185
|
+
const root = await scanGroupNode(rootRealpath, [], 0, visitedRealpaths, resolvedRoot, rootRealpath, excludeDirs, ignoreMatchers);
|
|
186
|
+
return {
|
|
187
|
+
groups: root.groups,
|
|
188
|
+
standalone: root.demos,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
function countGroupDemos(groups) {
|
|
192
|
+
return groups.reduce((total, group) => total + group.demos.length + countGroupDemos(group.groups ?? []), 0);
|
|
193
|
+
}
|
|
194
|
+
export function countDemos(manifest) {
|
|
195
|
+
return (manifest.standalone?.length ?? 0) + countGroupDemos(manifest.groups ?? []);
|
|
196
|
+
}
|
|
197
|
+
export function serializeManifest(manifest) {
|
|
198
|
+
return `${JSON.stringify(manifest, null, 2)}\n`;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Scans `rootDir` once and watches it to keep the demo manifest updated purely in memory
|
|
202
|
+
* (without writing any files to disk).
|
|
203
|
+
*/
|
|
204
|
+
export async function createInMemoryManifestWatcher(options) {
|
|
205
|
+
const { rootDir, excludeDirs, debounceMs = 150, onUpdate, onError } = options;
|
|
206
|
+
let currentManifest = await scanDemos(rootDir, { excludeDirs });
|
|
207
|
+
let currentSerialized = serializeManifest(currentManifest);
|
|
208
|
+
const handle = createDebouncedWatcher({
|
|
209
|
+
rootDir,
|
|
210
|
+
debounceMs,
|
|
211
|
+
onChange: async () => {
|
|
212
|
+
const nextManifest = await scanDemos(rootDir, { excludeDirs });
|
|
213
|
+
const nextSerialized = serializeManifest(nextManifest);
|
|
214
|
+
if (nextSerialized !== currentSerialized) {
|
|
215
|
+
currentManifest = nextManifest;
|
|
216
|
+
currentSerialized = nextSerialized;
|
|
217
|
+
onUpdate?.({ demoCount: countDemos(nextManifest), manifest: nextManifest });
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
onError,
|
|
221
|
+
});
|
|
222
|
+
return {
|
|
223
|
+
getManifest: () => currentManifest,
|
|
224
|
+
getSerialized: () => currentSerialized,
|
|
225
|
+
close: () => handle.close(),
|
|
226
|
+
};
|
|
227
|
+
}
|