Migrate to electron-vite and electron-builder
- Switch from deprecated Vue CLI plugin to `electron-vite` (see nklayman/vue-cli-plugin-electron-builder#1982) - Update main/preload scripts to use `index.cjs` filenames to support `"type": "module"`, resolving crash issue (#233). This crash was related to Electron not supporting ESM (see electron/asar#249, electron/electron#21457). - This commit completes migration to Vite from Vue CLI (#230). Structure changes: - Introduce separate folders for Electron's main and preload processes. - Move TypeHelpers to `src/` to mark tit as accessible by the rest of the code. Config changes: - Make `vite.config.ts` reusable by Electron configuration. - On electron-builder, use `--publish` flag instead of `-p` for clarity. Tests: - Add log for preload script loading verification. - Implement runtime environment sanity checks. - Enhance logging in `check-desktop-runtime-errors`.
This commit is contained in:
16
src/infrastructure/Metadata/AppMetadataFactory.ts
Normal file
16
src/infrastructure/Metadata/AppMetadataFactory.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { IAppMetadata } from './IAppMetadata';
|
||||
import { ViteAppMetadata } from './Vite/ViteAppMetadata';
|
||||
|
||||
export class AppMetadataFactory {
|
||||
public static get Current(): IAppMetadata {
|
||||
if (!this.instance) {
|
||||
this.instance = new ViteAppMetadata();
|
||||
}
|
||||
return this.instance;
|
||||
}
|
||||
|
||||
private static instance: IAppMetadata;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
private constructor() {}
|
||||
}
|
||||
3
src/infrastructure/RuntimeSanity/ISanityCheckOptions.ts
Normal file
3
src/infrastructure/RuntimeSanity/ISanityCheckOptions.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export interface ISanityCheckOptions {
|
||||
readonly validateMetadata: boolean;
|
||||
}
|
||||
6
src/infrastructure/RuntimeSanity/ISanityValidator.ts
Normal file
6
src/infrastructure/RuntimeSanity/ISanityValidator.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
||||
|
||||
export interface ISanityValidator {
|
||||
shouldValidate(options: ISanityCheckOptions): boolean;
|
||||
collectErrors(): Iterable<string>;
|
||||
}
|
||||
28
src/infrastructure/RuntimeSanity/SanityChecks.ts
Normal file
28
src/infrastructure/RuntimeSanity/SanityChecks.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { ISanityCheckOptions } from './ISanityCheckOptions';
|
||||
import { ISanityValidator } from './ISanityValidator';
|
||||
import { MetadataValidator } from './Validators/MetadataValidator';
|
||||
|
||||
const SanityValidators: ISanityValidator[] = [
|
||||
new MetadataValidator(),
|
||||
];
|
||||
|
||||
export function validateRuntimeSanity(
|
||||
options: ISanityCheckOptions,
|
||||
validators: readonly ISanityValidator[] = SanityValidators,
|
||||
): void {
|
||||
if (!options) {
|
||||
throw new Error('missing options');
|
||||
}
|
||||
if (!validators?.length) {
|
||||
throw new Error('missing validators');
|
||||
}
|
||||
const errorMessages = validators.reduce((errors, validator) => {
|
||||
if (validator.shouldValidate(options)) {
|
||||
errors.push(...validator.collectErrors());
|
||||
}
|
||||
return errors;
|
||||
}, new Array<string>());
|
||||
if (errorMessages.length > 0) {
|
||||
throw new Error(`Sanity check failed.\n${errorMessages.join('\n---\n')}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
|
||||
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
|
||||
import { ISanityCheckOptions } from '../ISanityCheckOptions';
|
||||
import { ISanityValidator } from '../ISanityValidator';
|
||||
|
||||
export class MetadataValidator implements ISanityValidator {
|
||||
private readonly metadata: IAppMetadata;
|
||||
|
||||
constructor(metadataFactory: () => IAppMetadata = () => AppMetadataFactory.Current) {
|
||||
this.metadata = metadataFactory();
|
||||
}
|
||||
|
||||
public shouldValidate(options: ISanityCheckOptions): boolean {
|
||||
return options.validateMetadata;
|
||||
}
|
||||
|
||||
public* collectErrors(): Iterable<string> {
|
||||
if (!this.metadata) {
|
||||
yield 'missing metadata';
|
||||
return;
|
||||
}
|
||||
const keyValues = capturePropertyValues(this.metadata);
|
||||
if (!Object.keys(keyValues).length) {
|
||||
yield 'Unable to capture metadata key/value pairs';
|
||||
return;
|
||||
}
|
||||
const keysMissingValue = getMissingMetadataKeys(keyValues);
|
||||
if (keysMissingValue.length > 0) {
|
||||
yield `Metadata keys missing: ${keysMissingValue.join(', ')}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getMissingMetadataKeys(keyValuePairs: Record<string, unknown>): string[] {
|
||||
return Object.entries(keyValuePairs)
|
||||
.reduce((acc, [key, value]) => {
|
||||
if (!value) {
|
||||
acc.push(key);
|
||||
}
|
||||
return acc;
|
||||
}, new Array<string>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures values of properties and getters from the provided instance.
|
||||
* Necessary because code transformations can make class getters non-enumerable during bundling.
|
||||
* This ensures that even if getters are non-enumerable, their values are still captured and used.
|
||||
*/
|
||||
function capturePropertyValues(instance: unknown): Record<string, unknown> {
|
||||
const obj: Record<string, unknown> = {};
|
||||
const descriptors = Object.getOwnPropertyDescriptors(instance.constructor.prototype);
|
||||
|
||||
// Capture regular properties from the instance
|
||||
for (const [key, value] of Object.entries(instance)) {
|
||||
obj[key] = value;
|
||||
}
|
||||
|
||||
// Capture getter properties from the instance's prototype
|
||||
for (const [key, descriptor] of Object.entries(descriptors)) {
|
||||
if (typeof descriptor.get === 'function') {
|
||||
obj[key] = descriptor.get.call(instance);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
Reference in New Issue
Block a user