Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

feat: add support for alternative config files with cosmiconfig #178

Merged
merged 7 commits into from Sep 22, 2023

Conversation

ryanwilsonperkin
Copy link
Collaborator

@ryanwilsonperkin ryanwilsonperkin commented Sep 22, 2023

Fixes #118

Introduces cosmiconfig as requested in #118 in order to support loading the config from alternative file formats like:

.unimportedrc.js
.unimportedrc.yml
package.json > "unimported" key

I'm using cosmiconfigSync utilities instead of the async version, despite them being available since the getConfig is an async method. When we use the async equivalent, we hit a segfault in Node as a result of cosmiconfig trying to call a dynamic import on the file within a Jest context that doesn't allow it to. Patched in a recent version of Node, and once the test infra can require the latest version it should be fine to switch to the async version.

See nodejs/node#35889 and jestjs/jest#11438

I haven't made any efforts to change the update function to write updates to the loaded files, as this would be difficult/impossible to update something like a .js or .yml (if using features like anchors) in a meaningful way.

A few notes on the other changes included:

  1. cosmiconfig pulls in a newer version of TypeScript which was incompatible with the version of @types/node we used, updated it
  2. One of the tests produced invalid JSON to a config file, which failed silently before and passed the test, but now fails loudly when cosmiconfig tries to read and parse the JSON. Updated it to be valid to fulfill the spirit of the test.

Consistently running into a segfault coming from Node as a result of
cosmiconfig attempting to run a dynamic import on the listed .js config
file caused by:

nodejs/node#35889
jestjs/jest#11438

Using cosmiconfigSync to workaround this which will fall back to using a
synchronous require() call.
Copy link
Owner

@smeijer smeijer left a comment

Choose a reason for hiding this comment

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

Thanks! This looks amazing.

@smeijer
Copy link
Owner

smeijer commented Sep 22, 2023

@all-contributors please add @ryanwilsonperkin for code, test

@allcontributors
Copy link
Contributor

@smeijer

I've put up a pull request to add @ryanwilsonperkin! 🎉

src/__tests__/cli.ts Outdated Show resolved Hide resolved
@smeijer
Copy link
Owner

smeijer commented Sep 22, 2023

I've also added you as a collaborator on the project. Please continue to make PRs as you feel the need. You can now make your branches directly on the repo rather than your fork if you want.

Thanks for your contributions! Much appreciated. ❤️

@ryanwilsonperkin
Copy link
Collaborator Author

Thanks! Had started building a similar tool to help me do some dead code analysis and then came across this tool. Happy to be able to contribute to it

@smeijer smeijer merged commit 70ef403 into smeijer:main Sep 22, 2023
7 checks passed
@smeijer
Copy link
Owner

smeijer commented Sep 22, 2023

🎉 This PR is included in version 1.30.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@smeijer smeijer changed the title Add support for alternative config files with cosmiconfig feat: add support for alternative config files with cosmiconfig Sep 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow YAML or JS format for configuration file
2 participants