Skip to content

Commit

Permalink
foo (#2)
Browse files Browse the repository at this point in the history
* fixed typo and improved function

* fixed typo and improved function

* fixed typo and improved function

* fixed typo and improved function

* fixed typo and improved function

* fixed typo and improved function

* fixed typo and improved function

* fixed typo and improved function

* fixed typo and improved function

* Changes to be committed:
	modified:   .gitignore

* Add flake for brewy project with local package

- Created flake.nix to define the flake structure.
- Included default.nix for the local package definition.
- Used callPackage to integrate the local package into the flake.
- Set up default package for easy building and running.

* removed

* Changes to be committed:
	modified:   flake.nix

* updated and implemented new feature
  • Loading branch information
transgirllucy authored Dec 5, 2024
1 parent 69803ea commit d55083f
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 62 deletions.
42 changes: 42 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,45 @@ brewy-darwin-arm64
brewy-darwin-x64
brewy-linux
brewy-windows.exe

# Node.js dependencies
node_modules/

# Build output
dist/
.bun-build*

# Binaries
brewy
brewy-darwin-arm64
brewy-darwin-x64
brewy-linux
brewy-windows.exe

# Lock files
bun.lockb

# Logs
*.log

# OS generated files
.DS_Store
Thumbs.db

# Temporary files
*.tmp
*.temp

# Archive files
*.tar.gz

# IDE specific files
.vscode/
.idea/
*.sublime-project
*.sublime-workspace

# Environment variables
.env

.*.bun-build
23 changes: 0 additions & 23 deletions brewy.rb

This file was deleted.

48 changes: 48 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{ pkgs ? import <nixpkgs> {} }:

let
# Define the version and the base URL for the binaries
version = "1.0.0";
baseUrl = "https://github.com/transgirllucy/brewy/releases/download/${version}";

# Function to determine the binary URL and SHA256 based on the system
getBinaryInfo = pkgs: {
inherit (pkgs.stdenv) isDarwin isAarch64;

url = if isDarwin then
if isAarch64 then "${baseUrl}/brewy-darwin-arm64"
else "${baseUrl}/brewy-darwin-x64"
else "${baseUrl}/brewy-linux-x64";

sha256 = if isDarwin then
if isAarch64 then "672e8250fe54a41675f029eadb053bb238268ecda93bbe4803503c8a523a1726"
else "653192ecfa71ce38a31dc1bc57729c278e4cdcb891560d20c58c326b7bb61df3"
else "7dc8659cd61fe62f68293de13f53168c83bb77dd3294ba50cdb9d1e6f721e8c2";
};

binaryInfo = getBinaryInfo pkgs;

in

pkgs.stdenv.mkDerivation {
pname = "brewy";
inherit version;

src = pkgs.fetchurl {
url = binaryInfo.url;
sha256 = binaryInfo.sha256;
};

installPhase = ''
mkdir -p $out/bin
cp $src $out/bin/brewy
chmod +x $out/bin/brewy
'';

meta = with pkgs.lib; {
description = "A description of your project";
homepage = "https://github.com/transgirllucy/brewy";
license = licenses.mit; # Adjust this to the actual license of your project
maintainers = with maintainers; [ yourName ]; # Replace with your name or GitHub handle
};
}
22 changes: 22 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
description = "A flake for the brewy project";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};

outputs = { self, nixpkgs }: let
# Import the local package
brewy = nixpkgs.callPackage ./default.nix {};

in {
# Expose the package for building and running
packages.x86_64-linux = brewy;
packages.darwin = brewy;

# Default app to run
defaultPackage.x86_64-linux = brewy;
defaultPackage.aarch64-linux = brewy;
defaultPackage.darwin = brewy;
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "brewy",
"version": "1.0.0",
"module": "src/index.ts",
"type": "module",
"scripts": {
Expand Down
14 changes: 8 additions & 6 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { exec } from 'child_process';
import { consola } from 'consola';
import consola from 'consola';

export function runCommand(command: string): Promise<string> {
export function runCommand(command: string, options?: { cwd?: string }): Promise<string> {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
exec(command, options, (error, stdout, stderr) => {
if (error) {
reject(`❌ Error: ${stderr}`);
consola.error(`❌ Error executing command: "${command}"\n${stderr}`);
reject(`Error: ${stderr}`);
} else {
resolve(`✅ Success: ${stdout}`);
// consola.success(`✅ Command executed successfully: "${command}"`);
resolve(stdout.toString().trim());
}
});
});
}
}
22 changes: 19 additions & 3 deletions src/features/info.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { consola } from "consola";
import { runCommand } from "../command";

