mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-21 20:38:54 +00:00
#N/A: Add npm_wrong_command
rule
This commit is contained in:
parent
c5acee54ea
commit
778d5f3e6e
@ -184,6 +184,7 @@ using the matched rule and runs it. Rules enabled by default are as follows:
|
||||
* `mkdir_p` – adds `-p` when you trying to create directory without parent;
|
||||
* `mvn_no_command` – adds `clean package` to `mvn`;
|
||||
* `mvn_unknown_lifecycle_phase` – fixes misspelled lifecycle phases with `mvn`;
|
||||
* `npm_wrong_command` – fixes wrong npm commands like `npm urgrade`;
|
||||
* `no_command` – fixes wrong console commands, for example `vom/vim`;
|
||||
* `no_such_file` – creates missing directories with `mv` and `cp` commands;
|
||||
* `open` – prepends `http` to address passed to `open`;
|
||||
|
58
tests/rules/test_npm_wrong_command.py
Normal file
58
tests/rules/test_npm_wrong_command.py
Normal file
@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
from thefuck.rules.npm_wrong_command import match, get_new_command
|
||||
from tests.utils import Command
|
||||
|
||||
stdout = '''
|
||||
Usage: npm <command>
|
||||
|
||||
where <command> is one of:
|
||||
access, add-user, adduser, apihelp, author, bin, bugs, c,
|
||||
cache, completion, config, ddp, dedupe, deprecate, dist-tag,
|
||||
dist-tags, docs, edit, explore, faq, find, find-dupes, get,
|
||||
help, help-search, home, i, info, init, install, issues, la,
|
||||
link, list, ll, ln, login, logout, ls, outdated, owner,
|
||||
pack, ping, prefix, prune, publish, r, rb, rebuild, remove,
|
||||
repo, restart, rm, root, run-script, s, se, search, set,
|
||||
show, shrinkwrap, star, stars, start, stop, t, tag, team,
|
||||
test, tst, un, uninstall, unlink, unpublish, unstar, up,
|
||||
update, upgrade, v, verison, version, view, whoami
|
||||
|
||||
npm <cmd> -h quick help on <cmd>
|
||||
npm -l display full usage info
|
||||
npm faq commonly asked questions
|
||||
npm help <term> search for help on <term>
|
||||
npm help npm involved overview
|
||||
|
||||
Specify configs in the ini-formatted file:
|
||||
/home/nvbn/.npmrc
|
||||
or on the command line via: npm <command> --key value
|
||||
Config info can be viewed via: npm help config
|
||||
|
||||
npm@2.14.7 /opt/node/lib/node_modules/npm
|
||||
'''
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script', [
|
||||
'npm urgrdae',
|
||||
'npm urgrade -g',
|
||||
'npm -f urgrade -g',
|
||||
'npm urg'])
|
||||
def test_match(script):
|
||||
assert match(Command(script, stdout))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script, stdout', [
|
||||
('npm urgrade', ''),
|
||||
('npm', stdout),
|
||||
('test urgrade', stdout),
|
||||
('npm -e', stdout)])
|
||||
def test_not_match(script, stdout):
|
||||
assert not match(Command(script, stdout))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('script, result', [
|
||||
('npm urgrade', 'npm upgrade'),
|
||||
('npm -g isntall gulp', 'npm -g install gulp'),
|
||||
('npm isntall -g gulp', 'npm install -g gulp')])
|
||||
def test_get_new_command(script, result):
|
||||
assert get_new_command(Command(script, stdout)) == result
|
39
thefuck/rules/npm_wrong_command.py
Normal file
39
thefuck/rules/npm_wrong_command.py
Normal file
@ -0,0 +1,39 @@
|
||||
from thefuck.utils import replace_argument, for_app, eager, get_closest
|
||||
from thefuck.specific.sudo import sudo_support
|
||||
|
||||
|
||||
def _get_wrong_command(script_parts):
|
||||
commands = [part for part in script_parts[1:] if not part.startswith('-')]
|
||||
if commands:
|
||||
return commands[0]
|
||||
|
||||
|
||||
@sudo_support
|
||||
@for_app('npm')
|
||||
def match(command):
|
||||
return (command.script_parts[0] == 'npm' and
|
||||
'where <command> is one of:' in command.stdout and
|
||||
_get_wrong_command(command.script_parts))
|
||||
|
||||
|
||||
@eager
|
||||
def _get_available_commands(stdout):
|
||||
commands_listing = False
|
||||
for line in stdout.split('\n'):
|
||||
if line.startswith('where <command> is one of:'):
|
||||
commands_listing = True
|
||||
elif commands_listing:
|
||||
if not line:
|
||||
break
|
||||
|
||||
for command in line.split(', '):
|
||||
stripped = command.strip()
|
||||
if stripped:
|
||||
yield stripped
|
||||
|
||||
|
||||
def get_new_command(command):
|
||||
npm_commands = _get_available_commands(command.stdout)
|
||||
wrong_command = _get_wrong_command(command.script_parts)
|
||||
fixed = get_closest(wrong_command, npm_commands)
|
||||
return replace_argument(command.script, wrong_command, fixed)
|
Loading…
x
Reference in New Issue
Block a user