Skip to content

Commit

Permalink
added: mergeConfig fn
Browse files Browse the repository at this point in the history
  • Loading branch information
binjospookie committed Jan 2, 2024
1 parent 9cc3689 commit b254f8f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
13 changes: 5 additions & 8 deletions src/api/collectUsages.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { collectUsages as _collectUsages } from '~/collectUsages.js';
import { BASE_CONFIG, type Config } from '~/getConfig/index.js';
import { mergeConfig, type Config } from '~/getConfig/index.js';
import { Ok, Err } from '~/shared/index.js';

type ListItem = {
Expand All @@ -13,14 +13,11 @@ type ListItem = {
const collectUsages = async (name: string, list: ListItem[]) => {
const tasks = list.map((x) =>
_collectUsages({
config: {
batch: x.batch || BASE_CONFIG.batch,
exclude: x.exclude ? [...new Set([...BASE_CONFIG.exclude, ...x.exclude])] : BASE_CONFIG.exclude,
extensions: x.extensions || BASE_CONFIG.extensions,
dir: x.dir,
// @ts-expect-error 123
config: mergeConfig({
...x,
collectUsages: name,
parserConfig: x.parserConfig || BASE_CONFIG.parserConfig,
},
}),
}),
);

Expand Down
10 changes: 2 additions & 8 deletions src/api/findUnusedExports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { findUnusedExports as _findUnusedExports } from '~/findUnusedExports.js';
import { BASE_CONFIG, type Config } from '~/getConfig/index.js';
import { mergeConfig, type Config } from '~/getConfig/index.js';
import { readJSON, Ok, Err, type NonEmptyArray } from '~/shared/index.js';

type TaskResult = Awaited<ReturnType<typeof _findUnusedExports>>;
Expand Down Expand Up @@ -51,13 +51,7 @@ const findUnusedExports = async (entry: string, list: ListItem[]) => {
const tasks = list.map((x) =>
_findUnusedExports({
pkg,
config: {
batch: x.batch || BASE_CONFIG.batch,
exclude: x.exclude ? [...new Set([...BASE_CONFIG.exclude, ...x.exclude])] : BASE_CONFIG.exclude,
extensions: x.extensions || BASE_CONFIG.extensions,
dir: x.dir,
parserConfig: x.parserConfig || BASE_CONFIG.parserConfig,
},
config: mergeConfig(x),
}),
);

Expand Down
20 changes: 20 additions & 0 deletions src/getConfig/__tests__/mergeConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, test } from 'vitest';

import { mergeConfig } from '../index.js';

test('mergeConfig', () => {
const data = {
dir: 'dir',
batch: 1,
exclude: ['exclude'],
extensions: ['jsx'],
parserConfig: { syntax: 'ecmascript' } as const,
collectUsages: 'collectUsages',
entry: 'entry',
};

expect(mergeConfig(data)).toStrictEqual({
...data,
exclude: ['node_modules', ...data.exclude],
});
});
41 changes: 19 additions & 22 deletions src/getConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Config = {
const BASE_CONFIG: Config = {
batch: 100,
collectUsages: null,
entry: 'index.ts',
dir: '',
entry: 'index.ts',
exclude: ['node_modules'],
extensions: ['ts', 'tsx'],
parserConfig: {
Expand All @@ -28,30 +28,27 @@ const BASE_CONFIG: Config = {
},
};

const mergeConfig = (x: Partial<Config>): Config => ({
batch: x.batch || BASE_CONFIG.batch,
collectUsages: x.collectUsages || BASE_CONFIG.collectUsages,
dir: x.dir || getRepoRoot(),
entry: x.entry || BASE_CONFIG.entry,
exclude: x.exclude ? [...new Set([...BASE_CONFIG.exclude, ...x.exclude])] : BASE_CONFIG.exclude,
extensions: x.extensions || BASE_CONFIG.extensions,
parserConfig: x.parserConfig || BASE_CONFIG.parserConfig,
});

const getConfig = async (): Promise<Config> => {
const result = (await lilconfig('pure-index', {
searchPlaces: ['package.json', '.pure-index.json', '.pure-index.js', '.pure-index.cjs'],
}).search()) || { config: BASE_CONFIG };

const {
exclude = [],
entry = BASE_CONFIG.entry,
batch = BASE_CONFIG.batch,
extensions = BASE_CONFIG.extensions,
dir,
parserConfig = BASE_CONFIG.parserConfig,
} = result.config;

return {
entry: cli.flags.entry || entry,
batch,
parserConfig,
exclude: [...new Set([...BASE_CONFIG.exclude, ...exclude])],
collectUsages: cli.flags.collectUsages || BASE_CONFIG.collectUsages,
extensions,
dir: dir || getRepoRoot(),
};
}).search()) || { config: {} };

return mergeConfig({
...result.config,
entry: cli.flags.entry || result.config.entry,
collectUsages: cli.flags.collectUsages,
});
};

export { getConfig, BASE_CONFIG };
export { getConfig, mergeConfig, BASE_CONFIG };
export type { Config };

0 comments on commit b254f8f

Please sign in to comment.