-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (108 loc) · 3.12 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
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
'use strict';
var TreeTraverser = require('broccoli-tree-traverser'),
Writer = require('broccoli-writer'),
util = require('util'),
RSVP = require('rsvp'),
fs = require('fs'),
path = require('path');
/**
* Take an input tree, and roll it up into a JSON document.
* The resulting document will be named `srcDir.json`. it will have
* key names associated with each of the file and directories that `srcDir` contains.
* If the key represents the file, then the stringified file contents will be the value of the key.
* If the key represents a directory, then the value of that key will be an `Object`, represented
* by the same algorithm
*
* @param inputTree {string} - relative path to source tree
* @return {Tree2Json}
* @constructor
* @alias module:index
*/
function Tree2Json(inputTree) {
if (!(this instanceof Tree2Json)) return new Tree2Json(inputTree);
this.inputTree = inputTree.replace(/\/$/, '');
this.walker = new TreeTraverser(inputTree, this);
}
util.inherits(Tree2Json, Writer);
/**
* Take the contents of the `buffer`, and place it in the JSON at the
* path represented by filePath
* @param filePath
* @param buffer
*/
Tree2Json.prototype.loadJson = function (filePath, buffer) {
var elem = this.json;
var keys = filePath.substr(this.inputTree.length + 1).split(path.sep);
keys.forEach(function (key, i) {
var subelem;
if (i === keys.length - 1) {
var strval = buffer.toString();
try {
elem[key.split('.')[0]] = JSON.parse(strval);
} catch (e) {
elem[key.split('.')[0]] = strval;
}
} else {
subelem = elem[key] || {};
elem[key] = subelem;
elem = subelem;
}
});
};
/**
* Interface implementation required by Walker.
*
* Read the file, and load the results into json
*
* @param filePath
* @return {RSVP.Promise}
*/
Tree2Json.prototype.visit = function (filePath) {
var self = this;
return new RSVP.Promise(function (resolve, reject) {
fs.readFile(filePath, function (err, data) {
if (err) {reject(err);}
else {
self.loadJson(filePath, data);
resolve();
}
})
});
};
function getFilePath(destDir, inputTree) {
var fileName = inputTree;
var pathPos = fileName.lastIndexOf(path.sep);
if (pathPos > 0) {
fileName = fileName.substr(pathPos);
}
return path.join(destDir, fileName) + '.json';
}
/**
* Satisfy interface required by 'Write'.
*
* Defer to walker to read the tree (which will let me visit each file)
*
* @param readTree - a function to read the tree. It should return a promise
* @param destDir - the directory to write the results in
* @return {RSVP.Promise}
*/
Tree2Json.prototype.write = function (readTree, destDir) {
var self = this;
this.json = {};
return readTree(this.walker)
.then(function () {
return new RSVP.Promise(function (resolve, reject) {
fs.writeFile(getFilePath(destDir, self.inputTree), JSON.stringify(self.json), function (err) {
if (err) {reject(err);}
else resolve();
});
});
}).then(function () {
self.json = {};
});
};
/**
*
* @type {Tree2Json}
*/
module.exports = Tree2Json;