Package managers

Puchkov Vladimir, DEWEB Studio

Package managers for Frontend Developer

Puchkov Vladimir, DEWEB Studio

History. What is the reason?

A long time ago in a galaxy far, far away... There was the Frontender Hero, that could make everything. HTML, CSS and JS was piece of cake for him.

History. What is the reason?

He thought... Why? Why I should do all that task by myself. Backend developers are so rude. They think only of themselves...

Drupal7, Drupal 8, Twig, style me that, style me this...

But I can over wrap all that into div, sorry!

History. What is the reason?

He thought... Why? Why I should do all that task by myself. Every time I should install all JS libraries from the begining, search for update, delete previous etc.

Why every developer has a package manager, but frontend developer no?

I'll fix that! Beware!

History. What is the reason?

I made my package manager with preferance and accessible female packages!!!

Bower

What is package managers

A package manager or package management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer's operating system in a consistent manner.
A package manager deals ffwith packages, distributions of software and data in archive files.

What is package managers

Packages contain metadata, such as the software's name, description of its purpose, version number, vendor, checksum, and a list of dependencies necessary for the software to run properly. Upon installation, metadata is stored in a local package database.
Package managers are designed to eliminate the need for manual installs, search and updates.

What is package managers

Bower: Installation

Bower: Installation

			sudo npm install bower -g
		

After install check the version to verify:

			bower --version
			bower -v
		

Bower: Use

Search package

https://bower.io/search/

			bower search jquery
			bower s jquery
		

Installation of the package:

			bower install jquery
			bower i jquery
		

Bower: Use

It's important to understand and remember, that it's provide you method of plugin the libraries as AMD and Common.js. It only mount the packages that you need with their dependencies. All other things on sight of developer.

Bower: Use

Check the installed packages

			bower list
		

Information about the installed packages

			bower info
		

Uninstall package

			bower uninstall jquery
		

Bower: Use

Specific version of package

			bower install jquery#1.6.1
		

Updating the package

			bower update jquery
		

Bower: Use

Specific version of package

			bower install jquery#3.1.1
		

Resolution

			Unable to find a suitable version for jquery,
			 please choose one by typing one of the numbers below:
			1) jquery#^1.0.1 which resolved to 1.12.4 and is required by bower
			2) jquery#^3.1.1 which resolved to 3.1.1
		

Bower: Use

Semantic versioning - http://semver.org/lang/ru/

How to advoid dependencies hell, for backward compatibility

			#2.1.4 =  2.1.4
			#~2.1.4 >= 2.1.4 < 2.2.0
			#^2.1.4 >= 2.1.4 < 3.0.0
		

Bower: Use

Let's make some practise

			bower i jquery#1.0.1
			bower update jquery
		

Add ~ in manifest file

			bower update jquery
		

Bower: Use

Change ~ to ^

			bower update jquery
		

Change ^ to latest

			bower update jquery
		

Bower: Use of Manifest file

But the real deal of using bower is implement it with Manifest file.

We can add it by hands or with use of command

			bower init
		

Let's take a look closely to this command

Bower: Use of Manifest file

name - only field neccessary to be filled, name of the project

version - three digit number

author - author of the project

ignore - ignore pathes, that we want to be ignored

dependencies - dependencies of your project

main file - file that will be targeting to the --path that used gulp, grunt

Bower: Use of Manifest file

description, keyword - helps to find your package

licence - licence of using your package

Bower: Use of Manifest file

Initialize dependencies to manifest file:

			bower init
		

Add dependencies to the file

			"dependencies": {
    	  "jquery": "^3.1.1",
    	  "snap.svg": "latest"
  		}
		

Bower: Use

Then just install

			bower install
		

If we want to add to dependencies

			bower install jquery --save
			bower install jquery -S
		

Bower: Use

If we want to add to to developer dependencies

			bower install jquery --save-dev
			bower install jquery -D
		

Bower: Use

Uninstall from project and bower.json

			bower uninstall jquery --save
			bower uninstall jquery --save-dev
			bower uninstall jquery normalize.css -D
		

Bower: Use

Packages with own dependencies

