mirror of
https://github.com/atom/atom.git
synced 2026-04-06 03:02:13 -04:00
Decaffeinate StorageFolder
This commit is contained in:
@@ -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
57
src/storage-folder.js
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user