Puppeteer.launch depending on OS

Viewed 217

Is this possible to setup argument logic of the browser launch depending on OS.platform, something like this:

const puppeteer = require('puppeteer');
const browserFetcher = puppeteer.createBrowserFetcher();

    (async () => {
      const platforme = await browserFetcher.platform()
      const browser = await puppeteer.launch( 
  ??? ()=>{
         if (platforme=="win64"||"win32")
      //run this params   headless: false; defaultViewport: null;
         else if (platforme=="linux")
      //run this params headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox','--disable-dev-shm-usage']
     });
     const page = await browser.newPage();
2 Answers

If you really need this obscure brevity, you can use spread with conditional operator:

{
  const platforme = "win64";
  const config = { ...(
    platforme === "win64" || platforme === "win32" ? { headless: false } :
    platforme === "linux" ? { headless: true } :
    {}
  ) };
  console.log(config);
}

{
  const platforme = "linux";
  const config = { ...(
    platforme === "win64" || platforme === "win32" ? { headless: false } :
    platforme === "linux" ? { headless: true } :
    {}
  ) };
  console.log(config);
}

{
  const platforme = "aix";
  const config = { ...(
    platforme === "win64" || platforme === "win32" ? { headless: false } :
    platforme === "linux" ? { headless: true } :
    {}
  ) };
  console.log(config);
}

See more and some alternatives in https://2ality.com/2017/04/conditional-literal-entries.html

Another variant:

const configs = {
  win64: { headless: false },
  win32: { headless: false },
  linux: { headless: true },
}

{
  const platforme = "win64";
  const config = configs[platforme] ?? {};
  console.log(config);
}

{
  const platforme = "linux";
  const config = configs[platforme] ?? {};
  console.log(config);
}

{
  const platforme = "aix";
  const config = configs[platforme] ?? {};
  console.log(config);
}

Updated with working code thanks to vsemozhebuty

const puppeteer = require('puppeteer');
const browserFetcher = puppeteer.createBrowserFetcher();
            
(async () => {
   const platforme = await browserFetcher.platform()
   const config = { ...(
      platforme === "win64" || platforme === "win32" ? { headless: false, defaultViewport: null } :
      platforme === "linux" ? {headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox','--disable-dev-shm-usage']} :
      {}
   ) };
   console.log(config);
   const browser =   await puppeteer.launch(config);
   const page = await browser.newPage();
   //code
})();
Related