From e61c09b5856a08ed825fb51d0f497cf0365bf10a Mon Sep 17 00:00:00 2001 From: neil Date: Wed, 8 Feb 2023 08:44:23 +0800 Subject: [PATCH] #186 install package invoke handlr --- modules/desktop/electron/electron.ts | 6 +++++ modules/desktop/electron/libs/cli.ts | 29 +++++++++++++++++++++ modules/desktop/src/libs/cli.ts | 34 +++---------------------- modules/desktop/src/libs/stores/auth.ts | 6 ++--- 4 files changed, 41 insertions(+), 34 deletions(-) create mode 100644 modules/desktop/electron/libs/cli.ts diff --git a/modules/desktop/electron/electron.ts b/modules/desktop/electron/electron.ts index 3538e77..42782a2 100644 --- a/modules/desktop/electron/electron.ts +++ b/modules/desktop/electron/electron.ts @@ -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 @@ -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; +}); diff --git a/modules/desktop/electron/libs/cli.ts b/modules/desktop/electron/libs/cli.ts new file mode 100644 index 0000000..d478edb --- /dev/null +++ b/modules/desktop/electron/libs/cli.ts @@ -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)); + } + }); + }); +} diff --git a/modules/desktop/src/libs/cli.ts b/modules/desktop/src/libs/cli.ts index cb71432..d94f2a1 100644 --- a/modules/desktop/src/libs/cli.ts +++ b/modules/desktop/src/libs/cli.ts @@ -1,32 +1,6 @@ +const { ipcRenderer } = window.require('electron'); + 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(); - // } - // }; - - // 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); - }); + const res = await ipcRenderer.invoke('install-package', { full_name }); + console.log('install:', res); } diff --git a/modules/desktop/src/libs/stores/auth.ts b/modules/desktop/src/libs/stores/auth.ts index 434d899..1721418 100644 --- a/modules/desktop/src/libs/stores/auth.ts +++ b/modules/desktop/src/libs/stores/auth.ts @@ -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';