mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
feature(cloudcmd) add execon from bower
This commit is contained in:
parent
5fc8127230
commit
1c3a117d02
10 changed files with 83 additions and 314 deletions
|
|
@ -29,7 +29,8 @@
|
|||
"promise-polyfill": "~2.1.0",
|
||||
"rendy": "~1.1.0",
|
||||
"emitify": "~1.3.0",
|
||||
"philip": "~1.3.1",
|
||||
"smalltalk": "~1.5.3"
|
||||
"smalltalk": "~1.5.3",
|
||||
"execon": "~1.2.8",
|
||||
"philip": "~1.3.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,9 +32,10 @@ var CloudCmd;
|
|||
|
||||
moduleFiles = [
|
||||
window.Promise ? '' : 'promise-polyfill/Promise.min',
|
||||
'format-io/lib/format',
|
||||
'rendy/lib/rendy',
|
||||
'emitify/lib/emitify'
|
||||
libDir('format', 'format-io'),
|
||||
libDir('rendy'),
|
||||
libDir('emitify'),
|
||||
libDir('exec', 'execon')
|
||||
].filter(function(name) {
|
||||
return name;
|
||||
}).map(function(name) {
|
||||
|
|
@ -57,7 +58,14 @@ var CloudCmd;
|
|||
CloudCmd.init(prefix);
|
||||
});
|
||||
}
|
||||
|
||||
function libDir(name, dir) {
|
||||
if (!dir)
|
||||
dir = name;
|
||||
|
||||
return dir + '/lib/' + name;
|
||||
}
|
||||
|
||||
function createScript(url, callback) {
|
||||
var script = document.createElement('script');
|
||||
|
||||
|
|
|
|||
238
lib/util.js
238
lib/util.js
|
|
@ -5,12 +5,17 @@
|
|||
|
||||
/* global rendy */
|
||||
|
||||
if (typeof module === 'object' && module.exports)
|
||||
module.exports = new UtilProto();
|
||||
else if (!Scope.Util)
|
||||
Scope.Util = new UtilProto();
|
||||
var exec;
|
||||
|
||||
function UtilProto() {
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
exec = require('execon');
|
||||
module.exports = new UtilProto(exec);
|
||||
} else if (!Scope.Util) {
|
||||
exec = window.exec;
|
||||
Scope.Util = new UtilProto(exec);
|
||||
}
|
||||
|
||||
function UtilProto(exec) {
|
||||
var Util = this;
|
||||
|
||||
this.check = new checkProto();
|
||||
|
|
@ -247,228 +252,7 @@
|
|||
return type;
|
||||
}
|
||||
|
||||
this.exec = new ExecProto();
|
||||
|
||||
function ExecProto() {
|
||||
/**
|
||||
* function do save exec of function
|
||||
* @param callback
|
||||
* @param arg1
|
||||
* ...
|
||||
* @param argN
|
||||
*/
|
||||
var exec = function(callback) {
|
||||
var ret,
|
||||
isFunc = Util.type.function(callback),
|
||||
args = [].slice.call(arguments, 1);
|
||||
|
||||
if (isFunc)
|
||||
ret = callback.apply(null, args);
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
/*
|
||||
* return function that calls callback with arguments
|
||||
*/
|
||||
exec.with = function(callback) {
|
||||
var slice = Array.prototype.slice,
|
||||
args = slice.call(arguments, 1);
|
||||
|
||||
return function() {
|
||||
var array = slice.call(arguments),
|
||||
all = args.concat(array);
|
||||
|
||||
callback.apply(null, all);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* return save exec function
|
||||
* @param callback
|
||||
*/
|
||||
exec.ret = function() {
|
||||
var result,
|
||||
args = [].slice.call(arguments);
|
||||
|
||||
args.unshift(exec);
|
||||
result = exec.with.apply(null, args);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* function do conditional save exec of function
|
||||
* @param condition
|
||||
* @param callback
|
||||
* @param func
|
||||
*/
|
||||
exec.if = function(condition, callback, func) {
|
||||
var ret;
|
||||
|
||||
if (condition)
|
||||
exec(callback);
|
||||
else
|
||||
exec(func, callback);
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
/**
|
||||
* exec function if it exist in object
|
||||
*
|
||||
* @param obj
|
||||
* @param name
|
||||
* @param arg
|
||||
*/
|
||||
exec.ifExist = function(obj, name, arg) {
|
||||
var ret,
|
||||
func = obj && obj[name];
|
||||
|
||||
if (func)
|
||||
func = func.apply(obj, arg);
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
exec.parallel = function(funcs, callback) {
|
||||
var keys = [],
|
||||
callbackWas = false,
|
||||
arr = [],
|
||||
obj = {},
|
||||
count = 0,
|
||||
countFuncs = 0,
|
||||
type = Util.type(funcs);
|
||||
|
||||
Util.check(arguments, ['funcs', 'callback']);
|
||||
|
||||
switch(type) {
|
||||
case 'array':
|
||||
countFuncs = funcs.length;
|
||||
|
||||
funcs.forEach(function(func, num) {
|
||||
exec(func, function() {
|
||||
checkFunc(num, arguments, arr);
|
||||
});
|
||||
});
|
||||
break;
|
||||
|
||||
case 'object':
|
||||
keys = Object.keys(funcs);
|
||||
countFuncs = keys.length;
|
||||
|
||||
keys.forEach(function(name) {
|
||||
var func = funcs[name];
|
||||
|
||||
exec(func, function() {
|
||||
checkFunc(name, arguments, obj);
|
||||
});
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
function checkFunc(num, data, all) {
|
||||
var args = [].slice.call(data, 1),
|
||||
isLast = false,
|
||||
error = data[0],
|
||||
length = args.length;
|
||||
|
||||
++count;
|
||||
|
||||
isLast = count === countFuncs;
|
||||
|
||||
if (!error)
|
||||
if (length >= 2)
|
||||
all[num] = args;
|
||||
else
|
||||
all[num] = args[0];
|
||||
|
||||
if (!callbackWas && (error || isLast)) {
|
||||
callbackWas = true;
|
||||
|
||||
if (type === 'array')
|
||||
callback.apply(null, [error].concat(all));
|
||||
else
|
||||
callback(error, all);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* load functions thrue callbacks one-by-one
|
||||
* @param funcs {Array} - array of functions
|
||||
*/
|
||||
exec.series = function(funcs, callback) {
|
||||
var fn,
|
||||
i = funcs.length,
|
||||
check = function(error) {
|
||||
var done;
|
||||
|
||||
--i;
|
||||
|
||||
if (!i || error) {
|
||||
done = true;
|
||||
exec(callback, error);
|
||||
}
|
||||
|
||||
return done;
|
||||
};
|
||||
|
||||
if (!Array.isArray(funcs))
|
||||
throw(Error('funcs should be array!'));
|
||||
|
||||
fn = funcs.shift();
|
||||
|
||||
exec(fn, function(error) {
|
||||
if (!check(error))
|
||||
exec.series(funcs, callback);
|
||||
});
|
||||
};
|
||||
|
||||
exec.each = function(array, iterator, callback) {
|
||||
var listeners = array.map(function(item) {
|
||||
return iterator.bind(null, item);
|
||||
});
|
||||
|
||||
if (!listeners.length)
|
||||
callback();
|
||||
else
|
||||
exec.parallel(listeners, callback);
|
||||
};
|
||||
|
||||
exec.eachSeries = function(array, iterator, callback) {
|
||||
var listeners = array.map(function(item) {
|
||||
return iterator.bind(null, item);
|
||||
});
|
||||
|
||||
if (typeof callback !== 'function')
|
||||
throw Error('callback should be function');
|
||||
|
||||
if (!listeners.length)
|
||||
callback();
|
||||
else
|
||||
exec.series(listeners, callback);
|
||||
};
|
||||
|
||||
/**
|
||||
* function execute param function in
|
||||
* try...catch block
|
||||
*
|
||||
* @param callback
|
||||
*/
|
||||
exec.try = function(callback) {
|
||||
var ret;
|
||||
try {
|
||||
ret = callback();
|
||||
} catch(error) {
|
||||
ret = error;
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
return exec;
|
||||
}
|
||||
this.exec = exec;
|
||||
|
||||
/**
|
||||
* function gets file extension
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"name": "execon",
|
||||
"version": "1.2.8",
|
||||
"homepage": "https://github.com/coderaiser/execon",
|
||||
"authors": [
|
||||
"coderaiser <mnemonic.enemy@gmail.com>"
|
||||
|
|
@ -21,14 +22,13 @@
|
|||
"test",
|
||||
"tests"
|
||||
],
|
||||
"version": "1.2.5",
|
||||
"_release": "1.2.5",
|
||||
"_release": "1.2.8",
|
||||
"_resolution": {
|
||||
"type": "version",
|
||||
"tag": "v1.2.5",
|
||||
"commit": "ea4e9ed95a445e84298ed670be59e42ce6fbe678"
|
||||
"tag": "v1.2.8",
|
||||
"commit": "7e8900c48fd9262a3a3acaa1c8b6c34ff17aa745"
|
||||
},
|
||||
"_source": "git://github.com/coderaiser/execon.git",
|
||||
"_target": "~1.2.2",
|
||||
"_target": "~1.2.7",
|
||||
"_originalSource": "execon"
|
||||
}
|
||||
|
|
@ -1,3 +1,25 @@
|
|||
2015.10.21, v1.2.8
|
||||
|
||||
fix:
|
||||
- (exec) with do not return value
|
||||
|
||||
feature:
|
||||
- (travis) add
|
||||
- (gitignore) add
|
||||
|
||||
|
||||
2015.10.01, v1.2.7
|
||||
|
||||
fix:
|
||||
- (exec) with: concat arguments
|
||||
|
||||
|
||||
2015.10.01, v1.2.6
|
||||
|
||||
feature:
|
||||
- (exec) with: bind.apply -> function apply
|
||||
|
||||
|
||||
2015.07.21, v1.2.5
|
||||
|
||||
fix:
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
# Execon [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL]
|
||||
# Execon [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![BuildStatusIMGURL]][BuildStatusURL]
|
||||
|
||||
Patterns of function calls.
|
||||
|
||||
## Install
|
||||
|
|
@ -149,4 +150,5 @@ MIT
|
|||
[BuildStatusURL]: https://travis-ci.org/coderaiser/execon "Build Status"
|
||||
[DependencyStatusURL]: https://gemnasium.com/coderaiser/execon "Dependency Status"
|
||||
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"
|
||||
|
||||
[BuildStatusIMGURL]: https://img.shields.io/travis/coderaiser/execon/master.svg?style=flat
|
||||
[BuildStatusURL]: https://travis-ci.org/coderaiser/execon "Build Status"
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"name": "execon",
|
||||
"version": "1.2.8",
|
||||
"homepage": "https://github.com/coderaiser/execon",
|
||||
"authors": [
|
||||
"coderaiser <mnemonic.enemy@gmail.com>"
|
||||
|
|
@ -20,6 +21,5 @@
|
|||
"bower_components",
|
||||
"test",
|
||||
"tests"
|
||||
],
|
||||
"version": "1.2.5"
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,14 +29,16 @@
|
|||
/*
|
||||
* return function that calls callback with arguments
|
||||
*/
|
||||
|
||||
exec.with = function(callback) {
|
||||
var args = [].slice.call(arguments),
|
||||
bind = Function.prototype.bind;
|
||||
var slice = Array.prototype.slice,
|
||||
args = slice.call(arguments, 1);
|
||||
|
||||
args[0] = null;
|
||||
|
||||
return bind.apply(callback, args);
|
||||
return function() {
|
||||
var array = slice.call(arguments),
|
||||
all = args.concat(array);
|
||||
|
||||
return callback.apply(null, all);
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -98,10 +100,10 @@
|
|||
type = getType(funcs);
|
||||
|
||||
if (!funcs)
|
||||
throw(Error('funcs' + ERROR));
|
||||
throw Error('funcs' + ERROR);
|
||||
|
||||
if (!callback)
|
||||
throw(Error('callback' + ERROR));
|
||||
throw Error('callback' + ERROR);
|
||||
|
||||
switch(type) {
|
||||
case 'array':
|
||||
|
|
@ -109,7 +111,7 @@
|
|||
|
||||
funcs.forEach(function(func, num) {
|
||||
exec(func, function() {
|
||||
checkFunc(num, arguments, arr);
|
||||
checkFunc(num, arguments);
|
||||
});
|
||||
});
|
||||
break;
|
||||
|
|
@ -128,7 +130,7 @@
|
|||
break;
|
||||
}
|
||||
|
||||
function checkFunc(num, data, all) {
|
||||
function checkFunc(num, data) {
|
||||
var args = slice.call(data, 1),
|
||||
isLast = false,
|
||||
error = data[0],
|
||||
|
|
@ -140,17 +142,17 @@
|
|||
|
||||
if (!error)
|
||||
if (length >= 2)
|
||||
all[num] = args;
|
||||
arr[num] = args;
|
||||
else
|
||||
all[num] = args[0];
|
||||
arr[num] = args[0];
|
||||
|
||||
if (!callbackWas && (error || isLast)) {
|
||||
callbackWas = true;
|
||||
|
||||
if (type === 'array')
|
||||
callback.apply(null, [error].concat(all));
|
||||
callback.apply(null, [error].concat(arr));
|
||||
else
|
||||
callback(error, all);
|
||||
callback(error, arr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -176,7 +178,7 @@
|
|||
};
|
||||
|
||||
if (!Array.isArray(funcs))
|
||||
throw(Error('funcs should be array!'));
|
||||
throw Error('funcs should be array!');
|
||||
|
||||
fn = funcs.shift();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
{
|
||||
"name": "execon",
|
||||
"version": "1.2.5",
|
||||
"version": "1.2.8",
|
||||
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
||||
"description": "Patterns of function calls",
|
||||
"homepage": "http://github.com/coderaiser/execon",
|
||||
"scripts": {
|
||||
"test": "tape test/*.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/coderaiser/execon.git"
|
||||
|
|
@ -13,5 +16,8 @@
|
|||
"engines": {
|
||||
"node": ">=0.8"
|
||||
},
|
||||
"main": "lib/exec.js"
|
||||
"main": "lib/exec.js",
|
||||
"devDependencies": {
|
||||
"tape": "~4.2.1"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,62 +25,6 @@
|
|||
should(ext).eql(EXT);
|
||||
});
|
||||
});
|
||||
|
||||
describe('exec', function() {
|
||||
it('should execute function with parameters', function() {
|
||||
var WORD = 'hello',
|
||||
func = function(word) {
|
||||
return word;
|
||||
},
|
||||
word = Util.exec(func, WORD);
|
||||
|
||||
WORD.should.equal(word);
|
||||
});
|
||||
|
||||
it('should not execute function, if type of first argument not function', function() {
|
||||
var WORD = 'hello',
|
||||
word = Util.exec(WORD);
|
||||
|
||||
(word === undefined).should.be.true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('exec.ret', function() {
|
||||
it('should return function that try to call callback', function() {
|
||||
var STR = 'hello world',
|
||||
func1 = function() {
|
||||
var args = [].slice.call(arguments);
|
||||
|
||||
return args.join(' ');
|
||||
},
|
||||
func2 = Util.exec.ret(func1, 'hello'),
|
||||
str = func2('world');
|
||||
|
||||
str.should.be.equal(STR);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('exec.parallel', function() {
|
||||
it('should execute a couple functions async and return results in callback', function() {
|
||||
var WORD = 'hello world',
|
||||
funcSlow = function(callback) {
|
||||
setTimeout(function() {
|
||||
callback(null, 'hello');
|
||||
}, 10);
|
||||
},
|
||||
funcFast = function(callback) {
|
||||
setTimeout(function() {
|
||||
callback(null, 'world');
|
||||
}, 1);
|
||||
};
|
||||
|
||||
Util.exec.parallel([funcSlow, funcFast], function(error, hello, world) {
|
||||
WORD.should.equal(hello + ' ' + world);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue