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

Merge e7232a437c60403a3e24b3cbc402b9730714a4fa into c7e7e1d884d3bb241ea6448f72a989434c2a35ec

This commit is contained in:
Kartik Soneji 2024-03-12 17:44:32 +01:00 committed by GitHub
commit 204b9d934e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 0 deletions

View File

@ -304,6 +304,7 @@ following rules are enabled by default:
* `npm_missing_script` &ndash; fixes `npm` custom script name in `npm run-script <script>`;
* `npm_run_script` &ndash; adds missing `run-script` for custom `npm` scripts;
* `npm_wrong_command` &ndash; fixes wrong npm commands like `npm urgrade`;
* `npx_add_npx_to_command` &ndash; adds `npx` for running locally installed `npm` commands, also corrects misspelled commands `jest/npx jest`, `npx jeet/npx jest` and `jeet/npx jest`;
* `no_command` &ndash; fixes wrong console commands, for example `vom/vim`;
* `no_such_file` &ndash; creates missing directories with `mv` and `cp` commands;
* `omnienv_no_such_command` &ndash; fixes wrong commands for `goenv`, `nodenv`, `pyenv` and `rbenv` (eg.: `pyenv isntall` or `goenv list`);

View File

@ -0,0 +1,58 @@
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(command))
def get_new_command(command):
skip = 1
if command.script_parts[0] == 'npx':
skip += 1
script_parts = command.script_parts[skip:]
return [
' '.join(['npx', e] + script_parts)
for e in get_matching_npm_executables_in_cd(command)
]
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)