Reputation: 159
But this is what i want to do.
{
"name": "tcp-web",
"version": "1.0.0",
"description": "The Curiosity Project",
"main": "index.html",
"scripts": {
"bundle": "browserfy "
},
"author": "@Grandmother#5057",
"license": "ISC"
}
The complete string i need to input in my console contains a ton of scripts and css files that need to be bundled into 1 file. Writing this out every time is a hassle and if i script it static in the .JSON file i always have to re-edit it.
I would like to know if it is possible to inject a command line string directly into the scripts: {} line in the JSON.
So my JSON file would already contain all the files i have statically added, then i would like to inject the new files in command line so they get combined with the already written script line.
I wonder if this is possible ?
Upvotes: 2
Views: 300
Reputation: 134
You could do something like...
"scripts": {
"main": "browserify index.js ${ADDITIONAL_FILES} -o assets/bundle/index.js"
}
Then call NPM like so:
ADDITIONAL_FILES="index2.js index3.js" npm run main
In this package.json
snippet, the main
script is interpolating a shell variable called ADDITIONAL_FILES
. If ADDITIONAL_FILES
is empty, it will be a blank string, which will not affect anything. If ADDITIONAL_FILES
has some value, it will be substituted. In the call to NPM, we can set ADDITIONAL_FILES
as we call npm
, getting your extra files into things. Setting the variable like above will not persist. It will only live for the duration of this command, so you don't have to worry about ADDITIONAL_FILES
being set in later runs.
I'm a little bit suspicious of why you need to do this. This suggests you have multiple entrypoints to your application, which is unusual. However, you know more about your project than anyone else :)
Hope this helps!
This looks like a package.json
file. Whatever is in scripts
will be executed on the command line, so you have a great deal of flexibility here.
You could do something like...
"scripts": {
"command": "MY_DATA=$(cat some_file) script_that_uses_MY_DATA_environment_variable.sh"
}
If you're looking to get file names, you could write a small Node script to collect and output them or even use ls
and awk
if you're more comfortable with Bash. This is flexible. There are multiple solutions.
If you need more specific help, let me know with a comment :)
Edit: Alternatively, if you want to describe the context around this goal, we may be able to help you come up with an even better solution
Also, browserify
will bundle modules recursively. As long as a Javascript file is referenced directly or indirectly by the first file, it will be included in the bundle. Ideally, you shouldn't have any orphaned, unconnected files, so this should cover most use cases.
Lastly, you have a typo in your package.json
. It's browserify
, not browserfy
.
Upvotes: 1