@@ -36,46 +36,81 @@ export interface Result {
36
36
export function processFiles ( files :string [ ] , opts :Options ) :Promise < ResultMap > {
37
37
"use strict" ;
38
38
39
- var result :ResultMap = { } ;
39
+ var resultMap :ResultMap = { } ;
40
40
var promises = files . map ( fileName => {
41
41
if ( ! fs . existsSync ( fileName ) ) {
42
42
console . error ( fileName + " is not exists. process abort." ) ;
43
43
process . exit ( 1 ) ;
44
44
return ;
45
45
}
46
+
46
47
var content = fs . readFileSync ( fileName ) . toString ( ) ;
48
+ return processString ( fileName , content , opts ) ;
49
+ } ) ;
50
+ return Promise . all ( promises ) . then ( resultList => {
51
+ resultList . forEach ( result => {
52
+ resultMap [ result . fileName ] = result ;
53
+ } ) ;
54
+ return resultMap ;
55
+ } ) ;
56
+ }
47
57
48
- var options = utils . createDefaultFormatCodeOptions ( ) ;
49
- var optGenPromises :any [ ] = [ ] ;
50
- if ( opts . tsfmt ) {
51
- optGenPromises . push ( base . makeFormatCodeOptions ( fileName , options ) ) ;
52
- }
53
- if ( opts . editorconfig ) {
54
- optGenPromises . push ( editorconfig . makeFormatCodeOptions ( fileName , options ) ) ;
55
- }
56
- if ( opts . tslint ) {
57
- optGenPromises . push ( tslintjson . makeFormatCodeOptions ( fileName , options ) ) ;
58
- }
59
- return Promise
60
- . all ( optGenPromises )
61
- . then ( ( ) => {
62
- var formattedCode = formatter ( content , options ) ;
63
- // TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
64
- if ( opts && opts . replace ) {
65
- if ( content !== formattedCode ) {
66
- fs . writeFileSync ( fileName , formattedCode ) ;
67
- console . log ( "replaced " + fileName ) ;
68
- }
69
- } else if ( opts && ! opts . dryRun ) {
70
- console . log ( formattedCode ) ;
71
- }
72
- result [ fileName ] = {
73
- fileName : fileName ,
74
- options : options ,
75
- src : content ,
76
- dest : formattedCode
77
- } ;
78
- } ) ;
58
+ export function processStream ( fileName :string , input :NodeJS . ReadableStream , opts :Options ) :Promise < Result > {
59
+ "use strict" ;
60
+
61
+ input . setEncoding ( "utf8" ) ;
62
+
63
+ var promise = new Promise < string > ( ( resolve , reject ) => {
64
+ var fragment = "" ;
65
+ input . on ( "data" , ( chunk :string ) => {
66
+ if ( chunk === "" ) {
67
+ return ;
68
+ }
69
+ fragment += chunk ;
70
+ } ) ;
71
+
72
+ input . on ( "end" , ( ) => {
73
+ resolve ( fragment ) ;
74
+ } ) ;
79
75
} ) ;
80
- return Promise . all ( promises ) . then ( ( ) => result ) ;
76
+ return promise . then ( content => processString ( fileName , content , opts ) ) ;
77
+ }
78
+
79
+ export function processString ( fileName :string , content :string , opts :Options ) :Promise < Result > {
80
+ "use strict" ;
81
+
82
+ var options = utils . createDefaultFormatCodeOptions ( ) ;
83
+ var optGenPromises :( ts . FormatCodeOptions | Promise < ts . FormatCodeOptions > ) [ ] = [ ] ;
84
+ if ( opts . tsfmt ) {
85
+ optGenPromises . push ( base . makeFormatCodeOptions ( fileName , options ) ) ;
86
+ }
87
+ if ( opts . editorconfig ) {
88
+ optGenPromises . push ( editorconfig . makeFormatCodeOptions ( fileName , options ) ) ;
89
+ }
90
+ if ( opts . tslint ) {
91
+ optGenPromises . push ( tslintjson . makeFormatCodeOptions ( fileName , options ) ) ;
92
+ }
93
+
94
+ return Promise
95
+ . all ( optGenPromises )
96
+ . then ( ( ) => {
97
+ var formattedCode = formatter ( content , options ) ;
98
+ // TODO replace newline code. NewLineCharacter params affect to only "new" newline. maybe.
99
+ if ( opts && opts . replace ) {
100
+ if ( content !== formattedCode ) {
101
+ fs . writeFileSync ( fileName , formattedCode ) ;
102
+ console . log ( "replaced " + fileName ) ;
103
+ }
104
+ } else if ( opts && ! opts . dryRun ) {
105
+ console . log ( formattedCode ) ;
106
+ }
107
+
108
+ var result :Result = {
109
+ fileName : fileName ,
110
+ options : options ,
111
+ src : content ,
112
+ dest : formattedCode
113
+ } ;
114
+ return Promise . resolve ( result ) ;
115
+ } ) ;
81
116
}
0 commit comments