From 0337928aa36a069a0739b822087df58356371c7a Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Fri, 23 Oct 2020 12:44:12 -0700 Subject: [PATCH] fix(env): respect =true/false as env values for boolean flags (#4228) --- src/install/installer.ts | 4 ++-- src/utils/utils.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/install/installer.ts b/src/install/installer.ts index ad0eba4a08..d6134387f3 100644 --- a/src/install/installer.ts +++ b/src/install/installer.ts @@ -22,7 +22,7 @@ import * as removeFolder from 'rimraf'; import * as lockfile from 'proper-lockfile'; import * as browserPaths from '../utils/browserPaths'; import * as browserFetcher from './browserFetcher'; -import { getFromENV } from '../utils/utils'; +import { getAsBooleanFromENV } from '../utils/utils'; const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs)); const fsReaddirAsync = util.promisify(fs.readdir.bind(fs)); @@ -34,7 +34,7 @@ const removeFolderAsync = util.promisify(removeFolder); export async function installBrowsersWithProgressBar(packagePath: string) { // PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD should have a value of 0 or 1 - if (!!Number(getFromENV('PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD'))) { + if (getAsBooleanFromENV('PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD')) { browserFetcher.logPolitely('Skipping browsers download because `PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD` env variable is set'); return false; } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 5387d6bc58..81afddd670 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -95,13 +95,18 @@ export function isUnderTest(): boolean { return _isUnderTest; } -export function getFromENV(name: string) { +export function getFromENV(name: string): string | undefined { let value = process.env[name]; - value = typeof value === 'undefined' ? process.env[`npm_config_${name.toLowerCase()}`] : value; - value = typeof value === 'undefined' ? process.env[`npm_package_config_${name.toLowerCase()}`] : value; + value = value === undefined ? process.env[`npm_config_${name.toLowerCase()}`] : value; + value = value === undefined ? process.env[`npm_package_config_${name.toLowerCase()}`] : value; return value; } +export function getAsBooleanFromENV(name: string): boolean { + const value = getFromENV(name); + return !!value && value !== 'false' && value !== '0'; +} + export async function mkdirIfNeeded(filePath: string) { // This will harmlessly throw on windows if the dirname is the root directory. await mkdirAsync(path.dirname(filePath), {recursive: true}).catch(() => {});