Web Technologies

Git: Hooks run “npm install” on checkout

When working on a project you usually install various packages from npm.
Of course these packages are maintained and updated, adding more features and security fixes, and stability patches.

One person on your team should run npm outdated once per week to see what all has been updated and test if you can integrate the packages into your project.
Thus the package.json is updated and causes a grand problem for all other developers on the project. If a package has major breaking changes the code will need to be adjusted, however that code will not run on the other develoepers environment. The other developers working on the project they have to run npm update to install the missing / outdated packages in their environment.

The solution to this problem are “git hooks”, essentially git can execute code on specific events, like before commiting your code, or pre push etc.
git hooks. For my use case I would like to run npm update after a developer checks out from the git repository, this is the event “post-checkout”.

Native Git Hooks

To create a git hook you need to add a file in your project called .git/hooks/post-checkout (On linux add the executable bit with chmod +x)

You will test this and say, yes this works as intended – lets commit it to the repository. – Now you will discover that you cannot commit files in .git to the repository.
In fact git does not allow you to do this, due to security concerns as git hooks can execute any shell script.

The work around for this issue is to simply add it into a folder called git-hooks/ and tell the developers to copy the file when they set up their dev environment.


As always if there is a Problem for development with javascript there is a npm package to solve the problem.
Husky uses the package.json to define the scripts that are executed via git hooks.
Simply Install Husky
npm install husky -D

Then edit the package.json:

The Husky solution would also allow you to execute your own js file, maybe also doing some cleanup of files or running tests etc.

Posted by happyneal in Programming, Web Technologies, 0 comments

Web Tooling and Automatisation using gulp 4

Now working with Gulp you will discover that you run into a couple of minor problems. Especially in sequencing the different types of tasks.
Lets take a simple clean task, it should run before all of the other tasks. However Gulp will run all tasks in parallel.

The team that is working on Gulp has been working on a solution for this problem and in Gulp 4.0 we will get gulp.series() and gulp.parallel(); to distinguish between these two types of operations.

Sadly it looks like that currently the release of Gulp 4.0 is delayed. However you can use the current Alpha version prior to its release.

Installing Gulp 4

You can simply install the next version of Gulp with NPM (A git client is required, as the package is not in the npm repository and will be cloned from Github)

Updating the gulpfile.js

The syntax for tasks has changed, so we need to rewrite parts of the file:
Our exisiting Gulp 3 task

Needs to be rewritten with gulp parallel as such:

However we should not stop there and improve upon this task, by running the clean task before all of the other tasks. To ensure that it runs before the other tasks we use gulp.series().

Improving on the production flag

With gulp.series() we now can stop using the –production flag. We simply define a production task.

As first operation we pass in a function that simply sets the production boolean to true:

The function uses “done” to signal gulp that the function has completed.

Hiding Tasks

Another improvement of Gulp 4.0 is that you can pass functions, as well as tasks to gulp.series and gulp.parallel.

This in turn lets you write normal functions that are hidden from the command line, ensuring that everybody on the project runs the default build task instead of only the html task.

Here is now our improved Gulpfile.js:


Posted by happyneal in Blog, Web Technologies, 0 comments

HTML 5: When to use <a> or <button>


  • <a> is used for page navigation
  • <button> is used for actions on the page
  • <input type="button" /> is used in a form and the value is used in the form

The Problem

Lets take a look at this small piece of code:

Now half of the people I showed this piece of code, said: well there is nothing wrong with the code.
That is a perfectly fine way of defining a button.

If you take a closer look, you will discover a couple of smaller issues with this piece of code.

First you would notice that hovering over the button you get a text cursor instead of an ‘hand’-cursor.
This is caused by leaving the attribute “href” undefined, you can fix it via defining something like ‘#’ or via CSS.

Then you would notice that by defining the “href” tag you would cause the browser to navigate to another page or the top of the page.
To prevent this you would then need to add some Javascript code and call ‘event.preventDefault()’, or add to your previous hack and define the href as ‘#0’. This solves the problem by relying on the browser not knowing what to do with illegal element ids (ids can never start with a number).

Voila, you are now in the situation that you are using some hack pattern that degrades the readability of your code.
Someone may come along and assume that someone made a mistake defining the href attribute /or used a placeholder and forgot to replace it.

The Solution

Now let’s improve the readability of the code:

