Skip to content

Instantly share code, notes, and snippets.

@armornick
Created March 19, 2015 09:19
Show Gist options
  • Save armornick/27c8a4df3c9a5b920ac8 to your computer and use it in GitHub Desktop.
Save armornick/27c8a4df3c9a5b920ac8 to your computer and use it in GitHub Desktop.
A featureless implementation of the UNIX ls utility using Node and Shelljs
// module imports
var ln = require('shelljs').ln,
test = require('shelljs').test,
exit = require('shelljs').exit,
path = require('path');
// command line argument parsing
var opts = require("nomnom")
.option('symbolic', {
abbr: 's',
flag: true,
help: 'create a symbolic link'
})
.option('force', {
abbr: 'f',
flag: true,
help: 'force creation of the link'
})
.parse();
// sanity check: first argument should be an existing file
if (opts[0] === undefined || !test('-e', opts[0])) {
console.error('source does not exist or was not given.');
exit(1);
};
// variable declations
var args = "-",
source = opts[0], // first argument is always the source
destination = opts[1] || "./" + path.basename(opts[0]); // take second argument OR
// do we need to create a symbolic link?
if (opts.symbolic) {
args += "s";
};
// do we need to create a link even if it already exists?
if (opts.force) {
args += "f";
};
// do the link creation
if (args !== "-") {
ln(args, source, destination);
} else {
ln(source, destination);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment