fix(env): respect =true/false as env values for boolean flags (#4228)

This commit is contained in:
Pavel Feldman 2020-10-23 12:44:12 -07:00 committed by GitHub
parent ba7949359f
commit 0337928aa3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View file

@ -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;
}

View file

@ -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(() => {});