Total Complexity | 3 |
Complexity/F | 0 |
Lines of Code | 33 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Coverage | 100% |
Changes | 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 |