lukeed / taskr

A fast, concurrency-focused task automation tool.
MIT License
2.53k stars 74 forks source link

Accept `opts.func` for Plugin API #253

Closed lukeed closed 7 years ago

lukeed commented 7 years ago

Current: plugin(name, opts, func)

The func parameter should be read as the fallback.

func = opts.func || func

This allows for cleaner plugin (inline & external) definitions; especially if relying on the default every and files settings.

// external
this.plugin('myName', {
  files: true,
  every: false,
  *func(arr) {
    // `arr` is all files
  }
})

// external w/defaults
this.plugin('myName', {
  *func(file) {
    // loop each `file` object
  }
})

// inline w/defaults
exports.fooTask = function * (fly) {
  yield fly.source('foo/*').run({
    *func(file) {
      // loop each `file` object
    }
  }).target('bar')
}

Reference: https://github.com/flyjs/fly/issues/233#issuecomment-274749015

lukeed commented 7 years ago

Now that I see this, external plugins should also accept a name key.

Doing so will convert the API to plugin(opts, [func]) for both inline & external plugins.

// external
this.plugin({
  name: 'myName',
  files: false,
  every: false,
  *func(arr) {
    // `arr` is all globs
  }
})

// external w/defaults
this.plugin({
  name: 'myName',
  *func(file) {
    // loop each `file` object
  }
})
jorgebucaran commented 7 years ago

@lukeed Good new, but can you clarify:

This allows for cleaner plugin (inline & external) definitions; especially if relying on the default every and files settings.

How does it make it cleaner? The examples, I think, show the new syntax. Can we get an example using the older / less clean syntax?

lukeed commented 7 years ago

Current: plugin(name, opts, func)

this.plugin('nyName', {every: false}, function * (files, opts) {
  // files: all file objects
  // opts: user options
});
jorgebucaran commented 7 years ago

@lukeed Thanks.

lukeed commented 7 years ago

@jbucaran No problem. So what do you think on the newer suggestion?

It also could mean for simpler external plugins (aka, installed via NPM):

// current
module.exports = function () {
  this.plugin('myName', {every: false}, function * (files, opts) {
    // files: all file objects
    // opts: user options
  });
}

// with proposed change
module.exports = {
  name: 'myName',
  every: false,
  *func(files, opts) {
    // files: all file objects
    // opts: user options
  }
}

cc: @devmondo @hzlmn @watilde

jorgebucaran commented 7 years ago

@lukeed Proposed style is easier to not mess up, since you don't need to remember that name comes first, etc.