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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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