Use 3rd party library from CDN or embed and host it on your server?

3 minute read

With any sizable website/app, you should already be using a handful of 3rd party libraries or frameworks in your code. When the time comes to host a Production site, you need to make a decision to:

  1. Embed 3rd party libraries and host them together with the site, or
  2. Reference them using external CDN

Most of the time your should reference them using external CDN. However there are cases where it’s better to include/embed them in your own code and host them together with the site.

Let’s take a closer look at the pros and cons of both options and when it’s better to use one vs the other.

read more

Count the number of files and folder’s depth with ‘find’

3 minute read

Sometimes you want to find out the statistic of your files like I needed to figure out why some File Synchronization Services didn’t work well. Typical questions are:

  1. How many files are there in a folder?
  2. How many of those are regular files?
  3. How many of those are sub-folders?
  4. What’s the maximum number of nested folders?

By default, Windows and Mac have their built-in Files Manager that give answers to some of these questions. File Explorer on Windows can answer #1, #2, #3 by going to Properties. Finder on Mac can answer #1 by viewing Get Info. Mac doesn’t have a way to find out #2 and #3 with the built-in Finder. And none of the default Files Manager on Windows nor Mac can answer #4.

find can. It is a UNIX command line tool that searches for files in a directory hierarchy. find is native in Linux, and Mac and Windows have their own version of it. However there are discrepancies among the versions that will trip up users.

read more

Copy a file and auto create folders structure

2 minute read

How to copy a file and auto create all the intermediate folders leading to it?. We can use the command cp --parents.

Assuming we have this:

├── d11
│   └── d111
│       └── f1.txt
└── d12

We can copy f1.txt and create all the folders leading to f1 which are d1/d11/d111 with:

$ cp --parents d1/d11/d111/f1.txt destinationFolder

# We'll then have
└── d1
    └── d11
        └── d111
            └── f1.txt

MacOS comes with a BSD version of cp which doesn’t have the --parents flag. We need to get GNU cp which is bundled in the coreutils package.

read more

Test development site over LAN

7 minute read

You’re developing a website/webapp and everything is working perfectly from your machine locally on the browser. You want to now test it from another machine or mobile device over LAN (Local Area Network). What are your options and when would you want to choose one over another?

It depends on the complexity of your website/webapp:

  • The site/app is a pure frontend (HTML/CSS/JS)
  • The site/app has a backend component (PHP, Ruby, Python, Node, etc.) and a database
  • The site/app has multiple components and services

In all cases, you first need to make sure your machine Firewall setting allow incoming requests from other devices. And then choose 1 of the 3 options for hosting your development site.

read more

Recursively walk a directory hierarchy with ‘bash’ globstar **

2 minute read

How to list all .js files in nested folders? We can use the ** glob pattern.

Assuming we have this:

├── d1
│   ├── d11
│   │   ├── d111
│   │   │   └── f5.js
│   │   └── f4.js
│   ├── d12
│   ├── f2.js
│   └── f3.js
├── d2
│   └── f6.js
└── f1.js
5 directories, 6 files

By default ** behaves exactly like * for file names and directory names:

$ ls *.js

$ ls **.js
# */ only matches first-level directories
$ ls */*.js
d1/f2.js  d1/f3.js  d2/f6.js

# Similarly, **/ only matches first-level directories
$ ls **/*.js
d1/f2.js  d1/f3.js  d2/f6.js

# Same behavior with **/
$ ls **/**.js
d1/f2.js  d1/f3.js  d2/f6.js

This isn’t quite what we want. Instead of matching only the first-level, we want ** to match nested directories recursively so that we can list all 6 files in the above example.

read more