wix-incubator / lerna-script

Lerna addon for adding custom tasks
MIT License
164 stars 13 forks source link

Immediately invoked functions are not a good™ API #413

Open ChristianUlbrich opened 4 years ago

ChristianUlbrich commented 4 years ago

The current (iter) API is built upon immediately invoked functions, i.e. lerna-script-magicz() returns a function that then can be called immediately.

What is the motivation for this pattern? I've never seen it in the wild (apart from IIFEs which serve a totally different use case), the closest you would get to is Reacts Hook Pattern and there they are almost exclusively never immediately invoke the function but assign to a variable and call it.

Looking at the code, I can see that it might simplify development but at the sacrifice of a bad API.

The iter stuff is sync, so it should either be possible to call it in a sync way iter.parallel(packages, packageFn) or use a fluent API such as iter.parallel(packages).with(packageFn).

Another possibility would be returning proxied arrays, which would also be feasible with lerna running on node.

Using the pattern

function parallel() {
  const retFun = taskFn => { /*..*/ };
  retFun.with = retFun;
  return retFun;
}

you could support this syntax without breaking the existing API too much at the benefit of a more natural API.