3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import { createWriteStream } from 'fs' ;
7
6
import { tmpdir } from 'os' ;
7
+ import { Queue } from 'vs/base/common/async' ;
8
8
import { randomPath } from 'vs/base/common/extpath' ;
9
+ import { Promises } from 'vs/base/node/pfs' ;
9
10
import { resolveTerminalEncoding } from 'vs/base/node/terminalEncoding' ;
10
11
11
12
export function hasStdinWithoutTty ( ) {
@@ -38,10 +39,6 @@ export function getStdinFilePath(): string {
38
39
}
39
40
40
41
export async function readFromStdin ( targetPath : string , verbose : boolean ) : Promise < void > {
41
-
42
- // open tmp file for writing
43
- const stdinFileStream = createWriteStream ( targetPath ) ;
44
-
45
42
let encoding = await resolveTerminalEncoding ( verbose ) ;
46
43
47
44
const iconv = await import ( '@vscode/iconv-lite-umd' ) ;
@@ -51,15 +48,21 @@ export async function readFromStdin(targetPath: string, verbose: boolean): Promi
51
48
}
52
49
53
50
// Pipe into tmp file using terminals encoding
51
+ // Use a `Queue` to be able to use `appendFile`
52
+ // which helps file watchers to be aware of the
53
+ // changes because each append closes the underlying
54
+ // file descriptor.
55
+ // (https://github.com/microsoft/vscode/issues/148952)
54
56
const decoder = iconv . getDecoder ( encoding ) ;
55
- process . stdin . on ( 'data' , chunk => stdinFileStream . write ( decoder . write ( chunk ) ) ) ;
57
+ const appendFileQueue = new Queue ( ) ;
58
+ process . stdin . on ( 'data' , chunk => {
59
+ const chunkStr = decoder . write ( chunk ) ;
60
+ appendFileQueue . queue ( ( ) => Promises . appendFile ( targetPath , chunkStr ) ) ;
61
+ } ) ;
56
62
process . stdin . on ( 'end' , ( ) => {
57
63
const end = decoder . end ( ) ;
58
64
if ( typeof end === 'string' ) {
59
- stdinFileStream . write ( end ) ;
65
+ appendFileQueue . queue ( ( ) => Promises . appendFile ( targetPath , end ) ) ;
60
66
}
61
- stdinFileStream . end ( ) ;
62
67
} ) ;
63
- process . stdin . on ( 'error' , error => stdinFileStream . destroy ( error ) ) ;
64
- process . stdin . on ( 'close' , ( ) => stdinFileStream . close ( ) ) ;
65
68
}
0 commit comments