javascript Node.js 获取/确定操作系统版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14989081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:05:06  来源:igfitidea点击:

Node.js to get/determine OS version

javascriptnode.js

提问by badunk

I'd like to determine whether some script that is being executed is running a particular version of Mac OSX. I realize I can exec/spawn the command:

我想确定正在执行的某个脚本是否正在运行特定版本的 Mac OSX。我意识到我可以执行/生成命令:

sw_vers -productVersion

Is there a way to do this synchronously (similar to process.arch) without node-exec-sync? I realize its an accepted bad practice to spawn/exec synchronously, but I don't see another way.

有没有办法在没有node-exec-sync 的情况下同步执行此操作(类似于 process.arch)?我意识到同步生成/执行是一种公认​​的不良做法,但我没有看到其他方式。

回答by AndyD

you could use the OS modulelike this:

你可以像这样使用 OS 模块

var os = require('os');
os.platform(); // 'darwin'
os.release(); //'10.8.0'

and then map the release version to a specific version of Mac OS X.

然后将发布版本映射到特定版本的 Mac OS X。

Darwin to Mac OS X mappings can be found here

Darwin 到 Mac OS X 的映射可以在这里找到

回答by ccnokes

As mentioned above in AndyD's answer's comments, os.release()returns the kernel version. If you want to get the same version number that a user sees in the "About this Mac" UI, you can read and parse out /System/Library/CoreServices/SystemVersion.plist, like so:

正如上面在 AndyD 的回答评论中提到的,os.release()返回内核版本。如果您想获得用户在“关于此 Mac”用户界面中看到的相同版本号,您可以读取并解析出/System/Library/CoreServices/SystemVersion.plist,如下所示:

const plist = require('plist');
let versionInfo = plist.parseFileSync('/System/Library/CoreServices/SystemVersion.plist');
console.log(JSON.stringify(versionInfo));

https://github.com/kevinsawicki/node-plist

https://github.com/kevinsawicki/node-plist