I would like to do something like this without specifying the filename:
"scripts": {
"test <any_file.js>": "*actual command* <any_file.js>"
}
I would like to do something like this without specifying the filename:
"scripts": {
"test <any_file.js>": "*actual command* <any_file.js>"
}
What you actually want to do is reading an argument. You can use process.argv
for that. More about the global node variable process
to be found here.
When your script tag looks something like this:
"scripts": {
"my-command": "my-script.js"
}
When your script file looks something like this:
console.log( process.argv );
Then all is left to do is run your command with npm
or yarn
.
npm run my-command <your-argument e.g. file.js>
If you want to pass it to another command, you can call that command with a require( 'child_process' ).exec
. A good example about that can be found here.