Skip to content

Commit 3eef857

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
fix: can open IDE from an ino file
Use a fallback frontend app config when starting IDE2 from an ino file (from Explorer, Finder, etc.) and the app config is not yet set. Closes #2209 Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
1 parent 73b6dc4 commit 3eef857

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

+56-6
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ import { Deferred } from '@theia/core/lib/common/promise-util';
1616
import { isObject, MaybePromise, Mutable } from '@theia/core/lib/common/types';
1717
import { ElectronSecurityToken } from '@theia/core/lib/electron-common/electron-token';
1818
import {
19-
ElectronMainApplication as TheiaElectronMainApplication,
2019
ElectronMainExecutionParams,
20+
ElectronMainApplication as TheiaElectronMainApplication,
2121
} from '@theia/core/lib/electron-main/electron-main-application';
2222
import type { TheiaBrowserWindowOptions } from '@theia/core/lib/electron-main/theia-electron-window';
2323
import { FileUri } from '@theia/core/lib/node/file-uri';
2424
import { inject, injectable } from '@theia/core/shared/inversify';
2525
import { URI } from '@theia/core/shared/vscode-uri';
2626
import { log as logToFile, setup as setupFileLog } from 'node-log-rotate';
2727
import { fork } from 'node:child_process';
28-
import { promises as fs, rm, rmSync } from 'node:fs';
28+
import { promises as fs, readFileSync, rm, rmSync } from 'node:fs';
2929
import type { AddressInfo } from 'node:net';
3030
import { isAbsolute, join, resolve } from 'node:path';
3131
import { Sketch } from '../../common/protocol';
@@ -745,6 +745,19 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
745745
}
746746
}
747747
}
748+
749+
// Fallback app config when starting IDE2 from an ino file (from Explorer, Finder, etc.) and the app config is not yet set.
750+
// https://github.com/arduino/arduino-ide/issues/2209
751+
private _fallbackConfig: FrontendApplicationConfig | undefined;
752+
override get config(): FrontendApplicationConfig {
753+
if (!this._config) {
754+
if (!this._fallbackConfig) {
755+
this._fallbackConfig = readFrontendAppConfigSync();
756+
}
757+
return this._fallbackConfig;
758+
}
759+
return super.config;
760+
}
748761
}
749762

750763
class InterruptWorkspaceRestoreError extends Error {
@@ -775,11 +788,8 @@ async function updateFrontendApplicationConfigFromPackageJson(
775788
return config;
776789
}
777790
try {
778-
const modulePath = __filename;
779-
// must go from `./lib/backend/electron-main.js` to `./package.json` when the app is webpacked.
780-
const packageJsonPath = join(modulePath, '..', '..', '..', 'package.json');
781791
console.debug(
782-
`Checking for frontend application configuration customizations. Module path: ${modulePath}, destination 'package.json': ${packageJsonPath}`
792+
`Checking for frontend application configuration customizations. Module path: ${__filename}, destination 'package.json': ${packageJsonPath}`
783793
);
784794
const rawPackageJson = await fs.readFile(packageJsonPath, {
785795
encoding: 'utf8',
@@ -827,6 +837,46 @@ async function updateFrontendApplicationConfigFromPackageJson(
827837
return config;
828838
}
829839

840+
const fallbackFrontendAppConfig: FrontendApplicationConfig = {
841+
applicationName: 'Arduino IDE',
842+
defaultTheme: {
843+
light: 'arduino-theme',
844+
dark: 'arduino-theme-dark',
845+
},
846+
defaultIconTheme: 'none',
847+
validatePreferencesSchema: false,
848+
defaultLocale: '',
849+
electron: {},
850+
};
851+
852+
// When the package.json must go from `./lib/backend/electron-main.js` to `./package.json` when the app is webpacked.
853+
// Only for production mode!
854+
const packageJsonPath = join(__filename, '..', '..', '..', 'package.json');
855+
856+
function readFrontendAppConfigSync(): FrontendApplicationConfig {
857+
if (environment.electron.isDevMode()) {
858+
console.debug(
859+
'Running in dev mode. Using the fallback fronted application config.'
860+
);
861+
return fallbackFrontendAppConfig;
862+
}
863+
try {
864+
const raw = readFileSync(packageJsonPath, { encoding: 'utf8' });
865+
const packageJson = JSON.parse(raw);
866+
const config = packageJson?.theia?.frontend?.config;
867+
if (config) {
868+
return config;
869+
}
870+
throw new Error(`Frontend application config not found. ${packageJson}`);
871+
} catch (err) {
872+
console.error(
873+
`Could not read package.json content from ${packageJsonPath}.`,
874+
err
875+
);
876+
return fallbackFrontendAppConfig;
877+
}
878+
}
879+
830880
/**
831881
* Mutates the `toUpdate` argument and returns with it.
832882
*/

0 commit comments

Comments
 (0)