This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.js
128 lines (101 loc) · 4.99 KB
/
console.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
116
117
118
119
120
121
122
123
124
125
126
127
128
process.chdir(__dirname);
const path = require('path');
const fs = require('fs');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const express = require('express');
const app = express();
function parseIdFromResultsPath(resultsPath) {
return path.dirname(resultsPath).substring(resultsPath.lastIndexOf('-') + 1);
}
function loadResults(resultsPath) {
const results = JSON.parse(fs.readFileSync(resultsPath));
results.id = parseIdFromResultsPath(resultsPath);
results.excludedRegionsFile = path.join(results.properties.baselineDir, "excluded-regions.json");
return {[results.id]: results};
}
function handleImageFileRequest(req, res, dirProperty) {
res.sendFile(path.join(results[req.params.id].properties[dirProperty], req.params.fileName));
}
function handleDiffAction(req, res, fn) {
const diffIndex = results[req.query.id].diffs.findIndex(diff => diff.fileName === req.query.fileName);
fn(results[req.query.id].diffs[diffIndex]);
results[req.query.id].diffs.splice(diffIndex, 1);
res.sendStatus(200);
}
function resolveNodeModulesDir() {
const nodeModulesDirName = 'node_modules';
const nodeModulesDirIndex = __dirname.indexOf('node_modules');
if (nodeModulesDirIndex >= 0) {
return __dirname.substring(0, nodeModulesDirIndex + nodeModulesDirName.length);
} else {
return path.join(__dirname, nodeModulesDirName);
}
}
const resultsFilePath = process.argv[2];
const port = parseInt(process.argv[3]);
const nodeModulesDir = resolveNodeModulesDir();
let results = loadResults(resultsFilePath);
app.use(bodyParser.text());
app.use('/baseline/:id/:fileName', (req, res) => handleImageFileRequest(req, res, 'baselineDir'));
app.use('/captured/:id/:fileName', (req, res) => handleImageFileRequest(req, res, 'capturedDir'));
app.use('/diffs/:id/:fileName', (req, res) => handleImageFileRequest(req, res, 'resultsDir'));
app.use('/scripts', express.static(path.join(__dirname, 'scripts')));
app.use('/scripts', express.static(path.join(nodeModulesDir, 'zooming', 'build')));
app.use('/scripts', express.static(path.join(nodeModulesDir, 'bootstrap', 'dist', 'js')));
app.use('/scripts', express.static(path.join(nodeModulesDir, 'jquery', 'dist')));
app.use('/scripts', express.static(path.join(nodeModulesDir, 'jquery-select-areas')));
app.use('/styles', express.static(path.join(__dirname, '/styles')));
app.use('/styles', express.static(path.join(nodeModulesDir, 'jquery-select-areas', 'resources')));
app.use('/styles', express.static(path.join(nodeModulesDir, 'bootstrap', 'dist', 'css')));
app.engine('hbs', exphbs({defaultLayout: 'single', extname: 'hbs'}));
app.set('view engine', 'hbs');
app.get('/', (req, res) => res.render('index', {
layout: false,
results: Object.keys(results).filter(key => results[key].diffs.length > 0).map(key => results[key])
}));
app.get('/results', (req, res) => res.send(results));
app.get('/excluded_regions', (req, res) => {
if (fs.existsSync(results[req.query.id].excludedRegionsFile)) {
const config = JSON.parse(fs.readFileSync(results[req.query.id].excludedRegionsFile));
if (config[req.query.fileName]) {
res.send(config[req.query.fileName]);
return;
}
}
res.send("{}");
});
app.post('/excluded_regions', (req, res) => {
const excludedRegionsFile = results[req.query.id].excludedRegionsFile;
const currentRegions = fs.existsSync(excludedRegionsFile) ? JSON.parse(fs.readFileSync(excludedRegionsFile)) : {};
// update regions for the file and persist it
currentRegions[req.query.fileName] = JSON.parse(req.body);
fs.writeFileSync(excludedRegionsFile, JSON.stringify(currentRegions, null, 2));
res.sendStatus(200);
});
app.post('/stop_console', () => process.exit(0));
app.post('/replace_baseline', (req, res) => {
handleDiffAction(req, res, diff =>
fs.copyFileSync(
path.join(results[req.query.id].properties.capturedDir, diff.fileName),
path.join(results[req.query.id].properties.baselineDir, diff.fileName)))
});
app.post('/discard_captured', (req, res) => {
handleDiffAction(req, res, () => ({}));
});
app.post('/register_results', (req, res) => {
const newResults = loadResults(req.body);
const newResultsProperties = Object.keys(newResults).map(key => newResults[key].properties)[0];
// remove records for results created from the same directories so we don't pollute the console with obsolete stuff
Object.keys(results)
.filter(key => results[key].properties.baselineDir === newResultsProperties.baselineDir
&& results[key].properties.capturedDir === newResultsProperties.capturedDir)
.forEach(key => delete results[key]);
results = {...results, ...newResults};
res.sendStatus(200);
// stop console if there are no diffs remaining
if (Object.keys(results).filter(key => results[key].diffs.length != 0).length === 0) {
process.exit(0);
}
});
app.listen(port, () => console.log('Listening on port ' + port));