From e1bdee1abe04e2776c703faa79bb1edabf9f0e8b Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Fri, 10 Jan 2025 16:24:08 -0500 Subject: [PATCH] dev: ability to run api tests from puter terminal --- .../development/LocalTerminalService.js | 17 +++++- .../src/puter-shell/coreutils/__exports__.js | 2 + .../src/puter-shell/coreutils/localsh.js | 55 +++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/phoenix/src/puter-shell/coreutils/localsh.js diff --git a/src/backend/src/modules/development/LocalTerminalService.js b/src/backend/src/modules/development/LocalTerminalService.js index 3fc2016a..205ced9b 100644 --- a/src/backend/src/modules/development/LocalTerminalService.js +++ b/src/backend/src/modules/development/LocalTerminalService.js @@ -42,7 +42,11 @@ class LocalTerminalService extends BaseService { '../../../../../', 'tools/api-tester', ), - shell: ['/usr/bin/env', 'node', 'apitest.js'], + shell: [ + '/usr/bin/env', 'node', + 'apitest.js', + '--config=config.yml' + ], allow_args: true, }, }; @@ -83,7 +87,7 @@ class LocalTerminalService extends BaseService { const profile = profiles[req.body.profile]; const args = profile.shell.slice(1); - if ( ! profile.allow_args && req.body.args ) { + if ( profile.allow_args && req.body.args ) { args.push(...req.body.args); } const proc = spawn(profile.shell[0], args, { @@ -129,6 +133,15 @@ class LocalTerminalService extends BaseService { proc.on('exit', () => { this.log.noticeme(`[${term_uuid}] Process exited (${proc.exitCode})`); delete this.sessions_[term_uuid]; + + const svc_socketio = req.services.get('socketio'); + svc_socketio.send( + { room: req.user.id }, + 'local-terminal.exit', + { + term_uuid, + }, + ); }); this.sessions_[term_uuid] = { diff --git a/src/phoenix/src/puter-shell/coreutils/__exports__.js b/src/phoenix/src/puter-shell/coreutils/__exports__.js index 4d4b8765..f351004e 100644 --- a/src/phoenix/src/puter-shell/coreutils/__exports__.js +++ b/src/phoenix/src/puter-shell/coreutils/__exports__.js @@ -60,6 +60,7 @@ import module_txt2img from './txt2img.js' import module_usages from './usages.js' import module_wc from './wc.js' import module_which from './which.js' +import module_localsh from './localsh.js' export default { "ai": module_ai, @@ -105,4 +106,5 @@ export default { "usages": module_usages, "wc": module_wc, "which": module_which, + "localsh": module_localsh, }; diff --git a/src/phoenix/src/puter-shell/coreutils/localsh.js b/src/phoenix/src/puter-shell/coreutils/localsh.js new file mode 100644 index 00000000..80f9302e --- /dev/null +++ b/src/phoenix/src/puter-shell/coreutils/localsh.js @@ -0,0 +1,55 @@ +import { TeePromise } from "@heyputer/putility/src/libs/promise"; + +export default { + name: 'localsh', + usage: 'localsh ', + description: 'Run a local shell script.', + args: { + $: 'simple-parser', + allowPositionals: true, + }, + // output: 'text', + execute: async ctx => { + const { puterSDK } = ctx.externs; + + const resp = await fetch(`${puterSDK.APIOrigin}/local-terminal/new`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${puterSDK.authToken}` + }, + body: JSON.stringify({ + profile: ctx.locals.positionals[0], + args: ctx.locals.positionals.slice(1), + }) + }); + + const convert = atob; + + const { term_uuid } = await resp.json(); + const fn_stdout = ({ term_uuid: term_uuid_, base64 }) => { + if ( term_uuid !== term_uuid_ ) return; + ctx.externs.err.write(convert(base64)); + } + puterSDK.fs.socket.on('local-terminal.stdout', fn_stdout); + const fn_stderr = ({ term_uuid: term_uuid_, base64 }) => { + if ( term_uuid !== term_uuid_ ) return; + ctx.externs.err.write(convert(base64)); + } + puterSDK.fs.socket.on('local-terminal.stderr', fn_stderr); + + const p = new TeePromise(); + + const fn_exit = ({ term_uuid: term_uuid_ }) => { + if ( term_uuid !== term_uuid_ ) return; + puterSDK.fs.socket.off('local-terminal.exit', fn_exit); + puterSDK.fs.socket.off('local-terminal.stdout', fn_stdout); + puterSDK.fs.socket.off('local-terminal.stderr', fn_stderr); + p.resolve(); + + }; + puterSDK.fs.socket.on('local-terminal.exit', fn_exit); + + await p; + } +}