Friday, March 27, 2015

Setting up a private Bower repository in an offline network

Previously I blogged about setting up an offline NPM repository (in a disconnected-from-the-internet network). This gave web devs in my organization a solution for working comfortably with nodejs packages. But what about front-end packages?

If you are not living on mars, you've probably heard of Bower, which is a nodejs package that helps you manage your front-end dependencies. This is the most popular front-end package manager existing today. So I decided to introduce it to my organization and let developers use it.

Like I said, Bower is a regular nodejs package installed on your machine. When you want to install angular in your project, you just go to your project folder and run bower install angular
How does this magic work? Where does Bower find the angular source code? There must be some service that Bower goes to to resolve the name "angular". Maybe it resolves to a git repository or something.
Well, this is exactly what happens. Bower is pre-configured with the address of a mapping service, which receives a name of a package and responds with a repository url. The most common repository types are github repositories. Then your bower client goes to that url, fetches the code and stores it in a folder called bower_components in your current path. Cool.

To make this work in a closed offline network I needed 3 things:
1. Have a package repository.
2. Create a similar mapping service that maps package names to our repository.
3. Have Bower reconfigured to communicate to our mapping service (registry in bower terms).

A package repository we already had. We were using Atlassian Stash as a git projects repository (like github). So I created a project named "bower libraries" and pushed some common libraries.
The mapping service I wrote with a colleague in a couple of hours. We found a large mapping file on bower.io with the original mappings to github, and we simply changed it to point to Stash. The service used this file and responded to requests looking at that file.
To reconfigure bower you simply create a .bowerrc file in your profile folder or project folder, containing this:
{
"registry": {
    "search": [
      "ADDRESS_OF_REGISTRY_HERE"
    ]
  }
}

Currently I'm helping teams to migrate from chaos to Bower :)

If you have any questions or need help - I'm here :)


Friday, February 13, 2015

Setting up a private NPM repository in an offline network

The organization I work in is disconnected from the internet.Yes, it is terrible. That's the reason I came up with this.

Effective web development requires good tools. Tools to help you lint, precompile css, test and run customized tasks. These tools exist and the comfortable way of doing this is using npm (Node Package Manager - comes bundled with nodejs). It allows you to easily install such tools.

Many developers are not familiar with these tools, and it is my job to acquaint them with better methods of developing software. One thing I learned is that the best chance to ease people into starting using a new technology is showing them that it is easy to use. And using these tools without simply running npm install karma is hard and annoying. The problem is, of course, npm install karma will not work for us because we are not connected to the internet and the central npm registry is not available for us.

So I looked for a way to make our own private npm registry. Like we have our own Nexus repository for Java artifacts.
I found several solutions.
There was one that required setting up a couch base instance. Too complicated.
I tried using npm_lazy and reggie but ran into too many bugs, and honestly these are not serious projects and it doesn't seem they will be maintained a lot longer.
Finally I stumbled upon a package sinopia, which seemed serious comparing to the former libs.

Sinopia now runs and workds and some teams are already using it. Still there are some small issues that I had to fix in the beginning.

So if you have a network behind a firewall and you need a private npm registry - use Sinopia! It's awesome! :)