1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-14 06:38:32 +00:00

Create npx_add_npx_to_command.py

This commit is contained in:
Kartik Soneji 2020-07-20 02:50:02 +05:30
parent c196e2901c
commit 3287c88e2e

View File

@ -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)