@jsonpages/cli 3.0.65 → 3.0.67

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.
@@ -584,7 +584,7 @@ cat << 'END_OF_FILE_CONTENT' > "package.json"
584
584
  "dev": "vite",
585
585
  "dev:clean": "vite --force",
586
586
  "build": "tsc && vite build",
587
- "dist": "bash ./src2Code.sh src index.html scripts docs package.json",
587
+ "dist": "bash ./src2Code.sh src index.html vite.config.ts scripts docs package.json",
588
588
  "preview": "vite preview",
589
589
  "bake:email": "tsx scripts/bake-email.tsx",
590
590
  "bakemail": "npm run bake:email --"
@@ -594,7 +594,7 @@ cat << 'END_OF_FILE_CONTENT' > "package.json"
594
594
  "@tiptap/extension-link": "^2.11.5",
595
595
  "@tiptap/react": "^2.11.5",
596
596
  "@tiptap/starter-kit": "^2.11.5",
597
- "@jsonpages/core": "^1.0.52",
597
+ "@jsonpages/core": "^1.0.54",
598
598
  "clsx": "^2.1.1",
599
599
  "lucide-react": "^0.474.0",
600
600
  "react": "^19.0.0",
@@ -7502,4 +7502,144 @@ declare module '*?inline' {
7502
7502
 
7503
7503
 
7504
7504
 
7505
+ END_OF_FILE_CONTENT
7506
+ echo "Creating vite.config.ts..."
7507
+ cat << 'END_OF_FILE_CONTENT' > "vite.config.ts"
7508
+ /**
7509
+ * Generated by @jsonpages/cli. Dev server API: /api/save-to-file, /api/upload-asset, /api/list-assets.
7510
+ */
7511
+ import { defineConfig } from 'vite';
7512
+ import react from '@vitejs/plugin-react';
7513
+ import tailwindcss from '@tailwindcss/vite';
7514
+ import path from 'path';
7515
+ import fs from 'fs';
7516
+ import { fileURLToPath } from 'url';
7517
+
7518
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7519
+
7520
+ const ASSETS_IMAGES_DIR = path.resolve(__dirname, 'public', 'assets', 'images');
7521
+ const DATA_CONFIG_DIR = path.resolve(__dirname, 'src', 'data', 'config');
7522
+ const DATA_PAGES_DIR = path.resolve(__dirname, 'src', 'data', 'pages');
7523
+ const IMAGE_EXT = new Set(['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.avif']);
7524
+ const IMAGE_MIMES = new Set([
7525
+ 'image/jpeg', 'image/png', 'image/webp', 'image/gif', 'image/svg+xml', 'image/avif',
7526
+ ]);
7527
+ const MAX_FILE_SIZE_BYTES = 5 * 1024 * 1024; // 5MB
7528
+
7529
+ function safeFilename(original, mimeType) {
7530
+ const base = (original.replace(/\.[^.]+$/, '').replace(/[^a-zA-Z0-9-_]/g, '_').slice(0, 128)) || 'image';
7531
+ const ext = original.includes('.') ? path.extname(original).toLowerCase() : (mimeType?.startsWith('image/') ? `.${(mimeType.split('/')[1] || 'png').replace('jpeg', 'jpg')}` : '.png');
7532
+ return `${Date.now()}-${base}${IMAGE_EXT.has(ext) ? ext : '.png'}`;
7533
+ }
7534
+
7535
+ function listImagesInDir(dir, urlPrefix) {
7536
+ const list = [];
7537
+ if (!fs.existsSync(dir)) return list;
7538
+ for (const name of fs.readdirSync(dir)) {
7539
+ if (IMAGE_EXT.has(path.extname(name).toLowerCase())) list.push({ id: name, url: `${urlPrefix}/${name}`, alt: name, tags: [] });
7540
+ }
7541
+ return list;
7542
+ }
7543
+
7544
+ function sendJson(res, status, body) {
7545
+ res.writeHead(status, { 'Content-Type': 'application/json' });
7546
+ res.end(JSON.stringify(body));
7547
+ }
7548
+
7549
+ function sendJsonFile(res, filePath) {
7550
+ try {
7551
+ const content = fs.readFileSync(filePath, 'utf8');
7552
+ res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
7553
+ res.end(content);
7554
+ } catch (e) {
7555
+ sendJson(res, 500, { error: e?.message || 'Read failed' });
7556
+ }
7557
+ }
7558
+
7559
+ function isTenantPageJsonRequest(req, pathname) {
7560
+ if (req.method !== 'GET' || !pathname.endsWith('.json')) return false;
7561
+ const viteOrStaticPrefixes = ['/api/', '/assets/', '/src/', '/node_modules/', '/public/', '/@'];
7562
+ return !viteOrStaticPrefixes.some((prefix) => pathname.startsWith(prefix));
7563
+ }
7564
+ export default defineConfig({
7565
+ plugins: [
7566
+ react(),
7567
+ tailwindcss(),
7568
+ {
7569
+ name: 'upload-asset-api',
7570
+ configureServer(server) {
7571
+ server.middlewares.use((req, res, next) => {
7572
+ const pathname = (req.url || '').split('?')[0];
7573
+ const isPageJsonRequest = isTenantPageJsonRequest(req, pathname);
7574
+
7575
+ if (isPageJsonRequest) {
7576
+ const normalizedPath = decodeURIComponent(pathname).replace(/\\/g, '/');
7577
+ const slug = normalizedPath.replace(/^\/+/, '').replace(/\.json$/i, '').replace(/^\/+|\/+$/g, '');
7578
+ const candidate = path.resolve(DATA_PAGES_DIR, `${slug}.json`);
7579
+ const isInsidePagesDir = candidate.startsWith(`${DATA_PAGES_DIR}${path.sep}`) || candidate === DATA_PAGES_DIR;
7580
+ if (!slug || !isInsidePagesDir || !fs.existsSync(candidate) || !fs.statSync(candidate).isFile()) {
7581
+ sendJson(res, 404, { error: 'Page JSON not found' });
7582
+ return;
7583
+ }
7584
+ sendJsonFile(res, candidate);
7585
+ return;
7586
+ }
7587
+ if (req.method === 'GET' && req.url === '/api/list-assets') {
7588
+ try { sendJson(res, 200, listImagesInDir(ASSETS_IMAGES_DIR, '/assets/images')); } catch (e) { sendJson(res, 500, { error: e?.message || 'List failed' }); }
7589
+ return;
7590
+ }
7591
+ if (req.method === 'POST' && pathname === '/api/save-to-file') {
7592
+ const chunks = [];
7593
+ req.on('data', (chunk) => chunks.push(chunk));
7594
+ req.on('end', () => {
7595
+ try {
7596
+ const raw = Buffer.concat(chunks).toString('utf8');
7597
+ if (!raw.trim()) { sendJson(res, 400, { error: 'Empty body' }); return; }
7598
+ const body = JSON.parse(raw);
7599
+ const { projectState, slug } = body;
7600
+ if (!projectState || typeof slug !== 'string') { sendJson(res, 400, { error: 'Missing projectState or slug' }); return; }
7601
+ if (!fs.existsSync(DATA_CONFIG_DIR)) fs.mkdirSync(DATA_CONFIG_DIR, { recursive: true });
7602
+ if (!fs.existsSync(DATA_PAGES_DIR)) fs.mkdirSync(DATA_PAGES_DIR, { recursive: true });
7603
+ if (projectState.site != null) fs.writeFileSync(path.join(DATA_CONFIG_DIR, 'site.json'), JSON.stringify(projectState.site, null, 2), 'utf8');
7604
+ if (projectState.theme != null) fs.writeFileSync(path.join(DATA_CONFIG_DIR, 'theme.json'), JSON.stringify(projectState.theme, null, 2), 'utf8');
7605
+ if (projectState.menu != null) fs.writeFileSync(path.join(DATA_CONFIG_DIR, 'menu.json'), JSON.stringify(projectState.menu, null, 2), 'utf8');
7606
+ if (projectState.page != null) {
7607
+ const safeSlug = (slug.replace(/[^a-zA-Z0-9-_]/g, '_') || 'page');
7608
+ fs.writeFileSync(path.join(DATA_PAGES_DIR, `${safeSlug}.json`), JSON.stringify(projectState.page, null, 2), 'utf8');
7609
+ }
7610
+ sendJson(res, 200, { ok: true });
7611
+ } catch (e) { sendJson(res, 500, { error: e?.message || 'Save to file failed' }); }
7612
+ });
7613
+ req.on('error', () => sendJson(res, 500, { error: 'Request error' }));
7614
+ return;
7615
+ }
7616
+ if (req.method !== 'POST' || req.url !== '/api/upload-asset') return next();
7617
+ const chunks = [];
7618
+ req.on('data', (chunk) => chunks.push(chunk));
7619
+ req.on('end', () => {
7620
+ try {
7621
+ const body = JSON.parse(Buffer.concat(chunks).toString('utf8'));
7622
+ const { filename, mimeType, data } = body;
7623
+ if (!filename || typeof data !== 'string') { sendJson(res, 400, { error: 'Missing filename or data' }); return; }
7624
+ const buf = Buffer.from(data, 'base64');
7625
+ if (buf.length > MAX_FILE_SIZE_BYTES) { sendJson(res, 413, { error: 'File too large. Max 5MB.' }); return; }
7626
+ if (mimeType && !IMAGE_MIMES.has(mimeType)) { sendJson(res, 400, { error: 'Invalid file type' }); return; }
7627
+ const name = safeFilename(filename, mimeType);
7628
+ if (!fs.existsSync(ASSETS_IMAGES_DIR)) fs.mkdirSync(ASSETS_IMAGES_DIR, { recursive: true });
7629
+ fs.writeFileSync(path.join(ASSETS_IMAGES_DIR, name), buf);
7630
+ sendJson(res, 200, { url: `/assets/images/${name}` });
7631
+ } catch (e) { sendJson(res, 500, { error: e?.message || 'Upload failed' }); }
7632
+ });
7633
+ req.on('error', () => sendJson(res, 500, { error: 'Request error' }));
7634
+ });
7635
+ },
7636
+ },
7637
+ ],
7638
+ resolve: { alias: { '@': path.resolve(__dirname, './src') } },
7639
+ });
7640
+
7641
+
7642
+
7643
+
7644
+
7505
7645
  END_OF_FILE_CONTENT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsonpages/cli",
3
- "version": "3.0.65",
3
+ "version": "3.0.67",
4
4
  "description": "The Sovereign CLI Engine for JsonPages.",
5
5
  "type": "module",
6
6
  "bin": {