From 5f91aee7e525e96301f4b843793b9c992fb22825 Mon Sep 17 00:00:00 2001 From: Matheus Benachio Date: Mon, 4 Jul 2022 12:06:57 -0300 Subject: [PATCH 1/2] feat: added 'start box' on cli daemon commands --- packages/tcore-api/src/Helpers/log.ts | 27 ++++++++++++++++++++++ packages/tcore-api/src/index.ts | 3 ++- packages/tcore-api/src/server.ts | 15 +++--------- packages/tcore-cli/src/Commands/Restart.ts | 1 + packages/tcore-cli/src/Commands/Start.ts | 4 +++- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/packages/tcore-api/src/Helpers/log.ts b/packages/tcore-api/src/Helpers/log.ts index fbc8fadf..75f94558 100644 --- a/packages/tcore-api/src/Helpers/log.ts +++ b/packages/tcore-api/src/Helpers/log.ts @@ -2,8 +2,13 @@ import { ESocketRoom } from "@tago-io/tcore-sdk/types"; import chalk from "chalk"; import ora from "ora"; import { getSystemName } from "@tago-io/tcore-shared"; +import boxen from "boxen"; +import { getMainSettings } from "../Services/Settings"; import { plugins } from "../Plugins/Host"; import { io } from "../Socket/SocketServer"; +import { getLocalIPs } from "../Services/Hardware"; +// @ts-ignore +import pkg from "../../../../package.json"; /** * Contains a history of all logs in the application and in the plugins. @@ -79,3 +84,25 @@ export function internalLog(channel: string, ...args: any[]) { console.log(colored, ...args); } } + +/** + * Logs the "start box" with the localhost and internal ips. + */ +export async function logSystemStart(port?: number | string) { + const systemName = getSystemName(); + const systemVers = pkg?.version; + + const settings = await getMainSettings(); + const realPort = port || settings.port; + + const lines = [`${systemName} v${systemVers} is ready!`, "", `- Local: http://localhost:${realPort}`]; + + const netAddresses = getLocalIPs(); + if (netAddresses[0]) { + lines.push(`- On your network: http://${netAddresses[0]}:${realPort}`); + } + + console.log(""); + console.log(chalk.green(boxen(lines.join("\n"), { padding: 1 }))); + console.log(""); +} diff --git a/packages/tcore-api/src/index.ts b/packages/tcore-api/src/index.ts index e1f2049e..e2bcb5e4 100644 --- a/packages/tcore-api/src/index.ts +++ b/packages/tcore-api/src/index.ts @@ -1,7 +1,7 @@ import { installPlugin } from "./Plugins/Install"; import { uninstallPlugin, uninstallPluginByFolder } from "./Plugins/Uninstall"; import { startServer } from "./server"; -import { log, logError } from "./Helpers/log"; +import { log, logError, logSystemStart } from "./Helpers/log"; import { getPlatformAndArch } from "./Helpers/Platform"; import { downloadFile } from "./Helpers/Download"; import Plugin from "./Plugins/Plugin/Plugin"; @@ -16,6 +16,7 @@ export { installPlugin, log, logError, + logSystemStart, Plugin, startServer, uninstallPlugin, diff --git a/packages/tcore-api/src/server.ts b/packages/tcore-api/src/server.ts index 71f9e91a..c7ab014f 100644 --- a/packages/tcore-api/src/server.ts +++ b/packages/tcore-api/src/server.ts @@ -1,7 +1,6 @@ import { createServer } from "http"; import path from "path"; import express from "express"; -import boxen from "boxen"; import cors from "cors"; import chalk from "chalk"; import ora from "ora"; @@ -25,10 +24,10 @@ import { getMainSettings } from "./Services/Settings"; import HardwareController from "./Controllers/Hardware"; import { setupSocketServer } from "./Socket/SocketServer"; import { shutdown } from "./Helpers/shutdown"; -import { getLocalIPs } from "./Services/Hardware"; import { getModuleList } from "./Services/Plugins"; import { startCallbackInterval } from "./Plugins/Worker/Worker"; import { startActionScheduleTimer } from "./Services/ActionScheduler"; +import { logSystemStart } from "./Helpers/log"; const app = express(); const httpServer = createServer(app); @@ -141,19 +140,11 @@ async function listenOnApplicationPort() { httpServer .listen(port, () => { - const lines = [`${systemName} is ready!`, "", `- Local: http://localhost:${port}`]; - - const netAddresses = getLocalIPs(); - if (netAddresses[0]) { - lines.push(`- On your network: http://${netAddresses[0]}:${port}`); - } - - console.log(""); - console.log(chalk.green(boxen(lines.join("\n"), { padding: 1 }))); - console.log(""); + logSystemStart(); }) .on("error", () => { ora(chalk.redBright(`Could not start ${systemName} on port ${port}`)).fail(); + ora(chalk.redBright(`Check if the port is not being used by another application`)).fail(); process.exit(1); }); } diff --git a/packages/tcore-cli/src/Commands/Restart.ts b/packages/tcore-cli/src/Commands/Restart.ts index 166230a8..bd0e7fd3 100644 --- a/packages/tcore-cli/src/Commands/Restart.ts +++ b/packages/tcore-cli/src/Commands/Restart.ts @@ -25,6 +25,7 @@ export async function restart() { const newApp = await pm2GetApp(); if (newApp) { log(`${getSystemName()} Server was ${chalk.green("successfully restarted")} with PID`, newApp.pid); + API.logSystemStart((app?.pm2_env as any)?.TCORE_PORT); } else { log(`${getSystemName()} Server ${chalk.redBright("could not be started")}.`); log(`You can check the logs by running ${chalk.cyan("tcore logs")}`); diff --git a/packages/tcore-cli/src/Commands/Start.ts b/packages/tcore-cli/src/Commands/Start.ts index 4636dc76..51b85382 100644 --- a/packages/tcore-cli/src/Commands/Start.ts +++ b/packages/tcore-cli/src/Commands/Start.ts @@ -56,6 +56,7 @@ async function startWithDaemon(opts: IStartOptions) { try { const systemName = getSystemName(); const exeName = path.basename(process.execPath); + const env = getEnv(opts); await pm2Connect(); @@ -86,7 +87,7 @@ async function startWithDaemon(opts: IStartOptions) { name: PM2_APP_NAME, output: pm2LogPath, error: pm2LogPath, - env: getEnv(opts), + env, }; await pm2Start(startOptions); @@ -95,6 +96,7 @@ async function startWithDaemon(opts: IStartOptions) { if (newApp?.pm2_env?.status === "online") { // successfully started new process log(`${systemName} Server successfully ${chalk.green("started")} with PID`, newApp.pid); + API.logSystemStart(env.TCORE_PORT); } else { // for some reason pm2 could not start the script log(`${systemName} Server ${chalk.redBright("could not be started")}.`); From e4d010f42daaa39686ade4b24829e191a087096a Mon Sep 17 00:00:00 2001 From: Matheus Benachio Date: Mon, 4 Jul 2022 15:13:09 -0300 Subject: [PATCH 2/2] fix: 'any' type in restart call --- packages/tcore-api/src/Helpers/log.ts | 2 +- packages/tcore-cli/src/Commands/Restart.ts | 9 ++++++++- packages/tcore-sdk/src/Types/Common/Common.types.ts | 12 ++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/tcore-api/src/Helpers/log.ts b/packages/tcore-api/src/Helpers/log.ts index 75f94558..047f254e 100644 --- a/packages/tcore-api/src/Helpers/log.ts +++ b/packages/tcore-api/src/Helpers/log.ts @@ -88,7 +88,7 @@ export function internalLog(channel: string, ...args: any[]) { /** * Logs the "start box" with the localhost and internal ips. */ -export async function logSystemStart(port?: number | string) { +export async function logSystemStart(port?: number | string | null) { const systemName = getSystemName(); const systemVers = pkg?.version; diff --git a/packages/tcore-cli/src/Commands/Restart.ts b/packages/tcore-cli/src/Commands/Restart.ts index bd0e7fd3..4c55eed3 100644 --- a/packages/tcore-cli/src/Commands/Restart.ts +++ b/packages/tcore-cli/src/Commands/Restart.ts @@ -2,6 +2,7 @@ import path from "path"; import fs from "fs"; import * as API from "@tago-io/tcore-api"; import chalk from "chalk"; +import { IEnvironmentVariables } from "@tago-io/tcore-sdk/types"; import { getSystemName } from "@tago-io/tcore-shared"; import { pm2Connect, pm2Disconnect, pm2Restart, pm2GetApp } from "../Helpers/PM2"; import { log } from "../Helpers/Log"; @@ -20,12 +21,18 @@ export async function restart() { const pm2LogPath = path.join(settingsPath, "tcore.log"); await fs.promises.unlink(pm2LogPath).catch(() => null); + // generate a type to access TCORE_PORT without `any` + type Pm2TCoreEnvironment = typeof app.pm2_env & IEnvironmentVariables; + + // store the env in a variable to make it easier to access + const env = app.pm2_env as Pm2TCoreEnvironment; + await pm2Restart(); const newApp = await pm2GetApp(); if (newApp) { log(`${getSystemName()} Server was ${chalk.green("successfully restarted")} with PID`, newApp.pid); - API.logSystemStart((app?.pm2_env as any)?.TCORE_PORT); + API.logSystemStart(env?.TCORE_PORT); } else { log(`${getSystemName()} Server ${chalk.redBright("could not be started")}.`); log(`You can check the logs by running ${chalk.cyan("tcore logs")}`); diff --git a/packages/tcore-sdk/src/Types/Common/Common.types.ts b/packages/tcore-sdk/src/Types/Common/Common.types.ts index daecb3c6..4d1cbb72 100644 --- a/packages/tcore-sdk/src/Types/Common/Common.types.ts +++ b/packages/tcore-sdk/src/Types/Common/Common.types.ts @@ -121,6 +121,18 @@ export const zTagsAutoGen = z .nullish() .transform((e) => e ?? []); // use transform instead of `default` because `default` doesn't apply to `null`. +/** + * Environment variables for the application. + */ +export const zEnvironmentVariables = z.object({ + TCORE_PORT: z.string().nullish(), + TCORE_DAEMON: z.string().nullish(), + TCORE_PLUGIN_FOLDER: z.string().nullish(), + TCORE_SETTINGS_FOLDER: z.string().nullish(), + TCORE_DATABASE_PLUGIN: z.string().nullish(), +}); + +export type IEnvironmentVariables = z.infer; export type TGenericID = string; export type TGenericToken = string; export type TDate = Date | number | null;