2014-11-07 18:15:59 +00:00
|
|
|
fs = require('fs')
|
|
|
|
path = require('path')
|
2014-11-14 17:19:37 +00:00
|
|
|
rimraf = require('rimraf')
|
2014-11-26 16:56:52 +00:00
|
|
|
fsUtils = require('../../fs-utils/fs-utils')
|
2014-11-07 18:15:59 +00:00
|
|
|
exports.prefix = require('./data-prefix')
|
|
|
|
|
|
|
|
haltIfNoPrefix = (callback) ->
|
|
|
|
return ->
|
|
|
|
if not exports.prefix.get()?
|
|
|
|
throw new Error('Did you forget to set a prefix?')
|
|
|
|
return callback.apply(null, arguments)
|
|
|
|
|
|
|
|
constructPath = (key) ->
|
|
|
|
prefix = exports.prefix.get()
|
|
|
|
result = path.join(prefix, key)
|
|
|
|
|
|
|
|
if not fsUtils.isValidPath(result)
|
|
|
|
throw new Error('Invalid path')
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
exports.get = haltIfNoPrefix (key, options, callback) ->
|
2014-11-14 18:56:52 +00:00
|
|
|
exports.has key, (hasKey) ->
|
|
|
|
if not hasKey
|
2014-11-14 19:26:25 +00:00
|
|
|
|
|
|
|
# Pass undefined explicitly, otherwise
|
|
|
|
# async gets confused
|
|
|
|
return callback?(null, undefined)
|
2014-11-14 18:56:52 +00:00
|
|
|
|
|
|
|
keyPath = constructPath(key)
|
|
|
|
fs.readFile(keyPath, options, callback)
|
2014-11-07 18:15:59 +00:00
|
|
|
|
|
|
|
exports.set = haltIfNoPrefix (key, value, options, callback) ->
|
|
|
|
keyPath = constructPath(key)
|
|
|
|
fs.writeFile(keyPath, value, options, callback)
|
2014-11-14 17:19:37 +00:00
|
|
|
|
2014-11-14 17:34:04 +00:00
|
|
|
exports.has = haltIfNoPrefix (key, callback) ->
|
|
|
|
keyPath = constructPath(key)
|
|
|
|
fs.exists(keyPath, callback)
|
|
|
|
|
2014-11-14 17:19:37 +00:00
|
|
|
exports.remove = haltIfNoPrefix (key, callback) ->
|
|
|
|
keyPath = constructPath(key)
|
|
|
|
|
|
|
|
fsUtils.isDirectory keyPath, (error, isDirectory) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
|
|
|
|
removeFunction = if isDirectory then rimraf else fs.unlink
|
|
|
|
removeFunction(keyPath, callback)
|