@@ -20,39 +20,41 @@ enum Theme {
20
20
// orange: '#fc9867',
21
21
// purple: '#ab9df2',
22
22
// cyan: '#78dce8',
23
- } ;
23
+ }
24
24
25
25
/* ================== logging ================== */
26
26
const cwd = nodeProcess . cwd ( ) ;
27
- const argv = minimist ( nodeProcess . argv . slice ( 2 ) ) . filter ( ( a : string | undefined ) => a ) ;
27
+ const argv = minimist ( nodeProcess . argv . slice ( 2 ) ) ;
28
28
29
29
const logDate = ( ) => chalk . hex ( Theme . date ) ( `[${ new Date ( ) . toISOString ( ) } ]` ) ;
30
30
31
- const debug = ( ...args : string [ ] ) => {
31
+ const debug = ( ...args : any [ ] ) => {
32
32
if ( argv . v || argv . verbose ) {
33
- console . debug ( logDate ( ) , chalk . hex ( Theme . debug ) ( 'DEBUG' ) , ...args ) ;
33
+ console . debug ( logDate ( ) , chalk . hex ( Theme . debug ) ( 'DEBUG' ) , ...args ) ;
34
34
}
35
- }
35
+ } ;
36
36
37
37
const log = ( ...args : string [ ] ) => {
38
- console . log ( logDate ( ) , ...args ) ;
39
- }
38
+ console . log ( logDate ( ) , ...args ) ;
39
+ } ;
40
40
41
41
const logModuleName = chalk . hex ( Theme . moduleName ) ;
42
42
43
43
debug ( 'arguments: ' , argv ) ;
44
44
45
- const moduleNameByPath : { [ key :string ] : string } = { } ;
45
+ const moduleNameByPath : { [ key : string ] : string } = { } ;
46
46
function getModuleNameForPath ( path : string ) : string {
47
47
if ( ! moduleNameByPath [ path ] ) {
48
- moduleNameByPath [ path ] = require ( `${ cwd } /${ path } /package.json` ) . name ;
48
+ moduleNameByPath [ path ] = require ( `${ cwd } /${ path } /package.json` ) . name ;
49
49
}
50
50
51
51
return moduleNameByPath [ path ] ;
52
52
}
53
53
54
54
function getModuleCommandForPath ( path : string ) : string {
55
- const packageJson = require ( `${ cwd } /${ path } /package.json` ) ;
55
+ const packageJson = JSON . parse (
56
+ fs . readFileSync ( `${ cwd } /${ path } /package.json` ) . toString ( )
57
+ ) ;
56
58
if ( packageJson [ 'watch-module' ] && packageJson [ 'watch-module' ] [ 'command' ] ) {
57
59
return packageJson [ 'watch-module' ] [ 'command' ] ;
58
60
}
@@ -64,12 +66,6 @@ function getModuleCommandForPath(path: string): string {
64
66
}
65
67
66
68
/* ================== build ================== */
67
- const changedModules : Set < string > = new Set ( ) ;
68
- function buildAll ( ) : void {
69
- changedModules . forEach ( buildPath ) ;
70
- changedModules . clear ( ) ;
71
- }
72
-
73
69
function buildPath ( path : string ) : void {
74
70
const moduleName = getModuleNameForPath ( path ) ;
75
71
log ( logModuleName ( moduleName ) , `Change detected` ) ;
@@ -92,25 +88,35 @@ function buildPath(path: string): void {
92
88
}
93
89
debug ( `Create module directory for "${ moduleName } " and copy files` ) ;
94
90
const modulePath = `${ cwd } /node_modules/${ moduleName } ` ;
95
- return fs . ensureDir ( modulePath )
91
+ return fs
92
+ . ensureDir ( modulePath )
96
93
. then ( ( ) =>
97
94
fs . copy ( path , modulePath , {
98
- filter : ( src : string , dest : string ) => {
95
+ filter : ( src : string , dest : string ) => {
99
96
const srcAppendSlash = `${ src } /` ;
100
97
101
98
return (
102
99
! srcAppendSlash . startsWith ( `${ path } /node_modules/` ) &&
103
100
! srcAppendSlash . startsWith ( `${ path } /.git/` )
104
101
) ;
105
- }
102
+ } ,
106
103
} )
107
104
)
108
105
. then ( ( ) => {
109
- log ( logModuleName ( moduleName ) , chalk . hex ( Theme . success ) ( 'build done' ) ) ;
106
+ log (
107
+ logModuleName ( moduleName ) ,
108
+ chalk . hex ( Theme . success ) ( 'build done' )
109
+ ) ;
110
110
} )
111
111
. catch ( console . error ) ;
112
112
}
113
- )
113
+ ) ;
114
+ }
115
+
116
+ const changedModules : Set < string > = new Set ( ) ;
117
+ function buildAll ( ) : void {
118
+ changedModules . forEach ( buildPath ) ;
119
+ changedModules . clear ( ) ;
114
120
}
115
121
116
122
/* ================== debounce & events ================== */
@@ -134,9 +140,15 @@ function main(): void {
134
140
135
141
// One-liner for current directory, ignores .dotfiles
136
142
chokidar
137
- . watch ( srcPaths , { ignored : / ( ^ | [ \ /\\ ] ) \. [ ^ \. \ /] / } )
143
+ . watch ( srcPaths , { ignored : / ( ^ | [ / \\ ] ) \. [ ^ . / ] / } )
138
144
. on ( 'all' , ( event : any , path : string ) => {
139
- const modulePath = modulePaths . find ( ( tmpPath : string ) => path . startsWith ( `${ tmpPath } /` ) ) ;
145
+ const modulePath = modulePaths . find ( ( tmpPath : string ) =>
146
+ path . startsWith ( `${ tmpPath } /` )
147
+ ) ;
148
+
149
+ if ( ! modulePath ) {
150
+ throw new Error ( 'Unable to find module path. This should not happen.' ) ;
151
+ }
140
152
141
153
onChange ( modulePath ) ;
142
154
} ) ;
0 commit comments