From a26597d2a3530004d36cf5d96a2c9352391bd50c Mon Sep 17 00:00:00 2001 From: Phoebe Lartisant Date: Wed, 2 Oct 2024 17:26:52 +0200 Subject: [PATCH] init config.ts & clean fn's --- cli/src/commands/config.ts | 33 +++++++++++++++++++++++++++++++++ cli/src/commands/upload.ts | 1 - cli/src/commands/utils.ts | 37 ------------------------------------- cli/src/index.ts | 5 +++-- 4 files changed, 36 insertions(+), 40 deletions(-) create mode 100644 cli/src/commands/config.ts diff --git a/cli/src/commands/config.ts b/cli/src/commands/config.ts new file mode 100644 index 0000000..95c671f --- /dev/null +++ b/cli/src/commands/config.ts @@ -0,0 +1,33 @@ +import { OptionValues } from 'commander' +import { readFileSync } from 'fs' + +interface Config { + wallet_password: string + wallet_path: string + node_url: string + chunk_size: number + secret_key: string +} + +export function parseConfigFile(filePath: string): Config { + const fileContent = readFileSync(filePath, 'utf-8') + try { + return JSON.parse(fileContent) + } catch (error) { + throw new Error(`Failed to parse file: ${error}`) + } +} + +export function mergeConfigAndOptions( + commandOptions: OptionValues, + configOptions: Config +): OptionValues { + if (!configOptions) return commandOptions + + const wallet = commandOptions.wallet || configOptions.wallet_path + const password = commandOptions.password || configOptions.wallet_password + const node_url = commandOptions.node_url || configOptions.node_url + const chunk_size = commandOptions.chunk_size || configOptions.chunk_size + + return { wallet, password, node_url, chunk_size } +} diff --git a/cli/src/commands/upload.ts b/cli/src/commands/upload.ts index 0b74840..71f1d7b 100644 --- a/cli/src/commands/upload.ts +++ b/cli/src/commands/upload.ts @@ -30,7 +30,6 @@ export const uploadCommand = new Command('upload') ) .action(async (websiteDirPath, options, command) => { const globalOptions = command.optsWithGlobals() - console.log('globalOptions:', globalOptions) // throw if global options are not defined if (!globalOptions) { diff --git a/cli/src/commands/utils.ts b/cli/src/commands/utils.ts index e0b6ed3..5c295d4 100644 --- a/cli/src/commands/utils.ts +++ b/cli/src/commands/utils.ts @@ -114,40 +114,3 @@ export function validateAddress(address: string) { process.exit(1) } } - -interface WalletConfig { - wallet: string - password: string -} - -interface Config { - wallet_config: WalletConfig - node_url: string - chunk_size: number - secret_key: string -} - -export function parseConfigFile(filePath: string): Config { - const fileContent = readFileSync(filePath, 'utf-8') - try { - const parsedData = JSON.parse(fileContent) - return parsedData - } catch (error) { - throw new Error(`Failed to parse file: ${error}`) - } -} - -export function setProgramOptions( - commandOptions: OptionValues, - configOptions: Config -): OptionValues { - if (!configOptions) return commandOptions - - const wallet = commandOptions.wallet || configOptions.wallet_config.wallet - const password = - commandOptions.password || configOptions.wallet_config.password - const node_url = commandOptions.node_url || configOptions.node_url - const chunk_size = commandOptions.chunk_size || configOptions.chunk_size - - return { wallet, password, node_url, chunk_size } -} diff --git a/cli/src/index.ts b/cli/src/index.ts index d272ee1..aed6138 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -5,8 +5,9 @@ import { deleteCommand } from './commands/delete' import { listFilesCommand } from './commands/list' import { showFileCommand } from './commands/showFile' import { uploadCommand } from './commands/upload' -import { parseConfigFile, setProgramOptions } from './commands/utils' + import { existsSync } from 'fs' +import { mergeConfigAndOptions, parseConfigFile } from './commands/config' const version = process.env.VERSION || 'dev' const defaultConfigPath = 'deweb_cli_config.json' @@ -41,7 +42,7 @@ if (existsSync(commandOptions.config)) { const configOptions = parseConfigFile(commandOptions.config) // commandOptions get priority over configOptions - const programOptions = setProgramOptions(commandOptions, configOptions) + const programOptions = mergeConfigAndOptions(commandOptions, configOptions) for (const [key, value] of Object.entries(programOptions)) { program.setOptionValue(key, value) }