process.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. import { globals, isMacintosh, isWindows } from './platform.js';
  6. let safeProcess;
  7. // Native sandbox environment
  8. if (typeof globals.vscode !== 'undefined' && typeof globals.vscode.process !== 'undefined') {
  9. const sandboxProcess = globals.vscode.process;
  10. safeProcess = {
  11. get platform() { return sandboxProcess.platform; },
  12. get arch() { return sandboxProcess.arch; },
  13. get env() { return sandboxProcess.env; },
  14. cwd() { return sandboxProcess.cwd(); }
  15. };
  16. }
  17. // Native node.js environment
  18. else if (typeof process !== 'undefined') {
  19. safeProcess = {
  20. get platform() { return process.platform; },
  21. get arch() { return process.arch; },
  22. get env() { return process.env; },
  23. cwd() { return process.env['VSCODE_CWD'] || process.cwd(); }
  24. };
  25. }
  26. // Web environment
  27. else {
  28. safeProcess = {
  29. // Supported
  30. get platform() { return isWindows ? 'win32' : isMacintosh ? 'darwin' : 'linux'; },
  31. get arch() { return undefined; /* arch is undefined in web */ },
  32. // Unsupported
  33. get env() { return {}; },
  34. cwd() { return '/'; }
  35. };
  36. }
  37. /**
  38. * Provides safe access to the `cwd` property in node.js, sandboxed or web
  39. * environments.
  40. *
  41. * Note: in web, this property is hardcoded to be `/`.
  42. */
  43. export const cwd = safeProcess.cwd;
  44. /**
  45. * Provides safe access to the `env` property in node.js, sandboxed or web
  46. * environments.
  47. *
  48. * Note: in web, this property is hardcoded to be `{}`.
  49. */
  50. export const env = safeProcess.env;
  51. /**
  52. * Provides safe access to the `platform` property in node.js, sandboxed or web
  53. * environments.
  54. */
  55. export const platform = safeProcess.platform;