Allow to save files that need user elevation (fixes #1614) (#40107)

This commit is contained in:
Benjamin Pasero
2017-12-12 16:03:25 +01:00
committed by GitHub
parent a4e30c1223
commit 0ef13b6d54
14 changed files with 195 additions and 22 deletions

View File

@@ -17,6 +17,7 @@ import { whenDeleted } from 'vs/base/node/pfs';
import { findFreePort } from 'vs/base/node/ports';
import { resolveTerminalEncoding } from 'vs/base/node/encoding';
import * as iconv from 'iconv-lite';
import { writeFileAndFlushSync } from 'vs/base/node/extfs';
function shouldSpawnCliProcess(argv: ParsedArgs): boolean {
return !!argv['install-source']
@@ -55,6 +56,31 @@ export async function main(argv: string[]): TPromise<any> {
return mainCli.then(cli => cli.main(args));
}
// Write Elevated
else if (args['write-elevated-helper']) {
const source = args._[0];
const target = args._[1];
// Validate
if (
!source || !target || source === target || // make sure source and target are provided and are not the same
!paths.isAbsolute(source) || !paths.isAbsolute(target) || // make sure both source and target are absolute paths
!fs.existsSync(source) || !fs.statSync(source).isFile() || // make sure source exists as file
!fs.existsSync(target) || !fs.statSync(target).isFile() // make sure target exists as file
) {
return TPromise.wrapError(new Error('Using --write-elevated-helper with invalid arguments.'));
}
// Write source to target
try {
writeFileAndFlushSync(target, fs.readFileSync(source));
} catch (error) {
return TPromise.wrapError(new Error(`Using --write-elevated-helper resulted in an error: ${error}`));
}
return TPromise.as(null);
}
// Just Code
else {
const env = assign({}, process.env, {