-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
48 lines (36 loc) · 1.22 KB
/
index.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
'use strict';
const SupportedLanguages = require('./lib/supportedlanguages');
module.exports = (function() {
const Svg = require('./lib/svg');
let langs = new SupportedLanguages();
function generate(language, source, options = {}) {
let plugin = validate(language, source);
return execute(plugin, source, options);
}
function supportedLanguages() {
return langs;
}
function validate(language, source) {
let plugin = langs.getCommand(language);
if (!plugin) {
throw new Error(`Unsupported language ${language}. Must be one of ${langs.languages.toString()} `);
}
if (!source) {
throw new Error('No source provided for input');
}
return plugin;
}
function execute(plugin, source, options) {
try {
let output = plugin.generate(source, options);
if (output) { return new Svg(output, options); }
} catch (e) {
throw new Error(`Unable to render graph language with ${plugin.lang()}: ${e}`);
}
return null;
}
return {
generate: generate,
supportedLanguages: supportedLanguages
};
})();