forked from ghempton/broccoli-json-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (49 loc) · 1.93 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
49
50
51
52
53
54
var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');
var Filter = require('broccoli-filter');
var deref = require('json-schema-deref');
module.exports = JsonModule
JsonModule.prototype = Object.create(Filter.prototype)
JsonModule.prototype.constructor = JsonModule
function JsonModule (inputTree, options) {
if (!(this instanceof JsonModule)) return new JsonModule(inputTree, options)
Filter.call(this, inputTree, options)
options = options || {};
}
JsonModule.prototype.extensions = ['json']
JsonModule.prototype.targetExtension = 'js'
JsonModule.prototype.processFile =
function processFile(srcDir, destDir, relativePath) {
var self = this;
var inputEncoding = this.inputEncoding;
var outputEncoding = this.outputEncoding;
if (inputEncoding === void 0) inputEncoding = 'utf8';
if (outputEncoding === void 0) outputEncoding = 'utf8';
var contents = fs.readFileSync(
srcDir + '/' + relativePath, { encoding: inputEncoding });
return Promise.resolve(this.processString(contents, relativePath, srcDir)).
then(function asyncOutputFilteredFile(outputString) {
var outputPath = self.getDestFilePath(relativePath);
if (outputPath == null) {
throw new Error('canProcessFile("' + relativePath + '") is true, but getDestFilePath("' + relativePath + '") is null');
}
outputPath = destDir + '/' + outputPath;
mkdirp.sync(path.dirname(outputPath));
fs.writeFileSync(outputPath, outputString, {
encoding: outputEncoding
});
});
};
JsonModule.prototype.processString = function (string, relativePath, srcDir) {
return new Promise(function(res, rej) {
var schema = JSON.parse(string);
deref(schema, { baseFolder: path.resolve(srcDir, path.dirname(relativePath)) }, (err, fullSchema) => {
if(err) {
rej(err);
} else {
res('export default ' + JSON.stringify(fullSchema) + ';');
}
});
});
}