@pnpm/link-bins
Advanced tools
Comparing version 10.0.2 to 10.0.3
@@ -25,3 +25,5 @@ "use strict"; | ||
const unnest_1 = __importDefault(require("ramda/src/unnest")); | ||
const groupBy_1 = __importDefault(require("ramda/src/groupBy")); | ||
const partition_1 = __importDefault(require("ramda/src/partition")); | ||
const semver_1 = __importDefault(require("semver")); | ||
const symlink_dir_1 = __importDefault(require("symlink-dir")); | ||
@@ -89,21 +91,8 @@ const fix_bin_1 = __importDefault(require("bin-links/lib/fix-bin")); | ||
return []; | ||
// deduplicate bin names to prevent race conditions (multiple writers for the same file) | ||
allCmds = deduplicateCommands(allCmds, binsDir); | ||
await fs_1.promises.mkdir(binsDir, { recursive: true }); | ||
const [cmdsWithOwnName, cmdsWithOtherNames] = (0, partition_1.default)(({ ownName }) => ownName, allCmds); | ||
const results1 = await (0, p_settle_1.default)(cmdsWithOwnName.map(async (cmd) => linkBin(cmd, binsDir, opts))); | ||
const usedNames = Object.fromEntries(cmdsWithOwnName.map((cmd) => [cmd.name, cmd.name])); | ||
const results2 = await (0, p_settle_1.default)(cmdsWithOtherNames.map(async (cmd) => { | ||
if (usedNames[cmd.name]) { | ||
binsConflictLogger.debug({ | ||
binaryName: cmd.name, | ||
binsDir, | ||
linkedPkgName: usedNames[cmd.name], | ||
skippedPkgName: cmd.pkgName, | ||
}); | ||
return Promise.resolve(undefined); | ||
} | ||
usedNames[cmd.name] = cmd.pkgName; | ||
return linkBin(cmd, binsDir, opts); | ||
})); | ||
const results = await (0, p_settle_1.default)(allCmds.map(async (cmd) => linkBin(cmd, binsDir, opts))); | ||
// We want to create all commands that we can create before throwing an exception | ||
for (const result of [...results1, ...results2]) { | ||
for (const result of results) { | ||
if (result.isRejected) { | ||
@@ -115,2 +104,34 @@ throw result.reason; | ||
} | ||
function deduplicateCommands(commands, binsDir) { | ||
const cmdGroups = (0, groupBy_1.default)(cmd => cmd.name, commands); | ||
return Object.values(cmdGroups) | ||
.filter((group) => group !== undefined && group.length !== 0) | ||
.map(group => resolveCommandConflicts(group, binsDir)); | ||
} | ||
function resolveCommandConflicts(group, binsDir) { | ||
return group.reduce((a, b) => { | ||
const [chosen, skipped] = compareCommandsInConflict(a, b) >= 0 ? [a, b] : [b, a]; | ||
logCommandConflict(chosen, skipped, binsDir); | ||
return chosen; | ||
}); | ||
} | ||
function compareCommandsInConflict(a, b) { | ||
if (a.ownName && !b.ownName) | ||
return 1; | ||
if (!a.ownName && b.ownName) | ||
return -1; | ||
if (a.pkgName !== b.pkgName) | ||
return a.pkgName.localeCompare(b.pkgName); // it's pointless to compare versions of 2 different package | ||
return semver_1.default.compare(a.pkgVersion, b.pkgVersion); | ||
} | ||
function logCommandConflict(chosen, skipped, binsDir) { | ||
binsConflictLogger.debug({ | ||
binaryName: skipped.name, | ||
binsDir, | ||
linkedPkgName: chosen.pkgName, | ||
linkedPkgVersion: chosen.pkgVersion, | ||
skippedPkgName: skipped.pkgName, | ||
skippedPkgVersion: skipped.pkgVersion, | ||
}); | ||
} | ||
async function isFromModules(filename) { | ||
@@ -143,2 +164,3 @@ const real = await fs_1.promises.realpath(filename); | ||
pkgName: manifest.name, | ||
pkgVersion: manifest.version, | ||
makePowerShellShim: POWER_SHELL_IS_SUPPORTED && manifest.name !== 'pnpm', | ||
@@ -145,0 +167,0 @@ nodeExecPath, |
{ | ||
"name": "@pnpm/link-bins", | ||
"version": "10.0.2", | ||
"version": "10.0.3", | ||
"description": "Link bins to node_modules/.bin", | ||
@@ -34,9 +34,10 @@ "main": "lib/index.js", | ||
"ramda": "npm:@pnpm/ramda@0.28.1", | ||
"semver": "^7.6.0", | ||
"symlink-dir": "^6.0.0", | ||
"@pnpm/manifest-utils": "6.0.2", | ||
"@pnpm/error": "6.0.1", | ||
"@pnpm/package-bins": "9.0.1", | ||
"@pnpm/read-modules-dir": "7.0.0", | ||
"@pnpm/manifest-utils": "6.0.2", | ||
"@pnpm/read-package-json": "9.0.2", | ||
"@pnpm/read-project-manifest": "6.0.2", | ||
"@pnpm/read-modules-dir": "7.0.0", | ||
"@pnpm/types": "10.1.0" | ||
@@ -49,7 +50,8 @@ }, | ||
"@types/ramda": "0.29.12", | ||
"@types/semver": "7.5.3", | ||
"cmd-extension": "^1.0.2", | ||
"path-exists": "^4.0.0", | ||
"tempy": "^1.0.1", | ||
"@pnpm/link-bins": "10.0.2", | ||
"@pnpm/test-fixtures": "0.1.22" | ||
"@pnpm/link-bins": "10.0.3", | ||
"@pnpm/test-fixtures": "0.1.23" | ||
}, | ||
@@ -56,0 +58,0 @@ "funding": "https://opencollective.com/pnpm", |
Sorry, the diff of this file is not supported yet
24615
277
18
10
+ Addedsemver@^7.6.0