-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
115 lines (105 loc) · 4.64 KB
/
routes.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { writeFile, readFile } = require('./fileOperations');
const { commitChanges, getCommitLogs, revertToCommit, getDiffs } = require('./gitOperations');
const jsFilePath = './public/script.js';
const cssFilePath = './public/style.css';
const htmlFilePath = './public/tree.html';
module.exports = function(app, io) {
// CRUD operations for HTML elements
app.post('/element', async (req, res) => {
// Assume htmlTree is being managed somewhere in the server state
const { id, content } = req.body;
global.htmlTree += `<div id="${id}">${content}</div>`; // Adding element
await writeFile(htmlFilePath, global.htmlTree);
io.emit('update', global.htmlTree);
await commitChanges(`Added element ${id}`);
res.status(201).send({ message: 'Element added', htmlTree: global.htmlTree });
});
app.put('/element/:id', async (req, res) => {
const { content } = req.body;
const start = global.htmlTree.indexOf(`<div id="${req.params.id}">`);
if (start === -1) {
res.status(404).send({ message: 'Element not found' });
return;
}
const end = global.htmlTree.indexOf('</div>', start) + 6;
const oldElement = global.htmlTree.substring(start, end);
const newElement = `<div id="${req.params.id}">${content}</div>`;
global.htmlTree = global.htmlTree.replace(oldElement, newElement);
await writeFile(htmlFilePath, global.htmlTree);
io.emit('update', global.htmlTree);
await commitChanges(`Updated element ${req.params.id}`);
res.send({ message: 'Element updated', htmlTree: global.htmlTree });
});
app.delete('/element/:id', async (req, res) => {
const id = req.params.id;
const start = global.htmlTree.indexOf(`<div id="${id}">`);
if (start === -1) {
res.status(404).send({ message: 'Element not found' });
return;
}
const end = global.htmlTree.indexOf('</div>', start) + 6;
const element = global.htmlTree.substring(start, end);
global.htmlTree = global.htmlTree.replace(element, '');
await writeFile(htmlFilePath, global.htmlTree);
io.emit('update', global.htmlTree);
await commitChanges(`Deleted element ${id}`);
res.send({ message: 'Element deleted', htmlTree: global.htmlTree });
});
// Route to handle JavaScript updates
app.post('/update_js', async (req, res) => {
try {
await writeFile(jsFilePath, req.body.content, append = true);
io.emit('update_js', req.body.content);
await commitChanges('Updated JavaScript content');
res.send({ message: 'JavaScript updated' });
} catch (error) {
res.status(500).send({ message: 'Failed to update JavaScript', error });
}
});
// Route to handle CSS updates
app.post('/update_css', async (req, res) => {
try {
await writeFile(cssFilePath, req.body.content, append = true);
io.emit('update_css', req.body.content);
await commitChanges('Updated CSS content');
res.send({ message: 'CSS updated' });
} catch (error) {
res.status(500).send({ message: 'Failed to update CSS', error });
}
});
// Route to get commit logs
app.get('/commits', async (req, res) => {
try {
const logs = await getCommitLogs(req.query);
res.send(logs);
} catch (error) {
res.status(500).send({ message: 'Failed to retrieve commit logs', error });
}
});
// Route to get diffs between commits
app.get('/diffs', async (req, res) => {
try {
// Extract 'from' and 'to' query parameters from the request
const { from, to } = req.query;
if (!from || !to) {
return res.status(400).send({ message: 'Both from and to parameters are required.' });
}
const diffs = await getDiffs({ from, to });
res.send({diffs});
} catch (error) {
res.status(500).send({ message: 'Failed to retrieve commit logs', error });
}
});
// Route to revert to a specific commit
app.post('/revert', async (req, res) => {
const { commitHash } = req.body;
try {
await revertToCommit(commitHash);
global.htmlTree = await readFile(htmlFilePath); // Reload the HTML tree file
io.emit('update', global.htmlTree);
res.send({ message: `Reverted to commit ${commitHash}` });
} catch (error) {
res.status(500).send({ message: 'Failed to revert to commit', error });
}
});
};