Skip to content

Commit

Permalink
Merge pull request #7 from KernelDeimos/master
Browse files Browse the repository at this point in the history
dev: add last-error command, context, and modules
  • Loading branch information
bitsnaps authored Feb 12, 2025
2 parents ecf3bb9 + b340cf2 commit 9685177
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"author": "Ibrahim.H",
"license": "MIT",
"dependencies": {
"@heyputer/putility": "^1.0.2",
"chalk": "^5.3.0",
"cli-table3": "^0.6.5",
"commander": "^13.0.0",
Expand Down
14 changes: 13 additions & 1 deletion src/commands/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Conf from 'conf';
import { execCommand, getPrompt } from '../executor.js';
import { getAuthToken, login } from './auth.js';
import { PROJECT_NAME } from '../commons.js';
import ErrorModule from '../modules/ErrorModule.js';
import putility, { AdvancedBase } from '@heyputer/putility';

const config = new Conf({ projectName: PROJECT_NAME });

Expand Down Expand Up @@ -32,6 +34,16 @@ export async function startShell() {
process.exit(0);
}

const modules = [
ErrorModule,
];

const context = new putility.libs.context.Context({
events: new putility.libs.event.Emitter(),
});

for ( const module of modules ) module({ context });

try {
console.log(chalk.green('Welcome to Puter-CLI! Type "help" for available commands.'));
rl.setPrompt(getPrompt());
Expand All @@ -41,7 +53,7 @@ export async function startShell() {
const trimmedLine = line.trim();
if (trimmedLine) {
try {
await execCommand(trimmedLine);
await execCommand(context, trimmedLine);
} catch (error) {
console.error(chalk.red(error.message));
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/sites.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { getCurrentUserName, getCurrentDirectory } from './auth.js';
import { API_BASE, getHeaders, generateAppName, resolvePath, isValidAppName } from '../commons.js';
import { displayNonNullValues, formatDate, isValidAppUuid } from '../utils.js';
import { getSubdomains, createSubdomain, deleteSubdomain } from './subdomains.js';
import { ErrorAPI } from '../modules/ErrorModule.js';


/**
* Listing subdomains
*/
export async function listSites(args = {}) {
export async function listSites(args = {}, context) {
try {
const data = await getSubdomains(args);

Expand Down Expand Up @@ -57,6 +58,7 @@ export async function listSites(args = {}) {
}

} catch (error) {
context.events.emit('error', { error });
console.error(chalk.red('Error listing sites:'), error.message);
throw error;
}
Expand Down
4 changes: 2 additions & 2 deletions src/commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

export const PROJECT_NAME = 'puter-cli';
export const API_BASE = 'https://api.puter.com';
export const BASE_URL = 'https://puter.com';
export const API_BASE = 'http://api.puter.localhost:4100';
export const BASE_URL = 'http://puter.localhost:4100';

/**
* Get headers with the correct Content-Type for multipart form data.
Expand Down
8 changes: 6 additions & 2 deletions src/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import inquirer from 'inquirer';
import { exec } from 'node:child_process';
import { parseArgs } from './utils.js';
import { rl } from './commands/shell.js';
import { ErrorAPI } from './modules/ErrorModule.js';

const config = new Conf({ projectName: PROJECT_NAME });

Expand Down Expand Up @@ -61,6 +62,9 @@ const commands = {
rl.write(commandToCopy);
}
},
'last-error': async (_, context) => {
context[ErrorAPI].showLast();
},
'app:create': async (rawArgs) => {
try {
const args = parseArgs(rawArgs.join(' '));
Expand Down Expand Up @@ -150,7 +154,7 @@ const commands = {
* Execute a command
* @param {string} input The command line input
*/
export async function execCommand(input) {
export async function execCommand(context, input) {
const [cmd, ...args] = input.split(' ');


Expand Down Expand Up @@ -184,7 +188,7 @@ export async function execCommand(input) {
}
if (commands[cmd]) {
try {
await commands[cmd](args);
await commands[cmd](args, context);
} catch (error) {
console.error(chalk.red(`Error executing command: ${error.message}`));
}
Expand Down
33 changes: 33 additions & 0 deletions src/modules/ErrorModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const ERROR_BUFFER_LIMIT = 20;

export const ErrorAPI = Symbol('ErrorAPI');

export default ({ context }) => {
// State Variables
const errors = [];

context.events.on('error', (error) => {
context[ErrorAPI].report(error);
});

// Module Methods
context[ErrorAPI] = {
// Add an error to the error history
report (error) {
errors.push(error);
if (errors.length > ERROR_BUFFER_LIMIT) {
errors = errors.slice(errors.length - ERROR_BUFFER_LIMIT);
}
},
// Print the last error from the error history,
// and remove it from the history
showLast () {
const err = errors.pop();
if (err) {
console.error(err);
} else {
console.log('No errors to report');
}
}
};
};

0 comments on commit 9685177

Please sign in to comment.