Skip to content

Commit 662687f

Browse files
committed
feat(config): discover supported config file extensions
1 parent 935c1f1 commit 662687f

4 files changed

Lines changed: 99 additions & 21 deletions

File tree

‎.changeset/quiet-dingos-detect.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@node-core/doc-kit': patch
33
---
44

5-
Automatically load `doc-kit.config.mjs` from the current working directory.
5+
Automatically discover the first supported `doc-kit.config.*` file in the current working directory when `--config-file` is omitted.

‎src/utils/__tests__/loaders.test.mjs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import assert from 'node:assert/strict';
2+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
3+
import { tmpdir } from 'node:os';
4+
import { join } from 'node:path';
25
import { describe, it, mock, afterEach } from 'node:test';
36

47
let fileContent = 'hello from file';
@@ -66,4 +69,35 @@ describe('importFromURL', () => {
6669
assert.ok(typeof mod.loadFromURL === 'function');
6770
assert.ok(typeof mod.importFromURL === 'function');
6871
});
72+
73+
const typeScriptConfigCases = [
74+
{
75+
extension: 'ts',
76+
source:
77+
"const input: string = 'src';\nexport default { global: { input } };\n",
78+
},
79+
{
80+
extension: 'cts',
81+
source:
82+
"const input: string = 'src';\nmodule.exports = { global: { input } };\n",
83+
},
84+
{
85+
extension: 'mts',
86+
source:
87+
"const input: string = 'src';\nexport default { global: { input } };\n",
88+
},
89+
];
90+
91+
for (const { extension, source } of typeScriptConfigCases) {
92+
it(`should import a type-strippable .${extension} config file`, async t => {
93+
const directory = await mkdtemp(join(tmpdir(), 'doc-kit-config-'));
94+
t.after(() => rm(directory, { recursive: true, force: true }));
95+
const configFile = join(directory, `doc-kit.config.${extension}`);
96+
await writeFile(configFile, source);
97+
98+
const config = await importFromURL(configFile);
99+
100+
assert.deepStrictEqual(config, { global: { input: 'src' } });
101+
});
102+
}
69103
});

‎src/utils/configuration/__tests__/index.test.mjs‎

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ const mockParseIndex = mock.fn(async index => [index]);
99
const mockImportFromURL = mock.fn(async () => ({}));
1010
const mockExistsSync = mock.fn(() => false);
1111

