question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request: Lenient shell command parser

See original GitHub issue

Description

It’d be great if normal bash command modifiers could be accepted (see below for example). I know same can be accomplished with an external script or an npm script – but a one-liner would be super useful and less clutter.

Steps to reproduce

This example will fail the hook if there are any lingering .only()s left in staged test files. The ! is used to negate the exit code of grep.

  "lint-staged": {
    "src/**/*.test.js": [
      "!grep -q '\\.only\\W' "
    ]
  }

Debug Logs

...
Running tasks for src/**/*.test.js [started]
  lint-staged:make-cmd-tasks Creating listr tasks for commands [ "!grep -q '\\.only\\W' " ] +0ms
  lint-staged:find-bin Resolving binary for command `!grep -q '\.only\W' ` +349ms
Running tasks for src/**/*.test.js [failed]
→ !grep could not be found. Try `npm install !grep`.
Running linters... [failed]
!grep could not be found. Try `npm install !grep`.

This and not being able to position the file in the command, (e.g. grep -q '\.only\W' {}; test $? -eq 1 where {} is the (supposed!) file placeholder), is a real handicap in this otherwise very useful tool, IMHO.

Environment

  • OS: macOS High Sierra
  • Node.js: v11.13.0
  • lint-staged: v8.2.1

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mooscommented, Jul 1, 2019

@iiroj Yes that worked – I don’t know how, but it did. Brilliant solution to both complex commands and filename placeholder!

// lint-staged.config.js:
module.exports = {
  "*.js": [
    () => "! grep foo bar.j"
  ]
};
// bar.js:
// I haz foo

Output:

  ❯ Running linters...
    ❯ Running tasks for *.js
      ✖ () => "! grep foo bar.js"

✖ () => "! grep foo bar.js" found some errors. Please fix them and try committing again.
// I haz foo

Removing the ! is successful. Compound commands also works:

  "*.js": [
    () => "grep foo bar.js; test $? -eq 1"
  ]
0reactions
lkraavcommented, Aug 27, 2020

The ! is used to negate the exit code of grep.

PS nice idea @moos point_up

Not so simple with ! after all - obviously it also negates exit code 0. Had to convert to lint-staged.config.js and build my own command-line:

const path = require('path');

module.exports = {
    '*.php': (filenames) => {
        // @see https://github.com/okonet/lint-staged#example-use-relative-paths-for-commands
        const cwd = process.cwd();
        const relativeFilenames = filenames.map((file) => path.relative(cwd, file)).join(' ');

        return [
            `./vendor/bin/phpcbf ${relativeFilenames}; if [ $? -eq 1 ]; then exit 0; fi`,
            `./vendor/bin/phpcs -s ${relativeFilenames}`,
            `./vendor/bin/psalm ${relativeFilenames}`,
        ]
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple shell script parser - metacpan.org
This module implements a rudimentary shell script parser in Perl. It was primarily written as a backend for Syntax::Highlight::Shell , in order to...
Read more >
16 Parsing command line arguments with util.parseArgs()
The following steps are involved in processing command line arguments: The user inputs a text string. The shell parses the string into a...
Read more >
Parsing Command Line Arguments in C++? - Stack Overflow
I understand the use of this library in C code, but IMO, this is way too low level to be acceptable in any...
Read more >
jc | CLI tool and python library that converts the output of ...
Argument Command or Filetype Documentation ‑‑acpi acpi command parser details ‑‑airport airport ‑I command parser details ‑‑airport‑s airport ‑s command parser details
Read more >
picocli - a mighty tiny command line interface
Lenient Mode. From picocli 3.2, the parser can be configured to continue parsing invalid input to the end. When collectErrors is set to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found