Skip to content

Commit 1e81199

Browse files
committed
handle session keys in file
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent bf9ed01 commit 1e81199

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

src/services/node/index.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,34 @@ interface ExecParams {
1414
dir?: string
1515
}
1616

17+
// correct paths to be from workspace root rather than extension folder
18+
constgetWorkspacePath=(...paths: string[])=>{
19+
returnjoin(WORKSPACE_ROOT, ...paths)
20+
}
21+
1722
exportconstexec=(params: ExecParams): Promise<{stdout: string;stderr: string}>|never=>{
1823
constcwd=join(WORKSPACE_ROOT,params.dir||'')
1924
returnasyncExec(params.command,{ cwd })
2025
}
2126

2227
exportconstexists=(...paths: string[]): boolean|never=>{
23-
returnfs.existsSync(join(WORKSPACE_ROOT,...paths))
28+
returnfs.existsSync(getWorkspacePath(...paths))
2429
}
2530

2631
exportconstremoveFile=(...paths: string[])=>{
27-
returnasyncRemoveFile(join(WORKSPACE_ROOT,...paths))
32+
returnasyncRemoveFile(getWorkspacePath(...paths))
2833
}
2934

30-
exportconstreadFile=(...paths: string[])=>{
31-
returnasyncReadFile(join(...paths),'utf8')
35+
exportconstreadFile=(...paths: string[]): Promise<string|void>=>{
36+
constfilePath=getWorkspacePath(...paths)
37+
returnasyncReadFile(getWorkspacePath(...paths),'utf8').catch((err)=>{
38+
console.warn(`Failed to read from ${filePath}`)
39+
})
3240
}
3341

34-
exportconstwriteFile=(data: any, ...paths: string[])=>{
35-
constfilePath=join(...paths)
42+
exportconstwriteFile=(data: any, ...paths: string[]): Promise<void>=>{
43+
constfilePath=getWorkspacePath(...paths)
3644
returnasyncWriteFile(filePath,JSON.stringify(data)).catch((err)=>{
37-
console.error(`Failed to write to ${filePath}`)
45+
console.warn(`Failed to write to ${filePath}`)
3846
})
3947
}

src/services/storage/index.ts

+28-11
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,53 @@ class Storage<T> {
2222
if(value){
2323
returnJSON.parse(value)
2424
}elseif(SESSION_FILE_PATH){
25-
// optionally read from file as a fallback to localstorage
26-
constsessionFile=awaitreadFile(SESSION_FILE_PATH)
2725
try{
26+
// optionally read from file as a fallback to local storage
27+
constsessionFile=awaitreadFile(SESSION_FILE_PATH)
28+
if(!sessionFile){
29+
thrownewError('No session file found')
30+
}
2831
constsession=JSON.parse(sessionFile)
29-
if(session&&session[this.key]){
30-
// TODO: validate session
31-
returnsession[this.key]
32+
33+
if(session){
34+
constkeys=Object.keys(session)
35+
// validate session
36+
if(keys.length){
37+
// should only be one
38+
this.key=keys[0]
39+
returnsession[this.key]
40+
}
3241
}
3342
}catch(err){
34-
console.error(`Failed to parse session file: ${SESSION_FILE_PATH}`)
43+
console.warn(`Failed to read or parse session file: ${SESSION_FILE_PATH}`)
3544
}
3645
}
3746
returnthis.defaultValue
3847
}
3948
publicset=(value: T): void=>{
4049
conststringValue=JSON.stringify(value)
4150
this.storage.update(this.key,stringValue)
51+
this.writeToSessionFile(stringValue)
4252
}
4353
publicupdate=async(value: T): Promise<void>=>{
4454
constcurrent=awaitthis.get()
4555
constnext=JSON.stringify({
4656
...current,
4757
...value,
4858
})
49-
this.storage.update(this.key,next).then(()=>{
50-
// optionally write to file
51-
if(SESSION_FILE_PATH){
52-
writeFile(this.storage,SESSION_FILE_PATH)
59+
awaitthis.storage.update(this.key,next)
60+
61+
this.writeToSessionFile(next)
62+
}
63+
publicwriteToSessionFile(data: string){
64+
// optionally write to file
65+
if(SESSION_FILE_PATH){
66+
try{
67+
writeFile({[this.key]: data},SESSION_FILE_PATH)
68+
}catch(err: any){
69+
console.warn(`Failed to write coderoad session to path: ${SESSION_FILE_PATH}`)
5370
}
54-
})
71+
}
5572
}
5673
publicreset=()=>{
5774
this.set(this.defaultValue)

0 commit comments

Comments
 (0)
close