import * as path from 'path' ; import * as FS from '../../tools/fs' ; import * as Objects from '../../tools/env.objects' ; import Logger from '../../tools/env.logger' ; export enum EProcessPluginType { single = 'single' , multiple = 'multiple' } export interface IChipmunk { version : string ; type : EProcessPluginType ; displayName ? : string ; } export interface IPackageJson { version : string ; main : string ; chipmunk : IChipmunk ; } const CRequiredFields = { main : 'string' , version : 'string' , } ; class description export default class ControllerPluginPackage { private _logger : Logger = new Logger ( 'ControllerPluginPackage' ) ; private _package : IPackageJson | undefined ; private _folder : string ; private _name : string ; param { string } folder constructor ( folder , name ) { this . _folder = folder ; this . _name = name ; } returns { Promise < IPackageJson > } public read ( ) { return new Promise ( ( resolve , reject ) => { FS . exist ( this . _folder ) . then ( ( exist ) => { if ( ! exist ) { return reject ( new Error ( this . _logger . warn ( `Folder "${ this . _folder }" doesn't exist` ) ) ) ; } const packageFile = path . resolve ( this . _folder , 'package.json' ) ; FS . readTextFile ( packageFile ) . then ( ( content ) => { const json = Objects . getJSON ( content ) ; if ( json instanceof Error ) { return reject ( new Error ( this . _logger . error ( `Cannot parse package file "${ packageFile }" due error: ${ json . message }` ) ) ) ; } const missed = [ ] ; Object . keys ( CRequiredFields ) . forEach ( ( key ) => { if ( typeof json [ key ] !== CRequiredFields [ key ] ) { missed . push ( key ) ; } } ) ; if ( missed . length > 0 ) { return reject ( new Error ( this . _logger . warn ( `Next field(s) in "${ packageFile }" wasn't found: ${ missed . join ( ', ' ) }` ) ) ) ; } this . _package = this . _setPluginsSettings ( json ) ; resolve ( ) ; } ) . catch ( ( error ) => { reject ( new Error ( this . _logger . warn ( `Fail to read package at "${ packageFile }" due error: ${ error . message }` ) ) ) ; } ) ; } ) . catch ( ( existErr ) => { reject ( new Error ( this . _logger . warn ( `Fail to read package due error: ${ existErr . message }` ) ) ) ; } ) ; } ) ; } public getPackageJson ( ) { return Object . assign ( { } , this . _package ) ; } public getPath ( ) { return this . _folder ; } public getDisplayName ( ) { if ( this . _package === undefined ) { return undefined ; } if ( this . _package . chipmunk === null || typeof this . _package . chipmunk !== 'object' ) { return undefined ; } return typeof this . _package . chipmunk . displayName === 'string' ? this . _package . chipmunk . displayName : undefined ; } private _setPluginsSettings ( packageJson ) { if ( typeof ( packageJson as any ) . logviewer === 'object' && ( packageJson as any ) . logviewer !== null && ( typeof packageJson . chipmunk !== 'object' || packageJson . chipmunk === null ) ) { packageJson . chipmunk = ( packageJson as any ) . logviewer ; } if ( typeof packageJson . chipmunk !== 'object' || packageJson . chipmunk === null ) { packageJson . chipmunk = { version : '' , type : EProcessPluginType . multiple , } ; } packageJson . chipmunk . type = packageJson . chipmunk . type === undefined ? EProcessPluginType . multiple : packageJson . chipmunk . type ; return packageJson ; } }