import { WebPlugin } from '@capacitor/core'; import { CapFeathersPluginPlugin } from './definitions'; const { remote } = require('electron'); const path = require('path'); const fsu = require('fs-jetpack'); export class CapFeathersPluginWeb extends WebPlugin implements CapFeathersPluginPlugin { app:any = null; serveur:any = null; feathersPath: any = null; feathersRef: any = null; port: number = null; start: boolean; init: boolean; NodeFS: any = null; RemoteRef: any = null; Path: any = null; express: any = null; constructor() { super({ name: 'CapFeathersPlugin', platforms: ['electron'] }); this.NodeFS = require('fs-jetpack'); this.Path = require('path'); this.express = require('@feathersjs/express'); this.RemoteRef = remote; } async echo(options: { value: string }): Promise<{value: string}> { console.log('ECHO', options); return options; } async setFeathersPath(chemin:string): Promise { this.feathersPath = chemin; if ( fsu.exists(path.resolve(`${this.feathersPath}/src/app.js`) ) === 'file' ) { if ( fsu.exists(path.resolve(`${this.feathersPath}/config/default.json`) ) === 'file' ) { process.env["NODE_CONFIG_DIR"] =`${this.feathersPath}/config/`; } console.log('Initialisation possible !'); this.init = true; this.start = false; this.app = require(`${this.feathersPath}/src/app`); return Promise.resolve(); } else { console.error('Initialisation impossible le fichier src/app.js dois exister!!'); this.init = false; this.start = false; } } /* private hasFeathers():boolean { console.log('init server !'); console.log('Path dir :',path.resolve(this.feathersPath))//'./server')); console.log('File dir :',fsu.exists(path.resolve(this.feathersPath)))//'./server'))) this.init = false; if (fsu.exists(path.resolve(this.feathersPath)) === 'dir') { console.log('Implémentaion de feathersjs possible !'); if ( fsu.exists(path.resolve(`${this.feathersPath}/src/app.js`)) === 'file' ) { console.log('Initialisation possible !'); this.init = true; } else { console.error('Initialisation impossible le fichier src/app.js dois exister!!') } } return this.init; } */ async startServer(): Promise { if (this.init && !(this.start)) { return new Promise((resolve,reject)=>{ try { if (this.serveur && this.serveur.listening) { this.serveur.close(); this.serveur = null; } if (this.app) { this.app = null; } this.app = require(`${this.feathersPath}/src/app`); this.serveur = this.app.listen(this.app.get('port')); this.feathersRef = this.app; this.serveur.once('error', (error:any) => { console.log('demarrage impossible :',error) this.serveur = null; reject() }); this.serveur.on('listening', () => { console.log('démarrage effectué!') this.start = true; if (this.start) { resolve(); } else { reject(); } }); } catch (error) { reject(); } }); } else { console.log('démarrage impossible pour une raison inconnue!!') } } async reloadServer(): Promise { if (this.init && this.start) { if (this.serveur && this.serveur.listening) { this.serveur.close(); this.serveur = null; } if (this.app) { this.app = null; } return new Promise((resolve,reject)=>{ try { this.app = require(`${this.feathersPath}/src/app`); this.serveur = this.app.listen(this.app.get('port')); this.feathersRef = this.app; this.serveur.once('error', (error:any) => { this.serveur = null; console.log('demarrage impossible :',error) reject(null) }); this.serveur.on('listening', () => { console.log('redémarrage effectué!') this.start = true; if (this.start) { resolve(); } else { reject(); } }); } catch (error) { reject(); } }); } else { console.log('Redemarrage impossible pour une raison inconnue!!') } } async stopServer(): Promise { //if (this.init && this.start) { if (this.serveur && this.serveur.listening) { this.serveur.close(); this.serveur = null; this.start = false; if (this.app) { this.app = null; //this.app = require(`${this.feathersPath}/src/app`); } return Promise.resolve() } //} else { // console.log('Arret du serveur impossible pour une raison inconnue!!'); //} } async getListenPort(): Promise { if (this.init && this.app) { return this.app.get('port'); } else { console.error('Action getListenPort impossible pour une raison inconnue!!') return null; } } async isStart(): Promise { if (this.init) { if (this.serveur && this.serveur.listening) { this.start = true; } else { this.start = false; } return Promise.resolve(this.start); } else { console.error('Action isStart impossible pour une raison inconnue!!'); return Promise.reject(false) } } async getFeathersRef(): Promise { console.log('Feather Server :',this.app); return this.app; } async setConfig(param:string,value:any): Promise { if (this.init && this.app && this.start) { this.app.set(param,value); } else { console.log('Action setConfig impossible pour une raison inconnue!!') } } async getConfig(param:string): Promise { if (this.init && this.app) { this.app.get(param); } else { console.log('Action getConfig impossible pour une raison inconnue!!') } } async changePort(port: number): Promise { if (this.init && this.app) { this.app.set('port',port); } else { console.log('Action changePort impossible pour une raison inconnue!!') } } } const CapFeathersPlugin = new CapFeathersPluginWeb(); export { CapFeathersPlugin }; import { registerWebPlugin } from '@capacitor/core'; registerWebPlugin(CapFeathersPlugin);