function getErrorMessage(error: any) { return error.message || error.toString(); } function logToUI(s: string) { console.log(s); } class Services { store: RhamtConfigurationStore; constructor(store: RhamtConfigurationStore) { this.store = store; } public getConfiguration(): Promise { return new Promise((resolve, reject) => { $.ajax({ url: `http://${this.store.host}/configuration/${this.store.id}`, method: 'GET', dataType: 'json', success: resolve, error: reject }); }); } public deleteOption(option: IOption): Promise { return new Promise((resolve, reject) => { $.ajax({ url: `http://${this.store.host}/configuration/${this.store.id}/option/${option.id}`, method: 'DELETE', dataType: 'json', success: resolve, error: reject }); }); } public updateOption(payload: any) { return new Promise((resolve, reject) => { $.ajax({ url: `http://${this.store.host}/configuration/${this.store.id}/option`, crossDomain: true, method: 'POST', headers: { 'content-type': 'application/json' }, dataType: 'json', success: resolve, error: reject, data: JSON.stringify(payload), statusCode: { 200: resolve, 500: reject } }); }); } public run() { return new Promise((resolve, reject) => { $.ajax({ url: `http://${this.store.host}/configuration/${this.store.id}/run`, crossDomain: true, method: 'POST', dataType: 'json', success: resolve, error: reject }); }); } } class RhamtConfigurationStore { host: string; id: string; config: RhamtConfiguration; onReload: (config: RhamtConfiguration) => void; constructor(host: string, id: string) { this.host = host; this.id = id; } setConfig(config: RhamtConfiguration) { this.config = config; this.onReload(config); } } class SocketWrapper { private _socket: SocketIOClient.Socket; constructor(_socket: SocketIOClient.Socket) { this._socket = _socket; } public onServerMessage(message: ServerMessage, fn: Function): SocketIOClient.Emitter { return this._socket.on(message, (...args: any[]) => { try { console.log('client received message!!! ' + message); console.log(args); fn(...args); } catch (err) { this.emitToHost('log', getErrorMessage(err)); logToUI(err); } }); } public emitToHost(message: ClientMessage, ...args: any[]): SocketIOClient.Socket { return this._socket.emit(message, ...args); } } export class ConfigClient { private _socket: SocketWrapper; private _services: Services; store: RhamtConfigurationStore; constructor(host: string, id: string) { const url = `http://${host}?id=${id}`; console.log('client connecting to: ' + url); this.store = new RhamtConfigurationStore(host, id); this._services = new Services(this.store); this._socket = new SocketWrapper(io.connect(url)); this._socket.onServerMessage('connect', (): void => { this.handleConnected(); }); this._socket.onServerMessage('disconnect', (): void => { this.handleDisconnected(); }); this._socket.onServerMessage('progress', (data: any): void => { this.handleDetails(data); }); this._socket.onServerMessage('notFound', (): void => { this.handleNotFound(); }); this._socket.onServerMessage('raasClientConnected', (data: any): void => { this.handleRaasClientConnected(data); }); this.loadOptions(); } // public addOption(option: Option) { // return new Promise((resolve, reject) => { // this._services.addOption(option).then((result: any) => { // option.id = result.id; // resolve(true); // }).catch(reject); // }); // } setOption(name: string, value: boolean): void { const payload = {type: 'boolean', name, value}; console.log('sending payload: '); console.log(JSON.stringify(payload)); this._services.updateOption(payload).then(result => { console.log('WE ROLLING!!!!!'); console.log(JSON.stringify(result)); }).catch(e => { console.log('***ERROR'); console.log(JSON.stringify(e)); }); } private log(s: string) { if (this._socket) { this._socket.emitToHost('log', s); } logToUI(s); } private handleConnected() { this.log(`client connected. connecting raas client...`); this._socket.emitToHost('connectRaasClient'); } private handleDisconnected() { this.log('client disconnected'); } private handleDetails(data: any) { this.log('client received details: ' + JSON.stringify(data)); } private handleNotFound() { this.log('configuration not found'); } private handleRaasClientConnected(data: any): void { console.log('host: ' + data.host); console.log('port: ' + data.port); } public loadOptions(): Promise { return new Promise(resolve => { this._services.getConfiguration().then(data => { const stub = new RhamtConfigurationImpl(); const config: RhamtConfiguration = Object.assign(stub, data['config']); this.store.setConfig(config); resolve(config); }).catch(e => { console.log('exception getting configuration.'); console.log(JSON.stringify(e)); }); }); } public run(): void { this._socket.emitToHost('startAnalysis', {id: this.store.id}); } public cancel(): Promise { return new Promise(resolve => { console.log('cancelling...'); resolve(); }); } } // export type ServerMessage = 'raasClientConnected' | 'progress' | 'notFound' | 'missmatch' | 'connect' | 'disconnect'; export type ClientMessage = 'connectRaasClient' | 'startAnalysis' | 'missmatch' | 'log'; export interface IUniqueElement { id: string; } export interface RhamtConfiguration extends IUniqueElement { name: string; input: string[]; output: string; options: IOption[]; server: Server; } export class RhamtConfigurationImpl implements RhamtConfiguration { id: string = ''; name: string = ''; input: string[] = []; output: string = ''; options: IOption[] = []; server: Server = new ServerImpl(); } export interface Server { host: string; port: number; } export class ServerImpl implements Server { constructor( public host: string = '', public port: number = 0) {} } export interface IExecutionResult extends IUniqueElement { hints: IHint[]; classifications: IClassification[]; } export interface IIssue extends IUniqueElement { quickfixes: IQuickFix[]; file: string; severity: string; ruleId: string; effort: string; title: string; messageOrDescription: string; links: ILink[]; report: string; } export interface IHint extends IIssue { originalLineSource: string; lineNumber: number; column: number; length: number; sourceSnippet: string; } export interface IClassification extends IIssue { } export type IQuickFixType = 'REPLACE' | 'DELETE_LINE' | 'INSERT_LINE' | 'TRANSFORMATION'; export interface IQuickFix extends IUniqueElement { type: IQuickFixType; searchString: string; replacementString: string; newLine: string; transformationId: string; name: string; file: string; } export interface ILink extends IUniqueElement { description: string; url: string; } export interface IReport extends IUniqueElement { inputFile: string; reportLocation: string; } export interface IMigrationPath extends IUniqueElement { pahtId: string; source: ITechnology; target: ITechnology; } export interface ITechnology extends IUniqueElement { technologyId: string; versionRange: string; } export interface IOption extends IUniqueElement { name: string; value: string; } export interface IIgnorePattern extends IUniqueElement { pattern: string; enabled: boolean; }