In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use node to obtain mac system version". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "how to use node to obtain mac system version" can help you solve the problem.
Get the system type
Since you need to do compatibility processing for mac systems, you first need to distinguish the system type. Fortunately, nodejs provides the relevant API, and you only need to use os.platform () to get the system type directly.
Import {platform} from 'os';console.log (platform ()); / /' darwin'
It is worth mentioning that the corresponding name of macOS is not macOS but darwin.
In addition to getting the system type through the os package, we can also get it through process.platform.
Import {platform} from 'process';console.log (platform); / /' darwin' gets the system version
Many students who are familiar with node will say that you can quickly get the system version through os.release (). However, when you execute os.release (), you will find that its results are not in line with conventional cognition.
For example, my system version is 12.0.1, but the execution result of os.release () is 21.1.0.
Looking at the official node documentation, you can see that node determines the operating system through uname (3), so the execution result is more contrary to conventional perception.
Get the system version from the command line
Since the system version obtained by os.release () is hard to understand, is there any way to get the actual system version number directly?
There is a sw_vers command on mac, which can obtain system information directly.
$sw_versProductName: macOSProductVersion: 12.0.1BuildVersion: 21A559
You can see that the sw_vers command gets the system version 12.0.1 directly.
If we do not need other system information, we can get the system version directly by adding command line parameters and filtering the command line results.
$sw_vers-ProductVersion12.0.1
Since the command line is case-insensitive, command-line arguments are case-insensitive, and you can also write-productversion or-ProductVersion.
Now that we know how to write on the command line, all we need to do is call the command line in node.
Import {execSync} from 'child_process';console.log (execSync (' sw_vers-ProductVersion', {encoding: 'utf-8'})); / / gracefully obtain the system version
The correct system version can be obtained by executing command-line commands through the node child process. However, this approach has performance drawbacks, which requires the creation of a child process to execute the command line, which is more expensive than the os.release () way.
The problem with using os.release () before is that returning the result is unconventional, but the result is actually correct, and it just needs a mapping.
Therefore, the system version can be gracefully obtained by os.release () with the mapping table.
Import os from 'os' Const macVersionMap = new Map ([[21, ['Monterey',' 12']], [20, ['Big Sur',' 11']], [19, ['Catalina',' 10.15']], [18, ['Mojave',' 10.14']], [17, ['High Sierra',' 10.13']], [16, ['Sierra',' 10.12']], [15, ['El Capitan''] '10.11'], [14, [' Yosemite', '10.10'], [13, [' Mavericks', '10.9']], [12, [' Mountain Lion', '10.8']], [11, [' Lion', '10.7']], [10, [' Snow Leopard', '10.6']], [9, [' Leopard', '10.5']], [8 ['Tiger',' 10.4']], [7, ['Panther',' 10.3'], [6, ['Jaguar',' 10.2']], [5, ['Puma',' 10.1']],]) Const getMacRelease = (release: string) = > {const macRelease = release? Os.release (); const firstReleaseVersion = Number (macRelease.split ('.') [0]); const [name, version] = macVersionMap.get (firstReleaseVersion) | | ['Unknown',',]; return {name, version,}; console.log (getMacRelease ()) / / 12
In order to make it convenient for mac to still recognize the new system after the release of the system, the new system version name is identified as Unknow and the new version is identified as an empty string.
This is to learn from the mobile model scoring platform. If the version is empty, it defaults to the high version and does not affect the compatibility of the lower version.
This is the end of the introduction on "how to use node to get the version of mac system". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.