code --wait should return control when closing window (fixes #18199)

This commit is contained in:
Benjamin Pasero
2017-07-03 16:30:05 +02:00
parent bed46e2d7a
commit 5c6a7d6e16
5 changed files with 70 additions and 13 deletions

View File

@@ -11,6 +11,10 @@ import { ParsedArgs } from 'vs/platform/environment/common/environment';
import product from 'vs/platform/node/product';
import pkg from 'vs/platform/node/package';
import * as fs from 'fs';
import * as paths from 'path';
import * as os from 'os';
function shouldSpawnCliProcess(argv: ParsedArgs): boolean {
return argv['list-extensions'] || !!argv['install-extension'] || !!argv['uninstall-extension'];
}
@@ -49,9 +53,34 @@ export function main(argv: string[]): TPromise<void> {
env['ELECTRON_ENABLE_LOGGING'] = '1';
}
// If we are started with --wait create a random temporary file
// and pass it over to the starting instance. We can use this file
// to wait for it to be deleted to monitor that the edited file
// is closed and then exit the waiting process.
let waitMarkerFilePath: string;
if (args.wait) {
let waitMarkerError: Error;
const randomTmpFile = paths.join(os.tmpdir(), Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10));
try {
fs.writeFileSync(randomTmpFile, '');
waitMarkerFilePath = randomTmpFile;
argv.push('--waitMarkerFilePath', waitMarkerFilePath);
} catch (error) {
waitMarkerError = error;
}
if (args.verbose) {
if (waitMarkerError) {
console.error(`Failed to create marker file for --wait: ${waitMarkerError.toString()}`);
} else {
console.log(`Marker file for --wait created: ${waitMarkerFilePath}`);
}
}
}
const options = {
detached: true,
env,
env
};
if (!args.verbose) {
@@ -65,9 +94,27 @@ export function main(argv: string[]): TPromise<void> {
child.stderr.on('data', (data: Buffer) => console.log(data.toString('utf8').trim()));
}
if (args.wait || args.verbose) {
if (args.verbose) {
return new TPromise<void>(c => child.once('exit', () => c(null)));
}
if (args.wait && waitMarkerFilePath) {
return new TPromise<void>(c => {
// Complete when process exits
child.once('exit', () => c(null));
// Complete when wait marker file is deleted
const interval = setInterval(() => {
fs.exists(waitMarkerFilePath, exists => {
if (!exists) {
clearInterval(interval);
c(null);
}
});
}, 1000);
});
}
}
return TPromise.as(null);