Skip to content

Commit

Permalink
fix: honor '-' when merging dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
arlac77 committed Oct 16, 2023
1 parent ddd1444 commit 3f965fb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/extract-from-package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { packageDirectory } from "pkg-dir";
import { packageWalker } from "npm-package-walker";
import { createContext } from "expression-expander";
import { satisfies } from "compare-versions";
import { asArray } from "./util.mjs";
import { asArray, mergeDependencies } from "./util.mjs";
import { NPMPackContentProvider } from "./content/npm-pack-content-provider.mjs";
import { NodeModulesContentProvider } from "./content/node-modules-content-provider.mjs";
import { FileContentProvider } from "./content/file-content-provider.mjs";
Expand Down Expand Up @@ -280,7 +280,7 @@ export async function* extractFromPackage(options = {}, env = {}) {
)) {
let arch = variant.arch;
let properties = {};
const depends = {};
let depends = {};
const output = {};
const content = [];

Expand Down Expand Up @@ -312,7 +312,7 @@ export async function* extractFromPackage(options = {}, env = {}) {
if (requirementsMet) {
arch = new Set([...arch, ...fragment.arch]);
properties = { ...fragment.properties, ...properties };
Object.assign(depends, fragment.depends);
depends = mergeDependencies(depends, fragment.depends);
Object.assign(output, fragment.output);
if (fragment.content) {
content.push([fragment.content, fragment.dir]);
Expand Down
17 changes: 15 additions & 2 deletions src/util.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ export const packageNameMapping = {
node: "nodejs"
};

export function filterOutUnwantedDependencies()
{
export function filterOutUnwantedDependencies() {
return ([name, version]) => version !== "-";
}

export function mergeDependencies(a, b) {
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);

const result = {};

for (const key of keys) {
if (a[key] !== "-" && b[key] !== "-") {
result[key] = a[key] || b[key];
}
}

return result;
}

/**
* Decode a password
* @param {string} password
Expand Down
14 changes: 14 additions & 0 deletions tests/merge-depencencies-ava.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import test from "ava";
import { mergeDependencies } from "../src/util.mjs";

test("mergeDependencies", t => {
const a = { a: "1.2.3" };
const b = { b: "3.4.5" };
t.deepEqual(mergeDependencies(a, b), { a: "1.2.3", b: "3.4.5" });
});

test("mergeDependencies remove", t => {
const a = { a: "1.2.3", a2: "1.0.0" };
const b = { a: "-" };
t.deepEqual(mergeDependencies(a, b), { a2: "1.0.0" });
});

0 comments on commit 3f965fb

Please sign in to comment.