shelljs / shx

Portable Shell Commands for Node
MIT License
1.72k stars 44 forks source link

sed: no files given #169

Closed viT-1 closed 4 years ago

viT-1 commented 4 years ago

can't npm run resolvevue

package.json:

"scripts": {
...
"resolvevue": "shx sed -i \"s/'vue'/'https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.esm.browser.js'/g\" dist/*.js"
}
viT-1 commented 4 years ago

Same issue but not my variant https://github.com/shelljs/shx/issues/165

nfischer commented 4 years ago

This looks like a dupe of #165. Single quotes don't work the same on Windows. Please ping this thread if I've misunderstood.

viT-1 commented 4 years ago

@nfischer No, I'm searching text contains single quotes.

nfischer commented 4 years ago

At a glance, your code looks like it should work in that case (although I'm not sure if dist/*.js needs to be dist\*.js on Windows).

Which OS did you try this on?

viT-1 commented 4 years ago

@nfischer Yeah, I'm trying on Windows. Tried simple change 'foo' to 'bar', dist\*.js path is correct.

nfischer commented 4 years ago

I think you need to escape the / characters in your replacement string, otherwise sed treats them as special delimiters. This behavior is consistent with unix sed:

# Failing to escape quotes in unix sed would fail like so:
sed -i "s/'vue'/'https://cccdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.esm.browser.js'/g" dist/*.js
sed: -e expression #1, char 17: unknown option to `s'

You should escape the characters with a "\" in the commandline like so:

# Escape '/' characters so sed doesn't treat them specially anymore:
shx sed -i "s/'vue'/'https:\/\/cdn.jsdelivr.net\/npm\/vue@2.6.10\/dist\/vue.esm.browser.js'/g" dist/*.js

# The same is necessary for unix sed:
sed -i "s/'vue'/'https:\/\/cdn.jsdelivr.net\/npm\/vue@2.6.10\/dist\/vue.esm.browser.js'/g" dist/*.js

To use this in a package.json, you need double \ so JSON passes a single \ onto shx:

{
  "scripts": {
    "resolvevue": "shx sed -i \"s/'vue'/'https:\\/\\/cdn.jsdelivr.net\\/npm\\/vue@2.6.10\\/dist\\/vue.esm.browser.js'/g\" dist/*.js"
  }
}
nfischer commented 4 years ago

Updating labels. I don't think this is Win-specific, and it's clear this is not really a duplicate of issue #165 despite the similar error message.

viT-1 commented 4 years ago

Yes! This (in package.json) works:

shx sed -i \"s/'vue'/'https:\\/\\/cdn.jsdelivr.net\\/npm\\/vue@2.6.10\\/dist\\/vue.esm.browser.js'/g\" dist/*.js

@nfischer Please, add this to documentation.