Skip to content

docs: generate config from json schema#1008

Merged
arcanis merged 5 commits into
yarnpkg:masterfrom
paul-soporan:docs/config-from-json-schema
Mar 4, 2020
Merged

docs: generate config from json schema#1008
arcanis merged 5 commits into
yarnpkg:masterfrom
paul-soporan:docs/config-from-json-schema

Conversation

@paul-soporan
Copy link
Copy Markdown
Member

This PR is an experiment.

What's the problem this PR addresses?

.yarnrc.yml files don't currently have an official / community-made JSON schema. I decided to create a JSON schema, but after I had made it, I realized that the descriptions would have to be kept in sync with the website.

How did you fix it?

This PR adds a way to generate a configuration page from a JSON schema.

It is currently only implemented for Yarnrc files.

What does this PR add?

  • A Yarnrc JSON schema (stored at packages/gatsby/src/pages/configuration/yarnrc.json). I have triple-checked it. The descriptions are up-to-date with the website. It also includes types for all of the entries (including regex patterns for some string properties).
  • A convertSchemaToConfiguration function used to generate a configuration page from a JSON schema (Currently only works for YAML configs, it can easily be made to work with JSON). It doesn't support options that aren't currently used for generating the Yarnrc configuration page, but it can be easily tweaked. Config entries can be configured using the $comment keyword. It is parsed as JSON.
{
  // For scalars
  "$comment": "{ \"anchor\": \"entry.anotherEntry\" }",
  // For objects
  "$comment": "{ \"margin\": false }",
  // Overriding ref entries (Currently only supports the `default` field, I should probably also add the `description` field)
  "$comment": "{ \"overrides\": { \"default\": \"anotherDefault\" } }",
}

What possibilities does this open up?

  • Adding the ability to fetch the JSON schema from a URL like https://yarnpkg.com/configuration/yarnrc.json or https://yarnpkg.com/configuration/schemas/yarnrc.
  • Adding an expandable details section that includes types for every config entry.

Comment thread packages/gatsby/src/utils/schemaUtils.js Outdated
@arcanis
Copy link
Copy Markdown
Member

arcanis commented Mar 2, 2020

I really like the idea btw 👍

Comment thread .yarnrc.yml
Comment on lines +36 to +38
"json-ref-lite@*":
dependencies:
"sync-request": "*"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need it? From a cursory glance in json-ref-lite, it's only used in a particular case:

image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You're right, it's not actually needed. I added it a few days ago to get rid of a weird error message that happened only in specific cases that I don't remember anymore and that I can't reproduce anymore.😅
As I was writing this, Netlify just made me remember.😄

Module not found: Error: A package is trying to access another package without   the second one being listed as a dependency of the first one
11:07:16 PM:   Required package: sync-request (via "sync-request")
11:07:16 PM:   Required by: json-ref-lite@npm:1.1.0

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If instead I do

"json-ref-lite@*":
  peerDependenciesMeta:
    "sync-request":
      optional: true

I get:

Module not found: Error: A package is trying to access a peer dependency that should be provi
  ded by its direct ancestor but isn't
  Required package: sync-request (via "sync-request")
  Required by: json-ref-lite@virtual

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Indeed 🙁 This is because Webpack resolves the require call even if it's a dead call... annoying, but I guess that's the way to go.

@arcanis arcanis merged commit f605546 into yarnpkg:master Mar 4, 2020
@paul-soporan paul-soporan deleted the docs/config-from-json-schema branch March 7, 2020 11:30
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.

2 participants