Skip to content

Commit

Permalink
fix(env): respect =true/false as env values for boolean flags (#4228)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Oct 23, 2020
1 parent ba79493 commit 0337928
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/install/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {});
Expand Down

0 comments on commit 0337928

Please sign in to comment.