-
-
Save bbuecherl/ebea873018c458600eeb5c9a58d173ec to your computer and use it in GitHub Desktop.
Workaround for Yarn bin links problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this file fixes yarn bugs with linked binaries inside docker mounts | |
const path = require("path"); | |
const fs = require("fs-extra"); | |
const TreeWalker = require("npm-tree-walker"); | |
const SH_TEMPLATE = binPath => | |
`#!/bin/sh | |
if [ -x "$basedir/node" ]; then | |
"$basedir/node" "${binPath}" "$@" | |
ret=$? | |
else | |
node "${binPath}" "$@" | |
ret=$? | |
fi | |
exit $ret | |
`; | |
const CMD_TEMPLATE = binPath => | |
`@IF EXIST "%~dp0\\node.exe" ( | |
"%~dp0\\node.exe" "${binPath}" %* | |
) ELSE ( | |
@SETLOCAL | |
@SET PATHEXT=%PATHEXT:;.JS;=;% | |
node "${binPath}" %* | |
) | |
`; | |
function createLink(linkName, templateFn, binPathRelative) { | |
const linkPath = path.resolve(process.cwd(), "node_modules", ".bin", linkName); | |
fs.writeFile(linkPath, templateFn(binPathRelative), { | |
mode: 755, | |
}); | |
} | |
function createLinks(err) { | |
if (err) throw new Error(err); | |
const walker = new TreeWalker(process.cwd()); | |
const binLinks = []; | |
walker.on("data", pkg => { | |
const pkgPath = path.resolve(process.cwd(), pkg.path); | |
const real = require(path.join(pkgPath, "package.json")); | |
if(real.bin) { | |
if(typeof real.bin === "string") { | |
console.log(path.join(pkgPath, real.bin)); | |
binLinks.push({ name: pkg.name, link: path.join(pkgPath, real.bin) }); | |
} else if(typeof real.bin === "object") { | |
Object.keys(real.bin).forEach(name => { | |
console.log(path.join(pkgPath, real.bin[name])); | |
binLinks.push({ | |
name: name, | |
link: path.join(pkgPath, real.bin[name]) | |
}) | |
}); | |
} | |
} | |
}); | |
walker.on("end", () => { | |
binLinks.forEach(binLink => { | |
if (fs.existsSync(binLink.link)) { | |
// Create link for POSIX | |
createLink(binLink.name, SH_TEMPLATE, binLink.link); | |
// Create link for Windows | |
createLink(`${binLink.name}.cmd`, CMD_TEMPLATE, binLink.link); | |
} | |
}); | |
console.log("finished fix-bin-links"); | |
}); | |
walker.on("error", err => { | |
console.error(err); | |
}); | |
walker.run(); | |
} | |
const binPath = path.resolve(process.cwd(), "node_modules", ".bin"); | |
fs.mkdirp(binPath, createLinks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment