process.js 2.2 KB

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