forked from lerna/lerna
/
UpdatedPackagesCollector.js
164 lines (130 loc) · 4.18 KB
/
UpdatedPackagesCollector.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import PackageUtilities from "./PackageUtilities";
import GitUtilities from "./GitUtilities";
import progressBar from "./progressBar";
import minimatch from "minimatch";
import logger from "./logger";
import find from "lodash.find";
import path from "path";
class Update {
constructor(pkg) {
this.package = pkg;
}
}
export default class UpdatedPackagesCollector {
constructor(packages, packageGraph, flags, publishConfig) {
this.packages = packages;
this.packageGraph = packageGraph;
this.flags = flags;
this.publishConfig = publishConfig;
}
getUpdates() {
this.updatedPackages = this.collectUpdatedPackages();
this.dependents = this.collectDependents();
return this.collectUpdates();
}
collectUpdatedPackages() {
logger.info("Checking for updated packages...");
progressBar.init(this.packages.length);
const hasTags = GitUtilities.hasTags();
let commits;
if (this.flags.canary) {
let currentSHA;
if (this.flags.canary !== true) {
currentSHA = this.flags.canary;
} else {
currentSHA = GitUtilities.getCurrentSHA();
}
commits = this.getAssociatedCommits(currentSHA);
} else if (hasTags) {
commits = GitUtilities.describeTag(GitUtilities.getLastTaggedCommit());
}
const updatedPackages = {};
this.packages.filter(pkg => {
progressBar.tick(pkg.name);
if (pkg.isPrivate()) {
return false;
}
if (!hasTags) {
return true;
}
var forcePublish = (this.flags.forcePublish || "").split(",");
if (forcePublish.indexOf("*") > -1) {
return true;
} else if (forcePublish.indexOf(pkg.name) > -1) {
return true;
} else {
return this.hasDiffSinceThatIsntIgnored(pkg, commits);
}
}).forEach(pkg => {
updatedPackages[pkg.name] = pkg;
});
progressBar.terminate();
return updatedPackages;
}
isPackageDependentOf(packageName, dependency) {
if (!this.cache[packageName]) {
this.cache[packageName] = {};
}
if (this.cache[packageName][dependency] === "dependent") {
return true;
} else if (this.cache[packageName][dependency] === "visited") {
return false;
}
let dependencies = this.packageGraph.get(packageName).dependencies;
if (dependencies.indexOf(dependency) > -1) {
this.cache[packageName][dependency] = "dependent";
return true;
}
this.cache[packageName][dependency] = "visited";
let hasSubDependents = false;
dependencies.forEach(dep => {
if (this.isPackageDependentOf(dep, dependency)) {
this.cache[packageName][dependency] = "dependent";
hasSubDependents = true;
}
});
return hasSubDependents;
}
collectDependents() {
const dependents = {};
this.cache = {};
this.packages.forEach(pkg => {
Object.keys(this.updatedPackages).forEach(dependency => {
if (this.isPackageDependentOf(pkg.name, dependency)) {
dependents[pkg.name] = pkg;
}
});
});
return dependents;
}
collectUpdates() {
return this.packages.filter(pkg => {
return this.updatedPackages[pkg.name] || (this.flags.onlyExplicitUpdates ? false : this.dependents[pkg.name]) || this.flags.canary;
}).map(pkg => {
return new Update(pkg);
});
}
getAssociatedCommits(sha) {
// if it's a merge commit, it will return all the commits that were part of the merge
// ex: If `ab7533e` had 2 commits, ab7533e^..ab7533e would contain 2 commits + the merge commit
return sha.slice(0, 8) + "^.." + sha.slice(0, 8);
}
hasDiffSinceThatIsntIgnored(pkg, commits) {
var folder = PackageUtilities.getPackagePath(PackageUtilities.getPackagesPath(""), pkg.name);
var diff = GitUtilities.diffSinceIn(commits, pkg.location);
if (diff === "") {
return false;
}
var changedFiles = diff.split("\n").map(file => {
return file.replace(folder + path.sep, "");
});
if (this.publishConfig.ignore) {
changedFiles = changedFiles.filter(file => {
return !find(this.publishConfig.ignore, pattern => {
return minimatch(file, pattern);
});
});
}
return !!changedFiles.length;
}
}