-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-save-content.ts
More file actions
49 lines (40 loc) · 1.45 KB
/
vite-plugin-save-content.ts
File metadata and controls
49 lines (40 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { Plugin } from 'vite';
import fs from 'fs';
import path from 'path';
export function saveContentPlugin(): Plugin {
return {
name: 'save-content',
configureServer(server) {
server.middlewares.use('/api/save-content', async (req, res, next) => {
if (req.method !== 'POST') {
res.statusCode = 405;
res.end('Method not allowed');
return;
}
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});
req.on('end', async () => {
try {
const content = JSON.parse(body);
const contentTsPath = path.resolve(__dirname, 'src/data/content.ts');
const fileContent = `// Centralized content data for the portfolio website
// Edit this file directly to update content across all pages
const contentData = ${JSON.stringify(content, null, 2)};
export default contentData;
`;
fs.writeFileSync(contentTsPath, fileContent, 'utf-8');
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ success: true, message: 'Content saved successfully' }));
} catch (error: any) {
res.statusCode = 500;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ success: false, error: error.message }));
}
});
});
},
};
}