-
Notifications
You must be signed in to change notification settings - Fork 2
/
dependencies.ts
49 lines (46 loc) · 1.46 KB
/
dependencies.ts
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
import mod from 'module'
import type {
IDependencyType,
IPackageDependencyChecker,
IPackageJson,
} from './types'
/**
* For a given package.json, check for dependencies
*
* Inspired by:
* - https://github.com/rollup/plugins/blob/auto-install-v2.1.0/packages/auto-install/src/index.ts#L43
*/
export const createPackageJsonDependenciesSet = (
pkg: IPackageJson,
type: IDependencyType = 'dependencies',
withBuiltIn: boolean = false,
): Set<string> => {
return new Set<string>(
Object.keys(pkg[type] || {}).concat(withBuiltIn ? mod.builtinModules : []),
)
}
/**
* Utility to check for build-time dependency.
* And to make sure host package has what's needed.
* In other words, it helps avoid build config mistakes for some easy to forget dependencies.
*/
export const createDependencyChecker = (
pkg: IPackageJson,
type: IDependencyType = 'dependencies',
withBuiltIn: boolean = true,
): IPackageDependencyChecker => {
const checker = createPackageJsonDependenciesSet(pkg, type, withBuiltIn)
return (name: string, mustHave: boolean = true): boolean => {
const hasDependency = checker.has(name)
if (hasDependency === false && mustHave === true) {
const message = `
Caution, make sure you add ${name} in your package.json ${type},
the recommended version is in this @renoirb/conventions-use-bili's ${type}
`
.replace(/[\n\s]+/g, ' ')
.trim()
throw new Error(message)
}
return hasDependency
}
}