Skip to content

fallback session state to file #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
handle session keys in file
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Nov 21, 2021
commit 1e81199435704be332049008a3e9f0580283a706
22 changes: 15 additions & 7 deletions src/services/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,34 @@ interface ExecParams {
dir?: string
}

// correct paths to be from workspace root rather than extension folder
const getWorkspacePath = (...paths: string[]) => {
return join(WORKSPACE_ROOT, ...paths)
}

export const exec = (params: ExecParams): Promise<{ stdout: string; stderr: string }> | never => {
const cwd = join(WORKSPACE_ROOT, params.dir || '')
return asyncExec(params.command, { cwd })
}

export const exists = (...paths: string[]): boolean | never => {
return fs.existsSync(join(WORKSPACE_ROOT, ...paths))
return fs.existsSync(getWorkspacePath(...paths))
}

export const removeFile = (...paths: string[]) => {
return asyncRemoveFile(join(WORKSPACE_ROOT, ...paths))
return asyncRemoveFile(getWorkspacePath(...paths))
}

export const readFile = (...paths: string[]) => {
return asyncReadFile(join(...paths), 'utf8')
export const readFile = (...paths: string[]): Promise<string | void> => {
const filePath = getWorkspacePath(...paths)
return asyncReadFile(getWorkspacePath(...paths), 'utf8').catch((err) => {
console.warn(`Failed to read from ${filePath}`)
})
}

export const writeFile = (data: any, ...paths: string[]) => {
const filePath = join(...paths)
export const writeFile = (data: any, ...paths: string[]): Promise<void> => {
const filePath = getWorkspacePath(...paths)
return asyncWriteFile(filePath, JSON.stringify(data)).catch((err) => {
console.error(`Failed to write to ${filePath}`)
console.warn(`Failed to write to ${filePath}`)
})
}
39 changes: 28 additions & 11 deletions src/services/storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,53 @@ class Storage<T> {
if (value) {
return JSON.parse(value)
} else if (SESSION_FILE_PATH) {
// optionally read from file as a fallback to localstorage
const sessionFile = await readFile(SESSION_FILE_PATH)
try {
// optionally read from file as a fallback to local storage
const sessionFile = await readFile(SESSION_FILE_PATH)
if (!sessionFile) {
throw new Error('No session file found')
}
const session = JSON.parse(sessionFile)
if (session && session[this.key]) {
// TODO: validate session
return session[this.key]

if (session) {
const keys = Object.keys(session)
// validate session
if (keys.length) {
// should only be one
this.key = keys[0]
return session[this.key]
}
}
} catch (err) {
console.error(`Failed to parse session file: ${SESSION_FILE_PATH}`)
console.warn(`Failed to read or parse session file: ${SESSION_FILE_PATH}`)
}
}
return this.defaultValue
}
public set = (value: T): void => {
const stringValue = JSON.stringify(value)
this.storage.update(this.key, stringValue)
this.writeToSessionFile(stringValue)
}
public update = async (value: T): Promise<void> => {
const current = await this.get()
const next = JSON.stringify({
...current,
...value,
})
this.storage.update(this.key, next).then(() => {
// optionally write to file
if (SESSION_FILE_PATH) {
writeFile(this.storage, SESSION_FILE_PATH)
await this.storage.update(this.key, next)

this.writeToSessionFile(next)
}
public writeToSessionFile(data: string) {
// optionally write to file
if (SESSION_FILE_PATH) {
try {
writeFile({ [this.key]: data }, SESSION_FILE_PATH)
} catch (err: any) {
console.warn(`Failed to write coderoad session to path: ${SESSION_FILE_PATH}`)
}
})
}
}
public reset = () => {
this.set(this.defaultValue)
Expand Down