forked from atom/atom
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin-powershell.js
More file actions
52 lines (48 loc) · 1.58 KB
/
win-powershell.js
File metadata and controls
52 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let powershellPath;
const path = require('path');
const Spawner = require('./spawner');
if (process.env.SystemRoot) {
const system32Path = path.join(process.env.SystemRoot, 'System32');
powershellPath = path.join(
system32Path,
'WindowsPowerShell',
'v1.0',
'powershell.exe'
);
} else {
powershellPath = 'powershell.exe';
}
// Spawn powershell.exe and callback when it completes
const spawnPowershell = function(args, callback) {
// Set encoding and execute the command, capture the output, and return it
// via .NET's console in order to have consistent UTF-8 encoding.
// See http://stackoverflow.com/questions/22349139/utf-8-output-from-powershell
// to address https://github.com/atom/atom/issues/5063
args[0] = `\
[Console]::OutputEncoding=[System.Text.Encoding]::UTF8
$output=${args[0]}
[Console]::WriteLine($output)\
`;
args.unshift('-command');
args.unshift('RemoteSigned');
args.unshift('-ExecutionPolicy');
args.unshift('-noprofile');
Spawner.spawn(powershellPath, args, callback);
};
// Get the user's PATH environment variable registry value.
//
// * `callback` The {Function} to call after registry operation is done.
// It will be invoked with the same arguments provided by {Spawner.spawn}.
//
// Returns the user's path {String}.
exports.getPath = callback =>
spawnPowershell(
["[environment]::GetEnvironmentVariable('Path','User')"],
function(error, stdout) {
if (error != null) {
return callback(error);
}
const pathOutput = stdout.replace(/^\s+|\s+$/g, '');
return callback(null, pathOutput);
}
);