Total Complexity | 3 |
Complexity/F | 0 |
Lines of Code | 34 |
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 | // 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 |