Generate bower file automatically? - javascript

In current project I have pretty big bower file. Many dependencies are out of dated and have hardcoded version like ~1.2. I have replaced it with "latest" and run bower install/update and everything went ok.
The problem is that I don't want to have such settings like "latest" on production server. What is the easiest way to grab current versions of bower components and put it to the bower file? aka generate bower file from existing dependencies.

Using bower init, you'll be able to regenerate from scratch your bower.json file with all the current dependencies

With bower install --save jquery#<version_number> you can install specific versions of bower components, e.g.
bower install --save jquery#1.10.2
the --save automatically adds the dependency to bower.json. Normally there is no need to edit bower.json manually.

Related

Use javascript file in a different project

I have a javascript (which uses files from it's own project) file that I need to use in a different project without copy the code.
How can I do it?
Thanks
Well... You could install Bower, upload your file to your own repository or gist on GitHub and then use bower to include them as a dependancy for your project.
Setting up (assuming you have Node.js already installed)
npm install -g bower
cd /path/to/project-A
bower init
bower install <link to your gist/repo> --save
cd /path/to/project-B
bower init
bower install <link to your gist/repo> --save
And then in the future, if you upload changes to your repo/gist (and the link has not changed...)
cd /path/to/project
bower update
EDIT: That or you have a remotely hosted version of this file that you directly link to, I guess...? Kinda sucks though, could wreck a live project from a dodgy edit that way

Installing individual plugin with bower

I'm looking at the d3-plugins repository and I want to install only the horizon chart plugin.
At the moment my bower.json file has this entry:
"d3-plugins": "*"
But this will install the entire repo containing all the other plugins as well.
Is there a way I can specify bower to install only one subdirectory of a project?
This is probably not optimal, but you can point bower directly to the horizon.js URL:
"dependencies":{
"d3-plugins-horizon": "https://raw.githubusercontent.com/d3/d3-plugins/master/horizon/horizon.js"
}

How to stop Bower pulling more than I need?

I've only just started using Bower, so I'm likely doing it wrong. However, when I pull down jQuery through bower using this:
bower install jQuery#1.9.1
I get a folder which is about 700k in size. The only thing I need in that folder however is the jquery.js or jquery.min.js, which are 239k, and 82k respectively.
As far as I understand it, this is because bower simply pulls down everything that's in the Github repository whether it's needed or not.
Is there any way to stop this? So that bower only pulls down what is needed to develop? Or Am I misunderstanding how I should be using Bower?
Thanks.
You're using it correctly. You should have a bower.json file with all of your dependencies specified so that you don't have to include bower's plugin directory with your source code, then just run bower install from a new machine to get the dependencies.
When you install a plugin and want your bower.json automatically updated, run bower install mydependency --save.

How to update bower.json with installed packages?

In my project I've installed bower components without save option. Now, I would like update to bower.json?
How can I update bower.json with installed packages?
Just list your dependencies:
bower list
Then you should run all install command with param '--save' like this:
bower install bootstrap --save
It's a hard work, but if you have a thousand dependencies, could you create a script to automatize the task.
A little trick if you don't want to write a script for that:
before doing anything rename your bower.json in bower2.json for example.
then you can do a:
$ bower init
(automatically create a bower.json file).
note that all questions should be pre-filled with your current config.
When it will ask you:
set currently installed components as dependencies?
say yes,
You now have all your dependencies in the new bower.json file (and you can check if everything is right with the old bower2.json)
A bit arduous way is to run bower list, look for packages labeled extraneous and add those manually to the dependencies in the bower.json.
If there are a lot of extraneous packages, it might be easier to workaround this by running bower init and answering Yes to "set currently installed components as dependencies?". This will take your current bower.json, read it and then create new one using information from the old one. So in an ideal case you will have the same file just with extraneous packages added.
Warning: Firstly, there might be something lost in the process (e.g. devDependecies). Secondly in the last version of bower (v1.2.7) this will not preserve current packages info! I feel it is a bug. However you can save the old file and (manually) merge it with the generated one.
Also, you should ask for an option or something by opening a bower issue as this would be welcomed by many developers.
You can use bower-check-updates (you need installed node.js on your machine):
bower-check-updates is a utility that automatically adjusts a bower.json with the latest version of all dependencies
bower-check-updates - is a fork of npm-check-updates, so it's all the same but updates bower.json, instead of package.json
npm install -g bower-check-updates
bower-check-updates -u
bower install
This will install bower-check-updates globally, so you can launch it from anywhere.
P.S. for more information about npm-check-updates pleas see this topic
If there aren't that many bower packages you have installed, try writing bower install [package_name] --save. This will just update your bower.json file.
After bower-check-updates -u you must run bower install instead of npm install

Can I add a git repository to my bower.json? [duplicate]

I have a very small repo in which I do all dev work in the master branch and use tags as "stable" points in history.
I guess by default Bower seems to fetch the latest tagged version of a repo. I'm trying to get the most recent commit in the master branch.
I've tried running all these, in every conceivable order:
bower cache-clean mypackage
bower install mypackage --force-latest
bower install mypackage --force --force-latest
bower install mypackage --force
I've also tried adding latest to my bower.json file:
"dependencies": {
"mypackage": "latest"
}
And then running:
bower update mypackage
No matter what it seems to always get the latest tagged state.
How do I get the latest, most up-to-date, untagged state of the project?
Specify a git commit SHA instead of a version:
bower install '<git-url>#<git-commit-sha>'
Example:
bower install 'git://github.com/yeoman/stringify-object.git#d2895fb97d'
You can also specify a branch instead of a SHA, but that's generally not recommended unless it's in development and you control all the parts.
Yes, you can point to the git url, or use name/repo shorthand (for github repos):
bower.json
{
"name": "bower-test",
"dependencies": {
"dpm": "git#github.com:okfn/dpm.git",
"docker-nmpjs": "terinjokes/docker-npmjs"
}
}
More in the docs
As #roi noted in the comments, you can use the --save flag to automatically add dependencies to bower.json, e.g. bower install terinjokes/docker-npmjs --save
You can install a branch in Bower > 1.0.0:
bower install xxx#foo-branch
More details at https://github.com/bower/bower/issues/107#issuecomment-22352689.
If you are using a bower.json file you specify the latest version of a branch with a line in either the dependencies or devDependencies as appropriate for your project configuration:
"angular-bootstrap": "git#github.com:angular-ui/bootstrap.git#bootstrap3",
Then when you run bower install the latest version of that branch is installed. That would be branch bootstrap3 of angular-ui in this example.
bower install --save package-name#master
adds this:
"dependencies": {
"package-name": "master"
}
using bower.json:
"dependencies": {
"jquery.slimscroll": "latest",
"jQuery": "1.11",
"fullPage.js": "git#github.com:overbyte/fullPage.js.git#1d6bbac3d4c3b1d3d7d4096cdbcabd1c3914393f",
}
where
"[library name - in this case a forked version of fullpage.js]" : "[from git clone box in github][#commit number if required - without this you will get latest tagged version]"

Categories

Resources