-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
packagereference.js
37 lines (33 loc) · 1.2 KB
/
packagereference.js
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
var fs = require('fs');
var path = require('path');
var parseXml = require('xml2js').parseString;
var safeBufferRead = require('./safeBufferRead');
module.exports.list = function (dir) {
const files = fs.readdirSync(dir).filter(file => file.toLowerCase().endsWith(".csproj") && fs.existsSync(file));
if (!files.length) return null;
const file = files[0];
var xml = safeBufferRead(fs.readFileSync(file));
var parsedOutput;
parseXml(xml, function (err, result) {
parsedOutput = result;
});
if (!(parsedOutput && parsedOutput.Project && parsedOutput.Project.ItemGroup)) {
return null;
}
try {
let itemGroup = parsedOutput.Project.ItemGroup;
if (!Array.isArray(itemGroup)) {
itemGroup = [itemGroup];
}
return itemGroup.filter(a => a && a.PackageReference).map(a => a.PackageReference.map(x => {
return {
id: x.$.Include,
version: x.$.Version || x.Version[0],
targetFramework: null
}
})).reduce((ret, cur) => ret.concat(cur), []);
} catch (err) {
console.log(`Cannot parse ${file}. Is it in a valid format?`);
return false;
}
}