Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding command line helper (cli.js) and path helper (pathExt.js)
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var nopt = require('nopt'); | ||
|
||
/** | ||
* Parse command line arguments | ||
* @param {Array} args the command line arguments array | ||
*/ | ||
exports.parseCommandLineArgs = function( args ) { | ||
var options = { | ||
'port': Number | ||
}, | ||
shorthand = { | ||
'p': '--port' | ||
}; | ||
|
||
return nopt(options, shorthand, args); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var path = require('path'); | ||
|
||
/** | ||
* Resolves a pathname to an absolute pathname. | ||
* Handles ~/ references and environmental variables. | ||
* | ||
* Example: | ||
* Say you have the $DATA environmental variable set to 'app/config' | ||
* | ||
* You call: resolve('~/$DATA/file.log') | ||
* Result would be something like: '/home/username/app/config/file.log' | ||
*/ | ||
exports.resolve = function(pathname) { | ||
if(typeof pathname !== 'string') { | ||
pathname = ''; | ||
} | ||
|
||
// resolve ~/ | ||
pathname = pathname.replace('~/', process.env.HOME + '/'); | ||
|
||
// resolve env variables | ||
pathname = pathname.replace(/\$(\w*)/g, function() { | ||
var env = process.env[arguments[1]]; | ||
if(env) { | ||
return env; | ||
} else { | ||
return ''; | ||
} | ||
}); | ||
|
||
return path.resolve(pathname); | ||
}; |