codepen-demokit 0.0.0-stage → 1.0.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 +229 -2
- package/bin/cli.mjs +154 -0
- package/lib/build-json.mjs +439 -0
- package/lib/build-site.mjs +193 -0
- package/lib/config.mjs +245 -0
- package/lib/init.mjs +651 -0
- package/lib/serve.mjs +369 -0
- package/lib/sync-codepen.mjs +1326 -0
- package/package.json +35 -4
- package/shell/index.html +48 -0
- package/shell/scripts.js +201 -0
- package/shell/styles.css +299 -0
- package/templates/AGENTS.md +76 -0
- package/templates/README.md +62 -0
- package/templates/config.json.example +10 -0
- package/templates/gitignore +27 -0
package/lib/config.mjs
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
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
|
+
export function renderShellIndexHtml(templateHtml, siteConfig) {
|
|
240
|
+
const site = { ...DEFAULT_SITE_CONFIG, ...siteConfig };
|
|
241
|
+
return templateHtml
|
|
242
|
+
.replaceAll('{{SITE_TITLE}}', escapeHtml(site.title))
|
|
243
|
+
.replaceAll('{{SITE_SUBTITLE}}', escapeHtml(site.subtitle))
|
|
244
|
+
.replaceAll('{{SITE_FOOTER}}', escapeHtml(site.footer));
|
|
245
|
+
}
|