-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy path_util.ts
74 lines (67 loc) · 2.41 KB
/
_util.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import exported from '..'
import { plugin as tseslintPlugin } from 'typescript-eslint'
import { TSESLint } from '@typescript-eslint/utils'
import semver from 'semver'
import type { PackageJson } from 'type-fest'
interface PkgDetails {
pkgPath: string
pkgJson: PackageJson
ourDeps: NonNullable<PackageJson['dependencies']>
ourPeerDeps: NonNullable<PackageJson['peerDependencies']>
ourDevDeps: NonNullable<PackageJson['devDependencies']>
}
export const getPkgDetails = async (): Promise<PkgDetails> => {
const { readPackageUp } = await import('read-pkg-up')
const readResult = await readPackageUp()
if (readResult === undefined) {
throw new Error()
}
const ourPkg = readResult.packageJson
if (ourPkg.dependencies === undefined) {
throw new Error()
}
const ourDeps = ourPkg.dependencies
if (ourPkg.peerDependencies === undefined) {
throw new Error()
}
const ourPeerDeps = ourPkg.peerDependencies
if (ourPkg.devDependencies === undefined) {
throw new Error()
}
const ourDevDeps = ourPkg.devDependencies
return {
pkgJson: ourPkg,
pkgPath: readResult.path,
ourDeps,
ourPeerDeps,
ourDevDeps,
}
}
export const isSingleCaretRange = (rangeStr: string): boolean => {
const range = new semver.Range(rangeStr)
/* eslint-disable @typescript-eslint/no-magic-numbers */
if (range.set.length !== 1) return false
const comparators = range.set[0]
if (comparators.length !== 2) return false
return comparators[0].operator === '>=' && comparators[1].operator === '<'
/* eslint-enable @typescript-eslint/no-magic-numbers */
}
export const isPinnedRange = (rangeStr: string): boolean => {
const range = new semver.Range(rangeStr)
/* eslint-disable @typescript-eslint/no-magic-numbers */
if (range.set.length !== 1) return false
const comparators = range.set[0]
return comparators.length === 1 && comparators[0].operator === ''
/* eslint-enable @typescript-eslint/no-magic-numbers */
}
export const extractVersionRange = (spec: string): string =>
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
spec.split('@').slice(-1)[0]
const ourRules_ = exported.rules
if (ourRules_ === undefined) throw new Error('we seem to be exporting no rules')
export const ourRules = ourRules_
export const equivalents = [
...new TSESLint.Linter({ configType: 'eslintrc' }).getRules().keys(),
].filter((name) =>
Object.prototype.hasOwnProperty.call(tseslintPlugin.rules, name),
)