import * as Os from 'os' ; import * as Fs from 'fs' ; import * as Path from 'path' ; import * as Util from 'util' ; import * as Types from '@meetalva/types' ; import * as getPort from 'get-port' ; import * as Model from '@meetalva/model' ; import * as M from '@meetalva/message' ; export class NodeHost implements Types . Host < Model . AlvaApp < M . Message > , Model . Project , M . Message > { public type = Types . HostType . Node ; private forced ? : Partial < Types . HostFlags > ; private process ? : NodeJS . Process ; private sender ? : Types . Sender < M . Message > ; private apps : Map < string , Model . AlvaApp < M . Message > > = new Map ( ) ; public static async fromProcess ( process , forced ? ) { const host = new NodeHost ( ) ; host . process = process ; host . forced = forced ; return host ; } public async getFlags ( ) { const yargsParser = require ( 'yargs-parser' ) ; return { ... yargsParser ( this . process ! . argv . slice ( 2 ) , { number : [ 'port' ] , boolean : [ 'localhost' ] } ) , ... this . forced } ; } public async getPort ( requested ? ) { return getPort ( { port : requested } ) ; } public async getApp ( id ) { return this . apps . get ( id ) ; } public async addApp ( app ) { this . apps . set ( app . getId ( ) , app ) ; } public async log ( message ? , ... optionalParams ) { console . log ( message , ... optionalParams ) ; } public async resolveFrom ( base , ... paths ) { const getBasePath = ( b ) : string => { switch ( b ) { case Types . HostBase . AppPath : case Types . HostBase . Source : return Path . resolve ( __dirname , '..' , '..' ) ; case Types . HostBase . AppData : return Path . resolve ( Os . tmpdir ( ) , 'alva' ) ; case Types . HostBase . UserData : return Path . resolve ( Os . homedir ( ) , '.alva' ) ; case Types . HostBase . UserHome : return Os . homedir ( ) ; } } ; return Path . resolve ( ... [ getBasePath ( base ) , ... paths ] ) ; } public async exists ( path ) { return Fs . existsSync ( path ) ; } public access ( path , mode ) { return new Promise ( resolve => { Fs . access ( path , mode , err => { if ( err ) { return resolve ( false ) ; } resolve ( true ) ; } ) ; } ) ; } public async stat ( path ) { return new Promise ( ( resolve , reject ) => { Fs . stat ( path , ( err , stats ) => { if ( err ) { return reject ( err ) ; } resolve ( stats ) ; } ) ; } ) ; } public async mkdir ( path ) { const mkdir = Util . promisify ( Fs . mkdir ) ; if ( ! Fs . existsSync ( path ) ) { await mkdir ( path ) ; } } public async readFile ( path ) { const readFile = Util . promisify ( Fs . readFile ) ; const buffer = await readFile ( path ) ; return { path , buffer , contents : buffer . toString ( 'utf-8' ) } ; } public async saveFile ( path , data ) { return ; } public async writeFile ( path , data ) { const writeFile = Util . promisify ( Fs . writeFile ) ; return writeFile ( path , data ) ; } public async getSender ( ) { return this . sender ! ; } public setSender ( sender ) { this . sender = sender ; } public async open ( uri ) { return ; } public async selectFile ( ) { return ; } public async selectSaveFile ( ) { return ; } public async showMessage ( ) { return ; } public async showContextMenu ( ) { return ; } public async writeClipboard ( input ) { return ; } public async readClipboard ( ) { return ; } public async createWindow ( ) { return ; } public async toggleDevTools ( ) { return ; } }