coffeescript
coffeescript copied to clipboard
CLI `npm` `scripts` and input `.coffee` file/s as last argument conflicting with `--watch`
The CLI doesn’t currently play nice with [p
]npm
scripts
, when calling one from another…
Expected Behavior
In this contrived, simplified example:
// package.json
"scripts": {
"compile": "coffee --compile *.coffee",
"watch": "npm run compile -- --watch"
}
npm run watch
should run coffee
in watch mode, but this seems to be impossible because of the design of the command line API.
Current Behavior
The above throws a File not found: .../my-project/--watch
error.
Possible Solution
Allow the positional *.coffee
parameter/s to be anywhere, rather than forcing it to be the last argument, or add some kind of -
[-i
]nput
option.
Context
- CoffeeScript version: 2.7.0
- Node.js version: 20.6.1
Allow the positional
*.coffee
parameter/s to be anywhere
I don’t think this is possible, because some flags take values. You could argue that when a non-flag argument is passed after a flag that accepts values, treat it as a value else treat it as input, but I think this could be confusing UX. It’s very common to require the input to come last, like how the node
command works.
Personally I don’t think this is so bad:
"compile": "coffee --compile *.coffee",
"wach": "coffee --watch --compile *.coffee"
If you’re determined for DRYness, maybe something like this would work:
"coffee": "coffee $COFFEE_ARGS *.coffee",
"compile": "COFFEE_ARGS=--compile npm run coffee",
"watch": "COFFEE_ARGS='--compile --watch' npm run coffee"
I don’t think this is possible, because some flags take values.
Maybe I am missing something, but dozens of other cli’s don’t seem to have this problem…
Probably most of them use one of the available argument parsing libraries…
Is this ultimately because CoffeeScript has no dependencies
?
Personally I don’t think this is so bad:
"compile": "coffee --compile *.coffee", "wach": "coffee --watch --compile *.coffee"
@GeoffreyBooth Sure, that isn’t so terrible. But as mentioned, the contrived example I gave was much simplified 😉
For reference, I also thought of this handy workaround (using pnpm
and package.yaml
, because—in the spirit of CoffeeScript—who sane wants to write JSON by choice?):
# package.yaml
scripts:
compile: coffee --compile *.coffee
watch: $npm_package_scripts_compile --watch