Created
February 6, 2019 11:16
-
-
Save kevyworks/f3efa99fa315f019bc57d26a8e498917 to your computer and use it in GitHub Desktop.
PNPM Unlink Binary
This file contains hidden or 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
{ | |
"name": "my-project", | |
"version": "1.0.0", | |
"description": "", | |
"keywords": [], | |
"license": "ISC", | |
"author": "", | |
"scripts": { | |
"clean": "pnpm run bin:unlink && pnpm m exec -- rm -rf node_modules; rm shrinkwrap.yaml; rm -rf node_modules", | |
"reset": "pnpm run clean && pnpm m install && pnpm run bin:link", | |
"bin:link": "node scripts/setup-bins --link", | |
"bin:unlink": "node scripts/setup-bins --unlink" | |
} | |
} |
This file contains hidden or 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
"use strict"; | |
const fs = require("fs"); | |
const path = require("path"); | |
const { execSync } = require("child_process"); | |
const rootDir = path.resolve(__dirname, ".."); | |
const packagesDir = path.resolve(rootDir, "packages"); | |
const pm = "pnpm"; | |
const globalBinDir = path.dirname(process.execPath); | |
const args = process.argv.slice(2, 3); | |
const opt_link = args.indexOf("--link") !== -1 ? true : args.indexOf("--unlink") !== -1 ? false : null; | |
const opt_force = args.indexOf("--force") !== -1 ? true : false; | |
if (opt_link === null) { | |
console.error("\nerror: No command argument specified. Try '--link or --unlink'\n"); | |
process.exit(1); | |
} | |
console.log("> Reading packages..."); | |
// Read packages `package.json` for bin | |
const bins = fs.readdirSync(packagesDir) | |
.reduce((arr, f) => { | |
try { | |
let __path = path.resolve(packagesDir, f); | |
let pkg = require(__path + "/package.json"); | |
pkg.__path = __path; | |
let bins = pkg["bin"]; | |
if (bins) { | |
// "bin": "bin/mybin.js" | |
if (typeof bins === "string") { | |
arr.push([ | |
path.basename(bins).replace(/(.js|.sh|\s)$/i, ""), | |
path.resolve(__path, bins), | |
pkg | |
]); | |
} | |
// "bin": {"mybin": "bin/script.js", ...} | |
else { | |
Object.keys(bins).forEach(k => arr.push([ | |
k, | |
path.resolve(__path, bins[k]), | |
pkg | |
])); | |
} | |
} | |
return arr; | |
} catch (e) {} | |
}, []); | |
if (bins.length === 0) { | |
console.log("> No known package with bin found."); | |
process.exit(0); | |
} else { | |
console.log(`> Reading ${bins.length} package(s) bins ${(opt_link ? "to link..." : "to unlink...")}`); | |
} | |
// Unlink Global Bins | |
for (let [src_name, src_path, src_pkg] of bins) { | |
let bin_path = path.resolve(globalBinDir, src_name); | |
if (fs.existsSync(bin_path)) { | |
if (opt_force || !opt_link) { | |
fs.unlinkSync(bin_path); | |
console.log("> %s: successfully unlinked '%s' %s", | |
src_pkg.name.padEnd(" ", 15), src_name, opt_force ? "--force" : ""); | |
} else { | |
console.log("> %s: '%s' already linked. Try '--link/unlink --force'", | |
src_pkg.name.padEnd(" ", 15), src_name); | |
continue; | |
} | |
} | |
if (!opt_link) continue; else { | |
try { | |
execSync(`cd ${src_pkg.__path} && ${pm} link`); | |
console.log("> %s: '%s' link created.", | |
src_pkg.name.padEnd(" ", 15), src_name); | |
} catch (e) { | |
console.log("> %s: link exception for '%s' %s", | |
src_pkg.name.padEnd(" ", 15), src_name, e.message, e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment