mirror of
https://github.com/arduino/arduino-ide.git
synced 2025-11-08 18:08:33 +00:00
Use eslint&prettier for code linting&formatting
This commit is contained in:
committed by
Francesco Stasi
parent
2a3873a923
commit
0592199858
@@ -11,9 +11,17 @@ import { ILogger } from '@theia/core/lib/common/logger';
|
||||
import { FileUri } from '@theia/core/lib/node/file-uri';
|
||||
import { Event, Emitter } from '@theia/core/lib/common/event';
|
||||
import { BackendApplicationContribution } from '@theia/core/lib/node/backend-application';
|
||||
import { ConfigService, Config, NotificationServiceServer, Network } from '../common/protocol';
|
||||
import {
|
||||
ConfigService,
|
||||
Config,
|
||||
NotificationServiceServer,
|
||||
Network,
|
||||
} from '../common/protocol';
|
||||
import { spawnCommand } from './exec-util';
|
||||
import { MergeRequest, WriteRequest } from './cli-protocol/cc/arduino/cli/settings/v1/settings_pb';
|
||||
import {
|
||||
MergeRequest,
|
||||
WriteRequest,
|
||||
} from './cli-protocol/cc/arduino/cli/settings/v1/settings_pb';
|
||||
import { SettingsServiceClient } from './cli-protocol/cc/arduino/cli/settings/v1/settings_grpc_pb';
|
||||
import * as serviceGrpcPb from './cli-protocol/cc/arduino/cli/settings/v1/settings_grpc_pb';
|
||||
import { ArduinoDaemonImpl } from './arduino-daemon-impl';
|
||||
@@ -25,8 +33,9 @@ import { deepClone } from '@theia/core';
|
||||
const track = temp.track();
|
||||
|
||||
@injectable()
|
||||
export class ConfigServiceImpl implements BackendApplicationContribution, ConfigService {
|
||||
|
||||
export class ConfigServiceImpl
|
||||
implements BackendApplicationContribution, ConfigService
|
||||
{
|
||||
@inject(ILogger)
|
||||
@named('config')
|
||||
protected readonly logger: ILogger;
|
||||
@@ -74,20 +83,26 @@ export class ConfigServiceImpl implements BackendApplicationContribution, Config
|
||||
if (Config.sameAs(this.config, config)) {
|
||||
return;
|
||||
}
|
||||
let copyDefaultCliConfig: DefaultCliConfig | undefined = deepClone(this.cliConfig);
|
||||
let copyDefaultCliConfig: DefaultCliConfig | undefined = deepClone(
|
||||
this.cliConfig
|
||||
);
|
||||
if (!copyDefaultCliConfig) {
|
||||
copyDefaultCliConfig = await this.getFallbackCliConfig();
|
||||
}
|
||||
const { additionalUrls, dataDirUri, downloadsDirUri, sketchDirUri, network } = config;
|
||||
const {
|
||||
additionalUrls,
|
||||
dataDirUri,
|
||||
downloadsDirUri,
|
||||
sketchDirUri,
|
||||
network,
|
||||
} = config;
|
||||
copyDefaultCliConfig.directories = {
|
||||
data: FileUri.fsPath(dataDirUri),
|
||||
downloads: FileUri.fsPath(downloadsDirUri),
|
||||
user: FileUri.fsPath(sketchDirUri)
|
||||
user: FileUri.fsPath(sketchDirUri),
|
||||
};
|
||||
copyDefaultCliConfig.board_manager = {
|
||||
additional_urls: [
|
||||
...additionalUrls
|
||||
]
|
||||
additional_urls: [...additionalUrls],
|
||||
};
|
||||
const proxy = Network.stringify(network);
|
||||
copyDefaultCliConfig.network = { proxy };
|
||||
@@ -108,46 +123,67 @@ export class ConfigServiceImpl implements BackendApplicationContribution, Config
|
||||
return this.configChangeEmitter.event;
|
||||
}
|
||||
|
||||
async getVersion(): Promise<Readonly<{ version: string, commit: string, status?: string }>> {
|
||||
async getVersion(): Promise<
|
||||
Readonly<{ version: string; commit: string; status?: string }>
|
||||
> {
|
||||
return this.daemon.getVersion();
|
||||
}
|
||||
|
||||
async isInDataDir(uri: string): Promise<boolean> {
|
||||
return this.getConfiguration().then(({ dataDirUri }) => new URI(dataDirUri).isEqualOrParent(new URI(uri)));
|
||||
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)));
|
||||
return this.getConfiguration().then(({ sketchDirUri }) =>
|
||||
new URI(sketchDirUri).isEqualOrParent(new URI(uri))
|
||||
);
|
||||
}
|
||||
|
||||
protected async loadCliConfig(): Promise<DefaultCliConfig | undefined> {
|
||||
const cliConfigFileUri = await this.getCliConfigFileUri();
|
||||
const cliConfigPath = FileUri.fsPath(cliConfigFileUri);
|
||||
try {
|
||||
const content = await promisify(fs.readFile)(cliConfigPath, { encoding: 'utf8' });
|
||||
const content = await promisify(fs.readFile)(cliConfigPath, {
|
||||
encoding: 'utf8',
|
||||
});
|
||||
const model = yaml.safeLoad(content) || {};
|
||||
// The CLI can run with partial (missing `port`, `directories`), the app cannot, we merge the default with the user's config.
|
||||
const fallbackModel = await this.getFallbackCliConfig();
|
||||
return deepmerge(fallbackModel, model) as DefaultCliConfig;
|
||||
} catch (error) {
|
||||
this.logger.error(`Error occurred when loading CLI config from ${cliConfigPath}.`, error);
|
||||
this.logger.error(
|
||||
`Error occurred when loading CLI config from ${cliConfigPath}.`,
|
||||
error
|
||||
);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
protected async getFallbackCliConfig(): Promise<DefaultCliConfig> {
|
||||
const cliPath = await this.daemon.getExecPath();
|
||||
const throwawayDirPath = await new Promise<string>((resolve, reject) => {
|
||||
track.mkdir({}, (err, dirPath) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve(dirPath);
|
||||
});
|
||||
});
|
||||
await spawnCommand(`"${cliPath}"`, ['config', 'init', '--dest-dir', `"${throwawayDirPath}"`]);
|
||||
const rawYaml = await promisify(fs.readFile)(path.join(throwawayDirPath, CLI_CONFIG), { encoding: 'utf-8' });
|
||||
const throwawayDirPath = await new Promise<string>(
|
||||
(resolve, reject) => {
|
||||
track.mkdir({}, (err, dirPath) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve(dirPath);
|
||||
});
|
||||
}
|
||||
);
|
||||
await spawnCommand(`"${cliPath}"`, [
|
||||
'config',
|
||||
'init',
|
||||
'--dest-dir',
|
||||
`"${throwawayDirPath}"`,
|
||||
]);
|
||||
const rawYaml = await promisify(fs.readFile)(
|
||||
path.join(throwawayDirPath, CLI_CONFIG),
|
||||
{ encoding: 'utf-8' }
|
||||
);
|
||||
const model = yaml.safeLoad(rawYaml.trim());
|
||||
return model as DefaultCliConfig;
|
||||
}
|
||||
@@ -160,22 +196,36 @@ export class ConfigServiceImpl implements BackendApplicationContribution, Config
|
||||
await this.initCliConfigTo(path.dirname(cliConfigPath));
|
||||
exists = await promisify(fs.exists)(cliConfigPath);
|
||||
if (!exists) {
|
||||
throw new Error(`Could not initialize the default CLI configuration file at ${cliConfigPath}.`);
|
||||
throw new Error(
|
||||
`Could not initialize the default CLI configuration file at ${cliConfigPath}.`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected async initCliConfigTo(fsPathToDir: string): Promise<void> {
|
||||
const cliPath = await this.daemon.getExecPath();
|
||||
await spawnCommand(`"${cliPath}"`, ['config', 'init', '--dest-dir', `"${fsPathToDir}"`]);
|
||||
await spawnCommand(`"${cliPath}"`, [
|
||||
'config',
|
||||
'init',
|
||||
'--dest-dir',
|
||||
`"${fsPathToDir}"`,
|
||||
]);
|
||||
}
|
||||
|
||||
protected async mapCliConfigToAppConfig(cliConfig: DefaultCliConfig): Promise<Config> {
|
||||
protected async mapCliConfigToAppConfig(
|
||||
cliConfig: DefaultCliConfig
|
||||
): Promise<Config> {
|
||||
const { directories } = cliConfig;
|
||||
const { data, user, downloads } = directories;
|
||||
const additionalUrls: Array<string> = [];
|
||||
if (cliConfig.board_manager && cliConfig.board_manager.additional_urls) {
|
||||
additionalUrls.push(...Array.from(new Set(cliConfig.board_manager.additional_urls)));
|
||||
if (
|
||||
cliConfig.board_manager &&
|
||||
cliConfig.board_manager.additional_urls
|
||||
) {
|
||||
additionalUrls.push(
|
||||
...Array.from(new Set(cliConfig.board_manager.additional_urls))
|
||||
);
|
||||
}
|
||||
const network = Network.parse(cliConfig.network?.proxy);
|
||||
return {
|
||||
@@ -183,7 +233,7 @@ export class ConfigServiceImpl implements BackendApplicationContribution, Config
|
||||
sketchDirUri: FileUri.create(user).toString(),
|
||||
downloadsDirUri: FileUri.create(downloads).toString(),
|
||||
additionalUrls,
|
||||
network
|
||||
network,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -196,14 +246,17 @@ export class ConfigServiceImpl implements BackendApplicationContribution, Config
|
||||
this.notificationService.notifyConfigChanged({ config: undefined });
|
||||
}
|
||||
|
||||
protected async updateDaemon(port: string | number, config: DefaultCliConfig): Promise<void> {
|
||||
protected async updateDaemon(
|
||||
port: string | number,
|
||||
config: DefaultCliConfig
|
||||
): Promise<void> {
|
||||
const client = this.createClient(port);
|
||||
const req = new MergeRequest();
|
||||
const json = JSON.stringify(config, null, 2);
|
||||
req.setJsonData(json);
|
||||
console.log(`Updating daemon with 'data': ${json}`);
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
client.merge(req, error => {
|
||||
client.merge(req, (error) => {
|
||||
try {
|
||||
if (error) {
|
||||
reject(error);
|
||||
@@ -224,7 +277,7 @@ export class ConfigServiceImpl implements BackendApplicationContribution, Config
|
||||
const cliConfigPath = FileUri.fsPath(cliConfigUri);
|
||||
req.setFilePath(cliConfigPath);
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
client.write(req, error => {
|
||||
client.write(req, (error) => {
|
||||
try {
|
||||
if (error) {
|
||||
reject(error);
|
||||
@@ -240,9 +293,14 @@ export class ConfigServiceImpl implements BackendApplicationContribution, Config
|
||||
|
||||
private createClient(port: string | number): SettingsServiceClient {
|
||||
// https://github.com/agreatfool/grpc_tools_node_protoc_ts/blob/master/doc/grpcjs_support.md#usage
|
||||
// @ts-ignore
|
||||
const SettingsServiceClient = grpc.makeClientConstructor(serviceGrpcPb['cc.arduino.cli.settings.v1.SettingsService'], 'SettingsServiceService') as any;
|
||||
return new SettingsServiceClient(`localhost:${port}`, grpc.credentials.createInsecure()) as SettingsServiceClient;
|
||||
const SettingsServiceClient = grpc.makeClientConstructor(
|
||||
// @ts-expect-error: ignore
|
||||
serviceGrpcPb['cc.arduino.cli.settings.v1.SettingsService'],
|
||||
'SettingsServiceService'
|
||||
) as any;
|
||||
return new SettingsServiceClient(
|
||||
`localhost:${port}`,
|
||||
grpc.credentials.createInsecure()
|
||||
) as SettingsServiceClient;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user