1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | let debug = require('debug')('mocoolka-tools');
|
7 | const fileTools = require('./file-tools.js');
|
8 | const fs = require('fs');
|
9 | const md5 = require('md5');
|
10 | const _ = require('lodash');
|
11 | const path = require('path');
|
12 | const assert = require('assert');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | let getModuleName = (root)=> (
|
18 | getModuleItem(root, 'name')
|
19 | );
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | let getModuleVersion = (root)=> (
|
25 | getModuleItem(root, 'version')
|
26 | );
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | let getModuleItem = (root, itemName)=> {
|
34 | root = root ;
|
35 | let item = null;
|
36 | try {
|
37 | item = require(path.join(root, 'package.json'))[itemName];
|
38 | } catch (e) {}
|
39 |
|
40 | return item;
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const showDotCount = 500;
|
49 | const showCountCount = 10000;
|
50 | let enumeratedFilesCount = 0;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | const enumerateFilesSync = (rootDir, blackList, targetFileType, checkNodeModules, callback)=> {
|
61 | enumeratedFilesCount = 0;
|
62 | let scannedFileNameHash = [];
|
63 | assert(rootDir);
|
64 | assert(typeof callback === 'function');
|
65 | if (checkNodeModules === undefined)
|
66 | checkNodeModules = false;
|
67 |
|
68 | return enumerateFilesSyncPriv(rootDir, rootDir, blackList, targetFileType,
|
69 | checkNodeModules, scannedFileNameHash, callback);
|
70 | };
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | let fileSyncScanned = (fileName, scannedFileNameHash)=> {
|
79 | let realFileName = process.browser ? fileName : fs.realpathSync(fileName);
|
80 | let fileNameHash = md5(realFileName);
|
81 | if (scannedFileNameHash.indexOf(fileNameHash) >= 0) {
|
82 | return true;
|
83 | } else {
|
84 | scannedFileNameHash.push(fileNameHash);
|
85 | return false;
|
86 | }
|
87 | };
|
88 |
|
89 | const enumerateFilesExcludeDicNames = ['test', 'node_modules', 'coverage'];
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | const enumerateFilesSyncPriv = (currentPath, rootDir, blackList, targetFileType, checkNodeModules,
|
101 | scannedFileNameHash, callback)=> {
|
102 |
|
103 | currentPath = path.resolve(currentPath);
|
104 |
|
105 |
|
106 |
|
107 | if (fileSyncScanned(currentPath, scannedFileNameHash)) {
|
108 | return;
|
109 | }
|
110 | rootDir = path.resolve(rootDir);
|
111 | blackList = Array.isArray(blackList) ? blackList : [];
|
112 | if (!Array.isArray(targetFileType)) targetFileType = [targetFileType];
|
113 |
|
114 |
|
115 | let skipDir = false;
|
116 | blackList.map(function (part) {
|
117 | if (typeof part !== 'string') return;
|
118 | if (currentPath.indexOf(part) >= 0) skipDir = true;
|
119 | });
|
120 |
|
121 | if (skipDir) {
|
122 |
|
123 | debug('*** skipping directory:%s', currentPath);
|
124 | return;
|
125 | }
|
126 |
|
127 | let files = null;
|
128 | try {
|
129 | files = fs.readdirSync(currentPath);
|
130 |
|
131 | } catch (e) {
|
132 | return;
|
133 | }
|
134 |
|
135 |
|
136 | files.map(item=> {
|
137 | if (item.indexOf('.') === 0) return;
|
138 | let child = path.join(currentPath, item);
|
139 | let stats = null;
|
140 | try {
|
141 | stats = fs.statSync(child);
|
142 | } catch (e) {
|
143 | return;
|
144 | }
|
145 |
|
146 | if (stats.isDirectory()) {
|
147 | item = item.toLowerCase();
|
148 | if (enumerateFilesExcludeDicNames.includes(item))
|
149 | return;
|
150 |
|
151 | enumerateFilesSyncPriv(child, rootDir, blackList, targetFileType, checkNodeModules,
|
152 | scannedFileNameHash, callback);
|
153 | } else {
|
154 | let fileType = fileTools.getFileExtName(item);
|
155 | let fileMKType = fileTools.getMKFileExtName(item);
|
156 |
|
157 |
|
158 | if (!targetFileType.includes(fileType) && !targetFileType.includes(fileMKType)) return;
|
159 | let content = fileTools.stripBom(fs.readFileSync(child, 'utf8'));
|
160 | debug('examining file:%s', child);
|
161 | if (checkNodeModules) {
|
162 | enumeratedFilesCount++;
|
163 | if (enumeratedFilesCount % showDotCount === 0) {
|
164 | process.stdout.write('.');
|
165 | if (enumeratedFilesCount % showCountCount === 0) {
|
166 | process.stdout.write(' ' + enumeratedFilesCount.toString() + '\n');
|
167 | }
|
168 | }
|
169 | }
|
170 |
|
171 | callback(content, child);
|
172 | };
|
173 | });
|
174 | if (checkNodeModules) {
|
175 | let moduleRootPaths = resolveDependencies(currentPath, rootDir);
|
176 |
|
177 | if (moduleRootPaths) {
|
178 | moduleRootPaths.map(modulePath=> {
|
179 |
|
180 | if (fileTools.IsExceedMaxDirectoryDepth(modulePath, rootDir)){
|
181 | return;
|
182 | }
|
183 |
|
184 | enumerateFilesSyncPriv(modulePath, rootDir, blackList, targetFileType, checkNodeModules,
|
185 | scannedFileNameHash, callback);
|
186 | });
|
187 | }
|
188 | }
|
189 | };
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | const resolveDependencies = (currentDir, rootDir, moduleRootPaths)=> {
|
199 | moduleRootPaths = moduleRootPaths || [];
|
200 |
|
201 | let packageJson = path.join(currentDir, 'package.json');
|
202 | let deps = null;
|
203 | try {
|
204 |
|
205 | deps = require(packageJson).dependencies;
|
206 | } catch (e) {
|
207 | return null;
|
208 | }
|
209 |
|
210 | if (deps === undefined || !deps) return null;
|
211 | deps = Object.keys(deps);
|
212 | if (deps.length === 0) return null;
|
213 | deps.map(dep=> {
|
214 | let depPath = moduleFileResolve(dep, currentDir, rootDir);
|
215 | if (depPath && moduleRootPaths.indexOf(depPath) < 0) {
|
216 | moduleRootPaths.push(depPath);
|
217 | resolveDependencies(depPath, rootDir, moduleRootPaths);
|
218 | }
|
219 | });
|
220 | moduleRootPaths = _.uniq(_.compact(moduleRootPaths));
|
221 |
|
222 | return moduleRootPaths;
|
223 | };
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | const moduleFileResolve = (depName, currentDir, rootDir)=> {
|
233 |
|
234 | let depPath = null;
|
235 | let stats = null;
|
236 | try {
|
237 | depPath = path.join(currentDir, 'node_modules', depName);
|
238 | stats = fs.statSync(depPath);
|
239 | } catch (e) {
|
240 | stats = null;
|
241 | try {
|
242 | depPath = path.join(rootDir, 'node_modules', depName);
|
243 | stats = fs.statSync(depPath);
|
244 | } catch (e) {
|
245 | return null;
|
246 | }
|
247 | }
|
248 |
|
249 | if (!stats) return null;
|
250 | return fileTools.unsymbolLink(depPath);
|
251 | };
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | const isRootModule = (path)=> {
|
259 | if (!global.MOCOOLKA_GLB) return false;
|
260 | return path === global.MOCOOLKA_GLB.MASTER_ROOT_DIR;
|
261 | };
|
262 |
|
263 | const moduleTools = {
|
264 | getModuleName,
|
265 | getModuleVersion,
|
266 | isRootModule,
|
267 | enumerateFilesSync,
|
268 | };
|
269 |
|
270 | module.exports = moduleTools;
|
271 |
|