Passed
Push — master ( 622eb3...88f58b )
by Vinicius
01:47
created

src/version.ts   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 33
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 33
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 3
mnd 3
bc 3
fnc 0
bpm 0
cpm 0
noi 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
  return require(pkgFile).version
10
}
11
12 2
const getVersion = async function (context: PluginContext, cache: Map<string, string | null>, moduleId: string): Promise<string | null> {
13 4
  if (cache.has(moduleId)) {
14 1
    return cache.get(moduleId) as string
15
  }
16
17 3
  const version = await detectVersion(moduleId)
18
19 3
  if (!version) {
20 1
    context.warn(`Missing version of ${moduleId}`)
21
  }
22
23 3
  const versionId = version
24
    ? `${moduleId}@${version}`
25
    : version
26
27 3
  cache.set(moduleId, versionId)
28
29 3
  return versionId
30
}
31
32
export { detectVersion, getVersion }
33