Procházet zdrojové kódy

dev: ability to run api tests from puter terminal

KernelDeimos před 4 měsíci
rodič
revize
e1bdee1abe

+ 15 - 2
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] = {

+ 2 - 0
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,
 };

+ 55 - 0
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 <PROFILE>',
+    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;
+    }
+}