forked from renovatebot/renovate
/
extract.ts
65 lines (58 loc) · 1.51 KB
/
extract.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
import { safeLoad } from 'js-yaml';
import * as datasourceDart from '../../datasource/dart';
import { logger } from '../../logger';
import type { PackageDependency, PackageFile } from '../types';
function getDeps(
depsObj: { [x: string]: any },
preset: { depType: string }
): PackageDependency[] {
if (!depsObj) {
return [];
}
return Object.keys(depsObj).reduce((acc, depName) => {
if (depName === 'meta') {
return acc;
}
const section = depsObj[depName];
let currentValue: string | null = null;
if (section?.version) {
currentValue = section.version.toString();
} else if (section) {
if (typeof section === 'string') {
currentValue = section;
}
if (typeof section === 'number') {
currentValue = section.toString();
}
}
const dep: PackageDependency = { ...preset, depName, currentValue };
return [...acc, dep];
}, []);
}
export function extractPackageFile(
content: string,
packageFile: string
): PackageFile | null {
try {
// TODO: fix me (#9610)
const doc = safeLoad(content, { json: true }) as any;
const deps = [
...getDeps(doc.dependencies, {
depType: 'dependencies',
}),
...getDeps(doc.dev_dependencies, {
depType: 'dev_dependencies',
}),
];
if (deps.length) {
return {
packageFile,
datasource: datasourceDart.id,
deps,
};
}
} catch (e) {
logger.debug({ packageFile }, 'Can not parse dependency');
}
return null;
}