2

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 ?

1 Answer 1

1

Edit:

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!


Old Answer:

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.

Sign up to request clarification or add additional context in comments.

4 Comments

What i would like to do is to have my default scripts in the package.json. And then when i start a certain script via the command line, i want to be able to inject an extra argument that will be injected into the script line before it executes. I have to simplify it made a pastebin where i got this a demo with the result i would like to have. PASTEBIN And thanks i did indeed spotted the typo !! Many thanks for your help buddy. You already learned me a thing a 2 and i love it. Brain food ;)
Ah, it's a shame that browserify takes files before options. npm has a way to append arguments to a run command, but that doesn't help you here because browserify expects options at the end instead of files. It's possible the files and options interchangeable, but I'm not at liberty to play with it right now. Hrm... Your pastebin smells funny... I feel like there should really be a way to not need this, but I don't know enough about why you want to do this. However, I think I have a solution. I'll edit the above for it.
true that is to bad indeed. I have not found any solution to this matter so i will just input the files manually. Most of my problems are solved by using a npm module so i can run multiple lines at the same time [npm concurrently]. But thanks for your time and help buddy
Great implementation with Angular CLI Server Run/Debug config in IntelliJ for my Angular/Spring-boot app. Worked very well to dynamically use different environment configs for each user without everyone having to modify package.json when they want to load their specifics and potentially push the changes to the repo. Also avoids devs having to ignore pushing/pulling package.json altogether.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.