Skip to content

Instantly share code, notes, and snippets.

@dev-techmoe

dev-techmoe/README.md

Last active Aug 23, 2018
Embed
What would you like to do?
npm-no-bin-link-command-patch

npm-no-bin-link-command-patch

This script will help you create scripts to fix the problem caused by enable no-bin-link option of NPM or Yarn.

Usage

Put command_patch.js to your work dir, and run it.

node command_patch.js

And it will create scripts that is associate to CLI scripts of every modules automatic.
All command of modules in your workdir will be usable after that.
I will write a npm module to make it better when I have free time.

License

MIT

const fs = require("fs");
const path = require("path");
// get package list from package.json
const packageList = require("./package.json").devDependencies;
// get all package name from packageList
const packageNameList = Object.keys(packageList);
const CLIList = [];
const convertPath = (packageName, binaryPath) => path.resolve(`./node_modules/${packageName}/`, binaryPath);
for (let packageName of packageNameList) {
// load package config
const packageConf = require(`./node_modules/${packageName}/package.json`);
if ("bin" in packageConf) {
if (typeof packageConf.bin !== "string") {
for (let cmd of Object.keys(packageConf.bin)) {
CLIList.push([
cmd, convertPath(packageName, packageConf.bin[cmd]),
])
}
} else {
CLIList.push([
packageName, convertPath(packageName, packageConf.bin), true,
])
}
}
}
if (!fs.existsSync("./node_modules/.bin")) {
fs.mkdir("./node_modules/.bin");
} else {
// TODO: safe check
}
// make script
for (const item of CLIList) {
const [filename, binPath, _] = item;
const script = `
#!/bin/bash
node "${binPath}" $*
`;
fs.writeFileSync(`./node_modules/.bin/${filename}`, script);
fs.chmodSync(`./node_modules/.bin/${filename}`, 777);
}
// console.log(CLIList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.