Pri*_*Nom 5 javascript unix linux node.js electron
我正在创建一个 Electron 应用程序来帮助我管理磁盘空间。不过,我希望它也能在 Linux/UNIX 上运行。
我编写了以下代码,该代码适用于 Windows,但不适用于 Linux/UNIX 系统。
window.onload = function(){
const cp = require('child_process')
cp.exec('wmic logicaldisk get size,freespace,caption', (error, stdout)=>{
let drives = stdout.trim()split('\r\r\n')
.map(value => value.trim().split(/\s{2,0}/))
.slice(1)
})
}
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样。
[
["560232439808", "C:", "999526756352", "System" ]
["999369699328", "D:", "999558213632", "SSD" ]
["1511570386944", "E:", "8001545039872", "Get" ]
["4620751712256", "F:", "8001545039872", "BR" ]
["788449492992", "G:", "4000650883072", "Seen" ]
["2296009408512", "H:", "4000768323584", "Seen 2" ]
["3594248679424", "I:", "8001545039872", "2160" ]
["3507750227968", "J:", "8001545039872", "1080" ]
["945300619264", "K:", "999625322496", "Trailer" ]
]
Run Code Online (Sandbox Code Playgroud)
由于我不熟悉 Linux/UNIX,我想知道如何在 Linux/UNIX 上实现相同的输出?
可能没有一个命令可以在所有平台上运行。
但您可以做的是获取当前平台并process.platform在每个平台上运行不同的命令。
例如:
const cp = require('child_process');
if (process.platform == 'win32') { // Run wmic for Windows.
cp.exec('wmic logicaldisk get size,freespace,caption', (error, stdout)=>{
let drives = stdout.trim()split('\r\r\n')
.map(value => value.trim().split(/\s{2,0}/))
.slice(1)
});
} else if (process.platform == 'linux') { // Run df for Linux.
cp.exec('df', (error, stdout)=>{
// Do your magic here.
});
} else {
// Run something for a mac.
}
Run Code Online (Sandbox Code Playgroud)
process.platform 您可以在这里阅读有关内容。