For example bootstrap

			bower install jquery#1.0.1 --save
			bower i bootstrap -S
		

Bower: Use

If we have a conflict in dependencies

			"resolutions": {
    	  "jquery": "1.9.1 - 3"
  		 }
		

Making own package

Let's create own package

Making own package

Create git repository for our package

Clone it

Initialize bower install

			bower init
		

Making own package

Add file, commit and push with tag

			git add bower.json
			git commit -m "Add bower.json"
			git tag "v0.0.1"
			git push origin master --tags
			git register [name] [path to git repository]
		

Making own package

Remove package from bower

			bower login
		

Use data from your GIThub

			? Username Vladimir
			? Password ********
			bower EAUTH         Logged in as VladimirPuchkov
			bower unregister [name]
		

Remove package from bower

			? You are about to remove component [name] from the bower
			registry (https://bower.herokuapp.com). It is generally considered bad
			on behavior to remove versions of a library that
			others are depending on. Are you really sure?
		

YES

Bower API

cache

			$ bower cache <command> [<args>]
		

Manage bower cache

cache clean

			$ bower cache clean
			$ bower cache clean <name> [<name> ...]
			$ bower cache clean <name>#<version> [<name>#<version> ..]
		

Bower API

cache list

Lists cached packages

			$ bower cache list
			$ bower cache list <name> [<name> ...]
		

Bower API

help

Display help information about Bower

			$ bower help <command>
		

Bower API

home

Opens a package homepage into your favorite browser.

			$ bower info <package>
			$ bower info <package> [<property>]
			$ bower info <package>#<version> [<property>]
			$ bower info <package>#&l;tversion> [&l;t;property>] [<options>]
		

Displays overall information of a package or of a particular version.

Bower API

init

Interactively create a bower.json file

			$ bower init
		

Bower API

install

Installs project dependencies recursively.

			$ bower install [<options>]
			$ bower install <endpoint> [<endpoint> ..] [<options>]
		

Bower API

When --save flag is used, all additional endpoint are saved to dependencies in bower.json.

Endpoints can have multiple forms:

Bower API

Where:

Bower API

Package can be any one of the following:

Registered package name jquery
normalize.css
Git endpoint https://github.com/user/package.git
git@github.com:user/package.git
Git endpoint without .git git+https://github.com/user/package
git+ssh://git@github.com/user/package
Local folder my/local/folder/

Bower API

Package can be any one of the following:

Public Subversion endpoint svn+http://package.googlecode.com/svn/
Private Subversion endpoint svn+ssh://package.googlecode.com/svn/
svn+https://package.googlecode.com/svn/
Shorthand (defaults to GitHub) user/package

Bower API

Package can be any one of the following:

URL http://example.com/script.js
http://example.com/style.css
http://example.com/package.zip (contents will be extracted)
http://example.com/package.tar (contents will be extracted)

Bower API

A version can be:

semver version #1.2.3
version range #1.2
#~1.2.3
#^1.2.3
#>=1.2.3 <2.0
Git tag #<tag>
Git commit SHA #<sha>

Bower API

A version can be:

Git branch #<branch>
Subversion revision #<revision>

Bower API

install options:

Bower API

link

The link functionality allows developers to easily test their packages. Linking is a two-step process.

			$ bower link
			$ bower link <name> [<local name>]
		

Using ‘bower link’ in a project folder will create a global link. Then, in some other package, bower link <name> will create a link in the components

Bower API

folder pointing to the previously created link.

This allows you to easily test a package because changes will be reflected immediately. When the link is no longer necessary, simply remove it with bower uninstall <name>.

Bower API

list

List local packages and possible updates.

			$ bower list [<options>]
		

list options

Bower API

lookup

Look up a package URL by name

			$ bower lookup <name>
		

Bower API

login

Authenticate with GitHub and store credentials. Required to unregister packages.

			$ bower login
		

login options

https://github.com/settings/tokens

Bower API

prune

Uninstalls local extraneous packages

			$ bower prune
		

Bower API

register

Register a package

			$ bower register <name> <url>
		

Bower API

search

Finds all packages or a specific package.

			$ bower search
			$ bower search <name>
		

Bower API

update

Updates installed packages to their newest version according to bower.json.

			$ bower update <name> [<name> ..] [<options>]
		

Bower API

uninstall

Uninstalls a package locally from your bower_components directory

			$ bower uninstall <name> [<name> ..] [<options>]
		

Bower API

unregister

Unregisters a package

			$ bower unregister <package>
		

Bower API

version

Run this in a package directory to bump the version and write the new data back to the bower.json file.

			$ bower version [<newversion> | major | minor | patch]
		

The newversion argument should be a valid semver string, or a valid second argument to semver.inc (one of “build”, “patch”, “minor”, or “major”). In the second case, the existing version will be incremented by 1

Bower API

in the specified field

If run in a git repo, it will also create a version commit and tag, and fail if the repo is not clean.

If supplied with --message (shorthand: -m) config option, bower will use it as a commit message when creating a version commit. If the message config contains %s then that will be replaced with the resulting version number.

Bower API Options

force

Makes various commands more forceful

			-f, --force
		

Bower API Options

Bower API Options

json

Output consumable JSON

			-j, --json
		

Bower API Options

loglevel

What level of logs to report. Possible values: error, conflict, warn, action, info, debug

			-l, --loglevel
		

Bower API Options

offline

Do not use network connection

			-o, --offline
		

Bower API Options

quiet

Only output important information. It is an alias for --loglevel=warn.

			-q, --quiet
		

Bower API Options

silent

Do not output anything, besides errors. It is an alias for --loglevel=error. Silent is also useful if you have private components that might leak credentials to your CI environment.

			-s, --silent
		

Bower API Options

verbose

Makes output more verbose. It is an alias for --loglevel=debug.

			-V, --verbose
		

Bower API Options

allow-root

Allows running commands as root. Bower is a user command, there is no need to execute it with superuser permissions. However, if you still want to run commands with sudo, use --allow-root option.

			--allow-root
		

Bower API Consiming a package

You can use build tools to easily consume Bower packages such as Gulp and Grunt

If you use bower list --paths or bower list --paths --json, you will get a simple name-to-path mapping:

			{
			  "backbone": "bower_components/backbone/backbone.js",
			  "jquery": "bower_components/jquery/dist/jquery.js",
			  "underscore": "bower_components/underscore/underscore.js"
		

Bower Programmatic API

Bower provides a powerful, programmatic API. All commands can be accessed through the bower.commands object.

			var bower = require('bower');
			bower.commands
			 .install(['jquery'], { save: true }, { /* custom config */ })
			 .on('end', function (installed) {
			  console.log(installed);
			  });
		

Bower Running on a continuous integration server

Bower will skip some interactive operations if it finds a CI environmental variable set to true. You will find that the CI variable is already set for you on many continuous integration servers, e.g., CircleCI and Travis-CI. More info https://bower.io/docs/api/#running-on-a-continuous-integration-server

Bower Tools for furthermore integration

Bower is used together with other tools to integrate with all sorts of setups and workflows. Gulp, Grunt, Rails, Ruby, Java, Apps & IDEs and more https://bower.io/docs/tools/

Bower Configuration

Bower can be configured using JSON in a .bowerrc file. For example:

			{
			 "directory": "app/components/",
			 "timeout": 120000,
			 "registry": {
			    "search": [
			      "http://localhost:8000",
			      "https://bower.herokuapp.com"
		

Bower Configuration

The config is obtained by merging multiple configurations by this order of importance

Bower Configuration

More about configuration

https://bower.io/docs/config/

https://github.com/bower/spec/blob/master/config.md

Bower Pluggable Resolvers

Pluggable resolvers allow you to use resolvers created by 3rd party JavaScript developers — including overriding default resolvers used by Bower. More https://bower.io/docs/pluggable-resolvers/

Why npm?

The main difference npm from Bower - way of mounting dependencies of packeges. npm install them for every package separately. So we have something like that node_modules/grunt/node_modules/glob/node_modules/… where can be several versions of the same package. In the client JS this is forbidden: you can't plug two different versions of the same library of jQuery for example. In the Bower we have only one version of the package, Bower will depricate this, to warn us.

Why it is useful for frontend?