Skip to content

Commit

Permalink
[#89] sketch of userland implementation of inline scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
thejmazz committed Oct 8, 2017
1 parent 8001ba9 commit 9a2e750
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/tasks/inline-script/bash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

const { task } = require('../../..')

const fs = require('fs')
const path = require('path')
const { spawn } = require('child_process')

const script = ({ dir, program }) => (strings, ...values) => {
let content = ''
strings.forEach((str, i) => {
content += str + (values[i] || '')
})

const scriptLocation = path.resolve(dir, 'script.' + (() => {
switch (program) {
case 'bash': return 'sh'
case 'python': return 'py'
}
})())

fs.writeFileSync(scriptLocation, `
#!/usr/bin/env ${program}
${content.replace(/\n\ {2}/g, '\n')}
`.trim())

fs.chmodSync(scriptLocation, '755')

const cp = spawn(scriptLocation)

cp.on('error', (err) => console.log(`${scriptLocation} error:`, err))
cp.on('close', () => console.log(`${scriptLocation} closed`))
cp.on('exit', () => console.log(`${scriptLocation} exited`))
cp.on('disconnect', () => console.log(`${scriptLocation} disconnected`))

cp.stdout.on('data', (chunk) => console.log(`${scriptLocation} stdout: ${chunk}`))
cp.stderr.on('data', (chunk) => console.log(`${scriptLocation} stderr: ${chunk}`))

return cp
}

const myTask = task({
name: 'my task',
input: '*.lowercase',
output: '*.uppercase'
}, ({ input, dir }) => script({ dir, program: 'bash' })`
input="${input}"
output="${input.replace(/\.lowercase$/, '.uppercase')}"
cat $input | awk '{print toupper($0)}' > $output
`)

myTask()
.then(() => console.log('done'))
.catch(console.error)

0 comments on commit 9a2e750

Please sign in to comment.