diff --git a/src/commands/downloadConfig.ts b/src/commands/downloadConfig.ts index c8975a0..e118eba 100644 --- a/src/commands/downloadConfig.ts +++ b/src/commands/downloadConfig.ts @@ -1,7 +1,7 @@ import { Command } from 'commander'; import { basicGithubVerifications, getConfiguration } from '../lib/github'; import { confirm } from '@inquirer/prompts'; -import { createConfig } from '../lib/utils'; +import { createConfig, error } from '../lib/utils'; const downloadConfig = new Command('download') .alias('d') @@ -14,10 +14,7 @@ const downloadConfig = new Command('download') const gist = getConfiguration(gistUrl); - if (!gist) { - console.error('Something went wrong...'); - process.exit(1); - } + if (!gist) error('Something went wrong...'); const update = await confirm({ message: `Do you want to make this your new configuration?\n\n${gist}\n`, @@ -28,8 +25,7 @@ const downloadConfig = new Command('download') try { createConfig(JSON.parse(gist)); } catch { - console.error('Something went wrong...'); - process.exit(1); + error('Something went wrong...'); } }); diff --git a/src/commands/index.ts b/src/commands/index.ts index 325f445..efa77b2 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -7,6 +7,7 @@ import openConfig from './openConfig'; import uploadConfig from './uploadConfig'; import downloadConfig from './downloadConfig'; import { + error, getClosestWord, getConfig, logList, @@ -67,11 +68,7 @@ const program = new Command() if (isFinite(closestWord.distance)) suggestion = `Did you mean: "${closestWord.word}"?`; - console.error(`command "${commandName}" not found.`); - - if (suggestion) console.log(suggestion); - - process.exit(1); + error(`command "${commandName}" not found.`, suggestion); } runUserCommand(command, args, program.getOptionValue('debug')); diff --git a/src/commands/renameCommand.ts b/src/commands/renameCommand.ts index a9de1c2..91af2c2 100644 --- a/src/commands/renameCommand.ts +++ b/src/commands/renameCommand.ts @@ -1,5 +1,5 @@ import { Command } from 'commander'; -import { createConfig, getConfig } from '../lib/utils'; +import { createConfig, error, getConfig } from '../lib/utils'; const renameCommand = new Command('rename') .alias('rn') @@ -9,10 +9,7 @@ const renameCommand = new Command('rename') .action((oldName: string, newName: string) => { const config = getConfig(); - if (!config.commands[oldName]) { - console.error('Command not found'); - process.exit(1); - } + if (!config.commands[oldName]) error('Command not found'); config.commands[newName] = config.commands[oldName]; delete config.commands[oldName]; diff --git a/src/commands/syncConfig.ts b/src/commands/syncConfig.ts index 944a618..c7cdd85 100644 --- a/src/commands/syncConfig.ts +++ b/src/commands/syncConfig.ts @@ -5,7 +5,7 @@ import { getConfiguration, basicGithubVerifications, } from '../lib/github'; -import { changeGistUrl, createConfig, getConfig } from '../lib/utils'; +import { changeGistUrl, createConfig, error, getConfig } from '../lib/utils'; const syncConfig = new Command('sync') .alias('s') @@ -21,32 +21,23 @@ const syncConfig = new Command('sync') gistUrl = uploadConfiguration(); - if (!gistUrl) { - console.error('Something went wrong...'); - process.exit(1); - } + if (!gistUrl) error('Something went wrong...'); changeGistUrl(gistUrl); - if (!updateCloudConfiguration()) { - console.error('Something went wrong...'); - process.exit(1); - } + if (!updateCloudConfiguration()) error('Something went wrong...'); } const newGist = getConfiguration(gistUrl); - if (!newGist) { - console.error('Something went wrong fetching you cloud configuration...'); - process.exit(1); - } + if (!newGist) + error('Something went wrong fetching you cloud configuration...'); try { createConfig(JSON.parse(newGist)); console.log('Configuration synced!'); } catch { - console.error('Something went wrong'); - process.exit(1); + error('Something went wrong'); } }); diff --git a/src/commands/uploadConfig.ts b/src/commands/uploadConfig.ts index 0b7a015..e432e7b 100644 --- a/src/commands/uploadConfig.ts +++ b/src/commands/uploadConfig.ts @@ -4,7 +4,7 @@ import { updateCloudConfiguration, basicGithubVerifications, } from '../lib/github'; -import { changeGistUrl, getConfig } from '../lib/utils'; +import { changeGistUrl, error, getConfig } from '../lib/utils'; const uploadConfig = new Command('upload') .alias('u') @@ -19,18 +19,12 @@ const uploadConfig = new Command('upload') const gistUrl = uploadConfiguration(); - if (!gistUrl) { - console.error('Something went wrong...'); - process.exit(1); - } + if (!gistUrl) error('Something went wrong...'); changeGistUrl(gistUrl); } - if (!updateCloudConfiguration()) { - console.error('Something went wrong...'); - process.exit(1); - } + if (!updateCloudConfiguration()) error('Something went wrong...'); console.log('Configuration uploaded!'); }); diff --git a/src/lib/github.ts b/src/lib/github.ts index b3d9347..318f285 100644 --- a/src/lib/github.ts +++ b/src/lib/github.ts @@ -1,5 +1,5 @@ import { spawnSync } from 'node:child_process'; -import { CONFIG_FILE_PATH, getConfig } from './utils'; +import { CONFIG_FILE_PATH, error, getConfig } from './utils'; import { confirm } from '@inquirer/prompts'; export function hasGithubCli(): boolean { @@ -67,10 +67,7 @@ export function updateCloudConfiguration(): boolean { } export async function basicGithubVerifications() { - if (!hasGithubCli()) { - console.error('You do not have github cli installed'); - process.exit(1); - } + if (!hasGithubCli()) error('You do not have github cli installed'); if (!isGithubLogged()) { console.error('Your are not logged on github cli.\n'); @@ -81,9 +78,6 @@ export async function basicGithubVerifications() { console.log(); - if (!login()) { - console.error('Something went wrong...'); - process.exit(1); - } + if (!login()) error('Something went wrong...'); } } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 4d6f7a0..af68b0e 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -59,10 +59,7 @@ export async function addConfigCommand(key: string, command: string) { export async function editConfigCommand(key: string, command: string) { const newConfig: Config = getConfig(); - if (!newConfig.commands[key]) { - console.error('Command not found'); - process.exit(1); - } + if (!newConfig.commands[key]) error('Command not found'); newConfig.commands[key] = command; @@ -139,12 +136,9 @@ export function formatCommand( allowRequired = false; } else { - if (!allowRequired) { - console.error( - 'You cannot have a required argument after a optional argument' - ); - process.exit(1); - } + if (!allowRequired) + error('You cannot have a required argument after a optional argument'); + object.name = arg.trim(); } @@ -156,10 +150,7 @@ export function formatCommand( ); if (necessaryArgs.length > args.length) { - console.log( - `The argument "${commandArguments[args.length].name}" is missing` - ); - return; + error(`The argument "${commandArguments[args.length].name}" is missing`); } args = args.map((arg) => (arg === '_' ? undefined : arg)); @@ -239,3 +230,9 @@ export function getClosestWord( { distance: Infinity, word: defaultWord } ); } + +export function error(error: string, optionalMessage?: string) { + console.error(error); + if (optionalMessage) console.log(optionalMessage); + process.exit(1); +}