how to pass args to package.json

22 Views Asked by At

I want users to be able to invoke npm run newpost <name> from the command line, which should result in the command hugo new blog/<name>.md being executed. So, e.g., my package.json file might contain a line like:

"scripts": { "newpost": "hugo new blog/$1.md" }

The idea is that the $1 would be replaced by the word <name>. Of course, I'm just making up the $1 syntax here, but wondering how to accomplish this in the real world. I've seen posts in stackoverflow that allow you to pass parameters directly from the command line to a script that is invoked by the scripts entry, but I want access to the parameter directly inside the package.json file so I can build a new parameter to pass to the script being invoked (or, as in this case, an executable).

Please don't bother giving me a solution that complicates what the user has to type, such as setting an environment variable. My requirement is that the user only has to type npm run newpost <name>. However, the scripts entry in the package.json file can be as complex as needed since once set correctly, it never has to be modified again.

0

There are 0 best solutions below