diff --git a/src/commands/logout/index.ts b/src/commands/logout/index.ts index 23e3d75..5a63a72 100644 --- a/src/commands/logout/index.ts +++ b/src/commands/logout/index.ts @@ -10,13 +10,12 @@ export const logoutCommand = program .description('Logout from the Storyblok CLI') .action(async () => { const verbose = program.opts().verbose - - const isAuth = await isAuthorized() - if (!isAuth) { - konsola.ok(`You are already logged out. If you want to login, please use the login command.`) - return - } try { + const isAuth = await isAuthorized() + if (!isAuth) { + konsola.ok(`You are already logged out. If you want to login, please use the login command.`) + return + } await removeAllNetrcEntries() konsola.ok(`Successfully logged out`) diff --git a/src/creds.ts b/src/creds.ts index a0a8714..a4f019a 100644 --- a/src/creds.ts +++ b/src/creds.ts @@ -89,7 +89,6 @@ export const getNetrcCredentials = async (filePath: string = getNetrcFilePath()) await access(filePath) } catch { - konsola.warn(`.netrc file not found at path: ${filePath}`) return {} } try { @@ -161,10 +160,16 @@ export const addNetrcEntry = async ({ let machines: Record = {} // Check if the file exists - await access(filePath) - // File exists, read and parse it - const content = await readFile(filePath, 'utf8') - machines = parseNetrcContent(content) + try { + await access(filePath) + // File exists, read and parse it + const content = await readFile(filePath, 'utf8') + machines = parseNetrcContent(content) + } + catch { + // File does not exist + konsola.ok(`.netrc file not found at path: ${filePath}. A new file will be created.`) + } // Add or update the machine entry machines[machineName] = { @@ -227,9 +232,14 @@ export const removeNetrcEntry = async ( } export function removeAllNetrcEntries(filePath = getNetrcFilePath()) { - return writeFile(filePath, '', { - mode: 0o600, // Set file permissions - }) + try { + writeFile(filePath, '', { + mode: 0o600, // Set file permissions + }) + } + catch (error) { + handleFileSystemError('write', error as NodeJS.ErrnoException) + } } export async function isAuthorized() { diff --git a/src/utils/error/filesystem-error.ts b/src/utils/error/filesystem-error.ts index 20c2cd0..ab42bad 100644 --- a/src/utils/error/filesystem-error.ts +++ b/src/utils/error/filesystem-error.ts @@ -73,6 +73,7 @@ export class FileSystemError extends Error { return { name: this.name, message: this.message, + code: this.code, cause: this.cause, errorId: this.errorId, stack: this.stack,