This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathdetect.js
46 lines (41 loc) · 1.66 KB
/
detect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pLocate from 'p-locate'
// Checks if the project is using a specific framework:
// - if `framework.npmDependencies` is set, one of them must be present in the
// `package.json` `dependencies|devDependencies`
// - if `framework.excludedNpmDependencies` is set, none of them must be
// present in the `package.json` `dependencies|devDependencies`
// - if `framework.configFiles` is set, one of the files must exist
export const usesFramework = async function (
{
detect: {
npmDependencies: frameworkNpmDependencies,
excludedNpmDependencies: frameworkExcludedNpmDependencies,
configFiles,
},
},
{ pathExists, npmDependencies },
) {
return (
usesNpmDependencies(frameworkNpmDependencies, npmDependencies) &&
lacksExcludedNpmDependencies(frameworkExcludedNpmDependencies, npmDependencies) &&
(await usesConfigFiles(configFiles, pathExists))
)
}
const usesNpmDependencies = function (frameworkNpmDependencies, npmDependencies) {
return (
frameworkNpmDependencies.length === 0 ||
frameworkNpmDependencies.some((frameworkNpmDependency) => npmDependencies.includes(frameworkNpmDependency))
)
}
const lacksExcludedNpmDependencies = function (frameworkExcludedNpmDependencies, npmDependencies) {
return frameworkExcludedNpmDependencies.every(
(frameworkNpmDependency) => !npmDependencies.includes(frameworkNpmDependency),
)
}
const configExists = async (configFiles, pathExists) => {
const exists = await pLocate(configFiles, (file) => pathExists(file))
return exists
}
const usesConfigFiles = async function (configFiles, pathExists) {
return configFiles.length === 0 || (await configExists(configFiles, pathExists))
}