src/version.ts   A
last analyzed

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 34
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 28
mnd 3
bc 3
fnc 0
dl 0
loc 34
ccs 16
cts 16
cp 1
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1 4
import path from 'path'
2 2
import findUp from 'find-up'
3
import { PluginContext } from 'rollup'
4
5 2
const detectVersion = async (id: string): Promise<string | null> => {
6 4
  const cwd = path.dirname(require.resolve(id))
7 4
  const pkgFile = await findUp('package.json', { cwd }) as string
8
9 4
  // eslint-disable-next-line @typescript-eslint/no-var-requires
10
  return require(pkgFile).version
11
}
12 2
13 4
const getVersion = async function (context: PluginContext, cache: Map<string, string | null>, moduleId: string): Promise<string | null> {
14 1
  if (cache.has(moduleId)) {
15
    return cache.get(moduleId) as string
16
  }
17 3
18
  const version = await detectVersion(moduleId)
19 3
20 1
  if (!version) {
21
    context.warn(`Missing version of ${moduleId}`)
22
  }
23 3
24
  const versionId = version
25
    ? `${moduleId}@${version}`
26
    : version
27 3
28
  cache.set(moduleId, versionId)
29 3
30
  return versionId
31
}
32 2
33
export { detectVersion, getVersion }
34