Skip to content

Commit 59cb7cd

Browse files
committed
Add(Explore Config): Support name with extension
1 parent 1cb07b5 commit 59cb7cd

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

packages/explore-config/src/index.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import crossImport from 'cross-import'
22
import { existsSync } from 'fs'
3-
import { resolve } from 'path'
3+
import { parse, resolve } from 'path'
44

55
export interface ExploreConfigOptions {
66
extensions?: ('js' | 'ts' | 'cjs' | 'cts' | 'mjs' | 'mts')[]
@@ -18,13 +18,23 @@ export default function exploreConfig(name: string, options: ExploreConfigOption
1818
// try to find the config file with the given name and options.extensions
1919
let foundConfigPath: string | undefined
2020
let foundBasename: string | undefined
21-
for (const eachExtension of options.extensions) {
22-
const eachBasename = name + '.' + eachExtension
23-
const eachPath = resolve(options.cwd || '', eachBasename)
24-
if (existsSync(eachPath)) {
25-
foundConfigPath = eachPath
26-
foundBasename = eachBasename
27-
break
21+
if (options.extensions.find(ext => name.endsWith('.' + ext))) {
22+
const resolvedPath = resolve(options.cwd || '', name)
23+
if (existsSync(resolvedPath)) {
24+
foundConfigPath = resolvedPath
25+
foundBasename = parse(name).base
26+
}
27+
} else {
28+
for (const eachExtension of options.extensions) {
29+
const eachBasename = name.endsWith('.' + eachExtension)
30+
? name
31+
: name + '.' + eachExtension
32+
const eachPath = resolve(options.cwd || '', eachBasename)
33+
if (existsSync(eachPath)) {
34+
foundConfigPath = eachPath
35+
foundBasename = eachBasename
36+
break
37+
}
2838
}
2939
}
3040
if (foundConfigPath) {

packages/explore-config/tests/test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname } from 'path'
1+
import { dirname, resolve } from 'path'
22
import exploreConfig from '../src'
33

44
it('read .js config', () => {
@@ -29,6 +29,20 @@ it('read .ts config', () => {
2929
})
3030
})
3131

32+
it('read .ts ext config', () => {
33+
const config = exploreConfig(resolve(__dirname, './fixtures/master.css.ts'), { cwd: __dirname })
34+
expect(config).toEqual({
35+
extends: [
36+
{
37+
styles: {
38+
card: 'inline-flex'
39+
}
40+
}
41+
],
42+
colors: { primary: '#ff0' }
43+
})
44+
})
45+
3246
it('read .mjs config', () => {
3347
const config = exploreConfig('./fixtures/master.css', { cwd: __dirname, extensions: ['mjs'] })
3448
expect(config).toEqual({

0 commit comments

Comments
 (0)