UNPKG

@keyshade/cli

Version:
126 lines (102 loc) 3.82 kB
import type { DefaultProfileConfig, PrivateKeyConfig, ProfileConfig, ProjectRootConfig } from '@/types/index.types' import { existsSync } from 'fs' import { mkdir, readFile, writeFile } from 'fs/promises' import { Logger } from './logger' export const getOsType = (): 'unix' | 'windows' => { return process.platform === 'win32' ? 'windows' : 'unix' } export const getHomeDirectory = (): string => { const osType = getOsType() return osType === 'windows' ? 'USERPROFILE' : 'HOME' } export const getProfileConfigurationFilePath = () => { const home = getHomeDirectory() return `${process.env[home]}/.keyshade/profiles.json` } export const getPrivateKeyConfigurationFilePath = () => { const home = getHomeDirectory() return `${process.env[home]}/.keyshade/private-keys.json` } export const getDefaultProfileConfigurationFilePath = () => { const home = getHomeDirectory() return `${process.env[home]}/.keyshade/default-profile.json` } export const fetchProfileConfig = async (): Promise<ProfileConfig> => { const path = getProfileConfigurationFilePath() if (!existsSync(path)) { await ensureDirectoryExists(path) await writeFile(path, '{}', 'utf8') Logger.info('~/.keyshade/profiles.json file was created.') } return JSON.parse(await readFile(path, 'utf8')) } export const fetchPrivateKeyConfig = async (): Promise<PrivateKeyConfig> => { const path = getPrivateKeyConfigurationFilePath() if (!existsSync(path)) { await writeFile(path, '{}', 'utf8') Logger.info('~/.keyshade/private-keys.json file was created.') } return JSON.parse(await readFile(path, 'utf8')) } export const fetchDefaultProfileConfig = async (): Promise<DefaultProfileConfig> => { const path = getDefaultProfileConfigurationFilePath() if (!existsSync(path)) { await writeFile(path, '{}', 'utf8') Logger.info('~/.keyshade/default-profile.json file was created.') } return JSON.parse(await readFile(path, 'utf8')) } export const fetchProjectRootConfig = async (): Promise<ProjectRootConfig> => { const path = './keyshade.json' if (!existsSync(path)) { throw new Error( 'Project root configuration not found. Please check if keyshade.json is present in the current directory.' ) } return JSON.parse(await readFile(path, 'utf8')) } 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) const existingConfig = await fetchPrivateKeyConfig() const updatedConfig = { ...existingConfig, ...config } await writeFile(path, JSON.stringify(updatedConfig, null, 2), 'utf8') } export const writeProjectRootConfig = async ( config: ProjectRootConfig ): Promise<void> => { await writeFile('./keyshade.json', JSON.stringify(config, null, 2), 'utf8') } export const writeDefaultProfileConfig = async ( config: DefaultProfileConfig ): Promise<void> => { const path = getDefaultProfileConfigurationFilePath() await ensureDirectoryExists(path) await writeFile(path, JSON.stringify(config, null, 2), 'utf8') } const ensureDirectoryExists = async (path: string) => { // Create the parent directory if it doesn't exist const parentDirectory = path.split('/').slice(0, -1).join('/') if (!existsSync(parentDirectory)) { await mkdir(parentDirectory, { recursive: true }) } } export const fetchPrivateKey = async (projectSlug: string): Promise<string> => { const privateKeys = await fetchPrivateKeyConfig() return privateKeys[projectSlug] }