Skip to content

Commit bd79955

Browse files
committed
portable mode
1 parent d631cb8 commit bd79955

4 files changed

Lines changed: 106 additions & 12 deletions

File tree

src/cli.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
Error.stackTraceLimit = 100; // increase number of stack frames (from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
7+
8+
const fs = require('fs');
9+
const path = require('path');
10+
const product = require('../product.json');
11+
const appRoot = path.dirname(__dirname);
12+
13+
function getApplicationPath() {
14+
if (process.env['VSCODE_DEV']) {
15+
return appRoot;
16+
} else if (process.platform === 'darwin') {
17+
return path.dirname(path.dirname(path.dirname(appRoot)));
18+
} else {
19+
return path.dirname(path.dirname(appRoot));
20+
}
21+
}
22+
23+
function getPortableDataPath() {
24+
return path.join(path.dirname(getApplicationPath()), product.portable);
25+
}
26+
27+
if (product.portable) {
28+
const portablePath = getPortableDataPath();
29+
try { fs.mkdirSync(portablePath); } catch (err) { if (err.code !== 'EEXIST') { throw err; } }
30+
31+
const tmpdir = path.join(portablePath, 'tmp');
32+
try { fs.mkdirSync(tmpdir); } catch (err) { if (err.code !== 'EEXIST') { throw err; } }
33+
34+
process.env[process.platform === 'win32' ? 'TEMP' : 'TMPDIR'] = tmpdir;
35+
}
36+
637
//#region Add support for using node_modules.asar
738
(function () {
839
const path = require('path');

src/main.js

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,43 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7-
let perf = require('./vs/base/common/performance');
7+
const perf = require('./vs/base/common/performance');
88
perf.mark('main:started');
99

1010
// Perf measurements
1111
global.perfStartTime = Date.now();
1212

1313
Error.stackTraceLimit = 100; // increase number of stack frames (from 10, https://github.com/v8/v8/wiki/Stack-Trace-API)
1414

15+
const fs = require('fs');
16+
const path = require('path');
17+
const product = require('../product.json');
18+
const appRoot = path.dirname(__dirname);
19+
20+
function getApplicationPath() {
21+
if (process.env['VSCODE_DEV']) {
22+
return appRoot;
23+
} else if (process.platform === 'darwin') {
24+
return path.dirname(path.dirname(path.dirname(appRoot)));
25+
} else {
26+
return path.dirname(path.dirname(appRoot));
27+
}
28+
}
29+
30+
function getPortableDataPath() {
31+
return path.join(path.dirname(getApplicationPath()), product.portable);
32+
}
33+
34+
if (product.portable) {
35+
const portablePath = getPortableDataPath();
36+
try { fs.mkdirSync(portablePath); } catch (err) { if (err.code !== 'EEXIST') { throw err; } }
37+
38+
const tmpdir = path.join(portablePath, 'tmp');
39+
try { fs.mkdirSync(tmpdir); } catch (err) { if (err.code !== 'EEXIST') { throw err; } }
40+
41+
process.env[process.platform === 'win32' ? 'TEMP' : 'TMPDIR'] = tmpdir;
42+
}
43+
1544
//#region Add support for using node_modules.asar
1645
(function () {
1746
const path = require('path');
@@ -36,18 +65,15 @@ Error.stackTraceLimit = 100; // increase number of stack frames (from 10, https:
3665
})();
3766
//#endregion
3867

39-
let app = require('electron').app;
68+
const app = require('electron').app;
4069

4170
// TODO@Ben Electron 2.0.x: prevent localStorage migration from SQLite to LevelDB due to issues
4271
app.commandLine.appendSwitch('disable-mojo-local-storage');
4372

44-
let fs = require('fs');
45-
let path = require('path');
46-
let minimist = require('minimist');
47-
let paths = require('./paths');
48-
let product = require('../product.json');
73+
const minimist = require('minimist');
74+
const paths = require('./paths');
4975

50-
let args = minimist(process.argv, {
76+
const args = minimist(process.argv, {
5177
string: [
5278
'user-data-dir',
5379
'locale',
@@ -350,9 +376,16 @@ function getNodeCachedDataDir() {
350376
}
351377
//#endregion
352378

379+
function getUserDataPath() {
380+
if (product.portable) {
381+
return path.join(getPortableDataPath(), 'user-data');
382+
}
383+
384+
return path.resolve(args['user-data-dir'] || paths.getDefaultUserDataPath(process.platform));
385+
}
386+
353387
// Set userData path before app 'ready' event and call to process.chdir
354-
let userData = path.resolve(args['user-data-dir'] || paths.getDefaultUserDataPath(process.platform));
355-
app.setPath('userData', userData);
388+
app.setPath('userData', getUserDataPath());
356389

357390
// Update cwd based on environment and platform
358391
try {

src/vs/platform/environment/node/environmentService.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,24 @@ export class EnvironmentService implements IEnvironmentService {
9090
get userHome(): string { return os.homedir(); }
9191

9292
@memoize
93-
get userDataPath(): string { return parseUserDataDir(this._args, process); }
93+
private get appPath(): string {
94+
if (process.env['VSCODE_DEV']) {
95+
return this.appRoot;
96+
} else if (process.platform === 'darwin') {
97+
return path.dirname(path.dirname(path.dirname(this.appRoot)));
98+
} else {
99+
return path.dirname(path.dirname(this.appRoot));
100+
}
101+
}
102+
103+
@memoize
104+
get userDataPath(): string {
105+
if (product.portable) {
106+
return path.join(path.dirname(this.appPath), product.portable, 'user-data');
107+
}
108+
109+
return parseUserDataDir(this._args, process);
110+
}
94111

95112
get appNameLong(): string { return product.nameLong; }
96113

@@ -127,7 +144,19 @@ export class EnvironmentService implements IEnvironmentService {
127144
get installSourcePath(): string { return path.join(this.userDataPath, 'installSource'); }
128145

129146
@memoize
130-
get extensionsPath(): string { return parsePathArg(this._args['extensions-dir'], process) || process.env['VSCODE_EXTENSIONS'] || path.join(this.userHome, product.dataFolderName, 'extensions'); }
147+
get extensionsPath(): string {
148+
const fromArgs = parsePathArg(this._args['extensions-dir'], process);
149+
150+
if (fromArgs) {
151+
return fromArgs;
152+
} else if (process.env['VSCODE_EXTENSIONS']) {
153+
return process.env['VSCODE_EXTENSIONS'];
154+
} else if (product.portable) {
155+
return path.join(path.dirname(this.appPath), product.portable, 'extensions');
156+
} else {
157+
return path.join(this.userHome, product.dataFolderName, 'extensions');
158+
}
159+
}
131160

132161
@memoize
133162
get extensionDevelopmentPath(): string { return this._args.extensionDevelopmentPath ? path.normalize(this._args.extensionDevelopmentPath) : this._args.extensionDevelopmentPath; }

src/vs/platform/node/product.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export interface IProductConfiguration {
7373
'darwin': string;
7474
};
7575
logUploaderUrl: string;
76+
portable?: string;
7677
}
7778

7879
export interface ISurveyData {

0 commit comments

Comments
 (0)