From 7bdd922b51ff8d56d6e25a4a027bb7a9141dbeee Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sat, 13 Feb 2021 14:24:50 +0200 Subject: [PATCH] some cleanup --- package.json | 2 +- src/channel/index.ts | 9 ++----- src/plugin/manager.ts | 21 +++++---------- src/plugin/repository/manager.ts | 44 +++++++++----------------------- src/util/run.ts | 6 ++--- 5 files changed, 24 insertions(+), 58 deletions(-) diff --git a/package.json b/package.json index 2735dc6..1212dba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@squeebot/core", - "version": "3.1.1", + "version": "3.2.0", "description": "Squeebot v3 core for the execution environment", "main": "lib/index.js", "module": "lib/", diff --git a/src/channel/index.ts b/src/channel/index.ts index 141e9aa..08d0851 100644 --- a/src/channel/index.ts +++ b/src/channel/index.ts @@ -95,13 +95,8 @@ export class ChannelManager { } } - public getChannelByName(name: string): IChannel | null { - for (const chan of this.channels) { - if (chan.name === name) { - return chan; - } - } - return null; + public getChannelByName(name: string): IChannel | undefined { + return this.channels.find(c => c.name === name); } public addChannel(chan: IChannel): IChannel { diff --git a/src/plugin/manager.ts b/src/plugin/manager.ts index 048c684..db7d273 100644 --- a/src/plugin/manager.ts +++ b/src/plugin/manager.ts @@ -11,7 +11,7 @@ import { NPMExecutor } from '../npm/executor'; import { logger } from '../core/logger'; -export function requireNoCache(file: string): object | null { +export function requireNoCache(file: string): object | undefined { const fullPath = path.resolve(file); const mod = require(fullPath); if (require.cache && require.cache[fullPath]) { @@ -34,28 +34,19 @@ export class PluginManager { this.addEvents(); } - public getAvailableByName(name: string): IPluginManifest | null { - for (const pl of this.availablePlugins) { - if (pl.name === name) { - return pl; - } - } - return null; + public getAvailableByName(name: string): IPluginManifest | undefined { + return this.availablePlugins.find(p => p.name === name); } - public getLoadedByName(name: string): IPlugin | null { + public getLoadedByName(name: string): IPlugin | undefined { if (this.plugins.has(name)) { return this.plugins.get(name) as IPlugin; } - return null; + return; } public getLoaded(): IPlugin[] { - const list = []; - for (const pl of this.plugins.values()) { - list.push(pl); - } - return list; + return Array.from(this.plugins.values()); } public addAvailable(manifest: IPluginManifest | IPluginManifest[]): boolean { diff --git a/src/plugin/repository/manager.ts b/src/plugin/repository/manager.ts index 99540b7..4eae623 100644 --- a/src/plugin/repository/manager.ts +++ b/src/plugin/repository/manager.ts @@ -17,25 +17,14 @@ export class RepositoryManager { constructor(private env: IEnvironment, private plugins: PluginManager) {} - public repoProvidesPlugin(repo: IRepository, mf: IPluginManifest): IRepoPluginDef | null { - for (const plugin of repo.plugins) { - if (plugin.name === mf.name && mf.repository === repo.name) { - return plugin; - } - } - - return null; + public repoProvidesPlugin(repo: IRepository, mf: IPluginManifest): IRepoPluginDef | undefined { + return repo.plugins.find(plugin => plugin.name === mf.name && repo.name === mf.repository); } - public findRepoForPlugin(pname: string): IRepository | null { - for (const [name, repo] of this.repositories) { - for (const plugin of repo.plugins) { - if (plugin.name === pname) { - return repo; - } - } - } - return null; + public findRepoForPlugin(pname: string): IRepository | undefined { + return Array.from(this.repositories.values()).find(repo => + repo.plugins.find(plugin => plugin.name === pname) !== undefined + ); } public getRepoByName(name: string): IRepository | undefined { @@ -43,11 +32,7 @@ export class RepositoryManager { } public getAll(): IRepository[] { - const list = []; - for (const irep of this.repositories.values()) { - list.push(irep); - } - return list; + return Array.from(this.repositories.values()); } public async installPlugin(name: string): Promise { @@ -192,18 +177,13 @@ export class RepositoryManager { // Checking for version differences in the plugins // Get locally installed plugins - const needsUpdates: IPluginManifest[] = []; - for (const avail of this.plugins.availablePlugins) { + return this.plugins.availablePlugins.filter(avail => { const repoPlugin = this.repoProvidesPlugin(oprep, avail); - if (repoPlugin) { - if (semver.gt(repoPlugin.version, avail.version)) { - // Plugin needs update - needsUpdates.push(avail); - } + if (!repoPlugin) { + return false; } - } - - return needsUpdates; + return semver.gt(repoPlugin.version, avail.version); + }); } public async updateRepository(repo: IRepository): Promise { diff --git a/src/util/run.ts b/src/util/run.ts index a16a273..a8ea896 100644 --- a/src/util/run.ts +++ b/src/util/run.ts @@ -8,7 +8,7 @@ export interface IProcessData { } export async function spawnProcess(execp: string, args: any[], env: IEnvironment): Promise { - return new Promise((resolve, reject): void => { + return new Promise((resolve): void => { const process = spawn(execp, args, { cwd: env.path }); const stdout: string[] = []; const stderr: string[] = []; @@ -29,8 +29,8 @@ export async function spawnProcess(execp: string, args: any[], env: IEnvironment } export async function execProcess(execp: string, env: IEnvironment): Promise { - return new Promise((resolve, reject): void => { - const cprog = exec(execp, (error: any, stdout: any, stderr: any): void => resolve({ + return new Promise((resolve): void => { + exec(execp, (error: any, stdout: any, stderr: any): void => resolve({ code: error.code, stderr, stdout, })); });