Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/environment/node/userDataPath.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { homedir } from 'os';
7
import { NativeParsedArgs } from '../common/argv.js';
8
9
// This file used to be a pure JS file and was always
10
// importing `path` from node.js even though we ship
11
// our own version of the library and prefer to use
12
// that.
13
// However, resolution of user-data-path is critical
14
// and while our version of `path` is a copy of node.js
15
// one, you never know. As such, preserve the use of
16
// the built-in `path` lib for the time being.
17
// eslint-disable-next-line local/code-import-patterns
18
import { resolve, isAbsolute, join } from 'path';
19
20
const cwd = process.env['VSCODE_CWD'] || process.cwd();
21
22
/**
23
* Returns the user data path to use with some rules:
24
* - respect portable mode
25
* - respect VSCODE_APPDATA environment variable
26
* - respect --user-data-dir CLI argument
27
*/
28
export function getUserDataPath(cliArgs: NativeParsedArgs, productName: string): string {
29
const userDataPath = doGetUserDataPath(cliArgs, productName);
30
const pathsToResolve = [userDataPath];
31
32
// If the user-data-path is not absolute, make
33
// sure to resolve it against the passed in
34
// current working directory. We cannot use the
35
// node.js `path.resolve()` logic because it will
36
// not pick up our `VSCODE_CWD` environment variable
37
// (https://github.com/microsoft/vscode/issues/120269)
38
if (!isAbsolute(userDataPath)) {
39
pathsToResolve.unshift(cwd);
40
}
41
42
return resolve(...pathsToResolve);
43
}
44
45
function doGetUserDataPath(cliArgs: NativeParsedArgs, productName: string): string {
46
47
// 0. Running out of sources has a fixed productName
48
if (process.env['VSCODE_DEV']) {
49
productName = 'code-oss-dev';
50
}
51
52
// 1. Support portable mode
53
const portablePath = process.env['VSCODE_PORTABLE'];
54
if (portablePath) {
55
return join(portablePath, 'user-data');
56
}
57
58
// 2. Support global VSCODE_APPDATA environment variable
59
let appDataPath = process.env['VSCODE_APPDATA'];
60
if (appDataPath) {
61
return join(appDataPath, productName);
62
}
63
64
// With Electron>=13 --user-data-dir switch will be propagated to
65
// all processes https://github.com/electron/electron/blob/1897b14af36a02e9aa7e4d814159303441548251/shell/browser/electron_browser_client.cc#L546-L553
66
// Check VSCODE_PORTABLE and VSCODE_APPDATA before this case to get correct values.
67
// 3. Support explicit --user-data-dir
68
const cliPath = cliArgs['user-data-dir'];
69
if (cliPath) {
70
return cliPath;
71
}
72
73
// 4. Otherwise check per platform
74
switch (process.platform) {
75
case 'win32':
76
appDataPath = process.env['APPDATA'];
77
if (!appDataPath) {
78
const userProfile = process.env['USERPROFILE'];
79
if (typeof userProfile !== 'string') {
80
throw new Error('Windows: Unexpected undefined %USERPROFILE% environment variable');
81
}
82
83
appDataPath = join(userProfile, 'AppData', 'Roaming');
84
}
85
break;
86
case 'darwin':
87
appDataPath = join(homedir(), 'Library', 'Application Support');
88
break;
89
case 'linux':
90
appDataPath = process.env['XDG_CONFIG_HOME'] || join(homedir(), '.config');
91
break;
92
default:
93
throw new Error('Platform not supported');
94
}
95
96
return join(appDataPath, productName);
97
}
98
99