mirror of
https://github.com/ivabus/gui
synced 2025-04-23 14:07:14 +03:00
Co-authored-by: neil molina <neil@neils-MacBook-Pro.local>
This commit is contained in:
parent
ee85674801
commit
3acce8ac0c
6 changed files with 112 additions and 7 deletions
|
@ -11,7 +11,9 @@ import { nameToSlug } from "./libs/package";
|
|||
import { checkUpdater } from "./libs/auto-updater";
|
||||
|
||||
import initializeHandlers, { setProtocolPath } from "./libs/ipc";
|
||||
import initializePushNotification from "./libs/push-notification";
|
||||
import initializePushNotification, {
|
||||
syncPackageTopicSubscriptions
|
||||
} from "./libs/push-notification";
|
||||
|
||||
log.info("App starting...");
|
||||
if (app.isPackaged) {
|
||||
|
@ -130,6 +132,8 @@ function createMainWindow() {
|
|||
} else {
|
||||
serveURL(mainWindow);
|
||||
}
|
||||
|
||||
syncPackageTopicSubscriptions();
|
||||
}
|
||||
|
||||
if (process.defaultApp) {
|
||||
|
|
|
@ -7,6 +7,7 @@ import path from "path";
|
|||
import Pushy from "pushy-electron";
|
||||
import * as log from "electron-log";
|
||||
import { nameToSlug } from "./package";
|
||||
import { subscribeToPackageTopic } from "./push-notification";
|
||||
|
||||
export async function installPackage(full_name: string) {
|
||||
return await new Promise((resolve, reject) => {
|
||||
|
@ -27,12 +28,8 @@ export async function installPackage(full_name: string) {
|
|||
|
||||
teaInstallation.on("exit", async (code) => {
|
||||
if (code === 0) {
|
||||
const topic = `packages-${nameToSlug(full_name)}`;
|
||||
try {
|
||||
if (Pushy.isRegistered()) {
|
||||
await Pushy.subscribe(topic);
|
||||
log.info("push: registered to topic ", topic);
|
||||
}
|
||||
await subscribeToPackageTopic(full_name);
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
} finally {
|
||||
|
|
|
@ -12,6 +12,8 @@ import { installPackage, openTerminal } from "./cli";
|
|||
|
||||
import { getUpdater } from "./auto-updater";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
import { syncPackageTopicSubscriptions } from "./push-notification";
|
||||
let teaProtocolPath = ""; // this should be empty string
|
||||
|
||||
export const setProtocolPath = (path: string) => {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
export const nameToSlug = (name: string) => {
|
||||
// github.com/Pypa/twine -> github_com_pypa_twine
|
||||
const slug = name.replace(/[^\w\s]/gi, "_").toLocaleLowerCase();
|
||||
const [nameOnly] = name.split("@");
|
||||
const slug = nameOnly.replace(/[^\w\s]/gi, "_").toLocaleLowerCase();
|
||||
return slug;
|
||||
};
|
||||
|
|
|
@ -3,6 +3,12 @@ import { readSessionData } from "./auth";
|
|||
import { post } from "./v1-client";
|
||||
import * as log from "electron-log";
|
||||
import { BrowserWindow } from "electron";
|
||||
import { nameToSlug } from "./package";
|
||||
import {
|
||||
getInstalledPackages,
|
||||
getPackagesInstalledList,
|
||||
updatePackageInstalledList
|
||||
} from "./tea-dir";
|
||||
|
||||
export default function initialize(mainWindow: BrowserWindow) {
|
||||
Pushy.listen();
|
||||
|
@ -26,3 +32,64 @@ export default function initialize(mainWindow: BrowserWindow) {
|
|||
Pushy.alert(mainWindow, data?.message as string);
|
||||
});
|
||||
}
|
||||
|
||||
export async function subscribeToPackageTopic(pkgFullname: string) {
|
||||
try {
|
||||
if (Pushy.isRegistered()) {
|
||||
const slug = nameToSlug(pkgFullname);
|
||||
const topic = `packages-${slug}`;
|
||||
await Pushy.subscribe(topic);
|
||||
log.info("push: registered to pkg-topic: ", topic);
|
||||
} else {
|
||||
log.info("pushy is not registered");
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function unsubscribeToPackageTopic(pkgFullname: string) {
|
||||
try {
|
||||
if (Pushy.isRegistered()) {
|
||||
const slug = nameToSlug(pkgFullname);
|
||||
const topic = `packages-${slug}`;
|
||||
await Pushy.unsubscribe(topic);
|
||||
log.info("push: unregistered from pkg-topic: ", topic);
|
||||
} else {
|
||||
log.info("pushy is not registered");
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function syncPackageTopicSubscriptions() {
|
||||
try {
|
||||
log.info("syncing package topic subscriptions");
|
||||
const [installedPackages, lastInstalledList] = await Promise.all([
|
||||
getInstalledPackages(),
|
||||
getPackagesInstalledList()
|
||||
]);
|
||||
|
||||
const previouslyInstalledNames = lastInstalledList.map((pkg) => pkg.full_name);
|
||||
const currentlyInstalledNames = installedPackages.map((pkg) => pkg.full_name);
|
||||
|
||||
const subscribedTo = currentlyInstalledNames.filter(
|
||||
(pkg) => !previouslyInstalledNames.includes(pkg)
|
||||
);
|
||||
const unsubscribedFrom = previouslyInstalledNames.filter(
|
||||
(pkg) => !currentlyInstalledNames.includes(pkg)
|
||||
);
|
||||
|
||||
for (const subscribe of subscribedTo) {
|
||||
await subscribeToPackageTopic(subscribe);
|
||||
}
|
||||
for (const unsubscribe of unsubscribedFrom) {
|
||||
await unsubscribeToPackageTopic(unsubscribe);
|
||||
}
|
||||
|
||||
await updatePackageInstalledList(installedPackages);
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import semver from "semver";
|
|||
import * as log from "electron-log";
|
||||
import type { InstalledPackage } from "../../src/libs/types";
|
||||
import semverCompare from "semver/functions/compare";
|
||||
import { mkdirp } from "mkdirp";
|
||||
|
||||
type Dir = {
|
||||
name: string;
|
||||
|
@ -19,6 +20,8 @@ export const getTeaPath = () => {
|
|||
return teaPath;
|
||||
};
|
||||
|
||||
const guiFolder = path.join(getTeaPath(), "tea.xyz/gui");
|
||||
|
||||
export const getGuiPath = () => {
|
||||
return path.join(getTeaPath(), "tea.xyz/gui");
|
||||
};
|
||||
|
@ -116,3 +119,34 @@ export const deepReadDir = async ({
|
|||
}
|
||||
return arrayOfFiles;
|
||||
};
|
||||
|
||||
const listFilePath = path.join(getGuiPath(), "installed.json");
|
||||
export const getPackagesInstalledList = async (): Promise<InstalledPackage[]> => {
|
||||
let list: InstalledPackage[] = [];
|
||||
try {
|
||||
if (fs.existsSync(listFilePath)) {
|
||||
log.info("gui/installed.json file exists!");
|
||||
const listBuffer = await fs.readFileSync(listFilePath);
|
||||
list = JSON.parse(listBuffer.toString()) as InstalledPackage[];
|
||||
} else {
|
||||
log.info("gui/installed.json does not exists!");
|
||||
await mkdirp(guiFolder);
|
||||
await updatePackageInstalledList([]);
|
||||
}
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
return list;
|
||||
};
|
||||
|
||||
export async function updatePackageInstalledList(list: InstalledPackage[]) {
|
||||
try {
|
||||
log.info("creating:", listFilePath);
|
||||
await mkdirp(guiFolder);
|
||||
await fs.writeFileSync(listFilePath, JSON.stringify(list), {
|
||||
encoding: "utf-8"
|
||||
});
|
||||
} catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue