codepen-demokit 0.0.0-stage → 1.1.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 +235 -2
- package/bin/cli.mjs +155 -0
- package/lib/blocks.mjs +1067 -0
- package/lib/build-json.mjs +507 -0
- package/lib/build-site.mjs +258 -0
- package/lib/config.mjs +254 -0
- package/lib/init.mjs +1084 -0
- package/lib/serve.mjs +507 -0
- package/lib/sync-codepen.mjs +1375 -0
- package/package.json +40 -4
- package/shell/index.html +69 -0
- package/shell/scripts.js +268 -0
- package/shell/styles.css +463 -0
- package/templates/AGENTS.md +77 -0
- package/templates/README.md +63 -0
- package/templates/config.json.example +10 -0
- package/templates/gitignore +27 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { existsSync, watch } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import {
|
|
6
|
+
PEN_HTML_SUFFIX,
|
|
7
|
+
applyPackagesBlock,
|
|
8
|
+
compileClassicPenHtml,
|
|
9
|
+
readPenBlocks,
|
|
10
|
+
renderUnsupportedBlocksPage,
|
|
11
|
+
} from './blocks.mjs';
|
|
12
|
+
import { buildDemosJson } from './build-json.mjs';
|
|
13
|
+
import { CONFIG_FILENAME, loadSiteConfig, renderShellIndexHtml } from './config.mjs';
|
|
14
|
+
|
|
15
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
16
|
+
const __dirname = path.dirname(__filename);
|
|
17
|
+
export const PACKAGE_DIR = path.resolve(__dirname, '..');
|
|
18
|
+
export const SHELL_DIR = path.join(PACKAGE_DIR, 'shell');
|
|
19
|
+
|
|
20
|
+
const SHELL_FILES = ['index.html', 'styles.css', 'scripts.js'];
|
|
21
|
+
|
|
22
|
+
function isWithinRoot(candidatePath, rootPath) {
|
|
23
|
+
return candidatePath === rootPath || candidatePath.startsWith(rootPath + path.sep);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function copySrcTree(
|
|
27
|
+
currentSrcDir,
|
|
28
|
+
currentDestDir,
|
|
29
|
+
srcRealpath,
|
|
30
|
+
isRootLevel,
|
|
31
|
+
inheritedPenContext = null,
|
|
32
|
+
) {
|
|
33
|
+
const localPenContext = await readPenBlocks(currentSrcDir, srcRealpath);
|
|
34
|
+
const penContext = localPenContext ?? inheritedPenContext;
|
|
35
|
+
|
|
36
|
+
if (localPenContext && localPenContext.unsupportedBlocks.length > 0) {
|
|
37
|
+
const relFromSrc = path.relative(srcRealpath, currentSrcDir);
|
|
38
|
+
const relSegments = relFromSrc ? relFromSrc.split(path.sep).filter(Boolean) : [];
|
|
39
|
+
const homeHref = relSegments.length > 0 ? '../'.repeat(relSegments.length) : null;
|
|
40
|
+
const demoRelPath =
|
|
41
|
+
relSegments.length > 0 ? `${relSegments.join('/')}/` : `${path.basename(currentSrcDir)}/`;
|
|
42
|
+
const { html } = await renderUnsupportedBlocksPage({
|
|
43
|
+
penContext: localPenContext,
|
|
44
|
+
rootRealpath: srcRealpath,
|
|
45
|
+
demoRelPath,
|
|
46
|
+
homeHref,
|
|
47
|
+
});
|
|
48
|
+
await fs.writeFile(path.join(currentDestDir, 'index.html'), html, 'utf8');
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const entries = await fs.readdir(currentSrcDir, { withFileTypes: true });
|
|
53
|
+
const existingNamesLower = new Set(entries.map((entry) => entry.name.toLowerCase()));
|
|
54
|
+
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
// Skip dotfiles/dotdirs (.cdpn.json, .cdpnignore, .codepen, .DS_Store, .gitkeep, etc.) and node_modules
|
|
57
|
+
if (entry.name.startsWith('.') || entry.name === 'node_modules') continue;
|
|
58
|
+
// Skip any stale top-level src/demos.json — dist/demos.json is generated freshly
|
|
59
|
+
if (isRootLevel && entry.name === 'demos.json') continue;
|
|
60
|
+
|
|
61
|
+
const srcPath = path.join(currentSrcDir, entry.name);
|
|
62
|
+
const destPath = path.join(currentDestDir, entry.name);
|
|
63
|
+
|
|
64
|
+
let lstat;
|
|
65
|
+
try {
|
|
66
|
+
lstat = await fs.lstat(srcPath);
|
|
67
|
+
} catch {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (lstat.isSymbolicLink()) continue;
|
|
72
|
+
|
|
73
|
+
const realSrcPath = await fs.realpath(srcPath);
|
|
74
|
+
if (!isWithinRoot(realSrcPath, srcRealpath)) continue;
|
|
75
|
+
|
|
76
|
+
if (lstat.isDirectory()) {
|
|
77
|
+
await fs.mkdir(destPath, { recursive: true });
|
|
78
|
+
await copySrcTree(realSrcPath, destPath, srcRealpath, false, penContext);
|
|
79
|
+
} else if (lstat.isFile()) {
|
|
80
|
+
const lowerName = entry.name.toLowerCase();
|
|
81
|
+
|
|
82
|
+
if (penContext?.hasClassic && lowerName.endsWith(PEN_HTML_SUFFIX)) {
|
|
83
|
+
const outName = `${entry.name.slice(0, -PEN_HTML_SUFFIX.length)}.html`;
|
|
84
|
+
if (!existingNamesLower.has(outName.toLowerCase())) {
|
|
85
|
+
const compiledHtml = await compileClassicPenHtml({
|
|
86
|
+
penHtmlPath: realSrcPath,
|
|
87
|
+
penContext,
|
|
88
|
+
rootRealpath: srcRealpath,
|
|
89
|
+
});
|
|
90
|
+
if (compiledHtml !== null) {
|
|
91
|
+
await fs.writeFile(path.join(currentDestDir, outName), compiledHtml, 'utf8');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (
|
|
98
|
+
penContext?.hasPackages &&
|
|
99
|
+
lowerName.endsWith('.html') &&
|
|
100
|
+
!lowerName.endsWith(PEN_HTML_SUFFIX)
|
|
101
|
+
) {
|
|
102
|
+
const rawHtml = await fs.readFile(realSrcPath, 'utf8');
|
|
103
|
+
const transformedHtml = await applyPackagesBlock({
|
|
104
|
+
html: rawHtml,
|
|
105
|
+
penContext,
|
|
106
|
+
rootRealpath: srcRealpath,
|
|
107
|
+
});
|
|
108
|
+
await fs.writeFile(destPath, transformedHtml, 'utf8');
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
await fs.copyFile(realSrcPath, destPath);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Builds a complete, static deployable site into `<projectDir>/dist/`:
|
|
119
|
+
* - Copies all non-hidden demos and assets from `<projectDir>/src/`
|
|
120
|
+
* - Layers framework `shell/` files (`index.html`, `styles.css`, `scripts.js`) unless overridden in `src/`
|
|
121
|
+
* - Injects `site` metadata from `config.json` into the default `shell/index.html`
|
|
122
|
+
* - Generates `<projectDir>/dist/demos.json`
|
|
123
|
+
*/
|
|
124
|
+
export async function buildSite({
|
|
125
|
+
projectDir = process.cwd(),
|
|
126
|
+
outDir = path.resolve(projectDir, 'dist'),
|
|
127
|
+
} = {}) {
|
|
128
|
+
const resolvedProjectDir = path.resolve(projectDir);
|
|
129
|
+
const srcDir = path.resolve(resolvedProjectDir, 'src');
|
|
130
|
+
const distDir = path.resolve(outDir);
|
|
131
|
+
|
|
132
|
+
if (!distDir.startsWith(resolvedProjectDir + path.sep)) {
|
|
133
|
+
throw new Error(`Refusing to build outside project directory: ${distDir}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let srcRealpath;
|
|
137
|
+
try {
|
|
138
|
+
const stat = await fs.stat(srcDir);
|
|
139
|
+
if (!stat.isDirectory()) {
|
|
140
|
+
throw new Error(`"src" in ${resolvedProjectDir} is not a directory.`);
|
|
141
|
+
}
|
|
142
|
+
srcRealpath = await fs.realpath(srcDir);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
throw new Error(`Directory "src/" not found in ${resolvedProjectDir}.`, { cause: error });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
await fs.rm(distDir, { recursive: true, force: true });
|
|
148
|
+
await fs.mkdir(distDir, { recursive: true });
|
|
149
|
+
|
|
150
|
+
// 1. Copy user demos and any user-provided index overrides from src/
|
|
151
|
+
await copySrcTree(srcRealpath, distDir, srcRealpath, true);
|
|
152
|
+
|
|
153
|
+
// 2. Layer framework shell files wherever the user has not overridden them in src/
|
|
154
|
+
const siteConfig = loadSiteConfig(resolvedProjectDir);
|
|
155
|
+
for (const filename of SHELL_FILES) {
|
|
156
|
+
const destPath = path.join(distDir, filename);
|
|
157
|
+
if (existsSync(destPath)) continue;
|
|
158
|
+
|
|
159
|
+
const shellPath = path.join(SHELL_DIR, filename);
|
|
160
|
+
if (filename === 'index.html') {
|
|
161
|
+
const templateHtml = await fs.readFile(shellPath, 'utf8');
|
|
162
|
+
const renderedHtml = renderShellIndexHtml(templateHtml, siteConfig);
|
|
163
|
+
await fs.writeFile(destPath, renderedHtml, 'utf8');
|
|
164
|
+
} else {
|
|
165
|
+
await fs.copyFile(shellPath, destPath);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 3. Generate static dist/demos.json
|
|
170
|
+
const { demoCount, manifest } = await buildDemosJson({
|
|
171
|
+
projectDir: resolvedProjectDir,
|
|
172
|
+
outputFile: path.join(distDir, 'demos.json'),
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
return { distDir, demoCount, manifest };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function watchSite({
|
|
179
|
+
projectDir = process.cwd(),
|
|
180
|
+
outDir = path.resolve(projectDir, 'dist'),
|
|
181
|
+
debounceMs = 200,
|
|
182
|
+
onRebuild,
|
|
183
|
+
onError,
|
|
184
|
+
} = {}) {
|
|
185
|
+
const resolvedProjectDir = path.resolve(projectDir);
|
|
186
|
+
const srcDir = path.resolve(resolvedProjectDir, 'src');
|
|
187
|
+
let timer = null;
|
|
188
|
+
let isBuilding = false;
|
|
189
|
+
let pendingRebuild = false;
|
|
190
|
+
let closed = false;
|
|
191
|
+
|
|
192
|
+
async function runBuild() {
|
|
193
|
+
if (closed) return;
|
|
194
|
+
if (isBuilding) {
|
|
195
|
+
pendingRebuild = true;
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
isBuilding = true;
|
|
200
|
+
try {
|
|
201
|
+
const result = await buildSite({ projectDir: resolvedProjectDir, outDir });
|
|
202
|
+
onRebuild?.(result);
|
|
203
|
+
} catch (error) {
|
|
204
|
+
onError?.(error);
|
|
205
|
+
} finally {
|
|
206
|
+
isBuilding = false;
|
|
207
|
+
if (pendingRebuild && !closed) {
|
|
208
|
+
pendingRebuild = false;
|
|
209
|
+
scheduleBuild();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function scheduleBuild() {
|
|
215
|
+
if (closed) return;
|
|
216
|
+
if (timer) clearTimeout(timer);
|
|
217
|
+
timer = setTimeout(() => {
|
|
218
|
+
timer = null;
|
|
219
|
+
void runBuild();
|
|
220
|
+
}, debounceMs);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const watchers = [];
|
|
224
|
+
|
|
225
|
+
const srcWatcher = watch(srcDir, { recursive: true }, (_eventType, filename) => {
|
|
226
|
+
if (typeof filename === 'string') {
|
|
227
|
+
const normalized = filename.split(path.sep).join('/');
|
|
228
|
+
const segments = normalized.split('/');
|
|
229
|
+
if (segments.at(-1) === '.DS_Store') return;
|
|
230
|
+
if (segments.includes('node_modules') || segments.includes('.git')) return;
|
|
231
|
+
}
|
|
232
|
+
scheduleBuild();
|
|
233
|
+
});
|
|
234
|
+
srcWatcher.on('error', (error) => onError?.(error));
|
|
235
|
+
watchers.push(srcWatcher);
|
|
236
|
+
|
|
237
|
+
// Also watch project root (non-recursively) for config.json updates
|
|
238
|
+
const rootWatcher = watch(resolvedProjectDir, (_eventType, filename) => {
|
|
239
|
+
if (filename === CONFIG_FILENAME) {
|
|
240
|
+
scheduleBuild();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
rootWatcher.on('error', (error) => onError?.(error));
|
|
244
|
+
watchers.push(rootWatcher);
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
close() {
|
|
248
|
+
closed = true;
|
|
249
|
+
if (timer) {
|
|
250
|
+
clearTimeout(timer);
|
|
251
|
+
timer = null;
|
|
252
|
+
}
|
|
253
|
+
for (const watcher of watchers) {
|
|
254
|
+
watcher.close();
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
};
|
|
258
|
+
}
|
package/lib/config.mjs
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export const CONFIG_FILENAME = 'config.json';
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_SITE_CONFIG = Object.freeze({
|
|
7
|
+
title: 'My CodePen Demos',
|
|
8
|
+
subtitle: 'A collection of self-contained web demos.',
|
|
9
|
+
footer: 'Powered by codepen-demokit.',
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const PROFILE_NAME_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/;
|
|
13
|
+
export const VISIBILITY_OPTIONS = Object.freeze([
|
|
14
|
+
'Private',
|
|
15
|
+
'Public',
|
|
16
|
+
'Protected',
|
|
17
|
+
'Collaborators',
|
|
18
|
+
]);
|
|
19
|
+
export const DEFAULT_VISIBILITY = 'Private';
|
|
20
|
+
export const VALID_VISIBILITIES = new Set(VISIBILITY_OPTIONS);
|
|
21
|
+
|
|
22
|
+
export function getConfigPath(projectDir = process.cwd()) {
|
|
23
|
+
return path.resolve(projectDir, CONFIG_FILENAME);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function readRawConfigFile(configPath) {
|
|
27
|
+
if (!fs.existsSync(configPath)) {
|
|
28
|
+
return { status: 'missing', parsed: null, error: null };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let parsed;
|
|
32
|
+
try {
|
|
33
|
+
const raw = fs.readFileSync(configPath, 'utf8');
|
|
34
|
+
parsed = JSON.parse(raw);
|
|
35
|
+
} catch (err) {
|
|
36
|
+
return { status: 'parse-error', parsed: null, error: err };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
40
|
+
return { status: 'invalid-root', parsed: null, error: null };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { status: 'ok', parsed, error: null };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Loads optional `site` metadata from `<projectDir>/config.json`, falling back
|
|
48
|
+
* to DEFAULT_SITE_CONFIG when `config.json` or individual fields are omitted.
|
|
49
|
+
*/
|
|
50
|
+
export function loadSiteConfig(projectDir = process.cwd()) {
|
|
51
|
+
const configPath = getConfigPath(projectDir);
|
|
52
|
+
const { status, parsed } = readRawConfigFile(configPath);
|
|
53
|
+
if (status !== 'ok') {
|
|
54
|
+
return { ...DEFAULT_SITE_CONFIG };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const site =
|
|
58
|
+
parsed.site && typeof parsed.site === 'object' && !Array.isArray(parsed.site)
|
|
59
|
+
? parsed.site
|
|
60
|
+
: {};
|
|
61
|
+
|
|
62
|
+
const pickString = (value, fallback) =>
|
|
63
|
+
typeof value === 'string' && value.trim() ? value.trim() : fallback;
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
title: pickString(site.title, DEFAULT_SITE_CONFIG.title),
|
|
67
|
+
subtitle: pickString(site.subtitle, DEFAULT_SITE_CONFIG.subtitle),
|
|
68
|
+
footer: pickString(site.footer, DEFAULT_SITE_CONFIG.footer),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Loads and strictly validates required `codepen` settings from `<projectDir>/config.json`.
|
|
74
|
+
* Returns `{ ok: true, requiredProfile, defaultNewAccess }` or `{ ok: false, warnings: string[] }`.
|
|
75
|
+
*/
|
|
76
|
+
export function loadCodePenConfig(projectDir = process.cwd()) {
|
|
77
|
+
const configPath = getConfigPath(projectDir);
|
|
78
|
+
const { status, parsed, error } = readRawConfigFile(configPath);
|
|
79
|
+
|
|
80
|
+
if (status === 'missing') {
|
|
81
|
+
return {
|
|
82
|
+
ok: false,
|
|
83
|
+
warnings: [
|
|
84
|
+
`Warning: ${CONFIG_FILENAME} not found at ${configPath}.`,
|
|
85
|
+
` Copy config.json.example to ${CONFIG_FILENAME} and configure "codepen.user" and "codepen.defaultVisibility".`,
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (status === 'parse-error') {
|
|
91
|
+
return {
|
|
92
|
+
ok: false,
|
|
93
|
+
warnings: [
|
|
94
|
+
`Warning: Failed to read or parse ${CONFIG_FILENAME}: ${error.message}`,
|
|
95
|
+
` Ensure ${CONFIG_FILENAME} contains valid JSON (see config.json.example).`,
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (status === 'invalid-root') {
|
|
101
|
+
return {
|
|
102
|
+
ok: false,
|
|
103
|
+
warnings: [
|
|
104
|
+
`Warning: ${CONFIG_FILENAME} must contain a JSON object.`,
|
|
105
|
+
' See config.json.example for the expected format.',
|
|
106
|
+
],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const issues = [];
|
|
111
|
+
const hasLegacyFlatKeys =
|
|
112
|
+
typeof parsed.codepen_user === 'string' ||
|
|
113
|
+
typeof parsed.codepen_default_visibility === 'string';
|
|
114
|
+
|
|
115
|
+
const codepenSection =
|
|
116
|
+
parsed.codepen && typeof parsed.codepen === 'object' && !Array.isArray(parsed.codepen)
|
|
117
|
+
? parsed.codepen
|
|
118
|
+
: null;
|
|
119
|
+
|
|
120
|
+
if (!codepenSection) {
|
|
121
|
+
if (hasLegacyFlatKeys) {
|
|
122
|
+
issues.push(
|
|
123
|
+
'Legacy flat keys ("codepen_user" / "codepen_default_visibility") found; nest them under "codepen": { "user": "...", "defaultVisibility": "..." }.'
|
|
124
|
+
);
|
|
125
|
+
} else {
|
|
126
|
+
issues.push('"codepen" section is missing.');
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
const rawUser =
|
|
130
|
+
typeof codepenSection.user === 'string' ? codepenSection.user.trim() : '';
|
|
131
|
+
if (!rawUser) {
|
|
132
|
+
issues.push('"codepen.user" is missing or empty.');
|
|
133
|
+
} else if (!PROFILE_NAME_PATTERN.test(rawUser)) {
|
|
134
|
+
issues.push(
|
|
135
|
+
`"codepen.user" contains invalid characters (got "${rawUser}").`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const rawVisibility =
|
|
140
|
+
typeof codepenSection.defaultVisibility === 'string'
|
|
141
|
+
? codepenSection.defaultVisibility.trim()
|
|
142
|
+
: '';
|
|
143
|
+
if (!rawVisibility) {
|
|
144
|
+
issues.push('"codepen.defaultVisibility" is missing or empty.');
|
|
145
|
+
} else if (!VALID_VISIBILITIES.has(rawVisibility)) {
|
|
146
|
+
issues.push(
|
|
147
|
+
`"codepen.defaultVisibility" must be one of: ${[...VALID_VISIBILITIES].join(', ')} (got "${rawVisibility}").`
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (issues.length === 0) {
|
|
152
|
+
return {
|
|
153
|
+
ok: true,
|
|
154
|
+
requiredProfile: rawUser,
|
|
155
|
+
defaultNewAccess: rawVisibility,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
ok: false,
|
|
162
|
+
warnings: [
|
|
163
|
+
`Warning: ${CONFIG_FILENAME} is missing required configuration or contains invalid values:`,
|
|
164
|
+
...issues.map((issue) => ` - ${issue}`),
|
|
165
|
+
' See config.json.example for the expected format.',
|
|
166
|
+
],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Loads an optional `codepen.user` profile from `<projectDir>/config.json`.
|
|
172
|
+
* Missing `config.json` or an empty `codepen.user` returns `{ ok: true, profile: null }`.
|
|
173
|
+
* Invalid JSON or invalid `codepen.user` syntax returns `{ ok: false, warnings }`.
|
|
174
|
+
*/
|
|
175
|
+
export function loadCodePenProfile(projectDir = process.cwd()) {
|
|
176
|
+
const configPath = getConfigPath(projectDir);
|
|
177
|
+
const { status, parsed, error } = readRawConfigFile(configPath);
|
|
178
|
+
|
|
179
|
+
if (status === 'missing') {
|
|
180
|
+
return { ok: true, profile: null };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (status === 'parse-error') {
|
|
184
|
+
return {
|
|
185
|
+
ok: false,
|
|
186
|
+
warnings: [
|
|
187
|
+
`Warning: Failed to read or parse ${CONFIG_FILENAME}: ${error.message}`,
|
|
188
|
+
` Ensure ${CONFIG_FILENAME} contains valid JSON (see config.json.example).`,
|
|
189
|
+
],
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (status === 'invalid-root') {
|
|
194
|
+
return {
|
|
195
|
+
ok: false,
|
|
196
|
+
warnings: [
|
|
197
|
+
`Warning: ${CONFIG_FILENAME} must contain a JSON object.`,
|
|
198
|
+
' See config.json.example for the expected format.',
|
|
199
|
+
],
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const codepenSection =
|
|
204
|
+
parsed.codepen && typeof parsed.codepen === 'object' && !Array.isArray(parsed.codepen)
|
|
205
|
+
? parsed.codepen
|
|
206
|
+
: null;
|
|
207
|
+
|
|
208
|
+
if (!codepenSection || typeof codepenSection.user !== 'string') {
|
|
209
|
+
return { ok: true, profile: null };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const rawUser = codepenSection.user.trim();
|
|
213
|
+
if (!rawUser) {
|
|
214
|
+
return { ok: true, profile: null };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (!PROFILE_NAME_PATTERN.test(rawUser)) {
|
|
218
|
+
return {
|
|
219
|
+
ok: false,
|
|
220
|
+
warnings: [
|
|
221
|
+
`Warning: ${CONFIG_FILENAME} contains an invalid "codepen.user" value ("${rawUser}").`,
|
|
222
|
+
' See config.json.example for the expected format.',
|
|
223
|
+
],
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return { ok: true, profile: rawUser };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export function escapeHtml(value) {
|
|
231
|
+
return String(value)
|
|
232
|
+
.replace(/&/g, '&')
|
|
233
|
+
.replace(/</g, '<')
|
|
234
|
+
.replace(/>/g, '>')
|
|
235
|
+
.replace(/"/g, '"')
|
|
236
|
+
.replace(/'/g, ''');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const CODEPEN_DEMOKIT_NPM_URL = 'https://npmjs.com/package/codepen-demokit';
|
|
240
|
+
|
|
241
|
+
function renderFooterHtml(footerText) {
|
|
242
|
+
return escapeHtml(footerText).replaceAll(
|
|
243
|
+
'codepen-demokit',
|
|
244
|
+
`<a href="${CODEPEN_DEMOKIT_NPM_URL}" target="_blank" rel="noopener noreferrer">codepen-demokit</a>`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function renderShellIndexHtml(templateHtml, siteConfig) {
|
|
249
|
+
const site = { ...DEFAULT_SITE_CONFIG, ...siteConfig };
|
|
250
|
+
return templateHtml
|
|
251
|
+
.replaceAll('{{SITE_TITLE}}', escapeHtml(site.title))
|
|
252
|
+
.replaceAll('{{SITE_SUBTITLE}}', escapeHtml(site.subtitle))
|
|
253
|
+
.replaceAll('{{SITE_FOOTER}}', renderFooterHtml(site.footer));
|
|
254
|
+
}
|