Skip to content

Instantly share code, notes, and snippets.

@jwilm
Created January 12, 2014 20:33
Show Gist options
  • Save jwilm/8390194 to your computer and use it in GitHub Desktop.
Save jwilm/8390194 to your computer and use it in GitHub Desktop.
CoffeeScript make temporary dir function for node - uses Bluebird promises and child_process.spawn.
spawn = require('child_process').spawn
Bluebird = require 'bluebird'
mktemp = (pattern) ->
deferred = Bluebird.defer()
out = []
s = spawn('mktemp', ['-t', pattern, '-d'])
s.stderr.setEncoding 'utf8'
s.stdout.setEncoding 'utf8'
s.stdout.on 'data', (data) ->
out.push data
s.stderr.on 'data', (err) ->
out.push err
s.on 'close', (code) ->
return deferred.reject { code: code, err: out.join('\n') } unless code is 0
deferred.resolve out[0]
return deferred.promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment