-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
99 lines (81 loc) · 3 KB
/
server.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
// server.js
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const fsExtra = require('fs-extra');
const app = express();
const { exec } = require('child_process');
const PORT = 3001;
app.use(cors());
app.use(bodyParser.json());
app.get('/api/json-files', (req, res) => {
const directoryPath = path.join(__dirname, 'checkpool');
fs.readdir(directoryPath, async (err, files) => {
if (err) {
return res.status(500).send('Unable to scan directory: ' + err);
}
const jsonFiles = [];
for (const file of files) {
if (path.extname(file) === '.json') {
const filePath = path.join(directoryPath, file);
const content = await fs.promises.readFile(filePath, 'utf-8');
jsonFiles.push({ fileName: file, content }); // Send filename and content
}
}
res.json(jsonFiles);
});
});
app.post('/api/selected-files', (req, res) => {
const { files } = req.body;
const scriptDirectory = __dirname;
const checkpoolDirectory = path.join(scriptDirectory, 'checkpool');
const selectedFilesDirectory = path.join(scriptDirectory, 'selected-files');
// Create selected-files directory if it doesn't exist
if (!fs.existsSync(selectedFilesDirectory)) {
fs.mkdirSync(selectedFilesDirectory);
}
files.forEach(file => {
const sourceFile = path.join(checkpoolDirectory, file);
const destinationFile = path.join(selectedFilesDirectory, file);
// Check if the source file exists
if (fs.existsSync(sourceFile)) {
// Check if the file already exists in the destination
if (!fs.existsSync(destinationFile)) {
fsExtra.copySync(sourceFile, destinationFile);
} else {
console.log(`File already exists: ${file}`);
}
} else {
console.log(`Source file does not exist: ${file}`);
}
});
console.log('Files copied successfully:', files);
const scriptPath = path.join(__dirname, 'run.sh');
exec(`bash ${scriptPath}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing script: ${error.message}`);
return;
}
if (stderr) {
console.error(`Script error output: ${stderr}`);
return;
}
console.log(`Script output:\n${stdout}`);
});
res.send({ message: 'Files copied successfully' });
});
app.get('/api/output', (req, res) => {
const outputFilePath = path.join(__dirname, 'output.txt');
fs.readFile(outputFilePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading output.txt:', err);
return res.status(500).send('Error reading file');
}
res.send(data);
});
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});