Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
WIP: Add a PuterAppCommandProvider for launching apps from the terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
AtkinsSJ committed Apr 3, 2024
1 parent 3029cc1 commit 2219ccb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/puter-shell/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { BetterReader } from 'dev-pty';
import { MultiWriter } from '../ansi-shell/ioutil/MultiWriter.js';
import { CompositeCommandProvider } from './providers/CompositeCommandProvider.js';
import { ScriptCommandProvider } from './providers/ScriptCommandProvider.js';
import { PuterAppCommandProvider } from './providers/PuterAppCommandProvider.js';

const argparser_registry = {
[SimpleArgParser.name]: SimpleArgParser
Expand Down Expand Up @@ -82,6 +83,8 @@ export const launchPuterShell = async (ctx) => {
// const commandProvider = new BuiltinCommandProvider();
const commandProvider = new CompositeCommandProvider([
new BuiltinCommandProvider(),
// PuterAppCommandProvider is only usable on Puter
...(ctx.platform.name === 'puter' ? [new PuterAppCommandProvider()] : []),
new ScriptCommandProvider(),
]);

Expand Down
69 changes: 69 additions & 0 deletions src/puter-shell/providers/PuterAppCommandProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Phoenix Shell.
*
* Phoenix Shell is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const BUILTIN_APPS = [
'explorer',
];

function launch(name, path) {
return {
name,
path,
// TODO: Parameters and options?
async execute(ctx) {
console.log(`%cTrying to launch app: ${name}`, 'color:green');
const args = {}; // TODO: Passed-in parameters and options would go here
await puter.ui.launchApp(name, args);
}
};
}

export class PuterAppCommandProvider {

async lookup (id) {
// Built-in apps will not be returned by the fetch query below, so we handle them separately.
if (BUILTIN_APPS.includes(id)) {
return launch(id, 'Built-in Puter app');
}

const request = await fetch(`${puter.APIOrigin}/drivers/call`, {
"headers": {
"Content-Type": "application/json",
"Authorization": `Bearer ${puter.authToken}`,
},
"body": JSON.stringify({ interface: 'puter-apps', method: 'read', args: { id: { name: id } } }),
"method": "POST",
});

const { success, result } = await request.json();

if (!success) return;

const { name, index_url } = result;
return launch(name, index_url);
}

// Only a single Puter app can match a given name
async lookupAll (...a) {
const result = await this.lookup(...a);
if ( result ) {
return [ result ];
}
return undefined;
}
}

0 comments on commit 2219ccb

Please sign in to comment.