deepsweet / start

:red_circle: Functional task runner for Node.js
https://start.js.org/
MIT License
476 stars 19 forks source link

Passing arguments to each task #23

Closed tunnckoCore closed 7 years ago

tunnckoCore commented 7 years ago

Hi, start is great. Looking on it almost one year. And finally consider to plug it in my flow. It would be the core of my day to day flow.

But I'm thinking for this that I may need in some cases to pass specific things to each task. For example glob patterns for the start-files task.

Something like

start dev 'lib/**/*.js'
start dev '**/*.js'

But I believe it won't be need only for that task. So I think the start CLIs should pass rest argument after the task name to that task. And because each task is just a function which returns start(task1, task2, task3) it can be passed to it.

export const dev = (args) => start(
  env('NODE_ENV', 'development'),
  files('build/'),
  clean(),
  files(args[0]),
  watch((file) => start(
    files(file),
    read(),
    babel(),
    write('build/')
  ))
)

So in advance in that task could be done some arguments parsing, with minimist for example.

I need all this, because I don't want to transpile specific directory, or all of my js files in the root project - the test.js file for example. In most cases I have one simple index.js file and one test.js file in the root of the repository/project/package. But in some cases I may need to transpile more than that index.js. So I want in my preset to set index.js path as default, but to be able to change it through the cli if I need.

tunnckoCore commented 7 years ago

Passing just process.argv here simple-cli lib/index.js#L56 would be absolutely enough I think.

edit: But, actually, no. When rethink, this way will conflict? Because you can pass each task to other tasks.

tunnckoCore commented 7 years ago

Hm. Maybe the another way is if the task returns another function instead of promise (start()), then pass argv to it, so

export const dev = () => (processArgv) => start(
  env('NODE_ENV', 'development'),
  files('build/'),
)

and in simple-cli

let promise = tasksRunner()

if (typeof promise === 'function') {
  promise = promise(process.argv)
}

promise.catch(() => {
  process.exit(1)
})
tunnckoCore commented 7 years ago

Also should add check here in lib/index.js - kinda if current is function.

deepsweet commented 7 years ago

hi! I suck at promoting, so every new active user is really a big event :) thank you.

I like this approach:

export const dev = (args) => start(
  env('NODE_ENV', 'development'),
  files('build/'),
  clean(),
  files(args[0]),
  watch((file) => start(
    files(file),
    read(),
    babel(),
    write('build/')
  ))
)

but yeah, need to think more about conflicting with nested runners... well, it's not really a "conflict", but some behavior may be broken.

btw have you seen this? maybe to write some abstract task like that to connect nested runners (and to solve passing additional args as a bonus) is a good idea. anyway, thanks for "mental pabulum", I'll try to find the best way.

deepsweet commented 7 years ago

something like this:

export const test = (arg) => start(
  env('NODE_ENV', 'test'),
  files(arg),
  mocha()
);

export coverage = (arg) => start(
  env('NODE_ENV', 'test'),
  files(arg),
  clean(),
  files('lib/**/*.js'),
  istanbul.instrument(),
  (/*input*/) => test(arg),
  istanbul.report()
)
tunnckoCore commented 7 years ago

Haha, no problem.

Yea first approach looks better, but I think second is better and won't have breaking changes to the API (hate aggressive bumping of versions [because greenkeepr] - that's why i commented on the commits of last major version)

edit: btw, while thinking, this "passing arguments" feature will allow to pass multiple tasks too.

deepsweet commented 7 years ago

so, all we need is to update readme in Start itself and bump CLI with a minor version (new functionality to pass args through). that's why I like Start approach so much – with that level of modularity it's possible to do almost anything very accurate w/o breaking every package.

tunnckoCore commented 7 years ago

new functionality to pass args through

yea. i kind of dislike semver because of this, haha. but yea.

with that level of modularity it's possible to do almost anything very accurate w/o breaking every package.

Yea, love how it looks. And the simplicity of using it (hence, just npm start: start -p preset is absolutely cool).

deepsweet commented 7 years ago

👍

tunnckoCore commented 7 years ago

great, but actually don't like it much. idea intentionally was to pass raw process.argv, to allow users to do what they want