dont hurry me

This commit is contained in:
Daniel Hübleitner
2019-08-26 22:17:16 +02:00
parent 7acd853745
commit c21f5fea07
+86 -9
View File
@@ -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<Status>{
abstract class Extension<Status extends SharedStatus>{
constructor(
public name: string,
protected name: string
){}
/**
* promises the generic Status object
*/
abstract status():Promise<Status>
abstract async status():Promise<Status>
/**
* promises a boolean.
* true => isInstalled should be true afterwards
* false => an error happend and nothing was installed
*/
abstract install():Promise<boolean>
abstract async install():Promise<boolean>
/**
* 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<boolean>
abstract async uninstall():Promise<boolean>
/**
* promises a boolean.
@@ -68,8 +73,58 @@ abstract class Extension<Status>{
abstract async isInstalled():Promise<boolean>
}
export class NPMExtension extends Extension<NPMExtensionStatus>{
export type FSStatus = SharedStatus & {
prefix: string
exists: boolean
empty: boolean
}
export abstract class FSExtension extends Extension<FSStatus>{
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<FSStatus>{
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<boolean>{
return true //TODO
}
public async exists(): Promise<boolean>{
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<NpmExtensionStatus>{
constructor(
@@ -79,8 +134,30 @@ export class NPMExtension extends Extension<NPMExtensionStatus>{
super(name)
}
status(): Promise<any> {
throw new Error("Method not implemented.");
public async status(): Promise<NpmExtensionStatus> {
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<boolean> {