-
-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy patharduino-daemon.ts
83 lines (73 loc) · 3.56 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { join } from 'path';
import { exec, spawn, SpawnOptions } from 'child_process';
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 { environment } from '@theia/application-package/lib/environment';
import { DaemonLog } from './daemon-log';
import { ToolOutputServiceServer } from '../common/protocol/tool-output-service';
import { ArduinoCliContribution } from './arduino-cli-contribution';
import { ArduinoCli } from './arduino-cli';
@injectable()
export class ArduinoDaemon implements BackendApplicationContribution {
@inject(ILogger)
@named('daemon')
protected readonly logger: ILogger
@inject(ArduinoCli)
protected readonly cli: ArduinoCli;
@inject(ArduinoCliContribution)
protected readonly cliContribution: ArduinoCliContribution;
@inject(ToolOutputServiceServer)
protected readonly toolOutputService: ToolOutputServiceServer;
protected isReady = new Deferred<boolean>();
async onStart() {
try {
if (!this.cliContribution.debugCli) {
const executable = await this.cli.getExecPath();
const version = await this.cli.getVersion();
this.logger.info(`>>> Starting ${version.toLocaleLowerCase()} daemon from ${executable}...`);
const daemon = exec(`"${executable}" daemon -v --log-level info --format json --log-format json`,
{ encoding: 'utf8', maxBuffer: 1024 * 1024 }, (err, stdout, stderr) => {
if (err || stderr) {
console.log(err || new Error(stderr));
return;
}
console.log(stdout);
});
const options: SpawnOptions = {
env: environment.electron.runAsNodeEnv(),
detached: true,
stdio: 'ignore'
}
const command = process.execPath;
const cp = spawn(command, [join(__dirname, 'daemon-watcher.js'), String(process.pid), String(daemon.pid)], options);
cp.unref();
if (daemon.stdout) {
daemon.stdout.on('data', data => {
this.toolOutputService.publishNewOutput('daemon', DaemonLog.toPrettyString(data.toString()));
DaemonLog.log(this.logger, data.toString());
});
}
if (daemon.stderr) {
daemon.stderr.on('data', data => {
this.toolOutputService.publishNewOutput('daemon error', DaemonLog.toPrettyString(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}.`));
}
}
await new Promise(resolve => setTimeout(resolve, 2000));
this.isReady.resolve();
if (!this.cliContribution.debugCli) {
this.logger.info(`<<< The 'arduino-cli' daemon is up an running.`);
} else {
this.logger.info(`Assuming the 'arduino-cli' already runs in debug mode.`);
}
} catch (error) {
this.isReady.reject(error || new Error('failed to start arduino-cli'));
}
}
}