Skip to content

Commit

Permalink
fix(deps): remove @lerna/collect-updates dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ext committed Dec 10, 2023
1 parent 5c04541 commit 944b3d6
Show file tree
Hide file tree
Showing 9 changed files with 862 additions and 311 deletions.
2 changes: 1 addition & 1 deletion lib/generate-notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { readPackageUp } from "read-pkg-up";
import debugFactory from "debug";
import loadChangelogConfig from "@semantic-release/release-notes-generator/lib/load-changelog-config.js";
import HOSTS_CONFIG from "@semantic-release/release-notes-generator/lib/hosts-config.js";
import { makeDiffPredicate } from "@lerna/collect-updates/lib/make-diff-predicate.js";
import { Project } from "@lerna/project";
import { makeDiffPredicate } from "./utils/index.js";

const debug = debugFactory("semantic-release:release-notes-generator");

Expand Down
4 changes: 1 addition & 3 deletions lib/get-changed-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { format } from "node:util";
import { PackageGraph } from "@lerna/package-graph";
import { Project } from "@lerna/project";
import { execaSync } from "execa";
import { hasTags } from "@lerna/collect-updates/lib/has-tags.js";
import { collectPackages } from "@lerna/collect-updates/lib/collect-packages.js";
import { makeDiffPredicate } from "@lerna/collect-updates/lib/make-diff-predicate.js";
import { shouldLatch } from "./should-latch.js";
import { collectPackages, hasTags, makeDiffPredicate } from "./utils/index.js";

function describeRefSync(execOptions) {
const args = [
Expand Down
41 changes: 41 additions & 0 deletions lib/utils/collect-dependents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Build a set of nodes that are dependents of the input set.
*/
export function collectDependents(nodes) {
const collected = new Set();

nodes.forEach((currentNode) => {
if (currentNode.localDependents.size === 0) {
// no point diving into a non-existent tree
return;
}

// breadth-first search
const queue = [currentNode];
const seen = new Set();

const visit = (dependentNode, dependentName, siblingDependents) => {
if (seen.has(dependentNode)) {
return;
}

seen.add(dependentNode);

if (dependentNode === currentNode || siblingDependents.has(currentNode.name)) {
// a direct or transitive cycle, skip it
return;
}

collected.add(dependentNode);
queue.push(dependentNode);
};

while (queue.length) {
const node = queue.shift();

node.localDependents.forEach(visit);
}
});

return collected;
}
35 changes: 35 additions & 0 deletions lib/utils/collect-packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { collectDependents } from "./collect-dependents.js";

/**
* Build a list of graph nodes, possibly including dependents, using predicate if available.
*/
export function collectPackages(
packages,
{ isCandidate = () => true, onInclude, excludeDependents } = {},
) {
const candidates = new Set();

packages.forEach((node, name) => {
if (isCandidate(node, name)) {
candidates.add(node);
}
});

if (!excludeDependents) {
collectDependents(candidates).forEach((node) => candidates.add(node));
}

// The result should always be in the same order as the input
const updates = [];

packages.forEach((node, name) => {
if (candidates.has(node)) {
if (onInclude) {
onInclude(name);
}
updates.push(node);
}
});

return updates;
}
22 changes: 22 additions & 0 deletions lib/utils/has-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { execaSync } from "execa";
import log from "npmlog";

/**
* Determine if any git tags are reachable.
* @param {import("execa").SyncOptions} [opts]
*/
export function hasTags(opts) {
log.silly("hasTags");
let result = false;

try {
result = !!execaSync("git", ["tag"], opts);
} catch (err) {
log.warn("ENOTAGS", "No git tags were reachable from this branch!");
log.verbose("hasTags error", err);
}

log.verbose("hasTags", result);

return result;
}
3 changes: 3 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { collectPackages } from "./collect-packages.js";
export { hasTags } from "./has-tags.js";
export { makeDiffPredicate } from "./make-diff-predicate.js";
69 changes: 69 additions & 0 deletions lib/utils/make-diff-predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import path from "node:path/posix";
import { execaSync } from "execa";
import log from "npmlog";
import minimatch from "minimatch";

/**
* @param {string} committish
* @param {import("execa").SyncOptions} [execOpts]
* @param {string[]} ignorePatterns
*/
export function makeDiffPredicate(committish, execOpts, ignorePatterns = []) {
const ignoreFilters = new Set(
ignorePatterns.map((p) =>
minimatch.filter(`!${p}`, {
matchBase: true,
// dotfiles inside ignored directories should also match
dot: true,
}),
),
);

if (ignoreFilters.size) {
log.info("ignoring diff in paths matching", ignorePatterns);
}

return function hasDiffSinceThatIsntIgnored(node) {
const diff = diffSinceIn(committish, node.location, execOpts);

if (diff === "") {
log.silly("", "no diff found in %s", node.name);
return false;
}

log.silly("found diff in", diff);
let changedFiles = diff.split("\n");

if (ignoreFilters.size) {
for (const ignored of ignoreFilters) {
changedFiles = changedFiles.filter(ignored);
}
}

if (changedFiles.length) {
log.verbose("filtered diff", changedFiles);
} else {
log.verbose("", "no diff found in %s (after filtering)", node.name);
}

return changedFiles.length > 0;
};
}

/**
* @param {string} committish
* @param {string} location
* @param {import("execa").SyncOptions} [opts]
*/
function diffSinceIn(committish, location, opts) {
const args = ["diff", "--name-only", committish];
const formattedLocation = path.relative(opts.cwd, location);

if (formattedLocation) {
// avoid same-directory path.relative() === ""
args.push("--", formattedLocation);
}

log.silly("checking diff", formattedLocation);
return execaSync("git", args, opts);
}
Loading

0 comments on commit 944b3d6

Please sign in to comment.