Saving modules to files
This commit is contained in:
parent
ed103ed2cb
commit
d6466555df
3 changed files with 62 additions and 3 deletions
33
vite.config.ts
Normal file
33
vite.config.ts
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import { defineConfig } from 'vite'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
export default defineConfig({
|
||||
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/cg2', 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 }));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}]
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue