38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { defineConfig } from 'vite'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
export default defineConfig({
|
|
server: {
|
|
watch: {
|
|
ignored: ['**/src/cg/**']
|
|
}
|
|
},
|
|
plugins: [{
|
|
name: 'cg-save',
|
|
configureServer(server) {
|
|
server.middlewares.use('/api/save', (req, res) => {
|
|
if (req.method !== 'POST') {
|
|
res.statusCode = 405;
|
|
res.end();
|
|
return;
|
|
}
|
|
|
|
let body = '';
|
|
req.on('data', chunk => body += chunk);
|
|
req.on('end', () => {
|
|
try {
|
|
const { filename, content } = JSON.parse(body);
|
|
const filePath = path.join(__dirname, 'src/cg', filename + '.cg');
|
|
fs.writeFileSync(filePath, content);
|
|
res.statusCode = 200;
|
|
res.end(JSON.stringify({ ok: true }));
|
|
} catch (e: any) {
|
|
res.statusCode = 500;
|
|
res.end(JSON.stringify({ error: e.message }));
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}]
|
|
})
|