-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy patharduino-daemon.ts
69 lines (58 loc) · 2.43 KB
/
arduino-daemon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as os from 'os';
import { exec, ChildProcess } from 'child_process';
import { join, resolve } from 'path';
import { inject, injectable, named } from 'inversify';
import { ILogger } from '@theia/core/lib/common/logger';
import { BackendApplicationContribution } from '@theia/core/lib/node';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { DaemonLog } from './daemon-log';
import { ToolOutputServiceServer } from '../common/protocol/tool-output-service';
const EXECUTABLE_PATH = resolve(join(__dirname, '..', '..', 'build', `arduino-cli.${os.platform()}`))
@injectable()
export class ArduinoDaemon implements BackendApplicationContribution {
@inject(ILogger)
@named('daemon')
protected readonly logger: ILogger
@inject(ToolOutputServiceServer)
protected readonly toolOutputService: ToolOutputServiceServer;
protected process: ChildProcess | undefined;
protected isReady = new Deferred<boolean>();
async onStart() {
try {
const daemon = exec(`${EXECUTABLE_PATH} --debug daemon`, (err, stdout, stderr) => {
if (err || stderr) {
console.log(err || new Error(stderr));
return;
}
console.log(stdout);
});
if (daemon.stdout) {
daemon.stdout.on('data', data => {
this.toolOutputService.publishNewOutput('daemon', data.toString());
DaemonLog.log(this.logger, data.toString());
});
}
if (daemon.stderr) {
daemon.stderr.on('data', data => {
this.toolOutputService.publishNewOutput('daemon error', data.toString());
DaemonLog.log(this.logger, data.toString());
});
}
if (daemon.stderr) {
daemon.on('exit', (code, signal) => DaemonLog.log(this.logger, `Daemon exited with code: ${code}. Signal was: ${signal}.`));
}
this.process = daemon;
await new Promise((resolve, reject) => setTimeout(resolve, 2000));
this.isReady.resolve();
} catch (error) {
this.isReady.reject(error || new Error('failed to start arduino-cli'));
}
}
onStop() {
if (!this.process) {
return;
}
DaemonLog.log(this.logger, `Shutting down daemon.`);
this.process.kill("SIGTERM");
}
}