Skip to content
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

fix(cli): Added parent directory check #359

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/cli/src/util/configuration.ts
rajdip-b marked this conversation as resolved.
Show resolved Hide resolved
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
}
}
}