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 init printing undefined values #643

Merged
merged 2 commits into from
Dec 18, 2019

Conversation

Zach10za
Copy link
Contributor

What's the problem this PR addresses?
This PR fixes #624

To summarize the issue, when running yarn init in a new project, the unused fields are all listed with the value undefined. This is because in Manifest.ts exportTo method, fields in the data object are being set as undefined if not used.

Here is an example:

> yarn init
{
  name: 'example-project',
  bin: undefined,
  dependencies: undefined,
  optionalDependencies: undefined,
  devDependencies: undefined,
  peerDependencies: undefined,
  dependenciesMeta: undefined,
  peerDependenciesMeta: undefined,
  resolutions: undefined,
  files: undefined
}

How did you fix it?

Now in the Manifest.ts exportTo function, instead of setting unused fields as undefined they get deleted from the data object. The new output looks like this:

> yarn init
{ name: 'example-project' }

In order to get proper formatting, the compact option is now set to false in init.ts executeRegular. The final output looks like this:

> yarn init
{
  name: 'example-project'
}

Other solutions

This solution touches a lot of code. An easier fix could be adding something like this to the bottom of the exportTo function:

// Remove all keys with 'undefined' value
for (const key in data)
  if (data[key] === undefined)
    delete data[key];

I opted to go the other route because it seemed more explicit and I think it's better to handle the individual cases closer to the actual logic.

@arcanis arcanis merged commit a51d936 into yarnpkg:master Dec 18, 2019
@arcanis
Copy link
Member

arcanis commented Dec 18, 2019

Looks perfect, thanks! 👌

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.

[Bug] Yarn prints undefined fields w/ init
2 participants