1.0.0 • Published 4 years ago

cli-helper v1.0.0

Weekly downloads
116
License
MIT
Repository
github
Last release
4 years ago

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 class
var Helper = require('cli-helper').constructor;

class MyHelper extends Helper {
	. . .
}

Include as an instance:

var Helper = require('cli-helper').instance;

Helper.getOpt('foo');

Functions

Helper.getOpt([String:id])

Get arguments passed using the -- convention.

// execution
node ./foo.js --foo=bar --bar=baz

// getOpt()
Helper.getOpt('foo');
// -> 'bar'

Helper.getOptArg([String:id])

Get non key-value pair arguments

// execution
node ./foo.js foo

// getOptArg()
Helper.getOptArg('foo');
// -> true

Helper.getOptArg('bar');
// -> false

Helper.getRawArgs()

Returns all the raw arguments.

// execution
node ./foo.js foo --bar=lol

// getRawArgs()
Helper.getOptArg('foo');
// -> ['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.promiseIterator(list, next, done);

function next(item, i, step, stop) {
	console.log('list item:', item);
    console.log('list index:', i);

    // you can do some async call here
    $.ajax('//feed.com/' + item, function(data) {
    	console.log('got the data!', data);
		// continue to the next iteration
	    step();    
    });

    // or you can break the iteration
    // stop();
}

function done() {
	console.log('iteration done!');
}

Helper.shellCmd([String:cmd], [String:basePath], [Boolean:verbose])

Run a unix shell command using package: shelljs

ArgumentsDescription
cmdcommand 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 directory
Helper.shellCmd('ls -al', '/usr/local');

Helper.isPathExists([String:path])

Check if path exists, either if its a file or directory.

// isPathExists()
Helper.isPathExists('/urs/local');
// -> true

Helper.isFileExists([String:path])

Check if the path is a FILE.

// isPathExists()
Helper.isFileExists('/urs/local');
// -> false

Helper.isFileExists('/urs/local/foo.txt');
// -> true

Helper.createDir([String:path])

Creates a directory.

// createDir()
Helper.createDir('/usr/local/new-dir');

Helper.readFile([String:path])

Read file (utf-8).

// readFile()
Helper.readFile('/usr/local/foo.txt');
// -> {String} contents

Helper.writeFile([String:path], [String:content])

Write file (utf-8).

// writeFile()
Helper.writeFile('/usr/local/foo.txt', 'hello world!');
// -> {String} contents

Helper.base64([String:str], [Boolean:dec = false])

Base64 encode/decode.

// base64()

// encode
Helper.base64('encode me');
// -> {String} encoded string

// decode
Helper.base64('ZW5jb2RlIG1l', true);
// -> {String} decoded string