lukechilds / zsh-better-npm-completion

Better completion for npm
MIT License
464 stars 35 forks source link

Autocompleting npm scripts adds spaces #4

Closed ghost closed 7 years ago

ghost commented 7 years ago

When I use npm run something and the scripts object is indented, zsh-better-npm-completion adds the indentation like \ \ \ \ for 4 space indentation in front of the found script.

lukechilds commented 7 years ago

Yeah it turns out implementing a JSON parser in zsh is actually pretty tricky 😆

Thanks for reporting, I should have time to take a look at this tomorrow.

lukechilds commented 7 years ago

@con-ssc can you paste your package.json?

ghost commented 7 years ago

Sure

{
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "@types/d3": "^4.5.0",
    "@types/jquery": "^2.0.40",
    "@types/xrm": "^8.0.37"
  },
  "dependencies": {
    "d3": "^4.7.1",
    "stylus": "^0.54.5",
    "typescript": "^2.2.1"
  },
  "scripts": {
    "stylus": "stylus -w hierarchy/src/hierarchy.styl",
    "typescript": "tsc -w -p ./hierarchy",
    "browser-sync": "browser-sync start --server \"hierarchy/dist\" --files \"hierarchy/src/*.*\"",
    "dev": "concurrently --kill-others --prefix \"[{name}]\" --names \"STYLUS,TYPESCRIPT,HTTP\" -c \"bgBlue.bold,bgMagenta.bold,bgGreen.bold\" \"npm run stylus\" \"npm run typescript\" \"npm run browser-sync\""
  }
lukechilds commented 7 years ago

That's working ok for me, maybe it's a tab/space issue. Are you suing tabs or spaces?

ghost commented 7 years ago

I use spaces

lukechilds commented 7 years ago

Strange, if I paste that package.json it works. Could you upload the file somewhere and send me the link so I can test the exact file.

lukechilds commented 7 years ago

or link me to the repo if it's open source.

ghost commented 7 years ago

You are right, it must have been a differen package.json since this has only two spaces indent, I go checking.

lukechilds commented 7 years ago

Why do you have more than two spaces of indentation?

ghost commented 7 years ago

because I have a global editor config that says 4 spaces indent

ghost commented 7 years ago

Right now, the npm autocompletion doesn't work at all :( so I cannot reproduce the error.

lukechilds commented 7 years ago

Hmmn, I haven't pushed any updates for a while.

If you revert to default npm formatting does it work?

Running npm install --save foo should auto format it for you.

lukechilds commented 7 years ago

Ok, so I'm using the amount of spaces to determine the object depth:

cat package.json | sed -nE "/^  \"scripts\": \{$/,/^  \},?$/p"

That returns the scripts object, notice the two spaces hardcoded in. That's to stop us accidentally grabbing the wrong scripts object if the package.json looks something like this:

{
  "version": "1.0.0",
  "name": "some-pkg",
  "scripts": {
    "foo": "bar"
  },
  "babel": {
    "scripts": {
      "fizz": "buzz"
    }
  }
}

I can't really support your use case without adding a crazy amount of complexity to the plugin. Sorry 😕

ghost commented 7 years ago

Thanks for your effort. would it make sense to actually use jq? because then you would just type cat package.json | jq .scripts ./jq

lukechilds commented 7 years ago

If I could rely on jq being installed on users machines that would be perfect. Unfortunately I don't think it's very common.

ghost commented 7 years ago

Good point, but can't you add dependencies to your plugin or something?

ghost commented 7 years ago

Or maybe it is just a matter with using zsh on windows

lukechilds commented 7 years ago

No, there's no way to add a dependency. I think some plugin managers may support external shell scripts, but this needs to work on lots of different plugin managers + standalone script.