Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion packages/cli-kit/src/public/node/framework.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {resolveFramework} from './framework.js'
import {resolveFramework, _frameworks} from './framework.js'
import {inTemporaryDirectory, writeFile} from './fs.js'
import {joinPath} from './path.js'
import {describe, expect, test} from 'vitest'
Expand Down Expand Up @@ -131,4 +131,36 @@ describe('frontFrameworkUsed', () => {
expect(got).toEqual('unknown')
})
})

test('matches a custom framework if any detector in some matches and all in every match', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
_frameworks.push({
name: 'custom-framework',
detectors: {
some: [
{path: 'package.json', matchContent: '"custom-dep-1"'},
{path: 'package.json', matchContent: '"custom-dep-2"'},
],
every: [{path: 'package.json', matchContent: '"framework-core"'}],
},
})

try {
const packageJsonPath = joinPath(tmpDir, 'package.json')
const packageJson = {
dependencies: {'framework-core': '1.0.0', 'custom-dep-2': '1.0.0'},
}
await writeFile(packageJsonPath, JSON.stringify(packageJson))

// When
const got = await resolveFramework(tmpDir)

// Then
expect(got).toEqual('custom-framework')
} finally {
_frameworks.pop()
}
})
})
})
34 changes: 17 additions & 17 deletions packages/cli-kit/src/public/node/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,21 @@ const frameworks: Framework[] = [
export async function resolveFramework(rootDirectory: string): Promise<string> {
const fwConfigFiles: Record<string, string | undefined> = {}

const matchedFramework = frameworks.find(
(framework) =>
(!framework.detectors.some ||
framework.detectors.some.reduce(
(_previousDetectorsMatch: boolean, detector) =>
matchDetector(detector, loadFwConfigFile(rootDirectory, detector.path, fwConfigFiles)),
false,
)) &&
(!framework.detectors.every ||
framework.detectors.every.reduce(
(previousDetectorsMatch: boolean, detector) =>
previousDetectorsMatch
? matchDetector(detector, loadFwConfigFile(rootDirectory, detector.path, fwConfigFiles))
: false,
true,
)),
)
const matchedFramework = frameworks.find((framework) => {
const matchesSome =
!framework.detectors.some ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the frameworks definition, I think detectors.some is always undefined, should we simplify this? :thinking_face:

framework.detectors.some.some((detector) =>
matchDetector(detector, loadFwConfigFile(rootDirectory, detector.path, fwConfigFiles)),
)

const matchesEvery =
!framework.detectors.every ||
framework.detectors.every.every((detector) =>
matchDetector(detector, loadFwConfigFile(rootDirectory, detector.path, fwConfigFiles)),
)

return matchesSome && matchesEvery
})

return matchedFramework ? matchedFramework.name : 'unknown'
}
Expand Down Expand Up @@ -197,3 +195,5 @@ function loadFwConfigFile(
fwConfigFiles[fwConfigFileName] = rawContent
return fwConfigFiles
}

export const _frameworks = frameworks
Loading