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

Commit 70ef403

Browse files
Add support for alternative config files with cosmiconfig (#178)
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]>
1 parent 7af1e39 commit 70ef403

File tree

4 files changed

+147
-16
lines changed

4 files changed

+147
-16
lines changed

package-lock.json

Lines changed: 80 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@typescript-eslint/parser": "^5.27.1",
4242
"@typescript-eslint/typescript-estree": "^5.27.1",
4343
"chalk": "^4.1.0",
44+
"cosmiconfig": "^8.3.6",
4445
"debug": "^4.3.2",
4546
"file-entry-cache": "^6.0.1",
4647
"flow-remove-types": "2.156.0",

src/__tests__/cli.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,68 @@ cases(
181181
exitCode: 1,
182182
stdout: /1 unimported files.*bar.js/s,
183183
},
184+
{
185+
name: 'should support JSON config',
186+
files: [
187+
{ name: 'package.json', content: '{}' },
188+
{
189+
name: '.unimportedrc.json',
190+
content: '{ "entry": ["entry.js"] }',
191+
},
192+
{ name: 'entry.js', content: `import foo from './foo';` },
193+
{ name: 'foo.js', content: '' },
194+
{ name: 'bar.js', content: '' },
195+
],
196+
exitCode: 1,
197+
stdout: /1 unimported files.*bar.js/s,
198+
},
199+
{
200+
name: 'should support JS config',
201+
files: [
202+
{ name: 'package.json', content: '{}' },
203+
{
204+
name: '.unimportedrc.js',
205+
content: 'module.exports = { entry: ["entry.js"] }',
206+
},
207+
{ name: 'entry.js', content: `import foo from './foo';` },
208+
{ name: 'foo.js', content: '' },
209+
{ name: 'bar.js', content: '' },
210+
],
211+
exitCode: 1,
212+
stdout: /1 unimported files.*bar.js/s,
213+
},
214+
{
215+
name: 'should support YML config',
216+
files: [
217+
{ name: 'package.json', content: '{}' },
218+
{
219+
name: '.unimportedrc.yml',
220+
content: `
221+
entry:
222+
- entry.js
223+
`,
224+
},
225+
{ name: 'entry.js', content: `import foo from './foo';` },
226+
{ name: 'foo.js', content: '' },
227+
{ name: 'bar.js', content: '' },
228+
],
229+
exitCode: 1,
230+
stdout: /1 unimported files.*bar.js/s,
231+
},
232+
{
233+
name: 'should support package.json config',
234+
files: [
235+
{
236+
name: 'package.json',
237+
content: '{ "unimported": { "entry": ["entry.js"] } }',
238+
},
239+
{ name: 'entry.js', content: `import foo from './foo';` },
240+
{ name: 'foo.js', content: '' },
241+
{ name: 'bar.js', content: '' },
242+
],
243+
exitCode: 1,
244+
stdout: /1 unimported files.*bar.js/s,
245+
},
184246
{
185247
name: 'should identify unresolved imports',
186248
files: [
@@ -893,7 +955,7 @@ export default promise
893955
{ name: 'helpers/index.ts', content: '' },
894956
{
895957
name: '.unimportedrc.json',
896-
content: '{ "pathTransforms": { "(\\..+)\\.js$": "$1.ts" } }',
958+
content: '{ "pathTransforms": { "(..+).js$": "$1.ts" } }',
897959
},
898960
],
899961
exitCode: 0,

src/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { cosmiconfigSync } from 'cosmiconfig';
12
import { ProcessedResult } from './process';
23
import { readJson, writeJson } from './fs';
34
import { CliArguments, Context, PackageJson } from './index';
@@ -137,9 +138,8 @@ export async function getConfig(args?: CliArguments): Promise<Config> {
137138
return cachedConfig;
138139
}
139140

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

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

0 commit comments

Comments
 (0)