mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-20 20:09:07 +00:00
#N/A: Add npm_run_script
rule
This commit is contained in:
parent
34973fe97e
commit
b09a4e394e
@ -200,6 +200,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_run_script` – adds missing `run-script` for custom `npm` scripts;
|
||||
* `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;
|
||||
|
84
tests/rules/test_npm_run_script.py
Normal file
84
tests/rules/test_npm_run_script.py
Normal file
@ -0,0 +1,84 @@
|
||||
import pytest
|
||||
from io import BytesIO
|
||||
from thefuck.rules.npm_run_script 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, 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
|
||||
|
||||
'''
|
||||
|
||||
run_script_stdout = b'''
|
||||
Lifecycle scripts included in code-view-web:
|
||||
test
|
||||
jest
|
||||
|
||||
available via `npm run-script`:
|
||||
build
|
||||
cp node_modules/ace-builds/src-min/ -a resources/ace/ && webpack --progress --colors -p --config ./webpack.production.config.js
|
||||
develop
|
||||
cp node_modules/ace-builds/src/ -a resources/ace/ && webpack-dev-server --progress --colors
|
||||
watch-test
|
||||
jest --verbose --watch
|
||||
|
||||
'''
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def run_script(mocker):
|
||||
patch = mocker.patch('thefuck.rules.npm_run_script.Popen')
|
||||
patch.return_value.stdout = BytesIO(run_script_stdout)
|
||||
return patch.return_value
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('no_memoize')
|
||||
@pytest.mark.parametrize('script', [
|
||||
'npm watch-test', 'npm develop'])
|
||||
def test_match(script):
|
||||
command = Command(script, stdout)
|
||||
assert match(command)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('no_memoize')
|
||||
@pytest.mark.parametrize('command, run_script_out', [
|
||||
(Command('npm test', 'TEST FAIL'), run_script_stdout),
|
||||
(Command('npm watch-test', 'TEST FAIL'), run_script_stdout),
|
||||
(Command('npm test', stdout), run_script_stdout),
|
||||
(Command('vim watch-test', stdout), run_script_stdout)])
|
||||
def test_not_match(run_script, command, run_script_out):
|
||||
run_script.stdout = BytesIO(run_script_out)
|
||||
assert not match(command)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('no_memoize')
|
||||
@pytest.mark.parametrize('script, result', [
|
||||
('npm watch-test', 'npm run-script watch-test'),
|
||||
('npm -i develop', 'npm run-script -i develop'),
|
||||
('npm -i watch-script --path ..',
|
||||
'npm run-script -i watch-script --path ..')])
|
||||
def test_get_new_command(script, result):
|
||||
command = Command(script, stdout)
|
||||
assert get_new_command(command) == result
|
31
thefuck/rules/npm_run_script.py
Normal file
31
thefuck/rules/npm_run_script.py
Normal file
@ -0,0 +1,31 @@
|
||||
import re
|
||||
from subprocess import Popen, PIPE
|
||||
from thefuck.utils import for_app, memoize, eager
|
||||
|
||||
|
||||
@memoize
|
||||
@eager
|
||||
def get_scripts():
|
||||
proc = Popen(['npm', 'run-script'], stdout=PIPE)
|
||||
should_yeild = False
|
||||
for line in proc.stdout.readlines():
|
||||
line = line.decode()
|
||||
if 'available via `npm run-script`:' in line:
|
||||
should_yeild = True
|
||||
continue
|
||||
|
||||
if should_yeild and re.match(r'^ [^ ]+', line):
|
||||
yield line.strip().split(' ')[0]
|
||||
|
||||
|
||||
@for_app('npm')
|
||||
def match(command):
|
||||
return ('Usage: npm <command>' in command.stdout
|
||||
and not any(part.startswith('ru') for part in command.script_parts)
|
||||
and command.script_parts[1] in get_scripts())
|
||||
|
||||
|
||||
def get_new_command(command):
|
||||
parts = command.script_parts[:]
|
||||
parts.insert(1, 'run-script')
|
||||
return ' '.join(parts)
|
Loading…
x
Reference in New Issue
Block a user