Skip to content

Commit

Permalink
fix(cli): Added parent directory check (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
Princeyadav05 authored and rajdip-b committed Jul 29, 2024
1 parent 60010b4 commit 538ea7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion apps/cli/src/util/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
} from '@/types/index.types'
import { existsSync } from 'fs'
import { readFile, readdir, writeFile } from 'fs/promises'
import { ensureDirectoryExists } from './fileUtils.ts';

export const getOsType = (): 'unix' | 'windows' => {
return process.platform === 'win32' ? 'windows' : 'unix'
Expand Down Expand Up @@ -59,13 +60,15 @@ export const writeProfileConfig = async (
config: ProfileConfig
): Promise<void> => {
const path = getProfileConfigurationFilePath()
await ensureDirectoryExists(path);
await writeFile(path, JSON.stringify(config, null, 2), 'utf8')
}

export const writePrivateKeyConfig = async (
config: PrivateKeyConfig
): Promise<void> => {
const path = getPrivateKeyConfigurationFilePath()
await ensureDirectoryExists(path);
await writeFile(path, JSON.stringify(config, null, 2), 'utf8')
}

Expand All @@ -81,4 +84,4 @@ export const fetchUserRootConfigurationFiles = async (): Promise<string> => {
const path = `${process.env[home]}/.keyshade`
const files = await readdir(path)
return `- ${files.join('\n- ')}`
}
}
14 changes: 14 additions & 0 deletions apps/cli/src/util/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { existsSync, mkdir } from 'fs';
import { dirname, resolve } from 'path';

export async function ensureDirectoryExists(path: string): Promise<void> {
const dir = dirname(resolve(path));
if (!existsSync(dir)) {
try {
await mkdir(dir, { recursive: true });
} catch (error) {
console.error('Failed to create directory:', error);
throw error; // Re-throw the error to be handled by the caller
}
}
}

0 comments on commit 538ea7f

Please sign in to comment.