Skip to content

Instantly share code, notes, and snippets.

@StreetStrider
Created July 15, 2016 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StreetStrider/199568dfb1b3bf753b554d74b7dac8aa to your computer and use it in GitHub Desktop.
Save StreetStrider/199568dfb1b3bf753b554d74b7dac8aa to your computer and use it in GitHub Desktop.
ensurer
function Ensure (check, action)
{
return function ensurer (data)
{
return capture(() => check(data))
.then(so =>
{
if (! so)
{
if (! action)
{
throw new TypeError('Ensure: cannot handle')
}
return capture(() => action(data))
}
})
}
}
Ensure.directory_exists = () =>
{
var fs = require('fs-sync')
return Ensure(fs.exists, fs.mkdir)
}
Ensure.directory_must_exist = () =>
{
var exists = require('fs-sync').exists
return Ensure(dir =>
{
if (! exists(dir))
{
throw new TypeError('directory_must_exist')
}
return true
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment