-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpackage.nupkg.js
80 lines (64 loc) · 2.77 KB
/
package.nupkg.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var path = require('path');
var fs = require('fs');
var zip = require('zip');
var parseXml = require('xml2js').parseString;
var safeBufferRead = require('./safeBufferRead');
module.exports.readNuspec = function (packagesFolder, package, settings) {
if (!packagesFolder) throw new Error("no packagesFolder");
var packageFilePath = path.join(packagesFolder, package.id + "." + package.version, package.id + "." + package.version + ".nupkg");
if (!fs.existsSync(packageFilePath)) {
packageFilePath = path.join(packagesFolder, package.id.toLowerCase() + "." + package.version, package.id.toLowerCase() + "." + package.version + ".nupkg");
if (!fs.existsSync(packageFilePath) && package.version) {
packageFilePath = path.join(packagesFolder, package.id, package.version, package.id + "." + package.version + ".nupkg");
if (!fs.existsSync(packageFilePath) && package.version) {
packageFilePath = path.join(packagesFolder, package.id.toLowerCase(), package.version, package.id.toLowerCase() + "." + package.version + ".nupkg");
if (!fs.existsSync(packageFilePath)) {
if (!settings.observingTargets) {
console.log("WARN: Cannot find nupkg file for " + package.id);
}
return [];
}
}
}
}
var nuspecXml = openNuspecFile(packageFilePath);
if (!nuspecXml) {
console.log("WARN: Cannot find nuspec file for " + package.id);
console.log("Attempted to open this file: " + packageFilePath);
return [];
}
return readAllDeps(nuspecXml);
}
function readAllDeps(nuspecXml) {
var dependencies = [];
parseXml(nuspecXml, (err, data) => {
if (err) console.error(err);
if (!data) return;
(data.package.metadata || []).forEach(metadata => {
(metadata.dependencies || []).forEach(dep => {
// if the nuget package targets multiple version, there are groups
(dep.group || []).forEach(dep => {
(dep.dependency || []).forEach(dep => {
dependencies.push(dep.$);
})
});
// otherwise there are no groups
(dep.dependency || []).forEach(dep => {
dependencies.push(dep.$);
})
});
});
});
return dependencies;
}
function openNuspecFile(packageFilePath) {
var pkgData = fs.readFileSync(packageFilePath);
var reader = zip.Reader(pkgData);
var nuspec;
reader.forEach(function (entry, next) {
if (path.extname(entry._header.file_name) === ".nuspec") {
nuspec = safeBufferRead(entry.getData())
}
});
return nuspec;
}