-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathconfig-service-impl.ts
46 lines (38 loc) · 1.55 KB
/
config-service-impl.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
import { mkdirpSync, existsSync } from 'fs-extra';
import { injectable, inject, postConstruct } from 'inversify';
import URI from '@theia/core/lib/common/uri';
import { FileUri } from '@theia/core/lib/node/file-uri';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { ConfigService, Config } from '../common/protocol/config-service';
import { ArduinoCli } from './arduino-cli';
@injectable()
export class ConfigServiceImpl implements ConfigService {
@inject(ArduinoCli)
protected readonly cli: ArduinoCli;
protected readonly config: Deferred<Config> = new Deferred();
@postConstruct()
protected init(): void {
this.cli.getDefaultConfig().then(config => {
const { dataDirUri, sketchDirUri } = config;
for (const uri of [dataDirUri, sketchDirUri]) {
const path = FileUri.fsPath(uri);
if (!existsSync(path)) {
mkdirpSync(path);
}
}
this.config.resolve(config);
});
}
async getConfiguration(): Promise<Config> {
return this.config.promise;
}
async getVersion(): Promise<string> {
return this.cli.getVersion();
}
async isInDataDir(uri: string): Promise<boolean> {
return this.getConfiguration().then(({ dataDirUri }) => new URI(dataDirUri).isEqualOrParent(new URI(uri)));
}
async isInSketchDir(uri: string): Promise<boolean> {
return this.getConfiguration().then(({ sketchDirUri }) => new URI(sketchDirUri).isEqualOrParent(new URI(uri)));
}
}