basecampjs 0.0.12 → 0.0.14

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.
Files changed (77) hide show
  1. package/dist/build/assets.d.ts +36 -0
  2. package/dist/build/assets.d.ts.map +1 -0
  3. package/dist/build/assets.js +272 -0
  4. package/dist/build/assets.js.map +1 -0
  5. package/dist/build/data.d.ts +6 -0
  6. package/dist/build/data.d.ts.map +1 -0
  7. package/dist/build/data.js +33 -0
  8. package/dist/build/data.js.map +1 -0
  9. package/dist/build/pages.d.ts +19 -0
  10. package/dist/build/pages.d.ts.map +1 -0
  11. package/dist/build/pages.js +110 -0
  12. package/dist/build/pages.js.map +1 -0
  13. package/dist/build/pipeline.d.ts +6 -0
  14. package/dist/build/pipeline.d.ts.map +1 -0
  15. package/dist/build/pipeline.js +98 -0
  16. package/dist/build/pipeline.js.map +1 -0
  17. package/dist/cli.d.ts +3 -0
  18. package/dist/cli.d.ts.map +1 -0
  19. package/dist/cli.js +140 -0
  20. package/dist/cli.js.map +1 -0
  21. package/dist/config.d.ts +11 -0
  22. package/dist/config.d.ts.map +1 -0
  23. package/dist/config.js +60 -0
  24. package/dist/config.js.map +1 -0
  25. package/dist/dev/server.d.ts +6 -0
  26. package/dist/dev/server.d.ts.map +1 -0
  27. package/dist/dev/server.js +64 -0
  28. package/dist/dev/server.js.map +1 -0
  29. package/dist/dev/watcher.d.ts +5 -0
  30. package/dist/dev/watcher.d.ts.map +1 -0
  31. package/dist/dev/watcher.js +49 -0
  32. package/dist/dev/watcher.js.map +1 -0
  33. package/dist/index.d.ts +17 -0
  34. package/dist/index.d.ts.map +1 -0
  35. package/dist/index.js +25 -0
  36. package/dist/index.js.map +1 -0
  37. package/dist/render/engines.d.ts +30 -0
  38. package/dist/render/engines.d.ts.map +1 -0
  39. package/dist/render/engines.js +102 -0
  40. package/dist/render/engines.js.map +1 -0
  41. package/dist/scaffolding.d.ts +25 -0
  42. package/dist/scaffolding.d.ts.map +1 -0
  43. package/dist/scaffolding.js +596 -0
  44. package/dist/scaffolding.js.map +1 -0
  45. package/dist/types.d.ts +91 -0
  46. package/dist/types.d.ts.map +1 -0
  47. package/dist/types.js +5 -0
  48. package/dist/types.js.map +1 -0
  49. package/dist/utils/fs.d.ts +29 -0
  50. package/dist/utils/fs.d.ts.map +1 -0
  51. package/dist/utils/fs.js +104 -0
  52. package/dist/utils/fs.js.map +1 -0
  53. package/dist/utils/logger.d.ts +9 -0
  54. package/dist/utils/logger.d.ts.map +1 -0
  55. package/dist/utils/logger.js +21 -0
  56. package/dist/utils/logger.js.map +1 -0
  57. package/dist/utils/paths.d.ts +17 -0
  58. package/dist/utils/paths.d.ts.map +1 -0
  59. package/dist/utils/paths.js +55 -0
  60. package/dist/utils/paths.js.map +1 -0
  61. package/package.json +36 -6
  62. package/src/build/assets.ts +314 -0
  63. package/src/build/data.ts +32 -0
  64. package/src/build/pages.ts +143 -0
  65. package/src/build/pipeline.ts +111 -0
  66. package/src/cli.ts +155 -0
  67. package/src/config.ts +61 -0
  68. package/src/dev/server.ts +66 -0
  69. package/src/dev/watcher.ts +52 -0
  70. package/src/index.ts +45 -0
  71. package/src/render/engines.ts +139 -0
  72. package/src/scaffolding.ts +656 -0
  73. package/src/types.ts +110 -0
  74. package/src/utils/fs.ts +109 -0
  75. package/src/utils/logger.ts +27 -0
  76. package/src/utils/paths.ts +56 -0
  77. package/index.js +0 -1516
