linuxUtils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getLinuxDistributionInfo = getLinuxDistributionInfo;
  6. exports.getLinuxDistributionInfoSync = getLinuxDistributionInfoSync;
  7. var _fs = _interopRequireDefault(require("fs"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * Copyright 2017 Google Inc. All rights reserved.
  11. * Modifications copyright (c) Microsoft Corporation.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. let didFailToReadOSRelease = false;
  26. let osRelease;
  27. async function getLinuxDistributionInfo() {
  28. if (process.platform !== 'linux') return undefined;
  29. if (!osRelease && !didFailToReadOSRelease) {
  30. try {
  31. var _fields$get, _fields$get2;
  32. // List of /etc/os-release values for different distributions could be
  33. // found here: https://gist.github.com/aslushnikov/8ceddb8288e4cf9db3039c02e0f4fb75
  34. const osReleaseText = await _fs.default.promises.readFile('/etc/os-release', 'utf8');
  35. const fields = parseOSReleaseText(osReleaseText);
  36. osRelease = {
  37. id: (_fields$get = fields.get('id')) !== null && _fields$get !== void 0 ? _fields$get : '',
  38. version: (_fields$get2 = fields.get('version_id')) !== null && _fields$get2 !== void 0 ? _fields$get2 : ''
  39. };
  40. } catch (e) {
  41. didFailToReadOSRelease = true;
  42. }
  43. }
  44. return osRelease;
  45. }
  46. function getLinuxDistributionInfoSync() {
  47. if (process.platform !== 'linux') return undefined;
  48. if (!osRelease && !didFailToReadOSRelease) {
  49. try {
  50. var _fields$get3, _fields$get4;
  51. // List of /etc/os-release values for different distributions could be
  52. // found here: https://gist.github.com/aslushnikov/8ceddb8288e4cf9db3039c02e0f4fb75
  53. const osReleaseText = _fs.default.readFileSync('/etc/os-release', 'utf8');
  54. const fields = parseOSReleaseText(osReleaseText);
  55. osRelease = {
  56. id: (_fields$get3 = fields.get('id')) !== null && _fields$get3 !== void 0 ? _fields$get3 : '',
  57. version: (_fields$get4 = fields.get('version_id')) !== null && _fields$get4 !== void 0 ? _fields$get4 : ''
  58. };
  59. } catch (e) {
  60. didFailToReadOSRelease = true;
  61. }
  62. }
  63. return osRelease;
  64. }
  65. function parseOSReleaseText(osReleaseText) {
  66. const fields = new Map();
  67. for (const line of osReleaseText.split('\n')) {
  68. const tokens = line.split('=');
  69. const name = tokens.shift();
  70. let value = tokens.join('=').trim();
  71. if (value.startsWith('"') && value.endsWith('"')) value = value.substring(1, value.length - 1);
  72. if (!name) continue;
  73. fields.set(name.toLowerCase(), value);
  74. }
  75. return fields;
  76. }