forked from oss-slu/DigitalBonesBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (45 loc) · 1.74 KB
/
server.js
File metadata and controls
55 lines (45 loc) · 1.74 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
49
50
51
52
53
54
55
const express = require('express');
const bodyParser = require('body-parser');
const simpleGit = require('simple-git');
const fs = require('fs');
const path = require('path');
const app = express();
const git = simpleGit('C:/DigitalBonesBox'); // Local repo path
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Serve the HTMX page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'editor', 'push.html'));
});
// Endpoint for the publish action
app.post('/publish', async (req, res) => {
const { fileName, jsonData } = req.body;
const filePath = path.join('C:/DigitalBonesBox', 'databones', fileName);
try {
// Checkout to the data branch
await git.checkout('data');
// Save the JSON data to a file
fs.writeFileSync(filePath, jsonData);
// Git operations: add, commit, and push to the data branch
await git.add(filePath);
const commitMessage = `Automated publish at ${new Date().toISOString()}`;
await git.commit(commitMessage);
await git.push('origin', 'data');
// Switch back to the main branch
await git.checkout('main');
res.send('Publish successful! Changes pushed to the data branch.');
} catch (error) {
console.error('Publish failed:', error);
// Attempt to switch back to main in case of error
try {
await git.checkout('main');
} catch (checkoutError) {
console.error('Failed to switch back to the main branch:', checkoutError);
}
res.status(500).send('Publish failed. Check server logs for details.');
}
});
const PORT = 3001;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});