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/builder.js
ADDED
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { PEN_HTML_SUFFIX } from '../constants.js';
|
|
5
|
+
import { CliError } from '../errors.js';
|
|
6
|
+
import { isIgnoredPath, loadIgnoreMatchers } from '../sync/fs.js';
|
|
7
|
+
import { isWithinRoot } from '../utils/fs-safe.js';
|
|
8
|
+
import { hasControlChars } from '../utils/strings.js';
|
|
9
|
+
import { createDebouncedWatcher } from '../utils/watcher.js';
|
|
10
|
+
import { applyPackagesBlock, compileClassicPenHtml, readPenBlocks, renderUnsupportedBlocksPage, } from './blocks/index.js';
|
|
11
|
+
import { classifyDirectoryMode, inspectTargetDirectory, } from './detect.js';
|
|
12
|
+
import { countDemos, isBuiltCollectionOutputDir, scanDemos, serializeManifest, } from './manifest.js';
|
|
13
|
+
import { CORE_SHELL_FILES, expandHomePath, listShellAssetFiles, renderShellIndexHtml, resolveActiveShell, resolveSiteMetadata, } from './shell.js';
|
|
14
|
+
export const DEFAULT_BUILD_DIR = 'build';
|
|
15
|
+
/**
|
|
16
|
+
* Common repository/package files at a collection root that are never part of the
|
|
17
|
+
* deployed static demo site (still copied normally when inside an individual Pen folder).
|
|
18
|
+
*/
|
|
19
|
+
const COLLECTION_ROOT_EXCLUDED_FILES = new Set([
|
|
20
|
+
'package.json',
|
|
21
|
+
'package-lock.json',
|
|
22
|
+
'pnpm-lock.yaml',
|
|
23
|
+
'yarn.lock',
|
|
24
|
+
'bun.lock',
|
|
25
|
+
'bun.lockb',
|
|
26
|
+
'readme.md',
|
|
27
|
+
'agents.md',
|
|
28
|
+
'license',
|
|
29
|
+
'license.md',
|
|
30
|
+
'changelog.md',
|
|
31
|
+
'tsconfig.json',
|
|
32
|
+
'config.json',
|
|
33
|
+
'config.json.example',
|
|
34
|
+
]);
|
|
35
|
+
async function resolveCanonicalPathThroughExistingAncestor(targetPath) {
|
|
36
|
+
const resolved = path.resolve(targetPath);
|
|
37
|
+
const trailingSegments = [];
|
|
38
|
+
let probe = resolved;
|
|
39
|
+
for (;;) {
|
|
40
|
+
try {
|
|
41
|
+
const realProbe = await fs.realpath(probe);
|
|
42
|
+
return trailingSegments.length > 0
|
|
43
|
+
? path.join(realProbe, ...trailingSegments.reverse())
|
|
44
|
+
: realProbe;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error.code === 'ENOENT') {
|
|
48
|
+
const parent = path.dirname(probe);
|
|
49
|
+
if (parent === probe) {
|
|
50
|
+
return resolved;
|
|
51
|
+
}
|
|
52
|
+
trailingSegments.push(path.basename(probe));
|
|
53
|
+
probe = parent;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Validates and resolves a build target directory relative to `sourceDir` (allowing
|
|
62
|
+
* targets both inside and outside `sourceDir`, while forbidding `.`, ancestor dirs,
|
|
63
|
+
* files, and symlinks).
|
|
64
|
+
*/
|
|
65
|
+
export async function resolveBuildTargetDir(rawTarget, sourceRealpath, options = {}) {
|
|
66
|
+
const trimmed = rawTarget.trim();
|
|
67
|
+
if (!trimmed) {
|
|
68
|
+
return { ok: false, error: 'Output directory cannot be empty.' };
|
|
69
|
+
}
|
|
70
|
+
if (hasControlChars(trimmed)) {
|
|
71
|
+
return { ok: false, error: 'Output directory cannot contain control characters.' };
|
|
72
|
+
}
|
|
73
|
+
const expanded = expandHomePath(trimmed, options.homeDir);
|
|
74
|
+
const resolvedOut = path.resolve(sourceRealpath, expanded);
|
|
75
|
+
let exists = false;
|
|
76
|
+
try {
|
|
77
|
+
const lstat = await fs.lstat(resolvedOut);
|
|
78
|
+
if (lstat.isSymbolicLink()) {
|
|
79
|
+
return {
|
|
80
|
+
ok: false,
|
|
81
|
+
error: `"${trimmed}" is a symbolic link. Please choose a regular directory.`,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (!lstat.isDirectory()) {
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
error: `"${trimmed}" already exists and is not a directory.`,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
exists = true;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
if (error.code !== 'ENOENT') {
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Also ensure no existing ancestor path component is a regular file or symlink loop
|
|
98
|
+
let canonicalOutDir;
|
|
99
|
+
try {
|
|
100
|
+
canonicalOutDir = await resolveCanonicalPathThroughExistingAncestor(resolvedOut);
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
return { ok: false, error: `Cannot resolve "${trimmed}": ${error.message}` };
|
|
104
|
+
}
|
|
105
|
+
// Guard against wiping the source directory itself or any ancestor directory (e.g. `.`, `..`, `/`, `~`)
|
|
106
|
+
if (canonicalOutDir === sourceRealpath) {
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
error: 'Output directory cannot be the same as the source directory.',
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
if (sourceRealpath.startsWith(canonicalOutDir + path.sep)) {
|
|
113
|
+
return {
|
|
114
|
+
ok: false,
|
|
115
|
+
error: 'Output directory cannot be a parent/ancestor of the source directory.',
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const isInsideSource = canonicalOutDir.startsWith(sourceRealpath + path.sep);
|
|
119
|
+
let relInsideSource = null;
|
|
120
|
+
if (isInsideSource) {
|
|
121
|
+
const rel = path.relative(sourceRealpath, canonicalOutDir);
|
|
122
|
+
const segments = rel.split(path.sep).filter(Boolean);
|
|
123
|
+
if (segments.some((s) => s.startsWith('.'))) {
|
|
124
|
+
return {
|
|
125
|
+
ok: false,
|
|
126
|
+
error: 'Output directory inside the project cannot use hidden folder segments.',
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
relInsideSource = segments.join('/');
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
ok: true,
|
|
133
|
+
target: {
|
|
134
|
+
outDir: resolvedOut,
|
|
135
|
+
canonicalOutDir,
|
|
136
|
+
displayPath: trimmed,
|
|
137
|
+
exists,
|
|
138
|
+
isInsideSource,
|
|
139
|
+
relInsideSource,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function collectManifestDirSets(manifest, rootRealpath) {
|
|
144
|
+
const demoDirs = new Set();
|
|
145
|
+
const groupDirs = new Set();
|
|
146
|
+
const registerDemo = (relPathWithSlash) => {
|
|
147
|
+
const segments = relPathWithSlash.replace(/\/+$/, '').split('/').filter(Boolean);
|
|
148
|
+
if (segments.length === 0)
|
|
149
|
+
return;
|
|
150
|
+
const demoAbs = path.resolve(rootRealpath, ...segments);
|
|
151
|
+
demoDirs.add(demoAbs);
|
|
152
|
+
for (let i = 1; i < segments.length; i++) {
|
|
153
|
+
groupDirs.add(path.resolve(rootRealpath, ...segments.slice(0, i)));
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const walkGroups = (groups) => {
|
|
157
|
+
for (const group of groups) {
|
|
158
|
+
for (const demo of group.demos) {
|
|
159
|
+
registerDemo(demo.path);
|
|
160
|
+
}
|
|
161
|
+
if (group.groups.length > 0) {
|
|
162
|
+
walkGroups(group.groups);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
for (const demo of manifest.standalone) {
|
|
167
|
+
registerDemo(demo.path);
|
|
168
|
+
}
|
|
169
|
+
walkGroups(manifest.groups);
|
|
170
|
+
return { demoDirs, groupDirs };
|
|
171
|
+
}
|
|
172
|
+
async function copyDirectoryTree(options) {
|
|
173
|
+
const { currentSrcDir, currentDestDir, rootRealpath, excludedRealpaths, isCollectionRootLevel, mode, insideDemo, allowedDemoDirs, allowedGroupDirs, inheritedPenContext, unsupportedDemos, } = options;
|
|
174
|
+
const localPenContext = await readPenBlocks(currentSrcDir, rootRealpath);
|
|
175
|
+
const penContext = localPenContext ?? inheritedPenContext;
|
|
176
|
+
if (localPenContext && localPenContext.unsupportedBlocks.length > 0) {
|
|
177
|
+
const relFromRoot = path.relative(rootRealpath, currentSrcDir);
|
|
178
|
+
const relSegments = relFromRoot ? relFromRoot.split(path.sep).filter(Boolean) : [];
|
|
179
|
+
const demoRelPath = relSegments.length > 0 ? `${relSegments.join('/')}/` : `${path.basename(currentSrcDir)}/`;
|
|
180
|
+
unsupportedDemos.push({
|
|
181
|
+
path: demoRelPath,
|
|
182
|
+
blocks: localPenContext.unsupportedBlocks,
|
|
183
|
+
});
|
|
184
|
+
const homeHref = mode === 'collection' && relSegments.length > 0 ? '../'.repeat(relSegments.length) : null;
|
|
185
|
+
const { html } = await renderUnsupportedBlocksPage({
|
|
186
|
+
penContext: localPenContext,
|
|
187
|
+
rootRealpath,
|
|
188
|
+
demoRelPath,
|
|
189
|
+
homeHref,
|
|
190
|
+
});
|
|
191
|
+
await fs.writeFile(path.join(currentDestDir, 'index.html'), html, 'utf8');
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
// Load local `.cdpnignore` when entering the root or a Pen folder
|
|
195
|
+
let activeIgnoreRoot = options.activeIgnoreRoot;
|
|
196
|
+
let activeIgnoreMatchers = options.activeIgnoreMatchers;
|
|
197
|
+
const localMatchers = await loadIgnoreMatchers(currentSrcDir);
|
|
198
|
+
if (localMatchers.length > 0) {
|
|
199
|
+
activeIgnoreRoot = currentSrcDir;
|
|
200
|
+
activeIgnoreMatchers = localMatchers;
|
|
201
|
+
}
|
|
202
|
+
const entries = await fs.readdir(currentSrcDir, { withFileTypes: true });
|
|
203
|
+
const existingNamesLower = new Set(entries.map((entry) => entry.name.toLowerCase()));
|
|
204
|
+
for (const entry of entries) {
|
|
205
|
+
// Skip dotfiles/dotdirs (.cdpn.json, .cdpnignore, .codepen, .git, .DS_Store, .env, etc.) and node_modules
|
|
206
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules')
|
|
207
|
+
continue;
|
|
208
|
+
// In collection mode at root level, skip stale demos.json and unrelated repo/package metadata files
|
|
209
|
+
if (isCollectionRootLevel) {
|
|
210
|
+
const lower = entry.name.toLowerCase();
|
|
211
|
+
if (lower === 'demos.json' || COLLECTION_ROOT_EXCLUDED_FILES.has(lower)) {
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const srcPath = path.join(currentSrcDir, entry.name);
|
|
216
|
+
const destPath = path.join(currentDestDir, entry.name);
|
|
217
|
+
let lstat;
|
|
218
|
+
try {
|
|
219
|
+
lstat = await fs.lstat(srcPath);
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (lstat.isSymbolicLink())
|
|
225
|
+
continue;
|
|
226
|
+
const realSrcPath = await fs.realpath(srcPath);
|
|
227
|
+
if (!isWithinRoot(realSrcPath, rootRealpath))
|
|
228
|
+
continue;
|
|
229
|
+
if (excludedRealpaths.has(realSrcPath))
|
|
230
|
+
continue;
|
|
231
|
+
if (activeIgnoreRoot && activeIgnoreMatchers.length > 0) {
|
|
232
|
+
const relFromIgnore = path.relative(activeIgnoreRoot, realSrcPath);
|
|
233
|
+
if (relFromIgnore && !relFromIgnore.startsWith('..')) {
|
|
234
|
+
const penPosix = `/${relFromIgnore.split(path.sep).join('/')}`;
|
|
235
|
+
if (isIgnoredPath(penPosix, activeIgnoreMatchers)) {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
if (lstat.isDirectory()) {
|
|
241
|
+
let nextInsideDemo = insideDemo;
|
|
242
|
+
if (mode === 'collection' && !insideDemo) {
|
|
243
|
+
if (await isBuiltCollectionOutputDir(realSrcPath, rootRealpath)) {
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
const isAllowedDemo = Boolean(allowedDemoDirs?.has(realSrcPath));
|
|
247
|
+
const isAllowedGroup = Boolean(allowedGroupDirs?.has(realSrcPath));
|
|
248
|
+
if (!isAllowedDemo && !isAllowedGroup) {
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
if (isAllowedDemo) {
|
|
252
|
+
nextInsideDemo = true;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
await fs.mkdir(destPath, { recursive: true });
|
|
256
|
+
await copyDirectoryTree({
|
|
257
|
+
currentSrcDir: realSrcPath,
|
|
258
|
+
currentDestDir: destPath,
|
|
259
|
+
rootRealpath,
|
|
260
|
+
excludedRealpaths,
|
|
261
|
+
isCollectionRootLevel: false,
|
|
262
|
+
mode,
|
|
263
|
+
insideDemo: nextInsideDemo,
|
|
264
|
+
allowedDemoDirs,
|
|
265
|
+
allowedGroupDirs,
|
|
266
|
+
inheritedPenContext: penContext,
|
|
267
|
+
activeIgnoreRoot,
|
|
268
|
+
activeIgnoreMatchers,
|
|
269
|
+
unsupportedDemos,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
else if (lstat.isFile()) {
|
|
273
|
+
// In collection mode, skip stray files sitting inside group folders outside any `.cdpn.json` demo
|
|
274
|
+
if (mode === 'collection' && !isCollectionRootLevel && !insideDemo) {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const lowerName = entry.name.toLowerCase();
|
|
278
|
+
if (penContext?.hasClassic && lowerName.endsWith(PEN_HTML_SUFFIX)) {
|
|
279
|
+
const outName = `${entry.name.slice(0, -PEN_HTML_SUFFIX.length)}.html`;
|
|
280
|
+
if (!existingNamesLower.has(outName.toLowerCase())) {
|
|
281
|
+
const compiledHtml = await compileClassicPenHtml({
|
|
282
|
+
penHtmlPath: realSrcPath,
|
|
283
|
+
penContext,
|
|
284
|
+
rootRealpath,
|
|
285
|
+
});
|
|
286
|
+
if (compiledHtml !== null) {
|
|
287
|
+
await fs.writeFile(path.join(currentDestDir, outName), compiledHtml, 'utf8');
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (penContext?.hasPackages &&
|
|
293
|
+
lowerName.endsWith('.html') &&
|
|
294
|
+
!lowerName.endsWith(PEN_HTML_SUFFIX)) {
|
|
295
|
+
const rawHtml = await fs.readFile(realSrcPath, 'utf8');
|
|
296
|
+
const transformedHtml = await applyPackagesBlock({
|
|
297
|
+
html: rawHtml,
|
|
298
|
+
penContext,
|
|
299
|
+
rootRealpath,
|
|
300
|
+
});
|
|
301
|
+
await fs.writeFile(destPath, transformedHtml, 'utf8');
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
await fs.copyFile(realSrcPath, destPath);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Builds a single Pen (`.cdpn.json`) or collection directory into `outDir`.
|
|
310
|
+
*/
|
|
311
|
+
export async function buildProject(options) {
|
|
312
|
+
const baseCwd = path.resolve(options.cwd ?? process.cwd());
|
|
313
|
+
let inspection;
|
|
314
|
+
let target;
|
|
315
|
+
if (options.preResolved) {
|
|
316
|
+
inspection = options.preResolved.inspection;
|
|
317
|
+
target = options.preResolved.target;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
const resolvedSource = path.resolve(baseCwd, options.sourceDir);
|
|
321
|
+
let sourceStat;
|
|
322
|
+
try {
|
|
323
|
+
sourceStat = await fs.stat(resolvedSource);
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
if (error.code === 'ENOENT') {
|
|
327
|
+
throw new CliError(404, `Source directory not found: ${resolvedSource}`);
|
|
328
|
+
}
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
if (!sourceStat.isDirectory()) {
|
|
332
|
+
throw new CliError(400, `Source path is not a directory: ${resolvedSource}`);
|
|
333
|
+
}
|
|
334
|
+
inspection = await inspectTargetDirectory(resolvedSource);
|
|
335
|
+
const targetValidation = await resolveBuildTargetDir(options.outDir, inspection.rootRealpath, {
|
|
336
|
+
homeDir: options.homeDir,
|
|
337
|
+
});
|
|
338
|
+
if (!targetValidation.ok) {
|
|
339
|
+
throw new CliError(400, targetValidation.error);
|
|
340
|
+
}
|
|
341
|
+
target = targetValidation.target;
|
|
342
|
+
}
|
|
343
|
+
const { outDir, canonicalOutDir } = target;
|
|
344
|
+
const excludedRealpaths = new Set([canonicalOutDir]);
|
|
345
|
+
const mode = classifyDirectoryMode(inspection);
|
|
346
|
+
let preScannedManifest = null;
|
|
347
|
+
let allowedDemoDirs = null;
|
|
348
|
+
let allowedGroupDirs = null;
|
|
349
|
+
if (mode === 'collection') {
|
|
350
|
+
preScannedManifest = await scanDemos(inspection.rootRealpath, {
|
|
351
|
+
excludeDirs: excludedRealpaths,
|
|
352
|
+
});
|
|
353
|
+
const sets = collectManifestDirSets(preScannedManifest, inspection.rootRealpath);
|
|
354
|
+
allowedDemoDirs = sets.demoDirs;
|
|
355
|
+
allowedGroupDirs = sets.groupDirs;
|
|
356
|
+
}
|
|
357
|
+
// In single-Pen mode, fail loudly when unsupported CodePen blocks are active
|
|
358
|
+
if (mode === 'pen' && inspection.penBlocks && inspection.penBlocks.unsupportedBlocks.length > 0) {
|
|
359
|
+
const list = inspection.penBlocks.unsupportedBlocks.join(', ');
|
|
360
|
+
throw new CliError(422, `Cannot build this Pen locally because it uses unsupported CodePen block(s): ${list}.`, { unsupported_blocks: inspection.penBlocks.unsupportedBlocks });
|
|
361
|
+
}
|
|
362
|
+
let activeShell = null;
|
|
363
|
+
if (mode === 'collection') {
|
|
364
|
+
activeShell = await resolveActiveShell({
|
|
365
|
+
explicitShell: options.explicitShell,
|
|
366
|
+
cwd: baseCwd,
|
|
367
|
+
homeDir: options.homeDir,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
else if (options.explicitShell !== undefined) {
|
|
371
|
+
await resolveActiveShell({
|
|
372
|
+
explicitShell: options.explicitShell,
|
|
373
|
+
cwd: baseCwd,
|
|
374
|
+
homeDir: options.homeDir,
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
// Wipe and recreate target output directory
|
|
378
|
+
await fs.rm(outDir, { recursive: true, force: true });
|
|
379
|
+
await fs.mkdir(outDir, { recursive: true });
|
|
380
|
+
const realOutDir = await fs.realpath(outDir);
|
|
381
|
+
excludedRealpaths.add(realOutDir);
|
|
382
|
+
const unsupportedDemos = [];
|
|
383
|
+
await copyDirectoryTree({
|
|
384
|
+
currentSrcDir: inspection.rootRealpath,
|
|
385
|
+
currentDestDir: realOutDir,
|
|
386
|
+
rootRealpath: inspection.rootRealpath,
|
|
387
|
+
excludedRealpaths,
|
|
388
|
+
isCollectionRootLevel: mode === 'collection',
|
|
389
|
+
mode,
|
|
390
|
+
insideDemo: mode === 'pen',
|
|
391
|
+
allowedDemoDirs,
|
|
392
|
+
allowedGroupDirs,
|
|
393
|
+
inheritedPenContext: null,
|
|
394
|
+
activeIgnoreRoot: null,
|
|
395
|
+
activeIgnoreMatchers: [],
|
|
396
|
+
unsupportedDemos,
|
|
397
|
+
});
|
|
398
|
+
let finalManifest = null;
|
|
399
|
+
let demoCount = mode === 'collection' ? 0 : 1;
|
|
400
|
+
if (mode === 'collection' && activeShell) {
|
|
401
|
+
const siteMeta = resolveSiteMetadata(inspection.rootRealpath, options.site);
|
|
402
|
+
// Layer custom/active shell assets wherever the source directory did not override them
|
|
403
|
+
const primaryFiles = await listShellAssetFiles(activeShell.realpath);
|
|
404
|
+
for (const relPosix of primaryFiles) {
|
|
405
|
+
const destPath = path.join(realOutDir, ...relPosix.split('/'));
|
|
406
|
+
if (!isWithinRoot(destPath, realOutDir))
|
|
407
|
+
continue;
|
|
408
|
+
if (existsSync(destPath))
|
|
409
|
+
continue;
|
|
410
|
+
const srcPath = path.join(activeShell.realpath, ...relPosix.split('/'));
|
|
411
|
+
await fs.mkdir(path.dirname(destPath), { recursive: true });
|
|
412
|
+
if (relPosix === 'index.html') {
|
|
413
|
+
const templateHtml = await fs.readFile(srcPath, 'utf8');
|
|
414
|
+
await fs.writeFile(destPath, renderShellIndexHtml(templateHtml, siteMeta, { includeMissingDemoWarning: false }), 'utf8');
|
|
415
|
+
}
|
|
416
|
+
else {
|
|
417
|
+
await fs.copyFile(srcPath, destPath);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
// Fall back to built-in shell for any missing core files (supports partial custom shells)
|
|
421
|
+
if (activeShell.source !== 'builtin') {
|
|
422
|
+
for (const filename of CORE_SHELL_FILES) {
|
|
423
|
+
const destPath = path.join(realOutDir, filename);
|
|
424
|
+
if (existsSync(destPath))
|
|
425
|
+
continue;
|
|
426
|
+
const builtinPath = path.join(activeShell.builtinRealpath, filename);
|
|
427
|
+
if (!existsSync(builtinPath))
|
|
428
|
+
continue;
|
|
429
|
+
if (filename === 'index.html') {
|
|
430
|
+
const templateHtml = await fs.readFile(builtinPath, 'utf8');
|
|
431
|
+
await fs.writeFile(destPath, renderShellIndexHtml(templateHtml, siteMeta, { includeMissingDemoWarning: false }), 'utf8');
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
await fs.copyFile(builtinPath, destPath);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
// Generate fresh demos.json in outDir
|
|
439
|
+
finalManifest =
|
|
440
|
+
preScannedManifest ??
|
|
441
|
+
(await scanDemos(inspection.rootRealpath, { excludeDirs: excludedRealpaths }));
|
|
442
|
+
demoCount = countDemos(finalManifest);
|
|
443
|
+
await fs.writeFile(path.join(realOutDir, 'demos.json'), serializeManifest(finalManifest), 'utf8');
|
|
444
|
+
}
|
|
445
|
+
return {
|
|
446
|
+
mode,
|
|
447
|
+
sourceDir: inspection.rootRealpath,
|
|
448
|
+
outDir: realOutDir,
|
|
449
|
+
demoCount,
|
|
450
|
+
manifest: finalManifest,
|
|
451
|
+
shell: activeShell,
|
|
452
|
+
unsupportedDemos,
|
|
453
|
+
inspection,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
export function watchBuild(options) {
|
|
457
|
+
const { buildOptions, canonicalOutDir, debounceMs = 200, onRebuild, onError } = options;
|
|
458
|
+
const resolvedSource = path.resolve(buildOptions.cwd ?? process.cwd(), buildOptions.sourceDir);
|
|
459
|
+
const { preResolved: _ignored, ...freshBuildOptions } = buildOptions;
|
|
460
|
+
return createDebouncedWatcher({
|
|
461
|
+
rootDir: resolvedSource,
|
|
462
|
+
debounceMs,
|
|
463
|
+
shouldIgnore: (filename) => {
|
|
464
|
+
if (typeof filename !== 'string' || !filename)
|
|
465
|
+
return false;
|
|
466
|
+
const candidate = path.resolve(resolvedSource, filename);
|
|
467
|
+
return isWithinRoot(candidate, canonicalOutDir);
|
|
468
|
+
},
|
|
469
|
+
onChange: async () => {
|
|
470
|
+
const result = await buildProject(freshBuildOptions);
|
|
471
|
+
onRebuild?.(result);
|
|
472
|
+
},
|
|
473
|
+
onError,
|
|
474
|
+
});
|
|
475
|
+
}
|
package/site/detect.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { readProjectConfigFile } from '../config/project.js';
|
|
4
|
+
import { CODEPEN_DIR, PEN_CONFIG_FILENAME, PROJECT_CONFIG_FILENAME } from '../constants.js';
|
|
5
|
+
import { resolveSafeDir, resolveSafeFile } from '../utils/fs-safe.js';
|
|
6
|
+
import { readPenBlocks } from './blocks/index.js';
|
|
7
|
+
/**
|
|
8
|
+
* Inspects a directory directly (does not walk upward) to classify whether it is a
|
|
9
|
+
* CodePen Pen folder (`.cdpn.json`) or a multi-Pen collection directory.
|
|
10
|
+
*/
|
|
11
|
+
export async function inspectTargetDirectory(targetDir) {
|
|
12
|
+
const resolved = path.resolve(targetDir);
|
|
13
|
+
const rootRealpath = await fs.realpath(resolved);
|
|
14
|
+
const [cdpnJson, codepenDir, penConfig, indexHtml, classicPenHtml] = await Promise.all([
|
|
15
|
+
resolveSafeFile(path.join(rootRealpath, PROJECT_CONFIG_FILENAME), rootRealpath),
|
|
16
|
+
resolveSafeDir(path.join(rootRealpath, CODEPEN_DIR), rootRealpath),
|
|
17
|
+
resolveSafeFile(path.join(rootRealpath, CODEPEN_DIR, PEN_CONFIG_FILENAME), rootRealpath),
|
|
18
|
+
resolveSafeFile(path.join(rootRealpath, 'index.html'), rootRealpath),
|
|
19
|
+
resolveSafeFile(path.join(rootRealpath, 'index.pen.html'), rootRealpath),
|
|
20
|
+
]);
|
|
21
|
+
const hasCdpnJson = Boolean(cdpnJson);
|
|
22
|
+
const hasCodepenDir = Boolean(codepenDir);
|
|
23
|
+
const hasPenConfig = Boolean(penConfig);
|
|
24
|
+
const hasIndexHtml = Boolean(indexHtml);
|
|
25
|
+
const hasClassicPenHtml = Boolean(classicPenHtml);
|
|
26
|
+
let cdpnState = 'none';
|
|
27
|
+
let penTitle = null;
|
|
28
|
+
if (hasCdpnJson) {
|
|
29
|
+
try {
|
|
30
|
+
const parsed = await readProjectConfigFile(rootRealpath);
|
|
31
|
+
if (parsed.linked) {
|
|
32
|
+
cdpnState = parsed.config.editor === 'Classic' ? 'linked-classic' : 'linked-v2';
|
|
33
|
+
penTitle = parsed.config.info?.title?.trim() || null;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
cdpnState = 'unlinked';
|
|
37
|
+
penTitle = parsed.config.info?.title?.trim() || null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
cdpnState = 'invalid';
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const penBlocks = hasPenConfig ? await readPenBlocks(rootRealpath, rootRealpath) : null;
|
|
45
|
+
return {
|
|
46
|
+
rootDir: resolved,
|
|
47
|
+
rootRealpath,
|
|
48
|
+
hasCdpnJson,
|
|
49
|
+
cdpnState,
|
|
50
|
+
penTitle,
|
|
51
|
+
hasCodepenDir,
|
|
52
|
+
hasPenConfig,
|
|
53
|
+
hasIndexHtml,
|
|
54
|
+
hasClassicPenHtml,
|
|
55
|
+
penBlocks,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Resolves the effective operating mode for `serve` and `build`:
|
|
60
|
+
* - `'pen'`: directory directly contains a `.cdpn.json` file
|
|
61
|
+
* - `'collection'`: directory does not contain `.cdpn.json` (serves/builds `.cdpn.json` subdirectories)
|
|
62
|
+
*/
|
|
63
|
+
export function classifyDirectoryMode(inspection) {
|
|
64
|
+
return inspection.hasCdpnJson ? 'pen' : 'collection';
|
|
65
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { IGNORE_FILENAME } from '../constants.js';
|
|
4
|
+
import { isIgnoredPath, loadIgnoreMatchers } from '../sync/fs.js';
|
|
5
|
+
const GITIGNORE_FILENAME = '.gitignore';
|
|
6
|
+
/**
|
|
7
|
+
* Ensures `relInsideSource` (e.g. `build`) is ignored by `<penDir>/.cdpnignore`,
|
|
8
|
+
* appending cleanly without duplicating existing rules or clobbering existing entries.
|
|
9
|
+
*/
|
|
10
|
+
export async function ensureIgnoredInCdpnIgnore(penDir, relInsideSource) {
|
|
11
|
+
const normalizedRel = relInsideSource.replace(/^\/+/, '').replace(/\/+$/, '');
|
|
12
|
+
const entry = `${normalizedRel}/`;
|
|
13
|
+
const ignorePath = path.join(path.resolve(penDir), IGNORE_FILENAME);
|
|
14
|
+
const matchers = await loadIgnoreMatchers(penDir);
|
|
15
|
+
if (isIgnoredPath(`/${normalizedRel}`, matchers) &&
|
|
16
|
+
isIgnoredPath(`/${normalizedRel}/index.html`, matchers)) {
|
|
17
|
+
return { updated: false, created: false, alreadyIgnored: true, entry };
|
|
18
|
+
}
|
|
19
|
+
let existing = null;
|
|
20
|
+
try {
|
|
21
|
+
existing = await fs.readFile(ignorePath, 'utf8');
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (error.code !== 'ENOENT') {
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (existing === null) {
|
|
29
|
+
await fs.writeFile(ignorePath, `${entry}\n`, 'utf8');
|
|
30
|
+
return { updated: true, created: true, alreadyIgnored: false, entry };
|
|
31
|
+
}
|
|
32
|
+
const separator = existing.length === 0 || existing.endsWith('\n') ? '' : '\n';
|
|
33
|
+
await fs.writeFile(ignorePath, `${existing}${separator}${entry}\n`, 'utf8');
|
|
34
|
+
return { updated: true, created: false, alreadyIgnored: false, entry };
|
|
35
|
+
}
|
|
36
|
+
export async function inspectGitIgnoreStatus(dir, relInsideSource) {
|
|
37
|
+
const normalizedRel = relInsideSource.replace(/^\/+/, '').replace(/\/+$/, '');
|
|
38
|
+
const entry = `${normalizedRel}/`;
|
|
39
|
+
const filePath = path.join(path.resolve(dir), GITIGNORE_FILENAME);
|
|
40
|
+
let raw = null;
|
|
41
|
+
try {
|
|
42
|
+
raw = await fs.readFile(filePath, 'utf8');
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (error.code !== 'ENOENT') {
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (raw === null) {
|
|
50
|
+
return { exists: false, alreadyIgnored: false, entry, filePath };
|
|
51
|
+
}
|
|
52
|
+
const targetCandidates = new Set([
|
|
53
|
+
normalizedRel,
|
|
54
|
+
`/${normalizedRel}`,
|
|
55
|
+
`${normalizedRel}/`,
|
|
56
|
+
`/${normalizedRel}/`,
|
|
57
|
+
`${normalizedRel}/*`,
|
|
58
|
+
`/${normalizedRel}/*`,
|
|
59
|
+
]);
|
|
60
|
+
const topSegment = normalizedRel.split('/')[0];
|
|
61
|
+
if (topSegment && topSegment !== normalizedRel) {
|
|
62
|
+
targetCandidates.add(topSegment);
|
|
63
|
+
targetCandidates.add(`/${topSegment}`);
|
|
64
|
+
targetCandidates.add(`${topSegment}/`);
|
|
65
|
+
targetCandidates.add(`/${topSegment}/`);
|
|
66
|
+
}
|
|
67
|
+
const alreadyIgnored = raw
|
|
68
|
+
.split(/\r?\n/)
|
|
69
|
+
.map((line) => line.trim())
|
|
70
|
+
.some((line) => line && !line.startsWith('#') && targetCandidates.has(line));
|
|
71
|
+
return { exists: true, alreadyIgnored, entry, filePath };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Creates a `.gitignore` or appends `relInsideSource/` to an existing `.gitignore`.
|
|
75
|
+
*/
|
|
76
|
+
export async function updateGitIgnore(dir, relInsideSource) {
|
|
77
|
+
const status = await inspectGitIgnoreStatus(dir, relInsideSource);
|
|
78
|
+
if (status.alreadyIgnored) {
|
|
79
|
+
return { updated: false, created: false, entry: status.entry, filePath: status.filePath };
|
|
80
|
+
}
|
|
81
|
+
if (!status.exists) {
|
|
82
|
+
const content = ['# OS', '.DS_Store', 'Thumbs.db', '', '# Build output', status.entry, ''].join('\n');
|
|
83
|
+
await fs.writeFile(status.filePath, content, 'utf8');
|
|
84
|
+
return { updated: true, created: true, entry: status.entry, filePath: status.filePath };
|
|
85
|
+
}
|
|
86
|
+
const existing = await fs.readFile(status.filePath, 'utf8');
|
|
87
|
+
const separator = existing.length === 0 || existing.endsWith('\n') ? '' : '\n';
|
|
88
|
+
await fs.writeFile(status.filePath, `${existing}${separator}${status.entry}\n`, 'utf8');
|
|
89
|
+
return { updated: true, created: false, entry: status.entry, filePath: status.filePath };
|
|
90
|
+
}
|