1
- var _ = require ( 'lodash' ) ,
2
- path = require ( 'path' ) ,
3
- chokidar = require ( 'chokidar' ) ,
4
- Promise = require ( 'bluebird' ) ,
5
- cache = { } ,
6
- glob = Promise . promisify ( require ( 'glob' ) ) ,
7
- fs = require ( 'graceful-fs' ) ,
8
- lstat = Promise . promisify ( fs . lstat ) ,
9
- readFile = Promise . promisify ( fs . readFile ) ,
10
- provideRegExp = / g o o g \. p r o v i d e \( ( [ ' " ] ) ( ( [ ^ . ) ] + ) [ ^ ) ] * ) \1\) / g;
1
+ const _ = require ( 'lodash' ) ;
2
+ const path = require ( 'path' ) ;
3
+ const chokidar = require ( 'chokidar' ) ;
4
+ const Promise = require ( 'bluebird' ) ;
5
+ const glob = Promise . promisify ( require ( 'glob' ) ) ;
6
+ const fs = require ( 'graceful-fs' ) ;
11
7
12
- /**
13
- * Fulfills in an object wich maps namespaces to file paths found in given
14
- * directories.
15
- *
16
- * @param {string[] } directories Directories to be processed
17
- * @param {boolean } watch Watch for changes is mapped files to invalidate cache
18
- * @param {string } fileExt Searched file extensions
19
- * @returns {Promise }
20
- */
21
- module . exports = function ( directories , watch , fileExt ) {
22
- return Promise . map ( directories , function ( dir ) {
23
- return resolveAndCacheDirectory ( dir , watch , fileExt )
24
- } ) . then ( function ( results ) {
25
- return _ . assign . apply ( _ , results ) ;
26
- } )
27
- } ;
8
+ const cache = { } ;
9
+ const lstat = Promise . promisify ( fs . lstat ) ;
10
+ const readFile = Promise . promisify ( fs . readFile ) ;
11
+ const provideRegExp = / g o o g \. p r o v i d e \( ( [ ' " ] ) ( ( [ ^ . ) ] + ) [ ^ ) ] * ) \1\) / g;
28
12
29
13
/**
30
14
* Promisified watch
@@ -35,14 +19,39 @@ module.exports = function (directories, watch, fileExt) {
35
19
* @returns {Promise }
36
20
*/
37
21
function createWatchPromise ( directory ) {
38
- return new Promise ( function ( resolve , reject ) {
39
- var watcher = chokidar . watch ( directory )
40
- . on ( 'ready' , function ( ) {
41
- watcher . on ( 'all' , function ( ) {
42
- delete cache [ directory ] ;
43
- } ) ;
44
- resolve ( watcher ) ;
22
+ return new Promise ( resolve => {
23
+ const watcher = chokidar . watch ( directory ) . on ( 'ready' , ( ) => {
24
+ watcher . on ( 'all' , ( ) => {
25
+ delete cache [ directory ] ;
45
26
} ) ;
27
+ resolve ( watcher ) ;
28
+ } ) ;
29
+ } ) ;
30
+ }
31
+
32
+ /**
33
+ * Scans the given file path for occurences of `goog.provide()` and fulfills
34
+ * in an object wich mapps each namespace to the file path
35
+ *
36
+ * @param {string } filePath
37
+ * @returns {Promise }
38
+ */
39
+ function findProvideCalls ( filePath ) {
40
+ return lstat ( filePath ) . then ( stats => {
41
+ if ( ! stats . isFile ( ) ) {
42
+ return { } ;
43
+ }
44
+
45
+ return readFile ( filePath ) . then ( fileContent => {
46
+ const result = { } ;
47
+
48
+ let matches = null ;
49
+ // eslint-disable-next-line no-cond-assign
50
+ while ( ( matches = provideRegExp . exec ( fileContent ) ) !== null ) {
51
+ result [ matches [ 2 ] ] = filePath ;
52
+ }
53
+ return result ;
54
+ } ) ;
46
55
} ) ;
47
56
}
48
57
@@ -57,44 +66,32 @@ function createWatchPromise(directory) {
57
66
* @returns {Promise }
58
67
*/
59
68
function resolveAndCacheDirectory ( directory , watch , fileExt ) {
60
-
61
69
if ( cache [ directory ] ) {
62
70
return cache [ directory ] ;
63
71
}
64
72
65
- cache [ directory ] = ( watch ? createWatchPromise ( directory ) : Promise . resolve ( ) )
66
- . then ( function ( ) {
67
- return glob ( path . join ( directory , '**' , '*' + fileExt ) ) ;
68
- } )
69
- . map ( function ( filePath ) {
70
- return findProvideCalls ( filePath ) ;
71
- } )
72
- . then ( function ( results ) {
73
- return _ . assign . apply ( _ , results ) ;
74
- } ) ;
73
+ cache [ directory ] = ( watch
74
+ ? createWatchPromise ( directory )
75
+ : Promise . resolve ( )
76
+ )
77
+ . then ( ( ) => glob ( path . join ( directory , '**' , `*${ fileExt } ` ) ) )
78
+ . map ( filePath => findProvideCalls ( filePath ) )
79
+ . then ( results => _ . assign ( ...results ) ) ;
75
80
76
81
return cache [ directory ] ;
77
82
}
78
83
79
84
/**
80
- * Scans the given file path for occurences of `goog.provide()` and fulfills
81
- * in an object wich mapps each namespace to the file path
85
+ * Fulfills in an object wich maps namespaces to file paths found in given
86
+ * directories.
82
87
*
83
- * @param {string } filePath
88
+ * @param {string[] } directories Directories to be processed
89
+ * @param {boolean } watch Watch for changes is mapped files to invalidate cache
90
+ * @param {string } fileExt Searched file extensions
84
91
* @returns {Promise }
85
92
*/
86
- function findProvideCalls ( filePath ) {
87
- return lstat ( filePath ) . then ( function ( stats ) {
88
- if ( ! stats . isFile ( ) ) {
89
- return { } ;
90
- }
91
-
92
- return readFile ( filePath ) . then ( function ( fileContent ) {
93
- var result = { } ;
94
- while ( matches = provideRegExp . exec ( fileContent ) ) {
95
- result [ matches [ 2 ] ] = filePath ;
96
- }
97
- return result ;
98
- } ) ;
99
- } ) ;
100
- }
93
+ module . exports = function dependencyMapBuilder ( directories , watch , fileExt ) {
94
+ return Promise . map ( directories , dir =>
95
+ resolveAndCacheDirectory ( dir , watch , fileExt ) ,
96
+ ) . then ( results => _ . assign ( ...results ) ) ;
97
+ } ;
0 commit comments