为electron编写扩展_在Electron中编写特定于操作系统的代码
为electron编写扩展One of the advantages of using Electron is that — since it’s cross-platform — we don’t have to worry about the operating system on which our application is going to be run. 使用Electron的优点之
为electron编写扩展
One of the advantages of using Electron is that — since it’s cross-platform — we don’t have to worry about the operating system on which our application is going to be run.
使用Electron的优点之一是-由于它是跨平台的,因此我们不必担心将在其上运行应用程序的操作系统。
However, sometimes we need our code to be OS-specific if, for example, we are going to be using the command console or need to retrieve some information about the system.
但是,有时,例如,如果我们要使用命令控制台或需要检索有关系统的某些信息,则我们的代码需要特定于操作系统。
Having to write multiple ifs each time we want to have some functionality on a given OS seems like excess work. It quickly obfuscates the code, making it difficult to be understood and analyzed.
每次我们想要在给定的OS上具有某些功能时,都必须编写多个if ,这似乎是多余的工作。 它会快速混淆代码,使其难以理解和分析。
In order to keep the code clean and readable, we can create a little helper and remove the ifs and any “OS-related” logic altogether.
为了保持代码的清洁和可读性,我们可以创建一个小帮手,并完全删除ifs和任何与“ OS相关”的逻辑。
实施平台 (Implementing Platforms)
const os = require('os');
const platforms = {
WINDOWS: 'WINDOWS',
MAC: 'MAC',
LINUX: 'LINUX',
SUN: 'SUN',
OPENBSD: 'OPENBSD',
ANDROID: 'ANDROID',
AIX: 'AIX',
};
const platformsNames = {
win32: platforms.WINDOWS,
darwin: platforms.MAC,
linux: platforms.LINUX,
sunos: platforms.SUN,
openbsd: platforms.OPENBSD,
android: platforms.ANDROID,
aix: platforms.AIX,
};
const currentPlatform = platformsNames[os.platform()];
const findHandlerOrDefault = (handlerName, dictionary) => {
const handler = dictionary[handlerName];
if (handler) {
return handler;
}
if (dictionary.default) {
return dictionary.default;
}
return () => null;
};
const byOS = findHandlerOrDefault.bind(null, currentPlatform);
// usage
const whatIsHeUsing = byOS({
[MAC]: username => `Hi ${username}! You are using Mac.`,
[WINDOWS]: username => `Hi ${username}! You are using Windows.`,
[LINUX]: username => `Hi ${username}! You are using Linux.`,
default: username => `Hi ${username}! You are using something different.`,
});
console.log(whatIsHeUsing('Maciej Cieslar')); // => Hi Maciej Cieslar! You are using Mac.
First, we see the platforms object which contains names of all supported operating systems. It is made only for convenience. We can then use platforms.WINDOWS instead of typing ‘WINDOWS’ each time into our object with handlers we pass to the byOS function.
首先,我们看到平台对象,其中包含所有受支持操作系统的名称。 仅为了方便而制造。 然后我们可以使用platform.WINDOWS,而不是每次通过处理程序将我们传递给byOS函数的对象键入“ WINDOWS” 。
Next, notice the platformsNames object. The keys are the result of calling os.platform(). The values are the keys from the platforms object. We simply map it to a more user-friendly name.
接下来,注意platformNames对象。 这些键是调用os.platform()的结果。 的 值是来自Platforms对象的键。 我们只是将其映射到一个更加用户友好的名称。
For example, when os.platform() returns win32, we map it to platforms.WINDOWS by calling platformsNames[os.platform()].
例如,当os.platform()返回win32时,我们通过调用platformNames [os.platform()]将其映射到platform.WINDOWS 。
In currentPlatform, we save the platform that we are using right now, so then we can match it against a given object with handlers.
在currentPlatform中,我们保存了当前正在使用的平台,因此可以使用处理程序将其与给定对象进行匹配。
实施版本 (Implementing Releases)
One might go even further and try to differentiate between releases of a given OS, for example, Windows 7, 8 and 10.
人们可能会走得更远,尝试区分给定操作系统的版本,例如Windows 7、8和10。
const os = require('os');
const releaseTest = {
[platforms.WINDOWS]: (version) => {
const [majorVersion, minorVersion] = version.split('.');
// Windows 10 (10,0)
if (majorVersion === '10') {
return releases.WIN10;
}
// Windows 8.1 (6,3)
// Windows 8 (6,2)
// Windows 7 (6,1)
if (majorVersion === '6') {
if (minorVersion === '3' || minorVersion === '2') {
return releases.WIN8;
}
return releases.WIN7;
}
return releases.WIN7;
},
[platforms.MAC]: () => releases.ANY,
[platforms.LINUX]: () => releases.ANY,
};
const currentRelease = releaseTest[currentPlatform](os.release());
const byRelease = findHandlerOrDefault.bind(null, currentRelease);
// usage
const whatWindowsIsHeUsing = byOS({
[WINDOWS]: byRelease({
[WIN7]: username => `Hi ${username}! You are using Windows 7.`,
[WIN8]: username => `Hi ${username}! You are using Windows 8.`,
[WIN10]: username => `Hi ${username}! You are using Windows 10.`,
}),
});
console.log(whatWindowsIsHeUsing('Maciej Cieslar')); // => Hi Maciej Cieslar! You are using Windows 7.
Now we can use os.release() to check for the system’s release.
现在我们可以使用os.release()来检查系统的版本。
We can split the resulting string and check the Windows version. A complete list can be found here. As for Linux/Mac, I didn’t really see how that could be useful, so I left it at releases.ANY.
我们可以分割结果字符串并检查Windows版本。 完整列表可以在这里找到。 至于Linux / Mac,我没有真正看到它的用处,所以我把它留在releases.ANY 。
In whatWindowsIsHeUsing you can see that we are only checking for different Windows’ releases if we are running the app on Windows.
在whatWindowsIsHeUsing中,您可以看到,如果我们在Windows上运行该应用程序,则我们仅检查不同的Windows版本。
You can see the code in the repository.
您可以在存储库中查看代码。
Thank you very much for reading! If you have better ideas on how to write OS specific code, share them down below!
非常感谢您的阅读! 如果您对如何编写特定于操作系统的代码有更好的想法,请在下面共享!
If you have any questions or comments feel free to put them in the comment section below or send me a message.
如果您有任何问题或评论,请随时将其放在下面的评论部分或给我发送消息 。
Check out my social media!
看看我的社交媒体 !
加入我的时事通讯 !
Originally published at www.mcieslar.com on August 28, 2018.
最初于2018年8月28日发布在www.mcieslar.com上。
翻译自: https://www.freecodecamp.org/news/how-to-write-os-specific-code-in-electron-bf6379c62ff6/
为electron编写扩展
更多推荐


所有评论(0)