Decaffeinate StorageFolder

This commit is contained in:
Max Brunsfeld
2018-01-18 12:02:43 -08:00
parent 9694448f9d
commit 400abcba34
2 changed files with 57 additions and 39 deletions

View File

@@ -1,39 +0,0 @@
path = require "path"
fs = require "fs-plus"
module.exports =
class StorageFolder
constructor: (containingPath) ->
@path = path.join(containingPath, "storage") if containingPath?
clear: ->
return unless @path?
try
fs.removeSync(@path)
catch error
console.warn "Error deleting #{@path}", error.stack, error
storeSync: (name, object) ->
return unless @path?
fs.writeFileSync(@pathForKey(name), JSON.stringify(object), 'utf8')
load: (name) ->
return unless @path?
statePath = @pathForKey(name)
try
stateString = fs.readFileSync(statePath, 'utf8')
catch error
unless error.code is 'ENOENT'
console.warn "Error reading state file: #{statePath}", error.stack, error
return undefined
try
JSON.parse(stateString)
catch error
console.warn "Error parsing state file: #{statePath}", error.stack, error
pathForKey: (name) -> path.join(@getPath(), name)
getPath: -> @path

57
src/storage-folder.js Normal file
View File

@@ -0,0 +1,57 @@
const path = require('path')
const fs = require('fs-plus')
module.exports =
class StorageFolder {
constructor (containingPath) {
if (containingPath) {
this.path = path.join(containingPath, 'storage')
}
}
clear () {
if (!this.path) return
try {
fs.removeSync(this.path)
} catch (error) {
console.warn(`Error deleting ${this.path}`, error.stack, error)
}
}
storeSync (name, object) {
if (!this.path) return
fs.writeFileSync(this.pathForKey(name), JSON.stringify(object), 'utf8')
}
load (name) {
if (!this.path) return
const statePath = this.pathForKey(name)
let stateString
try {
stateString = fs.readFileSync(statePath, 'utf8')
} catch (error) {
if (error.code !== 'ENOENT') {
console.warn(`Error reading state file: ${statePath}`, error.stack, error)
}
return null
}
try {
return JSON.parse(stateString)
} catch (error) {
console.warn(`Error parsing state file: ${statePath}`, error.stack, error)
}
}
pathForKey (name) {
return path.join(this.getPath(), name)
}
getPath () {
return this.path
}
}