macOS tips


Recently I put together a list or rather useful tricks on Macs that explain a lot of basic concepts (like keyboard navigation in Finder) as well as more advanced tweaks (such as custom directory for screenshots or muting the Apple chime on boot).

Here's the link to the document on GitHub:

Licensed as CC-BY-SA-4.0

Tags: macosapplemacfindertrickstipspdf

Batch conversion of file formats with ffmpeg


$ for i in *.mp3; do ffmpeg -i "$i" "${i%.*}.wav"; done

Tags: shell, bash

Blocking trackers via /etc/hosts


Some of the entries I keep in my /etc/hosts file. Feel free to borrow, a good starting point in blocking trackers. more

Tags: trackers, hosts, facebook, google, mac, linux

Importing env vars from .env file


There's a very quick way of importing all env vars defined in your .env file to the shell session you're currently in.

Adding this to your bash profile will allow you to run dotenv:

alias dotenv='export $(cat .env | grep -v ^# | xargs)'

To remove these variables you can also define unset-dotenv:

alias unset-dotenv='unset $(cat .env | grep -v ^# | sed -E "s/(.*)=.*/\1/" | xargs)'

Tags: env, dotenv, file, shell, bash, import, export, alias

Disabling mongo.log


In many installations of MongoDB verbose logging to the mongo.log file is switched on by default. This file can grow to quite substantial size (hundreds of megs are added regularly) and cleaning the file manually every time is rather tedious.

MongoDB recommends rotating logs but on local dev machines you aren't likely to actually need these logs in the first place.

To disable them edit /usr/local/etc/mongod.conf file (or whenever your mongo config is located - check the arguments provided to mongod with ps aux | grep mongod).

In mongod.conf comment out the logpath line. Restart the service and the logs won't be collected anymore. You may want to still remove that file (look in /usr/local/var/log/mongodb/mongo.log)

Tags: mongo, log, disabling

Switching node versions with Homebrew


Just a quick trick to switch between node versions when you install more than one version through Homebrew (brew install node)

You check the currently selected version with node --version. Working out version from paths in brew list node or looking for an asterisk next to a version listed in brew info node can also be used if you're after a different package.

1) List the versions present on your system: brew list --versions node

2) Switch to one of the versions, in my case I'm switching to 6.2.2: brew switch node 6.2.2

That's it, you can verify the operation was successful with node --version.

Tags: brew, homebrew, node, versions



It seems like nobody has created npm module to get prebuilt binaries for ffmpeg, ffprobe, ffplay and ffserver.

I went ahead and put something together quickly.

API and Node module


Node module:

Git repos


Node module:

At the moment it simply links to externally hosted archives containing binaries. In the future they will be repackaged into individual components and I will add automated unzipping in the npm module.

Issues and pull requests welcome.

Tags: ffbinaries, api, npm

Generating self-signed certificates for debugging


This post's purpose is to store the quick shell script that I created for myself the other day after I forgot the password to the previous certificate. Since these are really throwaway I figured I might as well have a single point of entry to regenerate the whole lot:

openssl genrsa -des3 -out myssl.key 1024
openssl req -new -key myssl.key -out myssl.csr
openssl x509 -req -days 365 -in myssl.csr -signkey myssl.key -out myssl.crt

Put that into "" file, run "chmod +x" on it and simply execute it for quick generation of self-signed certificates.

Tags: https, ssl, selfsigned, cert, certificate

Builds failing because of node-gyp missing common.gypi


This issue became apparent on one of CI build boxes. For whatever reason the common.gypi file has gone missing. Stack trace: gyp: /home/builder/.node-gyp/5.9.1/common.gypi not found (cwd: /home/builder/buildtmp/X-JOB1/app/node_modules/fsevents) while reading includes of binding.gyp while trying to load binding.gyp gyp ERR! configure error gyp ERR! stack Error: `gyp` failed with exit code: 1 gyp ERR! stack at ChildProcess.onCpExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:355:16) gyp ERR! stack at emitTwo (events.js:100:13) gyp ERR! stack at ChildProcess.emit (events.js:185:7) gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:204:12) gyp ERR! System Linux 3.10.0-229.7.2.el7.x86_64 gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/home/builder/buildtmp/X-JOB1/app/node_modules/fsevents/lib/binding/Release/node-v47-linux-x64/fse.node"... more

Tags: node, node-gyp, common.gypi, npm, js

Installing databases as services with Homebrew on a Mac


First steps

