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

feat: support comments in vscode settings #5436

Merged
merged 9 commits into from
Feb 9, 2023

Conversation

tinfoil-knight
Copy link
Contributor

@tinfoil-knight tinfoil-knight commented Jan 28, 2023

πŸŽ‰ Thanks for submitting a pull request! πŸŽ‰

Summary

Fixes #5407

This PR adds support for having comments in the settings.json file used by VSCode while using the netlify recipes vscode command.


For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code πŸ§‘β€πŸ’». This ensures we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or something that`s on fire πŸ”₯ (e.g. incident related), you can skip this step.
  • Read the contribution guidelines πŸ“–. This ensures your code follows our style guide and
    passes our tests.
  • Update or add tests (if any source code was changed or added) πŸ§ͺ
  • Update or add documentation (if features were changed or added) πŸ“
  • Make sure the status checks below are successful βœ…

A picture of a cute animal (not mandatory, but encouraged)

@tinfoil-knight tinfoil-knight self-assigned this Jan 28, 2023
@tinfoil-knight tinfoil-knight added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Jan 28, 2023
@github-actions
Copy link

github-actions bot commented Jan 28, 2023

πŸ“Š Benchmark results

Comparing with b44eb25

Package size: 260 MB

⬆️ 0.08% increase vs. b44eb25

^  265 MB  265 MB  265 MB  266 MB  265 MB  266 MB  266 MB  261 MB  262 MB  262 MB  260 MB  260 MB  260 MB 
β”‚   β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

@tinfoil-knight tinfoil-knight marked this pull request as ready for review January 28, 2023 14:21
@@ -1,17 +1,17 @@
import { mkdir, readFile, stat, writeFile } from 'fs/promises'
import { dirname, relative } from 'path'

import CommentJSON from 'comment-json'
Copy link
Contributor

@danez danez Jan 30, 2023

Choose a reason for hiding this comment

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

Looking at the types of comment-json, it looks like it does not have a default export: https://github.com/kaelzhang/node-comment-json/blob/master/index.d.ts#L106

could we instead do:

import { assign, parse, stringify } from 'comment-json'

Or does that not work? The lib is written in commonjs, but at a quick glance it should work.

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 works. I've updated it.

@kaelig
Copy link
Member

kaelig commented Jan 30, 2023

Thank you so much for taking that on! I'd love to see some tests to prevent any future regressions πŸ™πŸ»

@tinfoil-knight
Copy link
Contributor Author

I'd love to see some tests to prevent any future regressions πŸ™πŸ»

Will add a test for JSON with comments.

t.is(settings.someSetting, 'value')
t.is(settings['deno.enable'], true)
t.is(settings['deno.importMap'], '.netlify/edge-functions-import-map.json')
t.deepEqual([...settings['deno.enablePaths']], ['/some/path', 'netlify/edge-functions'])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Spreading the array here since that converts it from the CommentArray type to a native JS array for comparison.

Comment on lines +124 to +128
{
question: 'The Deno VS Code extension is recommended. Would you like to install it now?',
answer: answerWithValue(NO),
},
])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@danez Note here:

The Deno VS Code extension is recommended. Would you like to install it now? prompt is there on my local system but it seems like this question is not asked during the CI run. The test just ends up stuck on local env & didn't run before adding the question.

I've added the prompt for this test but this issue is there for other tests too. Should I add the extra prompt for other tests too (wherever applicable) in a separate PR?

@tinfoil-knight
Copy link
Contributor Author

@kaelig added the test.

tests/integration/640.command.recipes.test.cjs Outdated Show resolved Hide resolved
@@ -1,17 +1,17 @@
import { mkdir, readFile, stat, writeFile } from 'fs/promises'
import { dirname, relative } from 'path'

import { parse, assign, stringify } from 'comment-json'
Copy link
Member

Choose a reason for hiding this comment

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

This is totally down to personal preference, but I would suggest having a single JSONC import here and then use it throughout the file as JSONC.parse, JSONC.stringify, etc.

Seeing a function called stringify or parse doesn't give a lot of context and makes the code a little bit harder to parse, IMO.

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 was a single CommentJSON import earlier which is what I prefer. @danez suggested otherwise.

The separate functions seem slightly confusing to me too without any context. WDYT @danez?

Copy link
Contributor

Choose a reason for hiding this comment

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

The problem is that the TS types say that these are named exports. https://github.com/kaelzhang/node-comment-json/blob/master/index.d.ts#L83 So as soon as we switch to TS this will not work, because TS would say no default export.

Copy link
Member

Choose a reason for hiding this comment

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

Could we do import * as JSSONC from "json-comments"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I guess that should work.

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
@@ -42,7 +42,7 @@ export const getSettings = async (settingsPath) => {

return {
fileExists: true,
settings: JSON.parse(file),
settings: parse(file.toString()),
Copy link
Member

Choose a reason for hiding this comment

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

Rather than doing a toString() here, I think you can do await readFile(settingsPath, "utf8") above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. That seems better.

@tinfoil-knight
Copy link
Contributor Author

tinfoil-knight commented Feb 9, 2023

@danez I think we can merge this now. Also, please see: https://github.com/netlify/cli/pull/5436/files#r1096563844

@danez danez added the automerge Add to Kodiak auto merge queue label Feb 9, 2023
@kodiakhq kodiakhq bot merged commit 233bb57 into netlify:main Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Add to Kodiak auto merge queue type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

netlify recipes vscode crashes when reading .vscode/settings.json that contains comments
4 participants