Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix yarn config set for boolean values (#1237) #1292

Closed
wants to merge 1 commit into from
Closed

Fix yarn config set for boolean values (#1237) #1292

wants to merge 1 commit into from

Conversation

jsynowiec
Copy link
Contributor

Summary

All config values were treated as strings but some config keys require booleans, e.g. strict-ssl or ignore-scripts. See: #1237

Test plan

  1. Set strict-ssl to false
  2. Run yarn config list
  3. Verify if strict-ssl is set to boolean false

screenshot 2016-10-20 11 39 56

All config values were treated as strings but several config keys are
booleans. This introduces a check that verifies if a supplied
value equals 'true' or 'false'.
@@ -16,8 +16,15 @@ export const {run, setFlags} = buildSubCommands('config', {
return false;
}

const [key, val = true] = args;
const [key, val] = ((key, val = true) => {
if (typeof val === 'string' && (/^(true|false)$/i).test(val)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to `val === 'false' || val === 'true', it's not super obvious what this does on first glance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't work for upper/mixed-case letters, and simply usingval.toLowerCase() === 'false' || val.toLowerCase() === 'true' (or introducing another, intermediate variable to hold the lowercase value) will break on the default true value. The typeof check is required.

The regex can be replaced, e.g.

if (typeof val === 'string' && (val.toLowerCase() === 'false' || val.toLowerCase() === 'true')) {

but it's ugly :/

}

return [key, val];
})(...args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of the IIFE and spreading the arguments into it, we should do something like:

const key = args[0];
let val = args[1];

if (key === 'true' ...) {
   val = val === 'true';
}

We don't really need to handle the alternate casing of true.

Copy link
Contributor Author

@jsynowiec jsynowiec Oct 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const key = args[0];
let val = args[1];

You'll loose the default true value this way from the val assignment (I didn't want to introduce a regression). It could be replaced with let val = args[1] || true but it's not the same as a default value (falsy values don't work, most importantly 0), hence IIFE.

if (key === 'true' ...) {
   val = val === 'true';
}

See my previous comment.

We don't really need to handle the alternate casing of true.

What about calling config set without the value? AFAIK, NPM defaults to true for flags and an empty string for other keys?

@timdp
Copy link

timdp commented Oct 27, 2016

This also affects npm_config_* environment variables, right? npm_config_production=false yarn is being treated as yarn --prod for me because !!'false' is true.

sebmck pushed a commit that referenced this pull request Nov 1, 2016
@sebmck
Copy link
Contributor

sebmck commented Nov 1, 2016

Created #1590 to fix this, pending approval.

@sebmck sebmck closed this Nov 1, 2016
sebmck pushed a commit that referenced this pull request Nov 1, 2016
@jsynowiec jsynowiec deleted the fix/config-set-boolean branch November 11, 2016 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants