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

Commit

Permalink
Add support for alternative config files with cosmiconfig (#178)
Browse files Browse the repository at this point in the history
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.

---------

Co-authored-by: Stephan Meijer <[email protected]>
  • Loading branch information
ryanwilsonperkin and smeijer authored Sep 22, 2023
1 parent 7af1e39 commit 70ef403
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 16 deletions.
92 changes: 80 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@typescript-eslint/parser": "^5.27.1",
"@typescript-eslint/typescript-estree": "^5.27.1",
"chalk": "^4.1.0",
"cosmiconfig": "^8.3.6",
"debug": "^4.3.2",
"file-entry-cache": "^6.0.1",
"flow-remove-types": "2.156.0",
Expand Down
64 changes: 63 additions & 1 deletion src/__tests__/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,68 @@ cases(
exitCode: 1,
stdout: /1 unimported files.*bar.js/s,
},
{
name: 'should support JSON config',
files: [
{ name: 'package.json', content: '{}' },
{
name: '.unimportedrc.json',
content: '{ "entry": ["entry.js"] }',
},
{ name: 'entry.js', content: `import foo from './foo';` },
{ name: 'foo.js', content: '' },
{ name: 'bar.js', content: '' },
],
exitCode: 1,
stdout: /1 unimported files.*bar.js/s,
},
{
name: 'should support JS config',
files: [
{ name: 'package.json', content: '{}' },
{
name: '.unimportedrc.js',
content: 'module.exports = { entry: ["entry.js"] }',
},
{ name: 'entry.js', content: `import foo from './foo';` },
{ name: 'foo.js', content: '' },
{ name: 'bar.js', content: '' },
],
exitCode: 1,
stdout: /1 unimported files.*bar.js/s,
},
{
name: 'should support YML config',
files: [
{ name: 'package.json', content: '{}' },
{
name: '.unimportedrc.yml',
content: `
entry:
- entry.js
`,
},
{ name: 'entry.js', content: `import foo from './foo';` },
{ name: 'foo.js', content: '' },
{ name: 'bar.js', content: '' },
],
exitCode: 1,
stdout: /1 unimported files.*bar.js/s,
},
{
name: 'should support package.json config',
files: [
{
name: 'package.json',
content: '{ "unimported": { "entry": ["entry.js"] } }',
},
{ name: 'entry.js', content: `import foo from './foo';` },
{ name: 'foo.js', content: '' },
{ name: 'bar.js', content: '' },
],
exitCode: 1,
stdout: /1 unimported files.*bar.js/s,
},
{
name: 'should identify unresolved imports',
files: [
Expand Down Expand Up @@ -893,7 +955,7 @@ export default promise
{ name: 'helpers/index.ts', content: '' },
{
name: '.unimportedrc.json',
content: '{ "pathTransforms": { "(\\..+)\\.js$": "$1.ts" } }',
content: '{ "pathTransforms": { "(..+).js$": "$1.ts" } }',
},
],
exitCode: 0,
Expand Down
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cosmiconfigSync } from 'cosmiconfig';
import { ProcessedResult } from './process';
import { readJson, writeJson } from './fs';
import { CliArguments, Context, PackageJson } from './index';
Expand Down Expand Up @@ -137,9 +138,8 @@ export async function getConfig(args?: CliArguments): Promise<Config> {
return cachedConfig;
}

const configFile = await readJson<Partial<UnimportedConfig>>(
args?.config || CONFIG_FILE,
);
const cosmiconfigResult = cosmiconfigSync('unimported').search();
const configFile = cosmiconfigResult?.config as Partial<UnimportedConfig>;

const unimportedPkg = await readPkgUp({ cwd: __dirname });

Expand Down

0 comments on commit 70ef403

Please sign in to comment.