File I/O Generic Helpers
Generic helper functions used for development.
Install:
npm install cli-helper
Include:
Include as a constructor class:
// include as a constructor classvar Helper = constructor;
Include as an instance:
var Helper = instance; Helper;
Functions
Helper.getOpt([String:id])
Get arguments passed using the --
convention.
// executionnode /foojs --foo=bar --bar=baz // getOpt()Helper;// -> 'bar'
Helper.getOptArg([String:id])
Get non key-value pair arguments
// executionnode /foojs foo // getOptArg()Helper;// -> true Helper;// -> false
Helper.getRawArgs()
Returns all the raw arguments.
// executionnode /foojs foo --bar=lol // getRawArgs()Helper;// -> ['node', './foo.js', 'foo', '--bar=lol']
Helper.promiseIterator([Object:target], [Function:next], [Function:done])
Iterate over array or object with a next() step, promise-like execution.
// promiseIterator() var list = 'a' 'b' 'c'; Helper; { console; console; // you can do some async call here $; // or you can break the iteration // stop();} { console;}
Helper.shellCmd([String:cmd], [String:basePath], [Boolean:verbose])
Run a unix shell command using package: shelljs
Arguments | Description |
---|---|
cmd | command to run |
basePath | (optional) path where to execute. if not defined, command executes on current dir |
verbose | (optional) non-silent mode |
// shellCmd() - command ls -al output in /usr/local directoryHelper;
Helper.isPathExists([String:path])
Check if path exists, either if its a file or directory.
// isPathExists()Helper;// -> true
Helper.isFileExists([String:path])
Check if the path is a FILE.
// isPathExists()Helper;// -> false Helper;// -> true
Helper.createDir([String:path])
Creates a directory.
// createDir()Helper;
Helper.readFile([String:path])
Read file (utf-8).
// readFile()Helper;// -> {String} contents
Helper.writeFile([String:path], [String:content])
Write file (utf-8).
// writeFile()Helper;// -> {String} contents
Helper.base64([String:str], [Boolean:dec = false])
Base64 encode/decode.
// base64() // encodeHelper;// -> {String} encoded string // decodeHelper;// -> {String} decoded string