Skip to content

Commit

Permalink
feat(okam-build): add processor hook to allow overload
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhy committed Dec 6, 2018
1 parent 5fdf3b6 commit f0b318a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/okam-build/lib/processor/helper/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,39 @@ function initProcessorInfo(name, info, existedProcessors) {
return info;
}

/**
* Override the function value for the given object
*
* @param {?Object} curr the current object
* @param {?Object} old the old object
* @return {?Object}
*/
function overrideObjectFunctions(curr, old) {
if (!old) {
return curr;
}

if (!curr && curr !== undefined) {
return old;
}

let result = {};
Object.keys(curr).forEach(k => {
let v = curr[k];
let oldV = old[k];
if (typeof v === 'function' && typeof oldV === 'function') {
let currV = v;
v = function (...args) {
oldV.apply(this, args);
currV.apply(this, args);
};
}
result[k] = v;
});

return result;
}

/**
* Override the builtin processor definition
*
Expand All @@ -203,6 +236,10 @@ function overrideProcessor(existedProcessor, extnameProcessorMap, opts) {
newExtnames = v;
}

if (k === 'hook') {
v = overrideObjectFunctions(v, existedProcessor[k]);
}

existedProcessor[k] = v;
});

Expand Down

0 comments on commit f0b318a

Please sign in to comment.