From c21f5fea07c22241151ca7226b674f6cf7b9470a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=BCbleitner?= Date: Mon, 26 Aug 2019 22:17:16 +0200 Subject: [PATCH] dont hurry me --- src/backend/UpdateManger.ts | 95 +++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 9 deletions(-) diff --git a/src/backend/UpdateManger.ts b/src/backend/UpdateManger.ts index d4aac52..3f4a7e7 100644 --- a/src/backend/UpdateManger.ts +++ b/src/backend/UpdateManger.ts @@ -6,7 +6,7 @@ import * as Logger from 'log4js' import * as path from "path"; import { FrontblockAdmin } from "./FrontblockAdmin"; import { TableDefiniton } from "frontblock-generic/Admin"; -import { promises as fs, mkdirSync } from "fs" +import { promises as fs, mkdirSync as mkdir } from "fs" var exec = require('child-process-promise').exec; @@ -23,28 +23,33 @@ Logger.configure({ }) const logger = Logger.getLogger("admin/updatemanager") +type SharedStatus = { + name: string + installed: boolean + outdated: boolean +} -abstract class Extension{ +abstract class Extension{ constructor( - public name: string, + protected name: string ){} /** * promises the generic Status object */ - abstract status():Promise + abstract async status():Promise /** * promises a boolean. * true => isInstalled should be true afterwards * false => an error happend and nothing was installed */ - abstract install():Promise + abstract async install():Promise /** * promises a boolean. * true => isInstalled should be false afterwards * false => the function was not able to bring this plugin into an uninstalled state!!! */ - abstract uninstall():Promise + abstract async uninstall():Promise /** * promises a boolean. @@ -68,8 +73,58 @@ abstract class Extension{ abstract async isInstalled():Promise } -export class NPMExtension extends Extension{ +export type FSStatus = SharedStatus & { + prefix: string + exists: boolean + empty: boolean +} +export abstract class FSExtension extends Extension{ + constructor( + name: string, + protected readonly prefix: string){ + super(name) + logger.debug('Creating extension directory', path.resolve(prefix, name)) + mkdir(path.resolve(prefix, name)) + } + + public async status(): Promise{ + const installed = await this.isInstalled() + const outdated = await this.isOutdated() + const exists = await this.exists() + const empty = await this.isEmpty() + const status = { + name: this.name, + prefix: this.prefix, + installed: installed, + outdated: outdated, + exists: exists, + empty: empty + } + return status + } + public async isEmpty(): Promise{ + return true //TODO + } + public async exists(): Promise{ + try { + await fs.access(path.resolve(this.prefix, this.name)) + return true + } catch (error) { + return false + } + } +} + +type NpmExtensionStatus = SharedStatus & { + version?: string, + wanted?: string, + latest?: string, + name: string, + location: string, +} + +export class NPMExtension extends Extension{ constructor( @@ -79,8 +134,30 @@ export class NPMExtension extends Extension{ super(name) } - status(): Promise { - throw new Error("Method not implemented."); + public async status(): Promise { + try { + await fs.access(this.localPrefix); + } + catch (e) { + return { exists: false, empty: true, name: this.name, location: this.localPrefix }; + } + + const files = await fs.readdir(this.localPrefix) + const empty: boolean = files.length === 0 + if(empty){ + return { exists: true, empty: true, name: this.name, location: this.localPrefix }; + } + + const { _error, _stdout, _stderr } = await exec('npm outdated --prefix ' + this.localPrefix + " --json" + ' ' + this.name) + const { error, stdout, stderr } = await exec('npm info --prefix ' + this.localPrefix + ' ' + this.name + " version") + + if(_error | error){ + logger.warn( _stderr, stderr) + return {exists: true, empty: false, name: this.name, location: this.localPrefix} + } + + const statusJson:{latest:string, location: string, wanted:string} = JSON.parse(_stdout) + return { exists: true, empty: false, name: this.name, location: this.localPrefix, version: stdout, latest: statusJson.latest, wanted: statusJson.wanted } } public async install(): Promise {