Skip to content

Commit

Permalink
fix(LoginAuthenticator): create cli dir on init
Browse files Browse the repository at this point in the history
  • Loading branch information
john-u authored and rossiam committed Aug 17, 2020
1 parent 599c3c2 commit 27d7bee
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/lib/src/login-authenticator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import express from 'express'
import fs from 'fs'
import getPort from 'get-port'
import open from 'open'
import path from 'path'
import qs from 'qs'

import { Authenticator } from '@smartthings/core-sdk'
Expand Down Expand Up @@ -60,6 +61,9 @@ export class LoginAuthenticator implements Authenticator {
public static init(credentialsFile: string): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(global as any)._credentialsFile = credentialsFile

const cliDir = path.dirname(credentialsFile)
fs.mkdirSync(cliDir, { recursive: true })
}
private clientId: string

Expand Down Expand Up @@ -96,11 +100,14 @@ export class LoginAuthenticator implements Authenticator {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private readCredentialsFile(): { [profileName: string]: any } {
if (fs.existsSync(credentialsFile())) {
const fileData = fs.readFileSync(credentialsFile())
return JSON.parse(fileData.toString())
let fileData = '{}'
try {
fileData = fs.readFileSync(credentialsFile()).toString()
} catch (err) {
if (err.code !== 'ENOENT') { throw err }
}
return {}

return JSON.parse(fileData)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit 27d7bee

Please sign in to comment.