12+
const CONFIG_FILE_NAMES = [
13+
'doc-kit.config.js',
14+
'doc-kit.config.cjs',
15+
'doc-kit.config.mjs',
16+
'doc-kit.config.ts',
17+
'doc-kit.config.cts',
18+
'doc-kit.config.mts',
19+
];
20+
1221
const createMockConfig = (overrides = {}) => ({
1322
global: {},
1423
...overrides,
@@ -55,6 +64,8 @@ const resetAllMocks = () => {
5564
mockImportFromURL,
5665
mockExistsSync,
5766
].forEach(m => m.mock.resetCalls());
67+
mockImportFromURL.mock.mockImplementation(async () => ({}));
68+
mockExistsSync.mock.mockImplementation(() => false);
5869
};
5970

6071
// Helper to count specific function calls
@@ -158,26 +169,50 @@ describe('config.mjs', () => {
158169
});
159170

160171
describe('createRunConfiguration', () => {
161-
it('should auto-detect a config file in the current directory', async () => {
162-
const defaultConfigFile = join(process.cwd(), 'doc-kit.config.mjs');
163-
const mockConfig = createMockConfig({
164-
global: { input: 'auto-detected-src/' },
165-
});
166-
mockExistsSync.mock.mockImplementationOnce(() => true);
167-
mockImportFromURL.mock.mockImplementationOnce(async () => mockConfig);
172+
for (const [index, fileName] of CONFIG_FILE_NAMES.entries()) {
173+
it(`should auto-detect ${fileName} in the current directory`, async () => {
174+
const detectedConfigFile = join(process.cwd(), fileName);
175+
const mockConfig = createMockConfig({
176+
global: { input: 'auto-detected-src/' },
177+
});
178+
mockExistsSync.mock.mockImplementation(
179+
filePath => filePath === detectedConfigFile
180+
);
181+
mockImportFromURL.mock.mockImplementationOnce(async () => mockConfig);
168182

169-
const config = await createRunConfiguration({});
183+
const config = await createRunConfiguration({});
170184

171-
assert.strictEqual(config.global.input, 'auto-detected-src/');
172-
assert.strictEqual(mockExistsSync.mock.calls.length, 1);
173-
assert.strictEqual(
174-
mockExistsSync.mock.calls[0].arguments[0],
175-
defaultConfigFile
185+
assert.strictEqual(config.global.input, 'auto-detected-src/');
186+
assert.deepStrictEqual(
187+
mockExistsSync.mock.calls.map(call => call.arguments[0]),
188+
CONFIG_FILE_NAMES.slice(0, index + 1).map(candidate =>
189+
join(process.cwd(), candidate)
190+
)
191+
);
192+
assert.strictEqual(mockImportFromURL.mock.calls.length, 1);
193+
assert.strictEqual(
194+
mockImportFromURL.mock.calls[0].arguments[0],
195+
detectedConfigFile
196+
);
197+
});
198+
}
199+
200+
it('should use the first matching config file by priority', async () => {
201+
const existingConfigFiles = new Set([
202+
join(process.cwd(), 'doc-kit.config.cjs'),
203+
join(process.cwd(), 'doc-kit.config.mjs'),
204+
join(process.cwd(), 'doc-kit.config.cts'),
205+
]);
206+
mockExistsSync.mock.mockImplementation(filePath =>
207+
existingConfigFiles.has(filePath)
176208
);
209+
210+
await createRunConfiguration({});
211+
177212
assert.strictEqual(mockImportFromURL.mock.calls.length, 1);
178213
assert.strictEqual(
179214
mockImportFromURL.mock.calls[0].arguments[0],
180-
defaultConfigFile
215+
join(process.cwd(), 'doc-kit.config.cjs')
181216
);
182217
});
183218

@@ -262,10 +297,9 @@ describe('config.mjs', () => {
262297

263298
assert.ok(config);
264299
assert.strictEqual(config.threads, 4);
265-
assert.strictEqual(mockExistsSync.mock.calls.length, 1);
266-
assert.strictEqual(
267-
mockExistsSync.mock.calls[0].arguments[0],
268-
join(process.cwd(), 'doc-kit.config.mjs')
300+
assert.deepStrictEqual(
301+
mockExistsSync.mock.calls.map(call => call.arguments[0]),
302+
CONFIG_FILE_NAMES.map(fileName => join(process.cwd(), fileName))
269303
);
270304
assert.strictEqual(mockImportFromURL.mock.calls.length, 0);
271305
});

‎src/utils/configuration/index.mjs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ import { leftHandAssign } from '../generators.mjs';
1414
import { importFromURL } from '../loaders.mjs';
1515
import { deepMerge, lazy } from '../misc.mjs';
1616

17+
const CONFIG_FILE_NAMES = [
18+
'doc-kit.config.js',
19+
'doc-kit.config.cjs',
20+
'doc-kit.config.mjs',
21+
'doc-kit.config.ts',
22+
'doc-kit.config.cts',
23+
'doc-kit.config.mts',
24+
];
25+
1726
/**
1827
* Get's the default configuration
1928
*/
@@ -134,10 +143,11 @@ export const assertRunnableOptions = config => {
134143
* @returns {Promise<import('./types').Configuration>} The configuration
135144
*/
136145
export const createRunConfiguration = async options => {
137-
const defaultConfigFile = join(process.cwd(), 'doc-kit.config.mjs');
138146
const configFile =
139147
options.configFile ??
140-
(existsSync(defaultConfigFile) ? defaultConfigFile : undefined);
148+
CONFIG_FILE_NAMES.map(fileName => join(process.cwd(), fileName)).find(
149+
existsSync
150+
);
141151
const config = await loadConfigFile(configFile);
142152
config.target &&= enforceArray(config.target);
143153

0 commit comments

Comments
 (0)