package/index.js DELETED
@@ -1,1516 +0,0 @@
1
- #!/usr/bin/env node
2
- import { argv, exit } from "process";
3
- import { createServer } from "http";
4
- import { existsSync } from "fs";
5
- import { cp, mkdir, readFile, readdir, rename, rm, stat, writeFile } from "fs/promises";
6
- import { basename, dirname, extname, join, relative, resolve } from "path";
7
- import { pathToFileURL, fileURLToPath } from "url";
8
- import { createHash } from "crypto";
9
- import * as kolor from "kolorist";
10
- import chokidar from "chokidar";
11
- import matter from "gray-matter";
12
- import MarkdownIt from "markdown-it";
13
- import Mustache from "mustache";
14
- import nunjucks from "nunjucks";
15
- import { Liquid } from "liquidjs";
16
- import { minify as minifyCss } from "csso";
17
- import { minify as minifyHtml } from "html-minifier-terser";
18
- import sharp from "sharp";
19
-
20
- const cwd = process.cwd();
21
- const __dirname = dirname(fileURLToPath(import.meta.url));
22
- const md = new MarkdownIt({ html: true, linkify: true, typographer: true });
23
-
24
- const defaultConfig = {
25
- siteName: "Campsite",
26
- srcDir: "src",
27
- outDir: "dist",
28
- templateEngine: "nunjucks",
29
- markdown: true,
30
- minifyCSS: false,
31
- minifyHTML: false,
32
- cacheBustAssets: false,
33
- excludeFiles: [],
34
- compressPhotos: false,
35
- compressionSettings: {
36
- quality: 80,
37
- formats: [".webp"],
38
- inputFormats: [".jpg", ".jpeg", ".png"],
39
- preserveOriginal: true
40
- },
41
- integrations: { nunjucks: true, liquid: false, mustache: false, vue: false, alpine: false }
42
- };
43
-
44
- async function loadConfig(root) {
45
- const configPath = join(root, "campsite.config.js");
46
- if (!existsSync(configPath)) return { ...defaultConfig };
47
- try {
48
- const imported = await import(pathToFileURL(configPath));
49
- const user = imported.default || imported;
50
- return { ...defaultConfig, ...user };
51
- } catch (err) {
52
- console.error(kolor.red(`Failed to load config: ${err.message}`));
53
- return { ...defaultConfig };
54
- }
55
- }
56
-
57
- async function getVersion() {
58
- try {
59
- const pkgPath = join(__dirname, "package.json");
60
- const raw = await readFile(pkgPath, "utf8");
61
- const pkg = JSON.parse(raw);
62
- return pkg.version || "0.0.0";
63
- } catch {
64
- return "0.0.0";
65
- }
66
- }
67
-
68
- function showHelp() {
69
- console.log(kolor.cyan(kolor.bold("\n🏕️ CampsiteJS CLI")));
70
- console.log(kolor.dim("Build and manage your static campsite.\n"));
71
-
72
- console.log(kolor.bold("Usage:"));
73
- console.log(" camper <command> [arguments] [options]\n");
74
-
75
- console.log(kolor.bold("Project Commands:"));
76
- console.log(" " + kolor.cyan("init") + " Initialize a new Campsite project in current directory");
77
- console.log(" Creates config, folder structure, and starter files\n");
78
-
79
- console.log(kolor.bold("Development Commands:"));
80
- console.log(" " + kolor.cyan("dev") + " Start development server with hot reloading");
81
- console.log(" Watches for file changes and rebuilds automatically");
82
- console.log(" " + kolor.cyan("build") + " Build your site for production");
83
- console.log(" Optimizes and outputs to dist/ directory");
84
- console.log(" " + kolor.cyan("serve") + " Serve the built site locally");
85
- console.log(" Serves from dist/ folder on http://localhost:4173");
86
- console.log(" " + kolor.cyan("preview") + " Build and serve in production mode");
87
- console.log(" Combines build + serve for testing production output\n");
88
-
89
- console.log(kolor.bold("Utility Commands:"));
90
- console.log(" " + kolor.cyan("list") + " List all content (pages, layouts, components, etc.)");
91
- console.log(" Overview of your project structure");
92
- console.log(" " + kolor.cyan("clean") + " Remove build output directory");
93
- console.log(" Deletes dist/ folder for a fresh build");
94
- console.log(" " + kolor.cyan("check") + " Validate config and check for issues");
95
- console.log(" Diagnoses project structure and dependencies");
96
- console.log(" " + kolor.cyan("upgrade") + " Update CampsiteJS to the latest version");
97
- console.log(" Checks and upgrades basecampjs and dependencies\n");
98
-
99
- console.log(kolor.bold("Make Commands:"));
100
- console.log(" " + kolor.cyan("make:page") + " " + kolor.dim("<name>") + " Create a new page in src/pages/");
101
- console.log(" " + kolor.cyan("make:post") + " " + kolor.dim("<name>") + " Create a new blog post in src/pages/blog/");
102
- console.log(" " + kolor.cyan("make:layout") + " " + kolor.dim("<name>") + " Create a new layout in src/layouts/");
103
- console.log(" " + kolor.cyan("make:component") + " " + kolor.dim("<name>") + " Create a new component in src/components/");
104
- console.log(" " + kolor.cyan("make:partial") + " " + kolor.dim("<name>") + " Create a new partial in src/partials/");
105
- console.log(" " + kolor.cyan("make:collection") + " " + kolor.dim("<name>") + " Create a new JSON collection in src/collections/\n");
106
-
107
- console.log(kolor.bold("Options:"));
108
- console.log(" -h, --help Show this help message");
109
- console.log(" -v, --version Show version number\n");
110
-
111
- console.log(kolor.bold("Examples:"));
112
- console.log(" " + kolor.dim("# Initialize a new project"));
113
- console.log(" camper init\n");
114
- console.log(" " + kolor.dim("# Start development"));
115
- console.log(" camper dev\n");
116
- console.log(" " + kolor.dim("# Create new content"));
117
- console.log(" camper make:page about");
118
- console.log(" camper make:post \"My First Post\"");
119
- console.log(" camper make:collection products\n");
120
- console.log(" " + kolor.dim("# Build and preview"));
121
- console.log(" camper preview\n");
122
- console.log(kolor.dim("For more information, visit: https://campsitejs.dev"));
123
- console.log();
124
- }
125
-
126
- async function ensureDir(dir) {
127
- await mkdir(dir, { recursive: true });
128
- }
129
-
130
- async function loadData(dataDirs) {
131
- const collections = {};
132
- // Support both string and array input
133
- const dirs = Array.isArray(dataDirs) ? dataDirs : [dataDirs];
134
-
135
- for (const dataDir of dirs) {
136
- if (!existsSync(dataDir)) continue;
137
- const files = await walkFiles(dataDir);
138
- for (const file of files) {
139
- if (extname(file).toLowerCase() !== ".json") continue;
140
- const name = basename(file, ".json");
141
- try {
142
- const raw = await readFile(file, "utf8");
143
- collections[name] = JSON.parse(raw);
144
- } catch (err) {
145
- console.error(kolor.red(`Failed to load data ${relative(dataDir, file)}: ${err.message}`));
146
- }
147
- }
148
- }
149
- return collections;
150
- }
151
-
152
- async function cleanDir(dir) {
153
- await rm(dir, { recursive: true, force: true });
154
- await mkdir(dir, { recursive: true });
155
- }
156
-
157
- function shouldExcludeFile(filePath, excludePatterns) {
158
- if (!excludePatterns || excludePatterns.length === 0) return false;
159
-
160
- const fileName = basename(filePath).toLowerCase();
161
- const ext = extname(filePath).toLowerCase();
162
-
163
- return excludePatterns.some(pattern => {
164
- const normalized = pattern.toLowerCase();
165
- // Support extension patterns like '.pdf' or 'pdf'
166
- if (normalized.startsWith('.')) {
167
- return ext === normalized;
168
- }
169
- if (normalized.startsWith('*.')) {
170
- return ext === normalized.slice(1);
171
- }
172
- // Support exact filename matches
173
- if (fileName === normalized) {
174
- return true;
175
- }
176
- // Support glob-like patterns with wildcards
177
- if (normalized.includes('*')) {
178
- const regex = new RegExp('^' + normalized.replace(/\*/g, '.*').replace(/\?/g, '.') + '$');
179
- return regex.test(fileName);
180
- }
181
- return false;
182
- });
183
- }
184
-
185
- async function copyPublic(publicDir, outDir, excludePatterns = []) {
186
- if (!existsSync(publicDir)) return;
187
-
188
- const files = await walkFiles(publicDir);
189
- for (const file of files) {
190
- const rel = relative(publicDir, file);
191
-
192
- // Skip excluded files
193
- if (shouldExcludeFile(file, excludePatterns)) {
194
- console.log(kolor.dim(`Skipping excluded file: ${rel}`));
195
- continue;
196
- }
197
-
198
- const destPath = join(outDir, rel);
199
- await ensureDir(dirname(destPath));
200
- await cp(file, destPath);
201
- }
202
- }
203
-
204
- function shouldProcessImage(filePath, config) {
205
- if (!config.compressPhotos) return false;
206
- const ext = extname(filePath).toLowerCase();
207
- const inputFormats = config.compressionSettings?.inputFormats || [".jpg", ".jpeg", ".png"];
208
- return inputFormats.includes(ext);
209
- }
210
-
211
- async function processImage(inputPath, outDir, settings) {
212
- const ext = extname(inputPath);
213
- const baseName = basename(inputPath, ext);
214
- const dir = dirname(inputPath);
215
- const relDir = relative(outDir, dir);
216
-
217
- const results = [];
218
- const quality = settings.quality || 80;
219
- const formats = settings.formats || [".webp"];
220
-
221
- for (const format of formats) {
222
- try {
223
- const outputName = `${baseName}${format}`;
224
- const outputPath = join(dir, outputName);
225
-
226
- const sharpInstance = sharp(inputPath);
227
-
228
- if (format === ".webp") {
229
- await sharpInstance.webp({ quality }).toFile(outputPath);
230
- } else if (format === ".avif") {
231
- await sharpInstance.avif({ quality }).toFile(outputPath);
232
- } else if (format === ".jpg" || format === ".jpeg") {
233
- await sharpInstance.jpeg({ quality }).toFile(outputPath);
234
- } else if (format === ".png") {
235
- await sharpInstance.png({ quality }).toFile(outputPath);
236
- } else {
237
- continue;
238
- }
239
-
240
- const stats = await stat(outputPath);
241
- results.push({
242
- path: outputPath,
243
- format,
244
- size: stats.size
245
- });
246
- } catch (err) {
247
- console.error(kolor.red(`Failed to convert ${basename(inputPath)} to ${format}: ${err.message}`));
248
- }
249
- }
250
-
251
- return results;
252
- }
253
-
254
- async function processImages(outDir, config) {
255
- if (!config.compressPhotos) return;
256
-
257
- const settings = {
258
- quality: config.compressionSettings?.quality || 80,
259
- formats: config.compressionSettings?.formats || [".webp"],
260
- preserveOriginal: config.compressionSettings?.preserveOriginal !== false
261
- };
262
-
263
- console.log(kolor.cyan("🖼️ Processing images..."));
264
-
265
- const files = await walkFiles(outDir);
266
- const imageFiles = files.filter((file) => shouldProcessImage(file, config));
267
-
268
- if (imageFiles.length === 0) {
269
- console.log(kolor.dim("No images found to process"));
270
- return;
271
- }
272
-
273
- let totalGenerated = 0;
274
- let totalOriginalSize = 0;
275
- let totalConvertedSize = 0;
276
-
277
- await Promise.all(imageFiles.map(async (file) => {
278
- const originalStats = await stat(file);
279
- totalOriginalSize += originalStats.size;
280
-
281
- const results = await processImage(file, outDir, settings);
282
-
283
- if (results.length > 0) {
284
- const formats = results.map(r => r.format.slice(1)).join(", ");
285
- const rel = relative(outDir, file);
286
- console.log(kolor.dim(` ${rel} → ${formats}`));
287
-
288
- results.forEach(r => {
289
- totalConvertedSize += r.size;
290
- totalGenerated++;
291
- });
292
- }
293
-
294
- // Remove original if preserveOriginal is false
295
- if (!settings.preserveOriginal && results.length > 0) {
296
- await rm(file, { force: true });
297
- }
298
- }));
299
-
300
- const savedBytes = totalOriginalSize - totalConvertedSize;
301
- const savedPercent = totalOriginalSize > 0 ? ((savedBytes / totalOriginalSize) * 100).toFixed(1) : 0;
302
-
303
- console.log(kolor.green(`✓ Generated ${totalGenerated} image(s)`));
304
- if (!settings.preserveOriginal) {
305
- console.log(kolor.green(` Saved ${formatBytes(savedBytes)} (${savedPercent}% reduction)`));
306
- }
307
- }
308
-
309
- function formatBytes(bytes) {
310
- if (bytes === 0) return "0 B";
311
- const k = 1024;
312
- const sizes = ["B", "KB", "MB", "GB"];
313
- const i = Math.floor(Math.log(bytes) / Math.log(k));
314
- return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
315
- }
316
-
317
- async function minifyCSSFiles(outDir) {
318
- const files = await walkFiles(outDir);
319
- const cssFiles = files.filter((file) => extname(file).toLowerCase() === ".css");
320
-
321
- await Promise.all(cssFiles.map(async (file) => {
322
- try {
323
- const css = await readFile(file, "utf8");
324
- const { css: minified } = minifyCss(css);
325
- await writeFile(file, minified, "utf8");
326
- } catch (err) {
327
- console.error(kolor.red(`Failed to minify CSS ${relative(outDir, file)}: ${err.message}`));
328
- }
329
- }));
330
- }
331
-
332
- async function minifyHTMLFiles(outDir, config) {
333
- const files = await walkFiles(outDir);
334
- const htmlFiles = files.filter((file) => extname(file).toLowerCase() === ".html");
335
-
336
- await Promise.all(htmlFiles.map(async (file) => {
337
- try {
338
- const html = await readFile(file, "utf8");
339
- const minified = await minifyHtml(html, {
340
- collapseWhitespace: true,
341
- removeComments: true,
342
- minifyCSS: !!config.minifyCSS,
343
- minifyJS: true,
344
- keepClosingSlash: true,
345
- removeRedundantAttributes: true,
346
- removeScriptTypeAttributes: true,
347
- removeStyleLinkTypeAttributes: true
348
- });
349
- await writeFile(file, minified, "utf8");
350
- } catch (err) {
351
- console.error(kolor.red(`Failed to minify HTML ${relative(outDir, file)}: ${err.message}`));
352
- }
353
- }));
354
- }
355
-
356
- async function walkFiles(dir) {
357
- const results = [];
358
- if (!existsSync(dir)) return results;
359
- const entries = await readdir(dir, { withFileTypes: true });
360
- for (const entry of entries) {
361
- if (entry.name.startsWith(".")) continue;
362
- const full = join(dir, entry.name);
363
- if (entry.isDirectory()) {
364
- results.push(...await walkFiles(full));
365
- } else {
366
- results.push(full);
367
- }
368
- }
369
- return results;
370
- }
371
-
372
- function createNunjucksEnv(layoutsDir, pagesDir, srcDir, partialsDir) {
373
- // Allow templates to resolve from layouts, partials, pages, or the src root
374
- const searchPaths = [layoutsDir, partialsDir, pagesDir, srcDir].filter(Boolean);
375
- return new nunjucks.Environment(
376
- new nunjucks.FileSystemLoader(searchPaths, { noCache: true }),
377
- { autoescape: false }
378
- );
379
- }
380
-
381
- function createLiquidEnv(layoutsDir, pagesDir, srcDir, partialsDir) {
382
- // Liquid loader will search these roots for partials/layouts
383
- const root = [layoutsDir, partialsDir, pagesDir, srcDir].filter(Boolean);
384
- return new Liquid({
385
- root,
386
- extname: ".liquid",
387
- cache: false
388
- });
389
- }
390
-
391
- async function loadMustachePartials(partialsDir) {
392
- const partials = {};
393
- if (!existsSync(partialsDir)) return partials;
394
-
395
- try {
396
- const files = await walkFiles(partialsDir);
397
- await Promise.all(files.map(async (file) => {
398
- if (extname(file).toLowerCase() === ".mustache") {
399
- const content = await readFile(file, "utf8");
400
- const partialName = basename(file, ".mustache");
401
- partials[partialName] = content;
402
- }
403
- }));
404
- } catch (err) {
405
- console.error(kolor.yellow(`Warning: Failed to load Mustache partials: ${err.message}`));
406
- }
407
-
408
- return partials;
409
- }
410
-
411
- function toUrlPath(outRel) {
412
- const normalized = outRel.replace(/\\/g, "/");
413
- let path = `/${normalized}`;
414
- // Remove trailing index.html for directory-style URLs
415
- if (path.endsWith("index.html")) {
416
- path = path.slice(0, -"index.html".length);
417
- }
418
- // Strip trailing slash except for root
419
- if (path !== "/" && path.endsWith("/")) {
420
- path = path.slice(0, -1);
421
- }
422
- return path || "/";
423
- }
424
-
425
- function pageContext(frontmatter, html, config, relPath, data, path = "/") {
426
- return {
427
- site: { name: config.siteName, config },
428
- page: { ...frontmatter, content: html, source: relPath, path },
429
- collections: data,
430
- ...data
431
- };
432
- }
433
-
434
- function shouldRenderMarkdown(frontmatter, config, defaultValue) {
435
- if (typeof frontmatter?.markdown === "boolean") return frontmatter.markdown;
436
- return defaultValue;
437
- }
438
-
439
- async function renderWithLayout(layoutName, html, ctx, env, liquidEnv, layoutsDir, partials = {}) {
440
- if (!layoutName) return html;
441
- const ext = extname(layoutName).toLowerCase();
442
- const layoutCtx = {
443
- ...ctx,
444
- frontmatter: ctx.page || {},
445
- content: html,
446
- title: ctx.page?.title ?? ctx.site?.name
447
- };
448
-
449
- if (ext === ".njk") {
450
- return env.render(layoutName, layoutCtx);
451
- }
452
-
453
- if (ext === ".liquid" || layoutName.toLowerCase().endsWith(".liquid.html")) {
454
- return liquidEnv.renderFile(layoutName, layoutCtx);
455
- }
456
-
457
- if (ext === ".mustache") {
458
- const layoutPath = join(layoutsDir, layoutName);
459
- if (existsSync(layoutPath)) {
460
- const layoutTemplate = await readFile(layoutPath, "utf8");
461
- return Mustache.render(layoutTemplate, layoutCtx, partials);
462
- }
463
- }
464
-
465
- // Unknown layout type, return unwrapped content
466
- return html;
467
- }
468
-
469
- async function renderPage(filePath, { pagesDir, layoutsDir, outDir, env, liquidEnv, config, data, partialsDir }) {
470
- const rel = relative(pagesDir, filePath);
471
- const ext = extname(filePath).toLowerCase();
472
- const outRel = rel.replace(/\.liquid(\.html)?$/i, ".html").replace(ext, ".html");
473
- const outPath = join(outDir, outRel);
474
- const path = toUrlPath(outRel);
475
- await ensureDir(dirname(outPath));
476
-
477
- // Load Mustache partials if needed
478
- const partials = (ext === ".mustache" || (await readFile(filePath, "utf8")).match(/layout:.*\.mustache/))
479
- ? await loadMustachePartials(partialsDir)
480
- : {};
481
-
482
- if (ext === ".md") {
483
- const raw = await readFile(filePath, "utf8");
484
- const parsed = matter(raw);
485
- const html = md.render(parsed.content);
486
- const ctx = pageContext(parsed.data, html, config, rel, data, path);
487
- const rendered = await renderWithLayout(parsed.data.layout, html, ctx, env, liquidEnv, layoutsDir, partials);
488
- await writeFile(outPath, rendered, "utf8");
489
- return;
490
- }
491
-
492
- if (ext === ".njk") {
493
- const raw = await readFile(filePath, "utf8");
494
- const parsed = matter(raw);
495
- const ctx = pageContext(parsed.data, parsed.content, config, rel, data, path);
496
- const templateName = rel.replace(/\\/g, "/");
497
- let pageHtml = env.renderString(parsed.content, ctx, { path: templateName });
498
- if (shouldRenderMarkdown(parsed.data, config, false)) {
499
- pageHtml = md.render(pageHtml);
500
- }
501
- const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir, partials);
502
- await writeFile(outPath, rendered, "utf8");
503
- return;
504
- }
505
-
506
- if (ext === ".liquid" || filePath.toLowerCase().endsWith(".liquid.html")) {
507
- const raw = await readFile(filePath, "utf8");
508
- const parsed = matter(raw);
509
- const ctx = pageContext(parsed.data, parsed.content, config, rel, data, path);
510
- let pageHtml = await liquidEnv.parseAndRender(parsed.content, ctx);
511
- if (shouldRenderMarkdown(parsed.data, config, false)) {
512
- pageHtml = md.render(pageHtml);
513
- }
514
- const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir, partials);
515
- await writeFile(outPath, rendered, "utf8");
516
- return;
517
- }
518
-
519
- if (ext === ".mustache") {
520
- const raw = await readFile(filePath, "utf8");
521
- const parsed = matter(raw);
522
- const ctx = pageContext(parsed.data, parsed.content, config, rel, data, path);
523
- let pageHtml = Mustache.render(parsed.content, ctx, partials);
524
- if (shouldRenderMarkdown(parsed.data, config, false)) {
525
- pageHtml = md.render(pageHtml);
526
- }
527
- const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir, partials);
528
- await writeFile(outPath, rendered, "utf8");
529
- return;
530
- }
531
-
532
- if (ext === ".html") {
533
- const raw = await readFile(filePath, "utf8");
534
- const parsed = matter(raw);
535
- const ctx = pageContext(parsed.data, parsed.content, config, rel, data, path);
536
- let pageHtml = parsed.content;
537
- if (shouldRenderMarkdown(parsed.data, config, false)) {
538
- pageHtml = md.render(pageHtml);
539
- }
540
- const rendered = await renderWithLayout(parsed.data.layout, pageHtml, ctx, env, liquidEnv, layoutsDir, partials);
541
- await writeFile(outPath, rendered, "utf8");
542
- return;
543
- }
544
-
545
- await cp(filePath, outPath);
546
- }
547
-
548
- async function cacheBustAssets(outDir) {
549
- const assetMap = {}; // original path -> hashed path
550
- const files = await walkFiles(outDir);
551
- const assetFiles = files.filter((file) => {
552
- const ext = extname(file).toLowerCase();
553
- return ext === ".css" || ext === ".js";
554
- });
555
-
556
- // Hash and rename each asset
557
- for (const file of assetFiles) {
558
- try {
559
- const content = await readFile(file);
560
- const hash = createHash("sha256").update(content).digest("hex").slice(0, 10);
561
- const ext = extname(file);
562
- const base = basename(file, ext);
563
- const dir = dirname(file);
564
- const hashedName = `${base}-${hash}${ext}`;
565
- const hashedPath = join(dir, hashedName);
566
-
567
- await rename(file, hashedPath);
568
-
569
- // Store mapping of original relative path to hashed relative path
570
- const originalRel = relative(outDir, file).replace(/\\/g, "/");
571
- const hashedRel = relative(outDir, hashedPath).replace(/\\/g, "/");
572
- assetMap[originalRel] = hashedRel;
573
-
574
- // Log the cache-busted file
575
- console.log(kolor.dim(` ${originalRel}`) + kolor.cyan(` → `) + kolor.green(hashedRel));
576
- } catch (err) {
577
- console.error(kolor.red(`Failed to cache-bust ${relative(outDir, file)}: ${err.message}`));
578
- }
579
- }
580
-
581
- // Update HTML files to reference hashed assets
582
- const htmlFiles = files.filter((file) => extname(file).toLowerCase() === ".html");
583
-
584
- for (const htmlFile of htmlFiles) {
585
- try {
586
- let html = await readFile(htmlFile, "utf8");
587
- let updated = false;
588
-
589
- // Update <link> tags (CSS)
590
- for (const [original, hashed] of Object.entries(assetMap)) {
591
- if (original.endsWith(".css")) {
592
- // Match href="/path" or href="path" but ensure we stay within the tag
593
- const escaped = original.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
594
- const pattern = new RegExp(`(<link[^>]*?\\shref=["'])/?${escaped}(["'])`, "gi");
595
-
596
- const newHtml = html.replace(pattern, `$1/${hashed}$2`);
597
- if (newHtml !== html) {
598
- html = newHtml;
599
- updated = true;
600
- }
601
- }
602
- }
603
-
604
- // Update <script> tags (JS)
605
- for (const [original, hashed] of Object.entries(assetMap)) {
606
- if (original.endsWith(".js")) {
607
- // Match src="/path" or src="path" but ensure we stay within the tag
608
- const escaped = original.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
609
- const pattern = new RegExp(`(<script[^>]*?\\ssrc=["'])/?${escaped}(["'])`, "gi");
610
-
611
- const newHtml = html.replace(pattern, `$1/${hashed}$2`);
612
- if (newHtml !== html) {
613
- html = newHtml;
614
- updated = true;
615
- }
616
- }
617
- }
618
-
619
- if (updated) {
620
- await writeFile(htmlFile, html, "utf8");
621
- }
622
- } catch (err) {
623
- console.error(kolor.red(`Failed to update asset references in ${relative(outDir, htmlFile)}: ${err.message}`));
624
- }
625
- }
626
-
627
- return assetMap;
628
- }
629
-
630
- async function build(cwdArg = cwd) {
631
- const config = await loadConfig(cwdArg);
632
- const srcDir = resolve(cwdArg, config.srcDir || "src");
633
- const pagesDir = join(srcDir, "pages");
634
- const layoutsDir = join(srcDir, "layouts");
635
- const partialsDir = join(srcDir, "partials");
636
- const dataDir = join(srcDir, "data");
637
- const collectionsDir = join(srcDir, "collections");
638
- const publicDir = resolve(cwdArg, "public");
639
- const outDir = resolve(cwdArg, config.outDir || "dist");
640
- const env = createNunjucksEnv(layoutsDir, pagesDir, srcDir, partialsDir);
641
- // Allow user config to extend the Nunjucks environment (e.g., custom filters)
642
- if (config?.hooks?.nunjucksEnv && typeof config.hooks.nunjucksEnv === "function") {
643
- try {
644
- config.hooks.nunjucksEnv(env);
645
- } catch (err) {
646
- console.error(kolor.red(`Failed to apply nunjucksEnv hook: ${err.message}`));
647
- }
648
- }
649
- const liquidEnv = createLiquidEnv(layoutsDir, pagesDir, srcDir, partialsDir);
650
- // Allow user config to extend the Liquid environment (e.g., custom filters)
651
- if (config?.hooks?.liquidEnv && typeof config.hooks.liquidEnv === "function") {
652
- try {
653
- config.hooks.liquidEnv(liquidEnv);
654
- } catch (err) {
655
- console.error(kolor.red(`Failed to apply liquidEnv hook: ${err.message}`));
656
- }
657
- }
658
- const data = await loadData([dataDir, collectionsDir]);
659
-
660
- await cleanDir(outDir);
661
- await copyPublic(publicDir, outDir, config.excludeFiles);
662
-
663
- if (config.compressPhotos) {
664
- await processImages(outDir, config);
665
- }
666
-
667
- const files = await walkFiles(pagesDir);
668
- if (files.length === 0) {
669
- console.log(kolor.yellow("No pages found in src/pages."));
670
- return;
671
- }
672
-
673
- await Promise.all(files.map((file) => renderPage(file, { pagesDir, layoutsDir, outDir, env, liquidEnv, config, data, partialsDir })));
674
-
675
- if (config.minifyCSS) {
676
- await minifyCSSFiles(outDir);
677
- console.log(kolor.green("CSS minified"));
678
- }
679
-
680
- if (config.minifyHTML) {
681
- await minifyHTMLFiles(outDir, config);
682
- console.log(kolor.green("HTML minified"));
683
- }
684
-
685
- if (config.cacheBustAssets) {
686
- console.log(kolor.cyan("Cache-busting assets..."));
687
- const assetMap = await cacheBustAssets(outDir);
688
- const assetCount = Object.keys(assetMap).length;
689
- if (assetCount > 0) {
690
- console.log(kolor.green(`✓ Cache-busted ${assetCount} asset(s)`));
691
- } else {
692
- console.log(kolor.yellow("No assets found to cache-bust"));
693
- }
694
- }
695
-
696
- console.log(kolor.green(`Built ${files.length} page(s) → ${relative(cwdArg, outDir)}`));
697
- }
698
-
699
- function serve(outDir, port = 4173) {
700
- const mime = {
701
- ".html": "text/html",
702
- ".css": "text/css",
703
- ".js": "application/javascript",
704
- ".json": "application/json",
705
- ".svg": "image/svg+xml",
706
- ".png": "image/png",
707
- ".jpg": "image/jpeg",
708
- ".jpeg": "image/jpeg",
709
- ".gif": "image/gif",
710
- ".webp": "image/webp",
711
- ".ico": "image/x-icon"
712
- };
713
-
714
- const server = createServer(async (req, res) => {
715
- const urlPath = decodeURI((req.url || "/").split("?")[0]);
716
- const safePath = urlPath.replace(/\.\.+/g, "");
717
- const requestPath = safePath.replace(/^\/+/, "") || "index.html";
718
- let filePath = join(outDir, requestPath);
719
- const notFoundPath = join(outDir, "404.html");
720
- const indexPath = join(outDir, "index.html");
721
- let isNotFoundResponse = false;
722
- let stats;
723
-
724
- try {
725
- stats = await stat(filePath);
726
- if (stats.isDirectory()) {
727
- filePath = join(filePath, "index.html");
728
- stats = await stat(filePath);
729
- }
730
- } catch {
731
- if (existsSync(notFoundPath)) {
732
- filePath = notFoundPath;
733
- isNotFoundResponse = true;
734
- } else {
735
- filePath = indexPath;
736
- }
737
- }
738
-
739
- try {
740
- const data = await readFile(filePath);
741
- const type = mime[extname(filePath).toLowerCase()] || "text/plain";
742
- res.writeHead(isNotFoundResponse ? 404 : 200, { "Content-Type": type });
743
- res.end(data);
744
- } catch {
745
- res.writeHead(404, { "Content-Type": "text/plain" });
746
- res.end("Not found");
747
- }
748
- });
749
-
750
- server.listen(port, () => {
751
- console.log(kolor.green(`Serving dist at http://localhost:${port}`));
752
- });
753
-
754
- return server;
755
- }
756
-
757
- async function dev(cwdArg = cwd) {
758
- let building = false;
759
- let pending = false;
760
-
761
- const runBuild = async () => {
762
- if (building) {
763
- pending = true;
764
- return;
765
- }
766
- building = true;
767
- try {
768
- await build(cwdArg);
769
- } catch (err) {
770
- console.error(kolor.red(`Build failed: ${err.message}`));
771
- } finally {
772
- building = false;
773
- if (pending) {
774
- pending = false;
775
- runBuild();
776
- }
777
- }
778
- };
779
-
780
- await runBuild();
781
-
782
- const config = await loadConfig(cwdArg);
783
- const srcDir = resolve(cwdArg, config.srcDir || "src");
784
- const dataDir = join(srcDir, "data");
785
- const collectionsDir = join(srcDir, "collections");
786
- const publicDir = resolve(cwdArg, "public");
787
- const outDir = resolve(cwdArg, config.outDir || "dist");
788
- const watcher = chokidar.watch([srcDir, publicDir, dataDir, collectionsDir], { ignoreInitial: true });
789
-
790
- watcher.on("all", (event, path) => {
791
- console.log(kolor.cyan(`↻ ${event}: ${relative(cwdArg, path)}`));
792
- runBuild();
793
- });
794
-
795
- serve(outDir);
796
- }
797
-
798
- function slugify(text) {
799
- return text
800
- .toLowerCase()
801
- .trim()
802
- .replace(/[^\w\s-]/g, "")
803
- .replace(/[\s_-]+/g, "-")
804
- .replace(/^-+|-+$/g, "");
805
- }
806
-
807
- function formatDate(date) {
808
- return date.toISOString().split("T")[0];
809
- }
810
-
811
- async function makeContent(type) {
812
- // Get all arguments after the command and join them
813
- const args = argv.slice(3);
814
-
815
- if (args.length === 0) {
816
- console.log(kolor.red("❌ Missing name argument"));
817
- console.log(kolor.dim(`Usage: camper make:${type} <name> [name2, name3, ...]`));
818
- console.log(kolor.dim("\nExamples:"));
819
- console.log(kolor.dim(" camper make:page about"));
820
- console.log(kolor.dim(" camper make:page home, about, contact"));
821
- console.log(kolor.dim(" camper make:collection products, categories\n"));
822
- exit(1);
823
- }
824
-
825
- // Join all args and split by comma to support both formats:
826
- // camper make:page home about contact
827
- // camper make:page home, about, contact
828
- const namesString = args.join(" ");
829
- const names = namesString.split(",").map(n => n.trim()).filter(n => n.length > 0);
830
-
831
- if (names.length === 0) {
832
- console.log(kolor.red("❌ No valid names provided\n"));
833
- exit(1);
834
- }
835
-
836
- console.log(kolor.cyan(`\n🏕️ Creating ${names.length} ${type}(s)...\n`));
837
-
838
- const config = await loadConfig(cwd);
839
- const srcDir = resolve(cwd, config.srcDir || "src");
840
-
841
- // Determine file extension based on template engine
842
- const engineExtMap = {
843
- nunjucks: ".njk",
844
- liquid: ".liquid",
845
- mustache: ".mustache"
846
- };
847
- const defaultExt = engineExtMap[config.templateEngine] || ".njk";
848
-
849
- let successCount = 0;
850
- let skipCount = 0;
851
-
852
- for (const name of names) {
853
- const result = await createSingleContent(type, name, srcDir, config, defaultExt);
854
- if (result.success) successCount++;
855
- if (result.skipped) skipCount++;
856
- }
857
-
858
- console.log();
859
- if (successCount > 0) {
860
- console.log(kolor.green(`✅ Created ${successCount} ${type}(s)`));
861
- }
862
- if (skipCount > 0) {
863
- console.log(kolor.yellow(`⚠️ Skipped ${skipCount} existing file(s)`));
864
- }
865
- console.log(kolor.dim("\n🌲 Happy camping!\n"));
866
- }
867
-
868
- async function createSingleContent(type, name, srcDir, config, defaultExt) {
869
- // Check if user provided an extension
870
- const hasExtension = name.includes(".");
871
- const providedExt = hasExtension ? extname(name) : null;
872
- const nameWithoutExt = hasExtension ? basename(name, providedExt) : name;
873
-
874
- const slug = slugify(nameWithoutExt);
875
- const today = formatDate(new Date());
876
- const title = nameWithoutExt.charAt(0).toUpperCase() + nameWithoutExt.slice(1);
877
-
878
- let targetPath;
879
- let content;
880
- let fileExt;
881
-
882
- switch (type.toLowerCase()) {
883
- case "page": {
884
- // Priority: provided extension > template engine
885
- if (providedExt) {
886
- fileExt = providedExt;
887
- } else {
888
- fileExt = defaultExt;
889
- }
890
-
891
- targetPath = join(srcDir, "pages", `${slug}${fileExt}`);
892
-
893
- // Determine if we should use markdown content based on extension
894
- const useMarkdown = fileExt === ".md";
895
-
896
- if (useMarkdown) {
897
- content = `---
898
- layout: base${defaultExt}
899
- title: ${title}
900
- ---
901
-
902
- # ${title}
903
-
904
- Your new page content goes here.
905
- `;
906
- } else {
907
- content = `---
908
- layout: base${defaultExt}
909
- title: ${title}
910
- ---
911
-
912
- <h1>${title}</h1>
913
- <p>Your new page content goes here.</p>
914
- `;
915
- }
916
- break;
917
- }
918
-
919
- case "post": {
920
- const postsDir = join(srcDir, "pages", "blog");
921
- await ensureDir(postsDir);
922
-
923
- if (providedExt) {
924
- fileExt = providedExt;
925
- } else {
926
- fileExt = defaultExt;
927
- }
928
-
929
- targetPath = join(postsDir, `${slug}${fileExt}`);
930
-
931
- const useMarkdown = fileExt === ".md";
932
-
933
- if (useMarkdown) {
934
- content = `---
935
- layout: base${defaultExt}
936
- title: ${title}
937
- date: ${today}
938
- author: Your Name
939
- ---
940
-
941
- # ${title}
942
-
943
- Your blog post content goes here.
944
- `;
945
- } else {
946
- content = `---
947
- layout: base${defaultExt}
948
- title: ${title}
949
- date: ${today}
950
- author: Your Name
951
- ---
952
-
953
- <h1>${title}</h1>
954
- <p>Your blog post content goes here.</p>
955
- `;
956
- }
957
- break;
958
- }
959
-
960
- case "layout": {
961
- const layoutsDir = join(srcDir, "layouts");
962
- await ensureDir(layoutsDir);
963
- targetPath = join(layoutsDir, `${slug}.njk`);
964
- fileExt = ".njk";
965
- content = `<!DOCTYPE html>
966
- <html lang="en">
967
- <head>
968
- <meta charset="UTF-8">
969
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
970
- <title>{{ title or site.name }}</title>
971
- <link rel="stylesheet" href="/style.css">
972
- </head>
973
- <body>
974
- <main>
975
- {% block content %}
976
- {{ content | safe }}
977
- {% endblock %}
978
- </main>
979
- </body>
980
- </html>
981
- `;
982
- break;
983
- }
984
-
985
- case "component": {
986
- const componentsDir = join(srcDir, "components");
987
- await ensureDir(componentsDir);
988
- targetPath = join(componentsDir, `${slug}.njk`);
989
- fileExt = ".njk";
990
- content = `{# ${title} Component #}
991
- <div class="${slug}">
992
- {{ content | safe }}
993
- </div>
994
- `;
995
- break;
996
- }
997
-
998
- case "partial": {
999
- const partialsDir = join(srcDir, "partials");
1000
- await ensureDir(partialsDir);
1001
- targetPath = join(partialsDir, `${slug}.njk`);
1002
- fileExt = ".njk";
1003
- content = `{# ${title} Partial #}
1004
- <div class="${slug}">
1005
- {# Your partial content here #}
1006
- </div>
1007
- `;
1008
- break;
1009
- }
1010
-
1011
- case "collection": {
1012
- const collectionsDir = join(srcDir, "collections");
1013
- await ensureDir(collectionsDir);
1014
- targetPath = join(collectionsDir, `${slug}.json`);
1015
- fileExt = ".json";
1016
- content = `[
1017
- {
1018
- "id": 1,
1019
- "title": "Sample ${title} Item",
1020
- "description": "Add your collection items here"
1021
- }
1022
- ]
1023
- `;
1024
- break;
1025
- }
1026
-
1027
- default:
1028
- console.log(kolor.red(`❌ Unknown content type: ${type}`));
1029
- console.log(kolor.dim("\nSupported types: page, post, layout, component, partial, collection\n"));
1030
- return { success: false, skipped: false };
1031
- }
1032
-
1033
- if (existsSync(targetPath)) {
1034
- console.log(kolor.dim(` ⚠️ Skipped ${relative(cwd, targetPath)} (already exists)`));
1035
- return { success: false, skipped: true };
1036
- }
1037
-
1038
- await ensureDir(dirname(targetPath));
1039
- await writeFile(targetPath, content, "utf8");
1040
-
1041
- console.log(kolor.dim(` ✅ ${relative(cwd, targetPath)}`));
1042
- return { success: true, skipped: false };
1043
- }
1044
-
1045
- async function init() {
1046
- const targetDir = cwd;
1047
- console.log(kolor.cyan(kolor.bold("🏕️ Initializing Campsite in current directory...")));
1048
-
1049
- // Check if already initialized
1050
- if (existsSync(join(targetDir, "campsite.config.js"))) {
1051
- console.log(kolor.yellow("⚠️ This directory already has a campsite.config.js file."));
1052
- console.log(kolor.dim("Run 'camper dev' to start developing.\n"));
1053
- return;
1054
- }
1055
-
1056
- // Create basic structure
1057
- const dirs = [
1058
- join(targetDir, "src", "pages"),
1059
- join(targetDir, "src", "layouts"),
1060
- join(targetDir, "public")
1061
- ];
1062
-
1063
- for (const dir of dirs) {
1064
- await ensureDir(dir);
1065
- }
1066
-
1067
- // Create basic config file
1068
- const configContent = `export default {
1069
- siteName: "My Campsite",
1070
- srcDir: "src",
1071
- outDir: "dist",
1072
- templateEngine: "nunjucks",
1073
- markdown: true,
1074
- integrations: {
1075
- nunjucks: true,
1076
- liquid: false,
1077
- mustache: false,
1078
- vue: false,
1079
- alpine: false
1080
- }
1081
- };
1082
- `;
1083
- await writeFile(join(targetDir, "campsite.config.js"), configContent, "utf8");
1084
-
1085
- // Create basic layout
1086
- const layoutContent = `<!DOCTYPE html>
1087
- <html lang="en">
1088
- <head>
1089
- <meta charset="UTF-8">
1090
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
1091
- <title>{{ title or site.name }}</title>
1092
- <link rel="stylesheet" href="/style.css">
1093
- </head>
1094
- <body>
1095
- {% block content %}
1096
- {{ content | safe }}
1097
- {% endblock %}
1098
- </body>
1099
- </html>
1100
- `;
1101
- await writeFile(join(targetDir, "src", "layouts", "base.njk"), layoutContent, "utf8");
1102
-
1103
- // Create sample page
1104
- const pageContent = `---
1105
- layout: base.njk
1106
- title: Welcome to Campsite
1107
- ---
1108
-
1109
- # Welcome to Campsite! 🏕️
1110
-
1111
- Your cozy static site is ready to build.
1112
-
1113
- ## Get Started
1114
-
1115
- - Run \`camper dev\` to start developing
1116
- - Edit pages in \`src/pages/\`
1117
- - Customize layouts in \`src/layouts/\`
1118
-
1119
- Happy camping! 🌲🦊
1120
- `;
1121
- await writeFile(join(targetDir, "src", "pages", "index.md"), pageContent, "utf8");
1122
-
1123
- // Create basic CSS
1124
- const cssContent = `* {
1125
- margin: 0;
1126
- padding: 0;
1127
- box-sizing: border-box;
1128
- }
1129
-
1130
- body {
1131
- font-family: system-ui, -apple-system, sans-serif;
1132
- line-height: 1.6;
1133
- padding: 2rem;
1134
- max-width: 800px;
1135
- margin: 0 auto;
1136
- }
1137
-
1138
- h1 { color: #2d5016; margin-bottom: 1rem; }
1139
- h2 { color: #4a7c2c; margin-top: 1.5rem; }
1140
- `;
1141
- await writeFile(join(targetDir, "public", "style.css"), cssContent, "utf8");
1142
-
1143
- // Create .gitignore
1144
- const gitignoreContent = `node_modules/
1145
- dist/
1146
- .DS_Store
1147
- `;
1148
- await writeFile(join(targetDir, ".gitignore"), gitignoreContent, "utf8");
1149
-
1150
- // Create package.json
1151
- const packageJson = {
1152
- name: basename(targetDir),
1153
- version: "0.0.1",
1154
- type: "module",
1155
- scripts: {
1156
- dev: "camper dev",
1157
- build: "camper build",
1158
- serve: "camper serve",
1159
- preview: "camper preview"
1160
- },
1161
- dependencies: {
1162
- basecampjs: "^0.0.8"
1163
- }
1164
- };
1165
- await writeFile(join(targetDir, "package.json"), JSON.stringify(packageJson, null, 2), "utf8");
1166
-
1167
- console.log(kolor.green("✅ Campsite initialized successfully!\n"));
1168
- console.log(kolor.bold("Next steps:"));
1169
- console.log(kolor.dim(" 1. Install dependencies: npm install"));
1170
- console.log(kolor.dim(" 2. Start developing: camper dev\n"));
1171
- }
1172
-
1173
- async function clean() {
1174
- const config = await loadConfig(cwd);
1175
- const outDir = resolve(cwd, config.outDir || "dist");
1176
-
1177
- if (!existsSync(outDir)) {
1178
- console.log(kolor.dim(`Nothing to clean. ${outDir} does not exist.`));
1179
- return;
1180
- }
1181
-
1182
- console.log(kolor.cyan(`🧹 Cleaning ${relative(cwd, outDir)}...`));
1183
- await rm(outDir, { recursive: true, force: true });
1184
- console.log(kolor.green(`✅ Cleaned ${relative(cwd, outDir)}\n`));
1185
- }
1186
-
1187
- async function check() {
1188
- console.log(kolor.cyan(kolor.bold("🔍 Checking Campsite project...\n")));
1189
- let hasIssues = false;
1190
-
1191
- // Check if campsite.config.js exists
1192
- const configPath = join(cwd, "campsite.config.js");
1193
- if (!existsSync(configPath)) {
1194
- console.log(kolor.red("❌ campsite.config.js not found"));
1195
- console.log(kolor.dim(" Run 'camper init' to initialize a project\n"));
1196
- hasIssues = true;
1197
- } else {
1198
- console.log(kolor.green("✅ campsite.config.js found"));
1199
- }
1200
-
1201
- // Load and validate config
1202
- const config = await loadConfig(cwd);
1203
- const srcDir = resolve(cwd, config.srcDir || "src");
1204
- const pagesDir = join(srcDir, "pages");
1205
- const layoutsDir = join(srcDir, "layouts");
1206
- const publicDir = resolve(cwd, "public");
1207
-
1208
- // Check src directory
1209
- if (!existsSync(srcDir)) {
1210
- console.log(kolor.red(`❌ Source directory not found: ${relative(cwd, srcDir)}`));
1211
- hasIssues = true;
1212
- } else {
1213
- console.log(kolor.green(`✅ Source directory exists: ${relative(cwd, srcDir)}`));
1214
- }
1215
-
1216
- // Check pages directory
1217
- if (!existsSync(pagesDir)) {
1218
- console.log(kolor.yellow(`⚠️ Pages directory not found: ${relative(cwd, pagesDir)}`));
1219
- hasIssues = true;
1220
- } else {
1221
- const files = await walkFiles(pagesDir);
1222
- if (files.length === 0) {
1223
- console.log(kolor.yellow(`⚠️ No pages found in ${relative(cwd, pagesDir)}`));
1224
- hasIssues = true;
1225
- } else {
1226
- console.log(kolor.green(`✅ Found ${files.length} page(s) in ${relative(cwd, pagesDir)}`));
1227
- }
1228
- }
1229
-
1230
- // Check layouts directory
1231
- if (existsSync(layoutsDir)) {
1232
- const layouts = await readdir(layoutsDir).catch(() => []);
1233
- console.log(kolor.green(`✅ Found ${layouts.length} layout(s) in ${relative(cwd, layoutsDir)}`));
1234
- } else {
1235
- console.log(kolor.dim(`ℹ️ No layouts directory (${relative(cwd, layoutsDir)})`));
1236
- }
1237
-
1238
- // Check public directory
1239
- if (existsSync(publicDir)) {
1240
- console.log(kolor.green(`✅ Public directory exists: ${relative(cwd, publicDir)}`));
1241
- } else {
1242
- console.log(kolor.dim(`ℹ️ No public directory (${relative(cwd, publicDir)})`));
1243
- }
1244
-
1245
- // Check for package.json and dependencies
1246
- const pkgPath = join(cwd, "package.json");
1247
- if (existsSync(pkgPath)) {
1248
- try {
1249
- const pkgRaw = await readFile(pkgPath, "utf8");
1250
- const pkg = JSON.parse(pkgRaw);
1251
- if (pkg.dependencies?.basecampjs || pkg.devDependencies?.basecampjs) {
1252
- console.log(kolor.green("✅ basecampjs dependency found"));
1253
- } else {
1254
- console.log(kolor.yellow("⚠️ basecampjs not listed in dependencies"));
1255
- console.log(kolor.dim(" Consider adding: npm install basecampjs"));
1256
- }
1257
- } catch {
1258
- console.log(kolor.yellow("⚠️ Could not parse package.json"));
1259
- }
1260
- } else {
1261
- console.log(kolor.dim("ℹ️ No package.json found"));
1262
- }
1263
-
1264
- console.log();
1265
- if (hasIssues) {
1266
- console.log(kolor.yellow("⚠️ Some issues found. Review the messages above."));
1267
- } else {
1268
- console.log(kolor.green(kolor.bold("🎉 Everything looks good! Ready to build.")));
1269
- }
1270
- console.log();
1271
- }
1272
-
1273
- async function upgrade() {
1274
- console.log(kolor.cyan(kolor.bold("⬆️ Checking for CampsiteJS updates...\n")));
1275
-
1276
- // Check if package.json exists
1277
- const pkgPath = join(cwd, "package.json");
1278
- if (!existsSync(pkgPath)) {
1279
- console.log(kolor.red("❌ package.json not found"));
1280
- console.log(kolor.dim("This command should be run in a Campsite project directory.\n"));
1281
- exit(1);
1282
- }
1283
-
1284
- // Read current package.json
1285
- let pkg;
1286
- try {
1287
- const pkgRaw = await readFile(pkgPath, "utf8");
1288
- pkg = JSON.parse(pkgRaw);
1289
- } catch {
1290
- console.log(kolor.red("❌ Could not read package.json\n"));
1291
- exit(1);
1292
- }
1293
-
1294
- const currentVersion = pkg.dependencies?.basecampjs || pkg.devDependencies?.basecampjs;
1295
- if (!currentVersion) {
1296
- console.log(kolor.yellow("⚠️ basecampjs not found in dependencies"));
1297
- console.log(kolor.dim("Install it with: npm install basecampjs\n"));
1298
- exit(1);
1299
- }
1300
-
1301
- console.log(kolor.dim(`Current version: ${currentVersion}`));
1302
- console.log(kolor.cyan("\nUpgrading basecampjs to latest version...\n"));
1303
-
1304
- // Use dynamic import to run npm commands
1305
- const { spawn } = await import("child_process");
1306
-
1307
- return new Promise((resolve, reject) => {
1308
- const child = spawn("npm", ["install", "basecampjs@latest"], {
1309
- cwd,
1310
- stdio: "inherit",
1311
- shell: process.platform === "win32"
1312
- });
1313
-
1314
- child.on("close", async (code) => {
1315
- if (code === 0) {
1316
- console.log();
1317
- console.log(kolor.green("✅ CampsiteJS updated successfully!"));
1318
-
1319
- // Read updated version
1320
- try {
1321
- const updatedPkgRaw = await readFile(pkgPath, "utf8");
1322
- const updatedPkg = JSON.parse(updatedPkgRaw);
1323
- const newVersion = updatedPkg.dependencies?.basecampjs || updatedPkg.devDependencies?.basecampjs;
1324
- console.log(kolor.dim(`New version: ${newVersion}`));
1325
- } catch {}
1326
-
1327
- console.log();
1328
- console.log(kolor.dim("🌲 Tip: Run 'camper dev' to start developing with the latest version\n"));
1329
- resolve();
1330
- } else {
1331
- console.log();
1332
- console.log(kolor.red(`❌ Update failed with code ${code}\n`));
1333
- reject(new Error(`npm install failed with code ${code}`));
1334
- }
1335
- });
1336
-
1337
- child.on("error", (err) => {
1338
- console.log(kolor.red(`❌ Update failed: ${err.message}\n`));
1339
- reject(err);
1340
- });
1341
- });
1342
- }
1343
-
1344
- async function list() {
1345
- console.log(kolor.cyan(kolor.bold("🗺️ Listing Campsite content...\n")));
1346
-
1347
- const config = await loadConfig(cwd);
1348
- const srcDir = resolve(cwd, config.srcDir || "src");
1349
- const pagesDir = join(srcDir, "pages");
1350
- const layoutsDir = join(srcDir, "layouts");
1351
- const componentsDir = join(srcDir, "components");
1352
- const partialsDir = join(srcDir, "partials");
1353
- const collectionsDir = join(srcDir, "collections");
1354
- const dataDir = join(srcDir, "data");
1355
-
1356
- // List pages
1357
- if (existsSync(pagesDir)) {
1358
- const pages = await walkFiles(pagesDir);
1359
- if (pages.length > 0) {
1360
- console.log(kolor.bold("📄 Pages (") + kolor.cyan(pages.length.toString()) + kolor.bold(")"));
1361
- pages.forEach(page => {
1362
- const rel = relative(pagesDir, page);
1363
- console.log(" " + kolor.dim("• ") + rel);
1364
- });
1365
- console.log();
1366
- }
1367
- }
1368
-
1369
- // List layouts
1370
- if (existsSync(layoutsDir)) {
1371
- const layouts = await readdir(layoutsDir).catch(() => []);
1372
- if (layouts.length > 0) {
1373
- console.log(kolor.bold("📝 Layouts (") + kolor.cyan(layouts.length.toString()) + kolor.bold(")"));
1374
- layouts.forEach(layout => {
1375
- console.log(" " + kolor.dim("• ") + layout);
1376
- });
1377
- console.log();
1378
- }
1379
- }
1380
-
1381
- // List components
1382
- if (existsSync(componentsDir)) {
1383
- const components = await readdir(componentsDir).catch(() => []);
1384
- if (components.length > 0) {
1385
- console.log(kolor.bold("🧩 Components (") + kolor.cyan(components.length.toString()) + kolor.bold(")"));
1386
- components.forEach(component => {
1387
- console.log(" " + kolor.dim("• ") + component);
1388
- });
1389
- console.log();
1390
- }
1391
- }
1392
-
1393
- // List partials
1394
- if (existsSync(partialsDir)) {
1395
- const partials = await readdir(partialsDir).catch(() => []);
1396
- if (partials.length > 0) {
1397
- console.log(kolor.bold("🧰 Partials (") + kolor.cyan(partials.length.toString()) + kolor.bold(")"));
1398
- partials.forEach(partial => {
1399
- console.log(" " + kolor.dim("• ") + partial);
1400
- });
1401
- console.log();
1402
- }
1403
- }
1404
-
1405
- // List collections
1406
- if (existsSync(collectionsDir)) {
1407
- const collections = await readdir(collectionsDir).catch(() => []);
1408
- const jsonFiles = collections.filter(f => f.endsWith(".json"));
1409
- if (jsonFiles.length > 0) {
1410
- console.log(kolor.bold("📁 Collections (") + kolor.cyan(jsonFiles.length.toString()) + kolor.bold(")"));
1411
- jsonFiles.forEach(collection => {
1412
- console.log(" " + kolor.dim("• ") + collection);
1413
- });
1414
- console.log();
1415
- }
1416
- }
1417
-
1418
- // List data files
1419
- if (existsSync(dataDir)) {
1420
- const dataFiles = await readdir(dataDir).catch(() => []);
1421
- const jsonFiles = dataFiles.filter(f => f.endsWith(".json"));
1422
- if (jsonFiles.length > 0) {
1423
- console.log(kolor.bold("📊 Data (") + kolor.cyan(jsonFiles.length.toString()) + kolor.bold(")"));
1424
- jsonFiles.forEach(dataFile => {
1425
- console.log(" " + kolor.dim("• ") + dataFile);
1426
- });
1427
- console.log();
1428
- }
1429
- }
1430
-
1431
- console.log(kolor.dim("🌲 Tip: Use 'camper make:<type> <name>' to create new content\n"));
1432
- }
1433
-
1434
- async function preview() {
1435
- console.log(kolor.cyan(kolor.bold("🏔️ Building for production preview...\n")));
1436
- await build();
1437
- console.log();
1438
- const config = await loadConfig(cwd);
1439
- const outDir = resolve(cwd, config.outDir || "dist");
1440
- console.log(kolor.cyan(kolor.bold("🔥 Starting preview server...\n")));
1441
- serve(outDir);
1442
- }
1443
-
1444
- async function main() {
1445
- const command = argv[2] || "help";
1446
-
1447
- // Handle flags
1448
- if (command === "-h" || command === "--help" || command === "help") {
1449
- showHelp();
1450
- exit(0);
1451
- }
1452
-
1453
- if (command === "-v" || command === "--version") {
1454
- const version = await getVersion();
1455
- console.log(`v${version}`);
1456
- exit(0);
1457
- }
1458
-
1459
- // Handle make:type commands
1460
- if (command.startsWith("make:")) {
1461
- const type = command.substring(5); // Remove 'make:' prefix
1462
- if (!type) {
1463
- console.log(kolor.red("❌ No type specified"));
1464
- console.log(kolor.dim("Run 'camper --help' for available make commands.\n"));
1465
- exit(1);
1466
- }
1467
- await makeContent(type);
1468
- return;
1469
- }
1470
-
1471
- switch (command) {
1472
- case "init":
1473
- await init();
1474
- break;
1475
- case "dev":
1476
- await dev();
1477
- break;
1478
- case "build":
1479
- await build();
1480
- break;
1481
- case "serve": {
1482
- const config = await loadConfig(cwd);
1483
- const outDir = resolve(cwd, config.outDir || "dist");
1484
- if (!existsSync(outDir)) {
1485
- await build();
1486
- }
1487
- serve(outDir);
1488
- break;
1489
- }
1490
- case "preview":
1491
- await preview();
1492
- break;
1493
- case "clean":
1494
- case "cleanup":
1495
- await clean();
1496
- break;
1497
- case "check":
1498
- await check();
1499
- break;
1500
- case "list":
1501
- await list();
1502
- break;
1503
- case "upgrade":
1504
- await upgrade();
1505
- break;
1506
- default:
1507
- console.log(kolor.yellow(`Unknown command: ${command}`));
1508
- console.log(kolor.dim("Run 'camper --help' for usage information."));
1509
- exit(1);
1510
- }
1511
- }
1512
-
1513
- main().catch((err) => {
1514
- console.error(err);
1515
- exit(1);
1516
- });