-
Notifications
You must be signed in to change notification settings - Fork 183
/
index.js
38 lines (32 loc) · 1.14 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
'use strict';
var _ = require('lodash');
var Promise = require('bluebird');
var Formats = require('./lib/formats.js');
var Util = require('./lib/util.js');
var Converter = module.exports = {};
Converter.Formats = Formats;
Converter.BaseFormat = require('./lib/base_format.js');
Converter.ResourceReaders = Util.resourceReaders;
Converter.getSpec = function (source, format, callback) {
if (!Formats.hasOwnProperty(format)) {
throw new Error('Unknow format ' + format + ', you might have forgotten installing an optional dependency');
}
var spec = new Formats[format]();
return spec.resolveResources(source)
.return(spec)
.asCallback(callback);
}
Converter.getFormatName = function (name, version) {
var result;
_.each(Formats, function (format, formatName) {
format = format.prototype;
if (format.formatName === name && format.supportedVersions.indexOf(version) !== -1)
result = formatName;
});
return result;
};
Converter.convert = function(options, callback) {
return Converter.getSpec(options.source, options.from)
.then(fromSpec => fromSpec.convertTo(options.to, options.passthrough))
.asCallback(callback);
}