Skip to content

Commit

Permalink
Add(Explore Config): Support name with extension
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Dec 19, 2024
1 parent 1cb07b5 commit 59cb7cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
26 changes: 18 additions & 8 deletions packages/explore-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crossImport from 'cross-import'
import { existsSync } from 'fs'
import { resolve } from 'path'
import { parse, resolve } from 'path'

export interface ExploreConfigOptions {
extensions?: ('js' | 'ts' | 'cjs' | 'cts' | 'mjs' | 'mts')[]
Expand All @@ -18,13 +18,23 @@ export default function exploreConfig(name: string, options: ExploreConfigOption
// try to find the config file with the given name and options.extensions
let foundConfigPath: string | undefined
let foundBasename: string | undefined
for (const eachExtension of options.extensions) {
const eachBasename = name + '.' + eachExtension
const eachPath = resolve(options.cwd || '', eachBasename)
if (existsSync(eachPath)) {
foundConfigPath = eachPath
foundBasename = eachBasename
break
if (options.extensions.find(ext => name.endsWith('.' + ext))) {
const resolvedPath = resolve(options.cwd || '', name)
if (existsSync(resolvedPath)) {
foundConfigPath = resolvedPath
foundBasename = parse(name).base
}
} else {
for (const eachExtension of options.extensions) {
const eachBasename = name.endsWith('.' + eachExtension)
? name
: name + '.' + eachExtension
const eachPath = resolve(options.cwd || '', eachBasename)
if (existsSync(eachPath)) {
foundConfigPath = eachPath
foundBasename = eachBasename
break
}
}
}
if (foundConfigPath) {
Expand Down
16 changes: 15 additions & 1 deletion packages/explore-config/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname } from 'path'
import { dirname, resolve } from 'path'
import exploreConfig from '../src'

it('read .js config', () => {
Expand Down Expand Up @@ -29,6 +29,20 @@ it('read .ts config', () => {
})
})

it('read .ts ext config', () => {
const config = exploreConfig(resolve(__dirname, './fixtures/master.css.ts'), { cwd: __dirname })
expect(config).toEqual({
extends: [
{
styles: {
card: 'inline-flex'
}
}
],
colors: { primary: '#ff0' }
})
})

it('read .mjs config', () => {
const config = exploreConfig('./fixtures/master.css', { cwd: __dirname, extensions: ['mjs'] })
expect(config).toEqual({
Expand Down

0 comments on commit 59cb7cd

Please sign in to comment.