@@ -61,38 +61,51 @@ class RunFormatOnSave {
6161 const overReactFormatRangeIsValid = dependencyHasValidMinVersion ( this . overReactFormatKey , this . minOverReactFormatVersion , readFileSync ( `${ this . projectDir } /pubspec.yaml` , 'utf-8' ) , true ) ;
6262
6363 if ( pubspecContainsOverReactFormat && ! overReactFormatRangeIsValid ) {
64- this . showChannelMessage ( `OverReact Format range is not acceptable. Bump the minimum to 3.1.0 to use OverReact Format on Save.` ) ;
64+ this . showChannelMessage ( 'over_react_format range is not compatible with OverReact Format on Save.'
65+ + ' Bump the minimum to 3.1.0 to use OverReact Format on Save. Defaulting to using dartfmt instead.' ) ;
6566 }
6667 this . useOverReactFormat = pubspecContainsOverReactFormat && overReactFormatRangeIsValid ;
6768 }
6869 // No else condition because there's no penalty for the project not being a Dart project.
6970 // The `onDocumentSave` command will just be short-circuited if it is run on non-Dart files.
7071 }
7172
72- buildCommand ( fileName : string ) {
73- let command :string ;
74-
73+ startProcess ( fileName : string ) : process . ChildProcess {
7574 const customLineLength = this . config . get < Number > ( 'customLineLength' , 0 ) ;
7675 const shouldDetectLineLength = this . config . get < Boolean > ( 'detectCustomLineLength' ) ;
7776 const shouldUseCustomLineLength = customLineLength > 0 ;
77+ let executable : "pub" | "dartfmt" ;
78+ const args : Array < string > = [ ] ;
79+
7880
7981 if ( shouldUseCustomLineLength && shouldDetectLineLength ) {
8082 this . showChannelMessage ( `Both a custom line-length value and detectCustomLineLength set to true. Skipping line-length detection.` ) ;
8183 }
8284
8385 if ( this . useOverReactFormat ) {
86+ executable = 'pub'
87+ args . push ( 'run' , 'over_react_format' , fileName ) ;
8488 if ( shouldUseCustomLineLength ) {
85- command = `pub run over_react_format ${ fileName } -l ${ customLineLength } `;
89+ args . push ( '-l' , ` ${ customLineLength } `) ;
8690 } else {
87- const detectLineLengthFlag = shouldDetectLineLength && ! shouldUseCustomLineLength ? "--detect-line-length" : "" ;
88- command = `pub run over_react_format ${ fileName } -p ${ this . projectDir } ${ detectLineLengthFlag } ` ;
91+ args . push ( '-p' , this . projectDir ) ;
92+
93+ if ( shouldDetectLineLength ) {
94+ args . push ( '--detect-line-length' ) ;
95+ }
8996 }
9097 } else {
9198 // TODO add logic to detect line-length from dart_dev's config.dart
92- command = `dartfmt -w ${ fileName } ${ shouldUseCustomLineLength ? `-l ${ customLineLength } ` : '' } ` ;
99+ executable = 'dartfmt' ;
100+ args . push ( '-w' , fileName ) ;
101+ if ( shouldUseCustomLineLength ) {
102+ args . push ( '-l' , `${ customLineLength } ` ) ;
103+ }
93104 }
94105
95- return command ;
106+ const command = `${ executable } ${ args . join ( ' ' ) } ` ;
107+ this . showChannelMessage ( command ) ;
108+ return process . execFile ( executable , args , { cwd : this . projectDir } ) ;
96109 }
97110
98111 loadConfig ( ) {
@@ -130,11 +143,7 @@ class RunFormatOnSave {
130143
131144 this . showChannelMessage ( `Running ${ this . useOverReactFormat ? 'OverReact Format' : 'dartfmt' } ...` ) ;
132145
133- const command = this . buildCommand ( document . fileName ) ;
134-
135- this . showChannelMessage ( command ) ;
136-
137- const child :process . ChildProcess = process . exec ( command , { cwd : this . projectDir } ) ;
146+ const child :process . ChildProcess = this . startProcess ( document . fileName ) ;
138147
139148 child . stdout ! . on ( 'data' , data => this . channel . append ( data . toString ( ) ) ) ;
140149 child . stderr ! . on ( 'data' , data => this . channel . append ( data . toString ( ) ) ) ;
0 commit comments