'use strict'

// === WATERWHEEL ===
const { spawn } = require('child_process')
const globby = require('globby')

const task = (props, cb, next) => {
  const { output } = props

  const stream = cb(props)

  stream.on('end', () => globby(output).then(next) )

  return stream
}


// === PIPELINE ===
const ncbi = require('bionode-ncbi')

const config = {
  sraAccession: '2492428'
}

const samples = task(
  {
    input: {
      db: 'sra',
      accession: config.sraAccession
    },
    output: '**/*.sra'
  },
  ( ({ input }) => ncbi.download(input.db, input.accession) ),
  dump
  // ( (output) => dump(`fastq-dump --split-files --skip-technical --gzip ${output}`) )
)

// function dumps() {
//   return task({
//     input: '*.sra',
//     output: [1, 2].map(n => `*_${n}.fastq.gz`)
//   }, )
// }

function dump(data, cmd) {
  console.log(cmd)
  console.log(data[0])

  const process = spawn('fastq-dump', ['--split-files', '--skip-technical', '--gzip', data[0]])

  process.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`)
  })

  process.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`)
  })

  process.on('close', (code) => {
    console.log(`child process exited with code ${code}`)
  })
}

samples
  .on('data', console.log)
  .on('end', () => console.log('Finished SRA download'))

// ncbi.download('sra', config.sraAccession)
//   .on('data', console.log)
// samples
//   .on('end', path => console.log(`File saved at ${path}`))

@@ -198,16 +198,156 @@
 props)%0A%0A
+  // Check current dir for files matching the pattern provided to the task%0A  // Then call next task in the pipeline with the resolved files%0A
   stream
@@ -388,17 +388,16 @@
 en(next)
- 
 )%0A%0A  ret
@@ -410,16 +410,407 @@
 ream%0A%7D%0A%0A
+const shell = (cmd) =%3E %7B%0A  console.log('Starting: ' + cmd)%0A  cmd = cmd.split(' ')%0A%0A  const process = spawn(cmd%5B0%5D, cmd.slice(1))%0A%0A  process.stdout.on('data', (data) =%3E %7B%0A    console.log(%60stdout: $%7Bdata%7D%60)%0A  %7D)%0A%0A  process.stderr.on('data', (data) =%3E %7B%0A    console.log(%60stderr: $%7Bdata%7D%60)%0A  %7D)%0A%0A  process.on('close', (code) =%3E %7B%0A    console.log(%60child process exited with code $%7Bcode%7D%60)%0A  %7D)%0A%7D%0A
 %0A// === 
@@ -910,29 +910,106 @@
 %0A%7D%0A%0A
-const samples = task(
+// task(props, cb, next)%0Aconst samples = task(%0A  // these params are then made available to the cb
 %0A  %7B
@@ -1083,16 +1083,60 @@
 %0A    %7D,%0A
+    // will be globby'ed after cb completes%0A
     outp
@@ -1155,16 +1155,42 @@
 a'%0A  %7D,%0A
+  // the cb for this task%0A
   ( (%7B i
@@ -1250,36 +1250,89 @@
 ,%0A  
-dump%0A  // ( (output) =%3E dump
+// the next task for this task%0A  // data is the resolved output%0A  (data) =%3E shell
 (%60fa
@@ -1384,22 +1384,28 @@
 p $%7B
-output%7D%60) 
+data%5B0%5D%7D%60)%0A
 )%0A
-)
 %0A
+// wip
 %0A// 
@@ -1535,444 +1535,42 @@
  %7D%0A%0A
-function dump(data, cmd) %7B%0A  console.log(cmd)%0A  console.log(data%5B0%5D)%0A%0A  const process = spawn('fastq-dump', %5B'--split-files', '--skip-technical', '--gzip', data%5B0%5D%5D)%0A%0A  process.stdout.on('data', (data) =%3E %7B%0A    console.log(%60stdout: $%7Bdata%7D%60)%0A  %7D)%0A%0A  process.stderr.on('data', (data) =%3E %7B%0A    console.log(%60stderr: $%7Bdata%7D%60)%0A  %7D)%0A%0A  process.on('close', (code) =%3E %7B%0A    console.log(%60child process exited with code $%7Bcode%7D%60)%0A  %7D)%0A%7D%0A%0Asamples
+const pipeline = samples%0A%0Apipeline
 %0A  .

Move shell command description to samples task