We are now using the correct tag <button>, no need for any hacks to fix the cursor or some wierd href, and no additional line of Javascript.
You will need CSS to ensure that the button looks the same in all Browsers, however you would probably anyway used CSS to style your <a>-button.

But what about IE8? – for that we set the type=’button’, and IE8 is almost of no concern anymore (even for big corporate customers)

<button> vs <input type="button" />

Both types of buttons work in practice the same way.
However the Button element allows you to add content elements, like an image.
And usually you would use the <input> tag in a <form>.

As we are not working with a form and just want to have a button to execute some JS-Code I would use the Button tag.


  • <a> is used for page navigation
  • <button type='button'> is used for actions on the page, can contain other html elements
  • <input type="button" /> is used in a form

Some More Reading

Posted by happyneal in Web Technologies, 0 comments
JSP: Passing Variable Data to JavaScript

JSP: Passing Variable Data to JavaScript

When you try to marry old JSP Technology with the modern wonders of Typescript/ES6.
You will want to expose some data provided by the backend into the Javascript.

If you have the possibility you would use a fetch() call to receive JSON.
Sometimes, it is not possible to do a big rewrite of the jsp to fix a minor bug.
Here is a very dirty way to pass data from the JSP into the JS code. This code will utilise the HTML5 data-attribute.
Learn more about it here

JSP File:



This is a very elegant solution, now you can run ESLint on your Javascript Code and will not have any unresolved variables.

Posted by happyneal in Java, Programming, Web Technologies, 0 comments

Udacity – Web Tooling and Automatisation

I recently took a look at the course materials for Web Tooling and Automatisation.

Overall the course is very well structured and introduces Gulp and a couple of common packages used in webdevelopment. Besides their main topic, they cover topics on good engineering practices, like linting and testing to ensure code quality.

While working on the project I ran into several little smaller things that were quite annoying. Thankfully the gulp community is quite big, so somebody already solved some of the issues I was facing.

Passing an “--production” flag

When developing, you will probably create a version of your software that is suited for easily finding bugs and errors and an optimized version that is minified and optimized for optimal performance for the end user.

You would define two different tasks in gulp, one “default” and one “production” task. This however would in turn cause you to have to duplicate your code – with optimization and without.

I found the package “gulp-if” that allows you to control if a function like compression is active during the task.
The remaining issue was to actually set the parameter before the tasks run. (All tasks in gulp run in parallel).

To get a flag from the command line, you can use the process.argv Array. However you must add “–” before your flag name. If not gulp will assume it is another task name that should run.

In the end you would use something like this:

Note: In Gulp 4, you can use a sequencer and would not need to pass in the flag by command-line, but you would define a task that will run before all the other tasks.

Dealing with Asset sources and destinations

When using gulp.src() and gulp.dest(), typically people use strings to define the locations. However this is quite annoying if you want to get a quick overview which locations are used. For a better maintainability you should create a small variable block that defines these strings. In the long run it lets you be more flexible where your files are etc.

End Result

At the end of the course I ended up with this gulpfile.js. It adds support for Typescript, Pug(Jade), google-closure-compiler.

The common gulp tasks to run are:
* gulp serve: Uses browser-sync with css injection for live-editing
* gulp --production: Creates an optimized build

Next steps:
Depending on your webserver, you would want to add a gulp deploy task.



Posted by happyneal in Web Technologies, 0 comments

Continuous Integration (CI) for Gitbook using Gitlab and Gulp

Gitbook is a static site generator, that converts a collection of Markdown files into a HTML Site. Alternatively it can also convert the markdown files into a PDF or ebook.
If you are not writing a book, it is also a great tool to create a quick documentation for a project you are working on.

Initial set up

We will need gitbook. Gitbook does not automatically generate a SUMMARY.md file, however there is an existing gitbook-summary tool to take care of that.

Gulp will be our taskrunner.

I will deploy to my server via FTP. Since you are only serving HTML Files, there is usually no need for server restart etc.
To integreate it into Gulp I will be using vinyl-ftp.


Create a file called gulpfile.js and define your gulp tasks.

You should test especially the “deploy” task locally if everything is working correctly.

Gitlab CI Integration

You need to create a YAML File called .gitlab-ci.yml. Gitlab will recognise the file and run the commands in it.

That’s it. If you push something into the master branch, it will automatically run the commands in the yaml file and deploy your static website to your server.
When the build completes, you will recieve an email, telling you if everything went as planned.

Posted by happyneal in Blog, Web Technologies, 0 comments