Skip to content

Commit

Permalink
updated and implemented new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
transgirllucy committed Dec 4, 2024
1 parent 301320c commit 3f85be6
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
3 changes: 1 addition & 2 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export function runCommand(command: string, options?: { cwd?: string }): Promise
consola.error(`❌ Error executing command: "${command}"\n${stderr}`);
reject(`Error: ${stderr}`);
} else {
consola.success(`✅ Command executed successfully: "${command}"`);
// Convert stdout to string and trim it
// consola.success(`✅ Command executed successfully: "${command}"`);
resolve(stdout.toString().trim());
}
});
Expand Down
8 changes: 7 additions & 1 deletion src/features/search.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ora from "ora";
import { runCommand } from "../command";
import { consola } from "consola";

Expand Down Expand Up @@ -38,9 +39,14 @@ export async function searchPackage({ term, options }: { term: string; options:
command += ' --quiet';
}

const spinner = ora('🔍 Searching...').start();
const result = await runCommand(command);
if (!options.quiet) {
consola.info(`📦 Search results for "${term}":\n${result}`);
const formattedResults = result.split('\n').map(line => line.trim()).filter(line => line);
console.log("\n");
formattedResults.forEach(line => {
consola.info(`📦 ${line}`);
});
}
} catch (error: any) {
consola.error(`❌ Error searching for package "${term}": ${error.message}`);
Expand Down
50 changes: 50 additions & 0 deletions src/features/services.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { runCommand } from "../command"; // Assuming runCommand is a function that executes shell commands
import { consola } from "consola";
import ora from "ora";

interface ServiceOptions {
all?: boolean;
json?: boolean;
debug?: boolean;
quiet?: boolean;
verbose?: boolean;
file?: string;
serviceUser ?: string;
maxWait?: number;
noWait?: boolean;
}

export async function handleServices(subcommand: string, formula?: string, options?: ServiceOptions) {
const spinner = ora(`🔄 Executing brew services ${subcommand}...`).start(); // Start spinner with emoji

try {
let command = `brew services ${subcommand}`;

// Handle options
if (options) {
if (options.all) command += ' --all';
if (options.json) command += ' --json';
if (options.debug) command += ' --debug';
if (options.quiet) command += ' --quiet';
if (options.verbose) command += ' --verbose';
if (options.file) command += ` --file=${options.file}`;
if (options.serviceUser ) command += ` --sudo-service-user=${options.serviceUser }`;
if (options.maxWait !== undefined) command += ` --max-wait=${options.maxWait}`;
if (options.noWait) command += ' --no-wait';
}

if (formula) {
command += ` ${formula}`;
}

const result = await runCommand(command);
spinner.succeed(`✅ Successfully executed: ${command}`); // Success message with emoji

// Output the result
if (!options?.quiet) {
consola.info(`📜 ${result}`); // Output result with an emoji
}
} catch (error: any) {
spinner.fail(`❌ Error executing brew services ${subcommand}: ${error.message}`); // Error message with emoji
}
}
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { handleConfig } from './features/config';
import { handleCreate } from './features/create';
import { handleEdit } from './features/edit';
import { handleVersion } from './features/version';
import { handleServices } from './features/services';
const program = new Command();

program
Expand Down Expand Up @@ -141,6 +142,22 @@ program.command('uninstall <package>')
handleVersion();
});

program
.command('services <subcommand> [formula]')
.description('Manage background services with Homebrew')
.option('-a, --all', 'Run subcommand on all services')
.option('--json', 'Output as JSON')
.option('-d, --debug', 'Display any debugging information')
.option('-q, --quiet', 'Make some output more quiet')
.option('-v, --verbose', 'Make some output more verbose')
.option('--file <file>', 'Use the service file from this location to start the service')
.option('--sudo-service-user <user>', 'When run as root on macOS, run the service(s) as this user')
.option('--max-wait <seconds>', 'Wait at most this many seconds for stop to finish stopping a service')
.option('--no-wait', 'Don\'t wait for stop to finish stopping the service')
.action(async (subcommand, formula, options) => {
await handleServices(subcommand, formula, options);
});


program.parse(process.argv);

Expand Down

0 comments on commit 3f85be6

Please sign in to comment.