Skip to content

Commit

Permalink
refactor: add error utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
imLymei committed Sep 16, 2024
1 parent 84b8cf4 commit c2a9a97
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 64 deletions.
10 changes: 3 additions & 7 deletions src/commands/downloadConfig.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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`,
Expand All @@ -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...');
}
});

Expand Down
7 changes: 2 additions & 5 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import openConfig from './openConfig';
import uploadConfig from './uploadConfig';
import downloadConfig from './downloadConfig';
import {
error,
getClosestWord,
getConfig,
logList,
Expand Down Expand Up @@ -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'));
Expand Down
7 changes: 2 additions & 5 deletions src/commands/renameCommand.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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];
Expand Down
21 changes: 6 additions & 15 deletions src/commands/syncConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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');
}
});

Expand Down
12 changes: 3 additions & 9 deletions src/commands/uploadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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!');
});
Expand Down
12 changes: 3 additions & 9 deletions src/lib/github.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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');
Expand All @@ -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...');
}
}
25 changes: 11 additions & 14 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

Expand All @@ -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));
Expand Down Expand Up @@ -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);
}

0 comments on commit c2a9a97

Please sign in to comment.