UNPKG

4.33 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _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
9var _fs = require('fs');
10
11var _fs2 = _interopRequireDefault(_fs);
12
13var _path = require('path');
14
15var _path2 = _interopRequireDefault(_path);
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
20
21/**
22 * @type {RegExp}
23 */
24var UNDERSCORE_REGEX = /^_/;
25
26/**
27 * @SERVER
28 */
29
30var PageFinder = function () {
31
32 /**
33 * @param {Logger} logger
34 */
35 function PageFinder(logger) {
36 _classCallCheck(this, PageFinder);
37
38 this._logger = logger;
39 }
40
41 /**
42 * 1) Find recursively every component in the directory.
43 * 2) Validate the component.
44 *
45 * @param {string} absoluteRootDir
46 * @param {Function} callback
47 */
48
49
50 _createClass(PageFinder, [{
51 key: 'find',
52 value: function find(absoluteRootDir, callback) {
53 var i, vendorNames;
54 this._check(absoluteRootDir, true, false);
55 vendorNames = _fs2.default.readdirSync(absoluteRootDir);
56 for (i = 0; i < vendorNames.length; i++) {
57 this._checkComponentDir(absoluteRootDir, vendorNames[i], callback);
58 }
59 }
60 }, {
61 key: '_checkComponentDir',
62 value: function _checkComponentDir(absoluteRootDir, vendorName, callback) {
63 var i, filenames, vendorRootDir, compName, compRootDir, compMain;
64 vendorRootDir = _path2.default.join(absoluteRootDir, vendorName);
65 this._check(vendorRootDir, true, false);
66 filenames = _fs2.default.readdirSync(vendorRootDir);
67 for (i = 0; i < filenames.length; i++) {
68 compName = filenames[i];
69 compRootDir = _path2.default.join(vendorRootDir, compName);
70 compMain = _path2.default.join(vendorRootDir, compName, compName + '.js');
71
72 if (!this._check(compRootDir, true, true)) continue;
73 if (!this._check(compMain, false, true)) continue;
74
75 if (UNDERSCORE_REGEX.test(compName)) {
76 throw new Error('Component name starting with underscore is not allowed: ' + compName);
77 }
78
79 callback(vendorName, compName, compRootDir, _path2.default.join(vendorName, compName));
80 }
81 }
82
83 /**
84 * @param {string} absPath
85 * @param {boolean} shouldBeDir
86 * @param {boolean} shouldIgnore
87 * @return {boolean}
88 */
89
90 }, {
91 key: '_check',
92 value: function _check(absPath, shouldBeDir, shouldIgnore) {
93 var stat;
94 try {
95 stat = _fs2.default.statSync(absPath);
96 } catch (error) {
97 if (shouldIgnore) {
98 this._logger.notice('Ignored: Component file/dir does no exist: \'' + absPath + '\'.');
99 return false;
100 } else {
101 throw error;
102 }
103 }
104
105 var isMismatchType = shouldBeDir && !stat.isDirectory() || !shouldBeDir && !stat.isFile();
106 if (isMismatchType) {
107 if (shouldIgnore) {
108 this._logger.notice(this._mismatchTypeMessage(absPath, shouldBeDir, 'Ignored: '));
109 return false;
110 } else {
111 throw new Error(this._mismatchTypeMessage(absPath, shouldBeDir, ''));
112 }
113 }
114 return true;
115 }
116
117 /**
118 * @param {string} absPath
119 * @param {string} shouldBeDir
120 * @param {string} prefix
121 */
122
123 }, {
124 key: '_mismatchTypeMessage',
125 value: function _mismatchTypeMessage(absPath, shouldBeDir, prefix) {
126 if (shouldBeDir) {
127 return prefix + 'Expected directory, but file found: \'' + absPath + '\'.';
128 }
129 return prefix + 'Expected file, but directory found: \'' + absPath + '\'.';
130 }
131 }]);
132
133 return PageFinder;
134}();
135
136exports.default = PageFinder;
137//# sourceMappingURL=PageFinder.js.map
\No newline at end of file