实现说明
改动会落在函数 shellFor内,外围只是固定流程。
使用要点
- 无需安装:
node:os随 Node.js 本体提供。 - 每一步打印都带着自己的标签:
platform:、arch:、type:。
常见问题
运行之前需要装什么吗?
什么都不用装:node:os 随 Node.js 本体提供。
签名一览:function shellFor(p: NodeJS.Platform)
核心语句
return 'powershell.exe';
return 'zsh';
return 'sh';
代码示例
import os from 'node:os';
// platform() returns a literal union, so shell branches are checkable
function shellFor(p: NodeJS.Platform): string {
switch (p) {
case 'win32':
return 'powershell.exe';
case 'darwin':
return 'zsh';
default:
return 'sh';
}
}
console.log('platform:', os.platform());
console.log('arch:', os.arch());
console.log('type:', os.type());
console.log('release:', os.release());
console.log('EOL bytes:', os.EOL.length);
console.log('shell:', shellFor(os.platform()));