having fun with code

Command line scripting with Node.js

I often write custom deploy scripts for the projects I work on. In most cases, I choose python. You can do pretty much everything with shell and system commands, but python dictionaries and lists make it very easy to manipulate data, lists of files, text, etc. Networking is also very easy in python (http, ftp, etc).

To run a Javascript file from the console you can type “node myscript.js”. Or you can set the application that will run the file like you do with bash or python:

  1. #! /usr/bin/env node
  2.  
  3. console.log("Hello");
  4. setTimeout(function() {
  5.   console.log("world!");
  6. }, 2000);

Now you can change the permissions of the script to make it executable:

  1. $ chmod 755 myscript.js

And run the script by typing its name, like you do with shell or python scripts:

  1. $ ./myscript.js param1 param2 "param 3"
  2. Hello
  3. world!

Nice, uh? Now go and write some cool command line scripts in Javascript :)

Uninstalling brew (so I can reinstall)

Uninstall

  1. cd `brew —-prefix`
  2. rm -rf Cellar
  3. brew prune
  4. rm -rf Library .git .gitignore bin/brew README.md share/man/man1/brew
  5. rm -rf ~/Library/Caches/Homebrew

http://superuser.com/questions/203707/how-to-uninstall-homebrew-osx-packet-manager

Reinstall

  1. /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

https://github.com/mxcl/homebrew/wiki/Installation

Python script: remove empty folders

One of the scripts I often use is this one to remove empty folders under a specific location on your hard drive. The usage is simple:

  1. $ [path to the script]/remove_empty_folders.py [path_to_clean]

Here is the code:

  1. #! /usr/bin/env python
  2. import os, sys
  3.  
  4. def removeEmptyFolders(path):
  5.   if not os.path.isdir(path):
  6.     return
  7.  
  8.   # remove empty subfolders
  9.   files = os.listdir(path)
  10.   if len(files):
  11.     for f in files:
  12.       fullpath = os.path.join(path, f)
  13.       if os.path.isdir(fullpath):
  14.         removeEmptyFolders(fullpath)
  15.  
  16.   # if folder empty, delete it
  17.   files = os.listdir(path)
  18.   if len(files) == 0:
  19.     print "Removing empty folder:", path
  20.     os.rmdir(path)
  21.  
  22. removeEmptyFolders(sys.argv[1])

Here is a sample output:

  1. Removing empty folder: ./assorted/2010/12/13/20101213-202802
  2. Removing empty folder: ./assorted/2010/12/13
  3. Removing empty folder: ./assorted/2010/12/14/20101214-120800
  4. Removing empty folder: ./assorted/2010/12/14
  5. Removing empty folder: ./assorted/2010/12/01/20101201-070118
  6. Removing empty folder: ./assorted/2010/12/01/20101201-070111
  7. Removing empty folder: ./assorted/2010/12/01/20101201-070123
  8. Removing empty folder: ./assorted/2010/12/01
  9. Removing empty folder: ./assorted/2010/12

Please, use at your own risk. It should work on Windows also, but I have not tested it.

Enjoy!

My Sublime Text 2 settings

My Sublime Text 2 settings:

  1. // Settings in here override those in "Default/Base File.sublime-settings", and
  2. // are overridden in turn by file type specific settings. Place your settings
  3. // here, to ensure they're preserved when upgrading.
  4. {
  5.  // Set to false for horizontal scrolling
  6.  "word_wrap": false,
  7.  // Set to false to disable detection of tabs vs. spaces on load
  8.  "detect_indentation": false,
  9.  // Set to "none" to turn off drawing white space, "selection" to draw only the
  10.  // white space within the selection, and "all" to draw all white space
  11.  "draw_white_space": "all",
  12.  // Set to true to removing trailing white space on save
  13.  "trim_trailing_white_space_on_save": true,
  14.  // Set to true to ensure the last line of the file ends in a newline
  15.  // character when saving
  16.  "ensure_newline_at_eof_on_save": true
  17. }

Clone your OSX terminal window

Sometimes, when working on a terminal window I would like to open a new window on the exact same location or path. This little AppleScript will do the trick:

  1. tell app "Terminal" to do script "cd \"'`pwd`'\""

From the command line:

  1. osascript -e 'tell app "Terminal" to do script "cd \”'`pwd`'\”"'

You can also create an alias in ~/.profile for easy access:

  1. alias nt="osascript -e 'tell app \"Terminal\" to do script \"cd \\\"'`pwd`'\\\"\"'"

Enjoy!

Titanium Studio and Titanium Mobile 1.7

Titanium Studio: will this change anything? It is a big step, and offers something I really missed while working with Titanium in the past: integrated debugging. I think I will rather stay with XCode and code native Objective-C.

Link: Introducing Titanium Studio and Titanium Mobile 1.7.

Step back and watch what the “normals” do

“We as developer must remember that we are not the target for 99% of the software that is written. We are not the final judge on what will or will not work. If anything, we are the last people that should have an opinion on what is good or bad. We should be the ones that step back and watch what the “normals” do with it. They determine the success or failure; not us.”
http://www.cimgf.com/2011/06/03/why-so-serious

location.hash is dead (on html5 browsers)

Pretty cool technique to actually change the url of the page (something until now impossible to do in Javascript for security reasons):
http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate

Also good to read, the browser history MDC documentation:
https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history

And finally, pjax, a new script that combines the use oj Ajax calls and this technique to manipulate the url to achieve a beautiful ajax navigation experience, with permalinks and without the need of using location.hash.
http://pjax.heroku.com/

Angry Birds as a Web App

Really, really cool, Rovio has released Angry Birds (and all the game assets) as a web app, Javascript and Canvas based. I can’t wait to see clones of Angry Birds with custom assets, maps and sounds.

via Angry Birds as a Web App.

Finding duplicate ID’s on an HTML page

Looks like sometimes we forgot element ID’s are meant to be unique on a HTML page. Here is a little bit of code I just wrote to find duplicate ID’s on a page (run the code on your browser’s javascript console):

  1. var idList = {};
  2. var nodes = document.getElementsByClassName('');
  3. for (var i in nodes) {
  4.   if (!isNaN(i) && nodes[i].id) {
  5.     idList[nodes[i].id] = idList[nodes[i].id]? idList[nodes[i].id]+1:1;
  6.   }
  7. }
  8. for (var id in idList) {
  9.   if (idList[id] > 1) console.log("Duplicate id: #" + id);
  10. }

Have fun!

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info

PromoteJS

JavaScript JS Documentation