1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
8 |
|
9 | var _fs = require('fs');
|
10 |
|
11 | var _fs2 = _interopRequireDefault(_fs);
|
12 |
|
13 | var _path = require('path');
|
14 |
|
15 | var _path2 = _interopRequireDefault(_path);
|
16 |
|
17 | var _htmlEntities = require('html-entities');
|
18 |
|
19 | var _htmlEntities2 = _interopRequireDefault(_htmlEntities);
|
20 |
|
21 | var _helper = require('./data/redux/helper');
|
22 |
|
23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
24 |
|
25 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
26 |
|
27 | var entities = new _htmlEntities2.default.AllHtmlEntities();
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | var ComponentFinder = function () {
|
34 | function ComponentFinder() {
|
35 | _classCallCheck(this, ComponentFinder);
|
36 | }
|
37 |
|
38 | _createClass(ComponentFinder, [{
|
39 | key: 'find',
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | value: function find(absoluteProjectDir, callback) {
|
49 | this._find(absoluteProjectDir, '', callback);
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | }, {
|
60 | key: '_find',
|
61 | value: function _find(absoluteRootDir, relativeDir, callback) {
|
62 | var currentAbsoluteDir = _path2.default.join(absoluteRootDir, relativeDir);
|
63 | this._check(currentAbsoluteDir, true, false);
|
64 | var i, candidates, candidate, stat, candidatePath, newRelativeDir;
|
65 | candidates = _fs2.default.readdirSync(currentAbsoluteDir);
|
66 | for (i = 0; i < candidates.length; i++) {
|
67 | candidate = candidates[i];
|
68 | candidatePath = _path2.default.join(currentAbsoluteDir, candidate);
|
69 | stat = _fs2.default.statSync(candidatePath);
|
70 | if (stat.isDirectory()) {
|
71 | newRelativeDir = _path2.default.join(relativeDir, candidate);
|
72 | this._find(absoluteRootDir, newRelativeDir, callback);
|
73 | continue;
|
74 | }
|
75 | if (stat.isFile() && candidate == 'component.json') {
|
76 | this._reg(absoluteRootDir, relativeDir, candidatePath, callback);
|
77 | }
|
78 | }
|
79 | }
|
80 | }, {
|
81 | key: '_reg',
|
82 | value: function _reg(absoluteRootDir, relativePath, jsonAbsolutePath, callback) {
|
83 | var componentJson;
|
84 | try {
|
85 | componentJson = JSON.parse(_fs2.default.readFileSync(jsonAbsolutePath, 'utf8'));
|
86 | } catch (error) {
|
87 | console.log('IGNORED: Failed to read this component file: ' + jsonAbsolutePath);
|
88 | }
|
89 |
|
90 | var result = this._validateComponentJson(componentJson, jsonAbsolutePath);
|
91 | if (!result) return;
|
92 |
|
93 | var thumbRelativePath = _path2.default.join(relativePath, componentJson.thumbnail);
|
94 | var thumbFilePath = _path2.default.join(absoluteRootDir, thumbRelativePath);
|
95 | if (!this._fileExists(thumbFilePath)) {
|
96 | console.log('IGNORED: Thumbnail component does not exist: ' + thumbFilePath + ' in: ' + jsonAbsolutePath);
|
97 | return;
|
98 | }
|
99 | var unescapedCode = _fs2.default.readFileSync(thumbFilePath).toString();
|
100 | var code = entities.encode(unescapedCode);
|
101 |
|
102 | var testRelativePath = this._ensureComponentFileExists(componentJson, absoluteRootDir, relativePath, jsonAbsolutePath, 'test');
|
103 | var docRelativePath = this._ensureComponentFileExists(componentJson, absoluteRootDir, relativePath, jsonAbsolutePath, 'doc');
|
104 |
|
105 | callback(thumbRelativePath, testRelativePath, docRelativePath, code, componentJson);
|
106 | }
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | }, {
|
121 | key: '_ensureComponentFileExists',
|
122 | value: function _ensureComponentFileExists(componentJson, absoluteRootDir, relativePath, jsonAbsolutePath, name) {
|
123 | var fileRelativePath, absolutePath;
|
124 | if ((0, _helper.isStringAndNotEmpty)(componentJson[name])) {
|
125 | fileRelativePath = _path2.default.join(relativePath, componentJson[name]);
|
126 | absolutePath = _path2.default.join(absoluteRootDir, fileRelativePath);
|
127 | if (!this._fileExists(absolutePath)) {
|
128 | console.log('IGNORED: ' + name + ' component does not exist: ' + absolutePath + ' in ' + jsonAbsolutePath);
|
129 | return null;
|
130 | }
|
131 | return fileRelativePath;
|
132 | }
|
133 | return null;
|
134 | }
|
135 | }, {
|
136 | key: '_fileExists',
|
137 | value: function _fileExists(filePath) {
|
138 | try {
|
139 | var stats = _fs2.default.statSync(filePath);
|
140 | return stats.isFile();
|
141 | } catch (e) {
|
142 | if (e.code == 'ENOENT') return false;
|
143 | throw e;
|
144 | }
|
145 | }
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | }, {
|
154 | key: '_validateComponentJson',
|
155 | value: function _validateComponentJson(componentJson, jsonAbsolutePath) {
|
156 | var vendor = componentJson.vendor;
|
157 | var name = componentJson.name;
|
158 | var thumbnail = componentJson.thumbnail;
|
159 | var author = componentJson.author;
|
160 | var category = componentJson.category;
|
161 |
|
162 | if (!(0, _helper.isStringAndNotEmpty)(vendor)) {
|
163 | console.log('IGNORED: component.json does not contain vendor: ' + jsonAbsolutePath);
|
164 | return false;
|
165 | }
|
166 |
|
167 | if (!(0, _helper.isStringAndNotEmpty)(name)) {
|
168 | console.log('IGNORED: component.json does not contain name: ' + jsonAbsolutePath);
|
169 | return false;
|
170 | }
|
171 |
|
172 | if (!(0, _helper.isStringAndNotEmpty)(thumbnail)) {
|
173 | console.log('IGNORED: component.json does not contain thumbnail component: ' + jsonAbsolutePath);
|
174 | return false;
|
175 | }
|
176 |
|
177 | if (author == null) {
|
178 | console.log('IGNORED: component.json does not contain author information: ' + jsonAbsolutePath);
|
179 | return false;
|
180 | }
|
181 |
|
182 | if (!(0, _helper.isStringAndNotEmpty)(author.name) || !(0, _helper.isStringAndNotEmpty)(author.email)) {
|
183 | console.log('IGNORED: component.json does not contain author name and/or email: ' + jsonAbsolutePath);
|
184 | return false;
|
185 | }
|
186 |
|
187 | if (!(0, _helper.isStringAndNotEmpty)(category)) {
|
188 | console.log('IGNORED: component.json does not contain category: ' + jsonAbsolutePath);
|
189 | return false;
|
190 | }
|
191 |
|
192 | return true;
|
193 | }
|
194 |
|
195 | |
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | }, {
|
203 | key: '_check',
|
204 | value: function _check(absPath, shouldBeDir, shouldIgnore) {
|
205 | var stat;
|
206 | try {
|
207 | stat = _fs2.default.statSync(absPath);
|
208 | } catch (error) {
|
209 | if (shouldIgnore) {
|
210 | console.log('Ignored: Component file/dir does no exist: \'' + absPath + '\'.');
|
211 | return false;
|
212 | } else {
|
213 | throw error;
|
214 | }
|
215 | }
|
216 |
|
217 | var isMismatchType = shouldBeDir && !stat.isDirectory() || !shouldBeDir && !stat.isFile();
|
218 | if (isMismatchType) {
|
219 | if (shouldIgnore) {
|
220 | console.log(this._mismatchTypeMessage(absPath, shouldBeDir, 'Ignored: '));
|
221 | return false;
|
222 | } else {
|
223 | throw new Error(this._mismatchTypeMessage(absPath, shouldBeDir, ''));
|
224 | }
|
225 | }
|
226 | return true;
|
227 | }
|
228 |
|
229 | |
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | }, {
|
236 | key: '_mismatchTypeMessage',
|
237 | value: function _mismatchTypeMessage(absPath, shouldBeDir, prefix) {
|
238 | if (shouldBeDir) {
|
239 | return prefix + 'Expected directory, but file found: \'' + absPath + '\'.';
|
240 | }
|
241 | return prefix + 'Expected file, but directory found: \'' + absPath + '\'.';
|
242 | }
|
243 | }]);
|
244 |
|
245 | return ComponentFinder;
|
246 | }();
|
247 |
|
248 | exports.default = ComponentFinder;
|
249 |
|
\ | No newline at end of file |