async function handleInfo(packageName: string, options: { analytics?: boolean; json?: boolean; quiet?: boolean }) {
interface InfoOptions {
analytics?: boolean;
json?: boolean;
quiet?: boolean;
}

async function handleInfo(packageName: string, options: InfoOptions) {
try {
// Construct the command based on options
let command = `brew info ${packageName}`;
Expand All @@ -14,11 +20,21 @@ async function handleInfo(packageName: string, options: { analytics?: boolean; j
if (options.quiet) {
command += ' --quiet';
}

const result = await runCommand(command);
if (!options.quiet) {

// Handle output based on options
if (options.json) {
try {
const jsonData = JSON.parse(result);
consola.info(`📦 Information for "${packageName}":\n`, jsonData);
} catch (jsonError: any) {
consola.error(`Error parsing JSON output for "${packageName}": ${jsonError.message}`);
}
} else if (!options.quiet) {
consola.info(`📦 Information for "${packageName}":\n${result}`);
}
} catch (error: any) {
consola.error(`Error retrieving information for "${packageName}": ${error.message}`);
consola.error(`Error retrieving information for "${packageName}": ${error.message}`);
}
}
28 changes: 21 additions & 7 deletions src/features/install_package.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { runCommand } from '../command';
import { consola } from 'consola';
import ora from 'ora';
export async function handleInstall(packageName: string, options: { quiet?: boolean; force?: boolean; verbose?: boolean; dryRun?: boolean; }) {

interface InstallOptions {
quiet?: boolean;
force?: boolean;
verbose?: boolean;
dryRun?: boolean;
}

export async function handleInstall(packageName: string, options: InstallOptions) {
try {
let command = `brew install ${packageName}`;
if (options.force) {
Expand All @@ -16,24 +24,30 @@ export async function handleInstall(packageName: string, options: { quiet?: bool
if (options.quiet) {
command += ' --quiet';
}
const confirmation = await consola.prompt(`Do you want to install the following packages ${packageName}`, {

const confirmation = await consola.prompt(`Do you want to install the following package: "${packageName}"?`, {
type: "confirm",
});

if (confirmation) {
const spinner = ora(`🔄 Installing ${packageName}...`).start(); // Start the spinner after confirmation

const result = await runCommand(command);
spinner.succeed(`✅ Successfully installed "${packageName}":\n${result}`);
if (!options.quiet) {
consola.info(`✅ Successfully installed "${packageName}":\n${result}`);
try {
const result = await runCommand(command);
spinner.succeed(`✅ Successfully installed "${packageName}":\n${result}`);
if (!options.quiet) {
consola.info(`✅ Successfully installed "${packageName}":\n${result}`);
}
} catch (installError: any) {
spinner.fail(`❌ Failed to install "${packageName}": ${installError.message}`);
consola.error(`❌ Error details: ${installError.message}`);
}
} else {
consola.warn('❌ Installation cancelled.');
process.exit(1);
}

} catch (error: any) {
consola.error(`❌ Error installing "${packageName}": ${error.message}`);
consola.error(`❌ Error during installation process: ${error.message}`);
}
}
28 changes: 23 additions & 5 deletions src/features/search.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import ora from "ora";
import { runCommand } from "../command";
import { consola } from "consola";

export async function searchPackage({ term, options }: { term: string; options: { formula?: boolean; cask?: boolean; desc?: boolean; quiet?: boolean } }) {
interface SearchOptions {
formula?: boolean;
cask?: boolean;
desc?: boolean;
quiet?: boolean;
}

export async function searchPackage({ term, options }: { term: string; options: SearchOptions }) {
try {
if (!term) {
consola.warn('❌ Please provide a search term.');
return;
}

let command = `brew search `;
if (term.startsWith('/') && term.endsWith('/')) {
command += term;
command += term; // Use regex search
} else {
command += term.replace(/ /g, '\\ '); // Escape spaces in the term
}
Expand All @@ -26,11 +39,16 @@ 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) {
consola.error(error);
} catch (error: any) {
consola.error(`❌ Error searching for package "${term}": ${error.message}`);
}
}
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
}
}
Loading

0 comments on commit d55083f

Please sign in to comment.