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:
commit
204b9d934e
@ -304,6 +304,7 @@ following rules are enabled by default:
|
||||
* `npm_missing_script` – fixes `npm` custom script name in `npm run-script <script>`;
|
||||
* `npm_run_script` – adds missing `run-script` for custom `npm` scripts;
|
||||
* `npm_wrong_command` – fixes wrong npm commands like `npm urgrade`;
|
||||
* `npx_add_npx_to_command` – 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` – fixes wrong console commands, for example `vom/vim`;
|
||||
* `no_such_file` – creates missing directories with `mv` and `cp` commands;
|
||||
* `omnienv_no_such_command` – fixes wrong commands for `goenv`, `nodenv`, `pyenv` and `rbenv` (eg.: `pyenv isntall` or `goenv list`);
|
||||
|
58
thefuck/rules/npx_add_npx_to_command.py
Normal file
58
thefuck/rules/npx_add_npx_to_command.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user