mirror of
https://github.com/stackblitz/bolt.new
synced 2025-06-26 18:17:50 +00:00
Create api.copy-files.ts
This commit is contained in:
parent
c5270a066f
commit
4241f10d08
36
app/routes/api.copy-files.ts
Normal file
36
app/routes/api.copy-files.ts
Normal file
@ -0,0 +1,36 @@
|
||||
// api/copy-files.ts
|
||||
import express from 'express';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.post('/copy-files', async (req, res) => {
|
||||
try {
|
||||
const { files, targetDirectory } = req.body;
|
||||
|
||||
// ensure target directory exists
|
||||
await fs.mkdir(targetDirectory, { recursive: true });
|
||||
|
||||
// copy each file
|
||||
for (const [filePath, dirent] of Object.entries(files)) {
|
||||
if (dirent?.type === 'file' && dirent.content) {
|
||||
const fullPath = path.join(targetDirectory, filePath.startsWith('/') ? filePath.slice(1) : filePath);
|
||||
const dirPath = path.dirname(fullPath);
|
||||
|
||||
// ensure directory exists
|
||||
await fs.mkdir(dirPath, { recursive: true });
|
||||
|
||||
// write file
|
||||
await fs.writeFile(fullPath, dirent.content);
|
||||
}
|
||||
}
|
||||
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error copying files:', error);
|
||||
res.status(500).json({ error: 'Failed to copy files' });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Loading…
Reference in New Issue
Block a user