os Platform Branches For Build Scripts
platform() returns a literal union of platform names, so build-script branches on win32, darwin, and linux are exhaustively checkable.
Code
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()));Output
Captured from a sandboxed run of this snippet in the build pipeline.
platform: win32
arch: x64
type: Windows_NT
release: 10.0.26320
EOL bytes: 2
shell: powershell.exeWhat the code does
os Platform Branches For Build Scripts is built around the function shellFor.
What to watch for
- The dependency surface is just
node:os, and that one is built into Node.js.
Applying it
The main signature reads function shellFor(p: NodeJS.Platform)
The demo output is labeled (platform:, arch:, type:), so each printed line maps back to the step behind it.
Statements that matter
return 'powershell.exe';
return 'zsh';
return 'sh';
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- Do I need to install anything before running this?
- Nothing needs installing: `node:os` ships with Node.js itself.
Report a problem with this exampleOpens a pre-filled issue on GitHub.