6

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

Yvain
  • 882
  • 1
  • 10
  • 27
  • From briefly looking over the [VSCode API](https://code.visualstudio.com/api/references/vscode-api) it seems to be intended to be used as a cross-platform API, so ideally there should not be a need to make this kind of distinction. Are you missing any specific capability? – Boaz Sep 14 '20 at 10:56
  • It's related to my previous post https://stackoverflow.com/questions/50983314/vs-code-open-new-terminal-from-powershell/56439295#56439295 I use VS Extension to open terminal and send some shell commands. Some are slightly different between operating systems. – Yvain Sep 14 '20 at 12:05

3 Answers3

5

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.

marzetti
  • 369
  • 4
  • 8
1

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.

Mark
  • 143,421
  • 24
  • 428
  • 436
0

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.

mausworks
  • 1,607
  • 10
  • 23