zaaack / foy

A simple, light-weight, type-friendly and modern task runner for general purpose.
http://zaaack.github.io/foy
MIT License
268 stars 15 forks source link

task.js methods is not exported in index.js #3

Closed viuts closed 5 years ago

viuts commented 5 years ago

strict() is not exported directly in index.js, same

now I have to do const { strict, getGlobalTaskManager } = require('foy/lib/task')

zaaack commented 5 years ago

@viuts Thanks for report! I just fixed it, you can try with the newest version foy@^v0.1.2.

zaaack commented 5 years ago

ps: getGlobalTaskManager is an internal function, so I didn't export it, could you tell me in which case would you use this API?

viuts commented 5 years ago

Thanks for the quick fix!

how can I run nested tasks in this case?

like

task('task1', () => { ./* do something */ })
task('task2', () => { ./* do something */ })
task('task3', () => {
   // run task 1
   // run task 2
})
zaaack commented 5 years ago

@viuts you can pass dependencies in task function, like this:

task('task1', () => { ./* do something */ }) 
task('task2', () => { ./* do something */ }) 
task('task3', ['task1', 'task2'], () => { // run task 1 // run task 2 })

Dependencies will be executed serially and only once by default, but you can customize these behaviors via options:

task('task1', () => { ./* do something */ }) 
task('task2', () => { ./* do something */ }) 
task('task3', [{
  name: 'task1',
  async: true, // run task1 parallelly
 force: true, // force rerun task1 whether it is executed before or not,
} 'task2'], () => { // run task 1 // run task 2 })
viuts commented 5 years ago

@zaaack thanks for the prompt response :D

hmm, the use case will be a bit tricky in this case, cause it is declare before we get the ctx object, consider example below

const { task, desc, option } = require('foy')

desc('pull and fetch')
// option is some how not working for current version, will become a boolean if it passing as 3rd arguments
// from 4th it will get the string correctly
option('-p', '--project', 'project name, should be same as folder name')
task('pull', [{
  name: 'checkout',
  async: true,
  force: true,
}], async (ctx) => {
  await ctx.cd(`./${ctx.options.project}`)
  await ctx.exec('git fetch --all --prune')
  await ctx.exec('git pull')
})

desc('check out and pull')
option('-p', '--project, should be same as folder name')
task('checkout', async (ctx) => {
  await ctx.cd(`./${ctx.options.project}`)
  await ctx.exec(`git checkout ${ctx.options.branch}`)
})

we need to passing the options object into it's dependencies

zaaack commented 5 years ago

@viuts Passing options to dependencies is already supported, just didn't document yet, my fault. : )

task('task1', async ctx => {
  console.log(ctx.options) // "{ forceRebuild: true }"
})

task('task2', [{
  name: 'task1',
  options: { forceRebuild: true }
}])

ps: I just updated the readme.

viuts commented 5 years ago

@zaaack

yeah I also saw this in typing, but it is not really same logic with the example provided.

The options passing in dependencies array is statically typed, but not coming from parent's ctx.options

like in cli I want to type foy pull --project abc

zaaack commented 5 years ago

@viuts Now you can use global options or lazy options in dependency tasks, I think both of these features can solve your problem, just need to update to foy@^v0.1.3.

task('task1', async ctx => {
  console.log(ctx.options) // "{ forceRebuild: true, lazyOptions: 1 }"
  console.log(ctx.global.options) // options from command line "{ a: 1 }"
})

task('task2', [{
  name: 'task1',
  options: {
    forceRebuild: true,
  },
  // Some options that rely on ctx or asynchronization,
  // it will be merged to options.
  resolveOptions: async ctx => {
    return { lazyOptions: 1 }
  }
}])