utils.js 2.9 KB

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