@@ -3,17 +3,24 @@ import fs from "fs";
3
3
import path from "path" ;
4
4
import glob from "glob" ;
5
5
import chalk from "chalk" ;
6
+ import chokidar from "chokidar" ;
6
7
7
8
import { Options , fileToClassNames } from "./sass" ;
8
- import { classNamesToTypeDefinitions , ExportType } from "./typescript" ;
9
+ import {
10
+ classNamesToTypeDefinitions ,
11
+ ExportType ,
12
+ getTypeDefinitionPath
13
+ } from "./typescript" ;
9
14
10
15
interface MainOptions extends Options {
11
16
exportType : ExportType ;
17
+ watch : boolean ;
12
18
}
13
19
14
20
const error = ( message : string ) => console . log ( chalk . red ( `[ERROR] ${ message } ` ) ) ;
15
21
const warn = ( message : string ) => console . log ( chalk . yellowBright ( `${ message } ` ) ) ;
16
22
const notice = ( message : string ) => console . log ( chalk . gray ( `${ message } ` ) ) ;
23
+ const info = ( message : string ) => console . log ( chalk . blueBright ( `${ message } ` ) ) ;
17
24
const success = ( message : string ) => console . log ( chalk . green ( message ) ) ;
18
25
19
26
export const main = ( pattern : string , options : MainOptions ) : void => {
@@ -32,6 +39,41 @@ export const main = (pattern: string, options: MainOptions): void => {
32
39
pattern = path . resolve ( pattern , "**/*.scss" ) ;
33
40
}
34
41
42
+ if ( options . watch ) {
43
+ watch ( pattern , options ) ;
44
+ } else {
45
+ generate ( pattern , options ) ;
46
+ }
47
+ } ;
48
+
49
+ /**
50
+ * Watch a file glob and generate the corresponding types.
51
+ *
52
+ * @param pattern the file pattern to watch for file changes or additions
53
+ * @param options the CLI options
54
+ */
55
+ const watch = ( pattern : string , options : MainOptions ) : void => {
56
+ success ( "Watching files..." ) ;
57
+
58
+ chokidar
59
+ . watch ( pattern )
60
+ . on ( "change" , path => {
61
+ info ( `[CHANGED] ${ path } ` ) ;
62
+ writeFile ( path , options ) ;
63
+ } )
64
+ . on ( "add" , path => {
65
+ info ( `[ADDED] ${ path } ` ) ;
66
+ writeFile ( path , options ) ;
67
+ } ) ;
68
+ } ;
69
+
70
+ /**
71
+ * Given a file glob generate the corresponding types once.
72
+ *
73
+ * @param pattern the file pattern to generate type definitions for
74
+ * @param options the CLI options
75
+ */
76
+ const generate = ( pattern : string , options : MainOptions ) : void => {
35
77
// Find all the files that match the provied pattern.
36
78
const files = glob . sync ( pattern ) ;
37
79
@@ -50,28 +92,35 @@ export const main = (pattern: string, options: MainOptions): void => {
50
92
51
93
success ( `Found ${ files . length } files. Generating type defintions...` ) ;
52
94
53
- for ( let index in files ) {
54
- const file = files [ index ] ;
55
-
56
- fileToClassNames ( file , options )
57
- . then ( classNames => {
58
- const typeDefinition = classNamesToTypeDefinitions (
59
- classNames ,
60
- options . exportType
61
- ) ;
62
- const path = `${ file } .d.ts` ;
63
-
64
- if ( ! typeDefinition ) {
65
- notice ( `No types generated for ${ file } ` ) ;
66
- return null ;
67
- }
68
-
69
- fs . writeFileSync ( path , typeDefinition ) ;
70
- success ( `Generated type defintions: ${ path } ` ) ;
71
- } )
72
- . catch ( ( { message, file, line, column } : SassError ) => {
73
- const location = file ? `(${ file } [${ line } :${ column } ])` : "" ;
74
- error ( `${ message } ${ location } ` ) ;
75
- } ) ;
76
- }
95
+ files . map ( file => writeFile ( file , options ) ) ;
96
+ } ;
97
+
98
+ /**
99
+ * Given a single file generate the proper types.
100
+ *
101
+ * @param file the SCSS file to generate types for
102
+ * @param options the CLI options
103
+ */
104
+ const writeFile = ( file : string , options : MainOptions ) : void => {
105
+ fileToClassNames ( file , options )
106
+ . then ( classNames => {
107
+ const typeDefinition = classNamesToTypeDefinitions (
108
+ classNames ,
109
+ options . exportType
110
+ ) ;
111
+
112
+ if ( ! typeDefinition ) {
113
+ notice ( `[NO GENERATED TYPES] ${ file } ` ) ;
114
+ return null ;
115
+ }
116
+
117
+ const path = getTypeDefinitionPath ( file ) ;
118
+
119
+ fs . writeFileSync ( path , typeDefinition ) ;
120
+ success ( `[GENERATED TYPES] ${ path } ` ) ;
121
+ } )
122
+ . catch ( ( { message, file, line, column } : SassError ) => {
123
+ const location = file ? `(${ file } [${ line } :${ column } ])` : "" ;
124
+ error ( `${ message } ${ location } ` ) ;
125
+ } ) ;
77
126
} ;
0 commit comments