diff --git a/src/bootstrap-node.js b/src/bootstrap-node.js index 2686249978d..161d68d5941 100644 --- a/src/bootstrap-node.js +++ b/src/bootstrap-node.js @@ -58,3 +58,73 @@ exports.removeGlobalNodeModuleLookupPaths = function () { return paths.slice(0, paths.length - commonSuffixLength); }; }; + +/** + * Helper to enable portable mode. + * + * @param {{ portable?: string; applicationName: string; }} product + * @returns {{ portableDataPath: string; isPortable: boolean; }} + */ +exports.configurePortable = function (product) { + const fs = require('fs'); + const path = require('path'); + + const appRoot = path.dirname(__dirname); + + /** + * @param {import('path')} path + */ + function getApplicationPath(path) { + if (process.env['VSCODE_DEV']) { + return appRoot; + } + + if (process.platform === 'darwin') { + return path.dirname(path.dirname(path.dirname(appRoot))); + } + + return path.dirname(path.dirname(appRoot)); + } + + /** + * @param {import('path')} path + */ + function getPortableDataPath(path) { + if (process.env['VSCODE_PORTABLE']) { + return process.env['VSCODE_PORTABLE']; + } + + if (process.platform === 'win32' || process.platform === 'linux') { + return path.join(getApplicationPath(path), 'data'); + } + + // @ts-ignore + const portableDataName = product.portable || `${product.applicationName}-portable-data`; + return path.join(path.dirname(getApplicationPath(path)), portableDataName); + } + + const portableDataPath = getPortableDataPath(path); + const isPortable = !('target' in product) && fs.existsSync(portableDataPath); + const portableTempPath = path.join(portableDataPath, 'tmp'); + const isTempPortable = isPortable && fs.existsSync(portableTempPath); + + if (isPortable) { + process.env['VSCODE_PORTABLE'] = portableDataPath; + } else { + delete process.env['VSCODE_PORTABLE']; + } + + if (isTempPortable) { + if (process.platform === 'win32') { + process.env['TMP'] = portableTempPath; + process.env['TEMP'] = portableTempPath; + } else { + process.env['TMPDIR'] = portableTempPath; + } + } + + return { + portableDataPath, + isPortable + }; +}; diff --git a/src/bootstrap.js b/src/bootstrap.js index b25b34cc330..5392a5602cf 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -170,99 +170,6 @@ return nlsConfig; } - //#endregion - - - //#region Portable helpers - - /** - * @param {{ portable: string | undefined; applicationName: string; }} product - * @returns {{ portableDataPath: string; isPortable: boolean; } | undefined} - */ - function configurePortable(product) { - if (!path || !fs || typeof process === 'undefined') { - console.warn('configurePortable() is only available in node.js environments'); // TODO@sandbox Portable is currently non-sandboxed only - return; - } - - const appRoot = path.dirname(__dirname); - - /** - * @param {import('path')} path - */ - function getApplicationPath(path) { - if (process.env['VSCODE_DEV']) { - return appRoot; - } - - if (process.platform === 'darwin') { - return path.dirname(path.dirname(path.dirname(appRoot))); - } - - return path.dirname(path.dirname(appRoot)); - } - - /** - * @param {import('path')} path - */ - function getPortableDataPath(path) { - if (process.env['VSCODE_PORTABLE']) { - return process.env['VSCODE_PORTABLE']; - } - - if (process.platform === 'win32' || process.platform === 'linux') { - return path.join(getApplicationPath(path), 'data'); - } - - // @ts-ignore - const portableDataName = product.portable || `${product.applicationName}-portable-data`; - return path.join(path.dirname(getApplicationPath(path)), portableDataName); - } - - const portableDataPath = getPortableDataPath(path); - const isPortable = !('target' in product) && fs.existsSync(portableDataPath); - const portableTempPath = path.join(portableDataPath, 'tmp'); - const isTempPortable = isPortable && fs.existsSync(portableTempPath); - - if (isPortable) { - process.env['VSCODE_PORTABLE'] = portableDataPath; - } else { - delete process.env['VSCODE_PORTABLE']; - } - - if (isTempPortable) { - if (process.platform === 'win32') { - process.env['TMP'] = portableTempPath; - process.env['TEMP'] = portableTempPath; - } else { - process.env['TMPDIR'] = portableTempPath; - } - } - - return { - portableDataPath, - isPortable - }; - } - - //#endregion - - - //#region ApplicationInsights - - // Prevents appinsights from monkey patching modules. - // This should be called before importing the applicationinsights module - function avoidMonkeyPatchFromAppInsights() { - if (typeof process === 'undefined') { - console.warn('avoidMonkeyPatchFromAppInsights() is only available in node.js environments'); - return; - } - - // @ts-ignore - process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'] = true; // Skip monkey patching of 3rd party modules by appinsights - global['diagnosticsSource'] = {}; // Prevents diagnostic channel (which patches "require") from initializing entirely - } - function safeGlobals() { const globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {}); @@ -329,12 +236,29 @@ } //#endregion + + + //#region ApplicationInsights + + // Prevents appinsights from monkey patching modules. + // This should be called before importing the applicationinsights module + function avoidMonkeyPatchFromAppInsights() { + if (typeof process === 'undefined') { + console.warn('avoidMonkeyPatchFromAppInsights() is only available in node.js environments'); + return; + } + + // @ts-ignore + process.env['APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL'] = true; // Skip monkey patching of 3rd party modules by appinsights + global['diagnosticsSource'] = {}; // Prevents diagnostic channel (which patches "require") from initializing entirely + } + + //#endregion return { enableASARSupport, avoidMonkeyPatchFromAppInsights, - configurePortable, setupNLS, fileUriFromPath }; diff --git a/src/cli.js b/src/cli.js index c59d5ae2f6d..5624cb93bb5 100644 --- a/src/cli.js +++ b/src/cli.js @@ -7,13 +7,14 @@ 'use strict'; const bootstrap = require('./bootstrap'); +const bootstrapNode = require('./bootstrap-node'); const product = require('../product.json'); // Avoid Monkey Patches from Application Insights bootstrap.avoidMonkeyPatchFromAppInsights(); // Enable portable support -bootstrap.configurePortable(product); +bootstrapNode.configurePortable(product); // Enable ASAR support bootstrap.enableASARSupport(undefined); diff --git a/src/main.js b/src/main.js index 699ccb5b1ab..8ec0aa74c39 100644 --- a/src/main.js +++ b/src/main.js @@ -15,6 +15,7 @@ const path = require('path'); const fs = require('fs'); const os = require('os'); const bootstrap = require('./bootstrap'); +const bootstrapNode = require('./bootstrap-node'); const paths = require('./paths'); /** @type {any} */ const product = require('../product.json'); @@ -25,7 +26,7 @@ const { app, protocol, crashReporter } = require('electron'); app.allowRendererProcessReuse = false; // Enable portable support -const portable = bootstrap.configurePortable(product); +const portable = bootstrapNode.configurePortable(product); // Enable ASAR support bootstrap.enableASARSupport(undefined);