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
Expand file tree
/
Copy pathdetect.js
More file actions
61 lines (54 loc) · 2.14 KB
/
detect.js
File metadata and controls
61 lines (54 loc) · 2.14 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pLocate from 'p-locate'
import semver from 'semver'
// 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, npmDependenciesVersions },
) {
return (
usesNpmDependencies(frameworkNpmDependencies, npmDependencies, npmDependenciesVersions) &&
lacksExcludedNpmDependencies(frameworkExcludedNpmDependencies, npmDependencies) &&
(await usesConfigFiles(configFiles, pathExists))
)
}
const usesNpmDependencies = function (frameworkNpmDependencies, npmDependencies, npmDependenciesVersions) {
if (frameworkNpmDependencies.length === 0) {
return true
}
return frameworkNpmDependencies.some((frameworkNpmDependency) => {
if (typeof frameworkNpmDependency === 'string') {
return npmDependencies.includes(frameworkNpmDependency)
}
return (
npmDependencies.includes(frameworkNpmDependency.name) &&
(frameworkNpmDependency.version == null ||
semver.satisfies(
semver.minVersion(npmDependenciesVersions[frameworkNpmDependency.name]),
frameworkNpmDependency.version,
))
)
})
}
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))
}