Skip to content

Commit

Permalink
#186 install package invoke handlr
Browse files Browse the repository at this point in the history
  • Loading branch information
neil authored and neil committed Feb 8, 2023
1 parent 61ecc1e commit e61c09b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
6 changes: 6 additions & 0 deletions modules/desktop/electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import fs from 'fs';
import { getInstalledPackages } from './libs/teaDir';
import { readSessionData, writeSessionData } from './libs/auth';
import type { Session } from '../src/libs/types';
import { installPackage } from './libs/cli';

// try {
// //@ts-ignore only used in dev should not be packaged inprod
Expand Down Expand Up @@ -127,3 +128,8 @@ ipcMain.handle('get-session', async () => {
ipcMain.handle('update-session', async (_, data) => {
await writeSessionData(data as Session);
});

ipcMain.handle('install-package', async (_, data) => {
const result = await installPackage(data.full_name);
return result;
});
29 changes: 29 additions & 0 deletions modules/desktop/electron/libs/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { spawn } from 'child_process';
import { clean } from 'semver';

export async function installPackage(full_name: string) {
return await new Promise((resolve, reject) => {
let version = '';
let lastError = '';
const teaInstallation = spawn('tea', [`+${full_name}`, 'true']);

teaInstallation.stdout.on('data', (data) => {
console.log('stdout:', data);
});

teaInstallation.stderr.on('data', (err) => {
lastError = err.toString();
if (lastError && lastError.includes('installed') && lastError.includes(full_name)) {
version = lastError.split('/').pop() || '';
}
});

teaInstallation.on('exit', (code) => {
if (code === 0) {
resolve({ version: clean(version) });
} else {
reject(new Error(lastError));
}
});
});
}
34 changes: 4 additions & 30 deletions modules/desktop/src/libs/cli.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
export async function installPackageCommand(full_name: string) {
return new Promise((resolve, reject) => {
// const teaInstallCommand = new Command('tea-install', [`+${full_name}`, 'true']);
// teaInstallCommand.on('error', reject);

// const handleLineOutput = async (line: string | { code: number }) => {
// const c = await child;
// if (
// (typeof line === 'string' && line.includes('installed:')) ||
// (typeof line !== 'string' && line?.code === 0)
// ) {
// c.kill();
// resolve(c.pid);
// } else if (typeof line !== 'string' && line?.code === 1) {
// reject();
// }
// };
const { ipcRenderer } = window.require('electron');

// teaInstallCommand.stdout.on('data', handleLineOutput);
// teaInstallCommand.stderr.on('data', handleLineOutput);
// teaInstallCommand.on('close', (line: string) => {
// console.log('command closed!');
// handleLineOutput(line || '');
// });
// teaInstallCommand.on('error', (line: string) => {
// console.log('command error!', line);
// handleLineOutput(line || '');
// });
// const child = teaInstallCommand.spawn();
setTimeout(resolve, 10000);
});
export async function installPackageCommand(full_name: string) {
const res = await ipcRenderer.invoke('install-package', { full_name });
console.log('install:', res);
}
6 changes: 2 additions & 4 deletions modules/desktop/src/libs/stores/auth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { writable } from 'svelte/store';
// import { app } from 'electron';
// import { join } from 'upath';
// import fs from 'fs';
import { getDeviceAuth, registerDevice } from '@api';

import { getDeviceAuth } from '@api';
import type { Developer } from '@tea/ui/types';
import type { Session } from '$libs/types';

Expand Down

0 comments on commit e61c09b

Please sign in to comment.