How to get host operating system name from visual studio code extension?

Viewed 511

In my extension I have to call functions depending on the host operating system, how do I get infos about host operating system ?

3 Answers

You can also use the os package from node. See os.type:

const os = require('os'); or import it

Returns the operating system name as returned by uname(3). For example, it returns Linux on Linux, Darwin on macOS, and Windows_NT on Windows.

I do not believe you need to install the os package separately - it is made available to extension authors automatically.

This is what I did for my extension, it isn't the most robust solution but it works well enough for my purposes:

import { env } from 'vscode';

const isWindows = () => Boolean(env.appRoot && env.appRoot[0] !== "/");

The rationale is that all UNIX-like OS:es will have a root starting with /, and a Windows-based install will have something like C:.

If you don't mind having a hard-dependency on Node internals, you can use process.platform instead.

On Windows

process.platform==='win32'

is true, and that is still the case on my 64-bit windows 11.

On MacOs

process.platform==='darwin'

On Linux Mint

process.platform==='linux'

On my wsl/ubuntu installation

process.platform==='linux'

(.. it would also be nice to distinguish wsl from a full linux os)

There is no need to import/require process.

Related