Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1d28244

Browse files
committed
temporary commit
1 parent e8d66c6 commit 1d28244

File tree

8 files changed

+464
-54
lines changed

8 files changed

+464
-54
lines changed

.validate/index.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
const path = require('path');
22
const fs = require('fs-extra');
33

4-
const { listDirectories, listFiles } = require('./utils');
4+
const { listDirectories, listFiles, signale } = require('./utils');
55
const { rootDir } = require('./constants');
6-
const validate = {
7-
js: require('./js'),
8-
};
6+
const validators = require('./validators');
97

108
const categories = listDirectories(rootDir).filter(dir => dir !== 'node_modules');
11-
let hasError = false;
129
for (const category of categories) {
1310
const categoryDir = path.resolve(rootDir, category);
1411
const algorithms = listDirectories(categoryDir);
@@ -17,16 +14,28 @@ for (const category of categories) {
1714
const files = listFiles(algorithmDir);
1815
for (const file of files) {
1916
const ext = file.split('.').pop();
20-
const validator = validate[ext];
17+
const validator = validators[ext];
2118
if (validator) {
19+
const errors = [];
20+
const error = message => errors.push(message);
21+
const warns = [];
22+
const warn = message => warns.push(message);
2223
const filePath = path.resolve(algorithmDir, file);
2324
const content = fs.readFileSync(filePath, 'utf-8');
24-
if (!validator(category, algorithm, file, content)) {
25-
hasError = true;
26-
}
25+
validator(error, warn, category, algorithm, file, content)
26+
.then(() => {
27+
if (errors.length || warns.length) {
28+
signale.log(`${category}/${algorithm}/${file}`);
29+
errors.forEach(error => signale.error(error));
30+
warns.forEach(error => signale.warn(error));
31+
signale.log();
32+
}
33+
if (errors.length) {
34+
process.exitCode = 1;
35+
}
36+
})
37+
.catch(console.error);
2738
}
2839
}
2940
}
3041
}
31-
32-
process.exit(hasError ? 1 : 0);

.validate/js/index.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

.validate/validators/common.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { MAX_STEPS } = require('../constants');
2+
3+
function ensureCodeFoldings(error, warn, content) {
4+
if (!/\/\/ import visualization libraries {/.test(content)) {
5+
error('Missing the code folding for importing visualization libraries.');
6+
}
7+
if (!/\/\/ define tracer variables {/.test(content)) {
8+
error('Missing the code folding for defining tracer variables.');
9+
}
10+
if (!/\/\/ visualize {/.test(content)) {
11+
error('Missing the code folding for visualizing.');
12+
}
13+
}
14+
15+
function ensureMaxSteps(error, warn, commands) {
16+
const steps = commands.filter(command => command.method === 'delay').length;
17+
if (steps > MAX_STEPS) {
18+
warn('Too many steps.');
19+
}
20+
}
21+
22+
module.exports = {
23+
ensureCodeFoldings,
24+
ensureMaxSteps,
25+
};

.validate/validators/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const js = require('./js');
2+
const java = require('./java');
3+
4+
module.exports = {
5+
js,
6+
java,
7+
};

.validate/validators/java.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const rp = require('request-promise');
2+
const { ensureMaxSteps, ensureCodeFoldings } = require('./common');
3+
4+
module.exports = async (error, warn, category, algorithm, file, content) => {
5+
try {
6+
// TODO: Cache the response and restore if sha of the content matches
7+
const commands = await rp({
8+
method: 'POST',
9+
uri: 'https://algorithm-visualizer.org/api/tracers/java',
10+
form: { code: content },
11+
json: true,
12+
});
13+
ensureMaxSteps(error, warn, commands);
14+
} catch (e) {
15+
error(e);
16+
}
17+
ensureCodeFoldings(error, warn, content);
18+
};

.validate/validators/js.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
process.env.ALGORITHM_VISUALIZER = '1';
2+
3+
const path = require('path');
4+
const { ensureMaxSteps, ensureCodeFoldings } = require('./common');
5+
const { Commander } = require('algorithm-visualizer');
6+
const { rootDir } = require('../constants');
7+
8+
module.exports = async (error, warn, category, algorithm, file, content) => {
9+
try {
10+
Commander.init();
11+
require(path.resolve(rootDir, category, algorithm, file));
12+
ensureMaxSteps(error, warn, Commander.commands);
13+
} catch (e) {
14+
error(e);
15+
}
16+
ensureCodeFoldings(error, warn, content);
17+
};

0 commit comments

Comments
 (0)