utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const childProcess = require("child_process");
  4. const fs = require("fs-extra");
  5. const os = require("os");
  6. const path = require("path");
  7. async function useAndRemoveDirectory(directory, fn) {
  8. let result;
  9. try {
  10. result = await fn(directory);
  11. }
  12. finally {
  13. await fs.remove(directory);
  14. }
  15. return result;
  16. }
  17. async function withTempDirectoryIn(parentDirectory = os.tmpdir(), fn) {
  18. const tempDirectoryPrefix = 'electron-download-';
  19. const tempDirectory = await fs.mkdtemp(path.resolve(parentDirectory, tempDirectoryPrefix));
  20. return useAndRemoveDirectory(tempDirectory, fn);
  21. }
  22. exports.withTempDirectoryIn = withTempDirectoryIn;
  23. async function withTempDirectory(fn) {
  24. return withTempDirectoryIn(undefined, fn);
  25. }
  26. exports.withTempDirectory = withTempDirectory;
  27. function normalizeVersion(version) {
  28. if (!version.startsWith('v')) {
  29. return `v${version}`;
  30. }
  31. return version;
  32. }
  33. exports.normalizeVersion = normalizeVersion;
  34. /**
  35. * Runs the `uname` command and returns the trimmed output.
  36. */
  37. function uname() {
  38. return childProcess
  39. .execSync('uname -m')
  40. .toString()
  41. .trim();
  42. }
  43. exports.uname = uname;
  44. /**
  45. * Generates an architecture name that would be used in an Electron or Node.js
  46. * download file name.
  47. */
  48. function getNodeArch(arch) {
  49. if (arch === 'arm') {
  50. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  51. switch (process.config.variables.arm_version) {
  52. case '6':
  53. return uname();
  54. case '7':
  55. default:
  56. return 'armv7l';
  57. }
  58. }
  59. return arch;
  60. }
  61. exports.getNodeArch = getNodeArch;
  62. /**
  63. * Generates an architecture name that would be used in an Electron or Node.js
  64. * download file name, from the `process` module information.
  65. */
  66. function getHostArch() {
  67. return getNodeArch(process.arch);
  68. }
  69. exports.getHostArch = getHostArch;
  70. function ensureIsTruthyString(obj, key) {
  71. if (!obj[key] || typeof obj[key] !== 'string') {
  72. throw new Error(`Expected property "${key}" to be provided as a string but it was not`);
  73. }
  74. }
  75. exports.ensureIsTruthyString = ensureIsTruthyString;
  76. function isOfficialLinuxIA32Download(platform, arch, version, mirrorOptions) {
  77. return (platform === 'linux' &&
  78. arch === 'ia32' &&
  79. Number(version.slice(1).split('.')[0]) >= 4 &&
  80. typeof mirrorOptions === 'undefined');
  81. }
  82. exports.isOfficialLinuxIA32Download = isOfficialLinuxIA32Download;
  83. /**
  84. * Find the value of a environment variable which may or may not have the
  85. * prefix, in a case-insensitive manner.
  86. */
  87. function getEnv(prefix = '') {
  88. const envsLowerCase = {};
  89. for (const envKey in process.env) {
  90. envsLowerCase[envKey.toLowerCase()] = process.env[envKey];
  91. }
  92. return (name) => {
  93. return (envsLowerCase[`${prefix}${name}`.toLowerCase()] ||
  94. envsLowerCase[name.toLowerCase()] ||
  95. undefined);
  96. };
  97. }
  98. exports.getEnv = getEnv;
  99. function setEnv(key, value) {
  100. // The `void` operator always returns `undefined`.
  101. // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void
  102. if (value !== void 0) {
  103. process.env[key] = value;
  104. }
  105. }
  106. exports.setEnv = setEnv;
  107. //# sourceMappingURL=utils.js.map