From 3287c88e2eaa0cdc215d5499532ac7e1a5655cd3 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Mon, 20 Jul 2020 02:50:02 +0530 Subject: [PATCH 1/4] Create npx_add_npx_to_command.py --- thefuck/rules/npx_add_npx_to_command.py | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 thefuck/rules/npx_add_npx_to_command.py diff --git a/thefuck/rules/npx_add_npx_to_command.py b/thefuck/rules/npx_add_npx_to_command.py new file mode 100644 index 0000000..265204c --- /dev/null +++ b/thefuck/rules/npx_add_npx_to_command.py @@ -0,0 +1,54 @@ +import os +from os import path +from subprocess import Popen, PIPE +from thefuck.utils import memoize, eager, which, get_close_matches + + +priority = 900 +enabled_by_default = bool(which('npx')) + + +def match(command): + return \ + 'not found' in command.output.lower() and \ + bool(get_matching_npm_executables_in_cd()) + + +def get_new_command(command): + return [ + ' '.join(['npx', e, *command.script_parts[1:]]) + for e in get_matching_npm_executables_in_cd() + ] + + +def get_matching_npm_executables_in_cd(command): + """Get all matching npm binaries in current npm bin folder.""" + npm_bin = get_npm_bin_folder() + command_name = command.script_parts[0] + if command_name == 'npx': + command_name = command.script_parts[1] + return get_matching_npm_executables(npm_bin, command_name) + + +def get_npm_bin_folder(): + """Get current npm bin folder.""" + proc = Popen(['npm', 'bin'], stdout=PIPE) + return proc.stdout.readlines()[0].decode('utf-8').strip() + + +@memoize +@eager +def get_matching_npm_executables(bin, name): + """Get all matching npm binaries.""" + if not path.isdir(bin): + return [] + + exact_command_path = path.join(bin, name) + if path.isfile(exact_command_path): + return [name] + + all_executables = [ + f for f in os.listdir(bin) + if path.isfile(path.join(bin, f)) + ] + return get_close_matches(name, all_executables) From a6341fe20baf7dbf78863a2894b509e03f2d6615 Mon Sep 17 00:00:00 2001 From: Kartik Soneji Date: Mon, 20 Jul 2020 04:00:35 +0600 Subject: [PATCH 2/4] Add command description to readme. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5ac8b83..aba243c 100644 --- a/README.md +++ b/README.md @@ -270,6 +270,7 @@ following rules are enabled by default: * `npm_missing_script` – fixes `npm` custom script name in `npm run-script