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.

TypescriptCli Tooling

Code

typescriptsnippet
20 lines
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.exe

What 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

FAQ

Do I need to install anything before running this?
Nothing needs installing: `node:os` ships with Node.js itself.