Update your homebrew references and install services package to run the servers in the background.

brew update
brew tap homebrew/services

Installing services

Now you can install MongoDB, Redis or MySQL. Install whatever you need depending on your setup.

brew install mongodb
brew install redis
brew install mysql

Starting services

You can then control services with syntax typically seen in service launchers.

brew services start [service]
brew services stop [service]
brew services restart [service]

Replace [service] bit with mongodb, redis, mysql or whatever service you need to run and you're done.

Some debug options

brew services list
brew services cleanup

Tags: mac, homebrew, mongo, redis, mysql, services

HTTP Basic Auth with nginx on CentOS 7


The other day I decided to set up a fresh VPS for few of my small apps. Essentially it's a CentOs 7 box with nginx which serves one static site when requests come on one domain (in few different contexts/locations with different settings for each of them) and proxies traffic to a Node app on a different domain. Since CentOS7 doesn't have httpd-tools package installed by default we'll need to install that first: yum -y install httpd-tools Next - use it to generate password. We will want to output this to a .htpasswd file. Assuming username "exampleuser": htpasswd -c /etc/nginx/.htpasswd exampleuser This will prompt you for password which will be later encrypted. Then add these two lines to your nginx config (location or server block): auth_basic "Restricted"; auth_basic_user_file /your-app-path/.htpasswd; Restart nginx to pick up the changes and you should be asked for password when you next try to reach that URL. sudo systemctl restart nginx Extra... more

Tags: http, basic, auth, htpasswd, centos, nginx

Checking exit code of the last process in Terminal


When setting up your tools for Continuous Integration one of the things that you will have to do is making sure you're exiting with the correct code.

An easy way to check the exit code of last process run in Terminal is simply to execute this command:

echo $?

While in most cases the exit code will be 0 (indicating successful execution of the process) this can be extremely useful for troubleshooting and ensuring your CI tool catches the errors correctly.

Tags: mac, terminal

Checking DNS zone file


Since I keep forgetting this command, I thought it would be rather useful for the moments when a custom DNS records are needed (i.e. setting up an external email provider).

dig -t ANY

Tags: dns, zone, terminal

Fixing EMFILE error


If you're on a Mac like me and you develop sizeable Node.js applications you will at some point suffer from the issue which manifests itself in something like this:

EMFILE, too many open files /app/node_modules/random-module/index.js

The problem behind this is the Mac's unusually low open file descriptors limit (256).

To change this simply enter this command in terminal and your issue will be gone:

ulimit -n 1024

Then simply run your app in the same terminal session. You may need to increase the limit even further depending on the size and complexity of your application.

It's also worth checking out this thread on Stack Exchange about persisting ulimit settings.

Tags: node, error, troubleshooting

Simulating Retina resolutions on a non-Retina Mac


If like me you have a non-Retina Mac you may want to fake a Retina resolution in order to get a larger working area.

I was searching for a simple way to achieve this for a while. Finally I found an app called RDM (Retina DisplayMenu). It simply adds an icon to the menu bar which allows you to go beyond the default resolutions. You can download RDM here (direct link).

Since this is faking it and you're still limited by the physical number of pixels you should not expect absolute clarity so it clearly isn't a good solution for playing movies or games. For development and general browsing however it is quite a handy technique. I found that on my 13" MacBook Air 1920x1200 works quite well which is a pretty nice improvement over the native 1440x900.

Tags: mac

Unit testing of Node.js with Mocha and Istanbul


Useful tips

Basic usage

./node_modules/.bin/_mocha --reporter spec

This command will help you run just a subset of tests in a given directory.

./node_modules/.bin/_mocha recursive test/my-subdir

Quick coverage check

./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha

Tags: node

Publishing node modules to a private repository


This is a tutorial on how to use your own private repository to publish node modules to. In most cases this will be a CouchDB based proxy of the public npm registry like Sinopia. I assume that is already set up and all you need is to configure your local development machine to use that registry. Publishing your first package 1) Set your npm config to point to your private repo. npm set registry http://your-private-repo:4873/ npm set always-auth true npm adduser The last command (adduser) will prompt you for the username, password and email. 2) Go to the location of files you want to be included in your package and make sure you have a valid package.json there. You may also add .npmignore file to exclude some files from being part of your package (i.e. unit tests). node_modules folder is automatically ignored but it won’t hurt to add it just in case. Remember to add the publishConfig property. 3) (optional) If you’re publishing the first version of your package, use npm pack to... more

Tags: node, publish, private