environmentVariables.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.updateEnvironment = exports.setEnvironment = void 0;
  6. var _executeWinCommand = require("./executeWinCommand");
  7. /**
  8. * Creates a new variable in the user's environment
  9. */
  10. const setEnvironment = async (variable, value) => {
  11. // https://superuser.com/a/601034
  12. const command = `setx ${variable} "${value}"`;
  13. await (0, _executeWinCommand.executeCommand)(command);
  14. process.env[variable] = value;
  15. };
  16. /**
  17. * Prepends the given `value` to the user's environment `variable`.
  18. * @param {string} variable The environment variable to modify
  19. * @param {string} value The value to add to the variable
  20. * @returns {Promise<void>}
  21. */
  22. exports.setEnvironment = setEnvironment;
  23. const updateEnvironment = async (variable, value) => {
  24. // Avoid adding the value multiple times to PATH
  25. // Need to do the following to avoid TSLint complaining about possible
  26. // undefined values even if I check before via `typeof` or another way
  27. const envVariable = process.env[variable] || '';
  28. if (variable === 'PATH' && envVariable.includes(`${value};`)) {
  29. return;
  30. } // https://superuser.com/a/601034
  31. const command = `for /f "skip=2 tokens=3*" %a in ('reg query HKCU\\Environment /v ${variable}') do @if [%b]==[] ( @setx ${variable} "${value};%~a" ) else ( @setx ${variable} "${value};%~a %~b" )
  32. `;
  33. await (0, _executeWinCommand.executeCommand)(command);
  34. process.env[variable] = `${process.env[variable]}${value};`;
  35. };
  36. exports.updateEnvironment = updateEnvironment;
  37. //# sourceMappingURL=environmentVariables.js.map