Improve desktop runtime execution tests

Test improvements:

- Capture titles for all macOS windows, not just the frontmost.
- Incorporate missing application log files.
- Improve log clarity with enriched context.
- Improve application termination on macOS by reducing grace period.
- Ensure complete application termination on macOS.
- Validate Vue application loading through an initial log.
- Support ignoring environment-specific `stderr` errors.
- Do not fail the test if working directory cannot be deleted.
- Use retry pattern when installing dependencies due to network errors.

Refactorings:

- Migrate the test code to TypeScript.
- Replace deprecated `rmdir` with `rm` for error-resistant directory
  removal.
- Improve sanity checking by shifting from App.vue to Vue bootstrapper.
- Centralize environment variable management with `EnvironmentVariables`
  construct.
- Rename infrastructure/Environment to RuntimeEnvironment for clarity.
- Isolate WindowVariables and SystemOperations from RuntimeEnvironment.
- Inject logging via preloader.
- Correct mislabeled RuntimeSanity tests.

Configuration:

- Introduce `npm run check:desktop` for simplified execution.
- Omit `console.log` override due to `nodeIntegration` restrictions and
  reveal logging functionality using context-bridging.
This commit is contained in:
undergroundwires
2023-08-29 16:30:00 +02:00
parent 35be05df20
commit ad0576a752
146 changed files with 2418 additions and 1186 deletions

View File

@@ -1,6 +1,7 @@
export type Constructible<T, TArgs extends unknown[] = never> = {
prototype: T;
apply: (this: unknown, args: TArgs) => void;
readonly name: string;
};
export type PropertyKeys<T> = {

View File

@@ -1,25 +1,23 @@
import { IApplicationContext } from '@/application/Context/IApplicationContext';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { IApplication } from '@/domain/IApplication';
import { Environment } from '@/infrastructure/Environment/Environment';
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
import { RuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironment';
import { IApplicationFactory } from '../IApplicationFactory';
import { ApplicationFactory } from '../ApplicationFactory';
import { ApplicationContext } from './ApplicationContext';
export async function buildContext(
factory: IApplicationFactory = ApplicationFactory.Current,
environment = Environment.CurrentEnvironment,
environment = RuntimeEnvironment.CurrentEnvironment,
): Promise<IApplicationContext> {
if (!factory) { throw new Error('missing factory'); }
if (!environment) { throw new Error('missing environment'); }
const app = await factory.getApp();
const os = getInitialOs(app, environment);
const os = getInitialOs(app, environment.os);
return new ApplicationContext(app, os);
}
function getInitialOs(app: IApplication, environment: IEnvironment): OperatingSystem {
const currentOs = environment.os;
function getInitialOs(app: IApplication, currentOs: OperatingSystem): OperatingSystem {
const supportedOsList = app.getSupportedOsList();
if (supportedOsList.includes(currentOs)) {
return currentOs;

View File

@@ -7,14 +7,14 @@ import MacOsData from '@/application/collections/macos.yaml';
import LinuxData from '@/application/collections/linux.yaml';
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
import { Application } from '@/domain/Application';
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
import { IAppMetadata } from '@/infrastructure/EnvironmentVariables/IAppMetadata';
import { EnvironmentVariablesFactory } from '@/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory';
import { parseCategoryCollection } from './CategoryCollectionParser';
export function parseApplication(
categoryParser = parseCategoryCollection,
informationParser = parseProjectInformation,
metadata: IAppMetadata = AppMetadataFactory.Current.instance,
metadata: IAppMetadata = EnvironmentVariablesFactory.Current.instance,
collectionsData = PreParsedCollections,
): IApplication {
validateCollectionsData(collectionsData);

View File

@@ -1,13 +1,13 @@
import { IProjectInformation } from '@/domain/IProjectInformation';
import { ProjectInformation } from '@/domain/ProjectInformation';
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
import { IAppMetadata } from '@/infrastructure/EnvironmentVariables/IAppMetadata';
import { Version } from '@/domain/Version';
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
import { EnvironmentVariablesFactory } from '@/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory';
import { ConstructorArguments } from '@/TypeHelpers';
export function
parseProjectInformation(
metadata: IAppMetadata = AppMetadataFactory.Current.instance,
metadata: IAppMetadata = EnvironmentVariablesFactory.Current.instance,
createProjectInformation: ProjectInformationFactory = (
...args
) => new ProjectInformation(...args),

View File

@@ -1,32 +1,37 @@
import { Environment } from '@/infrastructure/Environment/Environment';
import { RuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironment';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { getWindowInjectedSystemOperations } from './SystemOperations/WindowInjectedSystemOperations';
export class CodeRunner {
constructor(
private readonly environment = Environment.CurrentEnvironment,
private readonly system = getWindowInjectedSystemOperations(),
private readonly environment = RuntimeEnvironment.CurrentEnvironment,
) {
if (!environment.system) {
if (!system) {
throw new Error('missing system operations');
}
}
public async runCode(code: string, folderName: string, fileExtension: string): Promise<void> {
const { system } = this.environment;
const dir = system.location.combinePaths(
system.operatingSystem.getTempDirectory(),
const { os } = this.environment;
const dir = this.system.location.combinePaths(
this.system.operatingSystem.getTempDirectory(),
folderName,
);
await system.fileSystem.createDirectory(dir, true);
const filePath = system.location.combinePaths(dir, `run.${fileExtension}`);
await system.fileSystem.writeToFile(filePath, code);
await system.fileSystem.setFilePermissions(filePath, '755');
const command = getExecuteCommand(filePath, this.environment);
system.command.execute(command);
await this.system.fileSystem.createDirectory(dir, true);
const filePath = this.system.location.combinePaths(dir, `run.${fileExtension}`);
await this.system.fileSystem.writeToFile(filePath, code);
await this.system.fileSystem.setFilePermissions(filePath, '755');
const command = getExecuteCommand(filePath, os);
this.system.command.execute(command);
}
}
function getExecuteCommand(scriptPath: string, environment: Environment): string {
switch (environment.os) {
function getExecuteCommand(
scriptPath: string,
currentOperatingSystem: OperatingSystem,
): string {
switch (currentOperatingSystem) {
case OperatingSystem.Linux:
return `x-terminal-emulator -e '${scriptPath}'`;
case OperatingSystem.macOS:
@@ -37,6 +42,6 @@ function getExecuteCommand(scriptPath: string, environment: Environment): string
case OperatingSystem.Windows:
return scriptPath;
default:
throw Error(`unsupported os: ${OperatingSystem[environment.os]}`);
throw Error(`unsupported os: ${OperatingSystem[currentOperatingSystem]}`);
}
}

View File

@@ -1,8 +0,0 @@
import { OperatingSystem } from '@/domain/OperatingSystem';
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
export interface IEnvironment {
readonly isDesktop: boolean;
readonly os: OperatingSystem | undefined;
readonly system: ISystemOperations | undefined;
}

View File

@@ -1,13 +0,0 @@
import { OperatingSystem } from '@/domain/OperatingSystem';
import { ISystemOperations } from './SystemOperations/ISystemOperations';
export type WindowVariables = {
system: ISystemOperations;
isDesktop: boolean;
os: OperatingSystem;
};
declare global {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Window extends WindowVariables { }
}

View File

@@ -0,0 +1,18 @@
import { IEnvironmentVariablesFactory } from './IEnvironmentVariablesFactory';
import { validateEnvironmentVariables } from './EnvironmentVariablesValidator';
import { ViteEnvironmentVariables } from './Vite/ViteEnvironmentVariables';
import { IEnvironmentVariables } from './IEnvironmentVariables';
export class EnvironmentVariablesFactory implements IEnvironmentVariablesFactory {
public static readonly Current = new EnvironmentVariablesFactory();
public readonly instance: IEnvironmentVariables;
protected constructor(validator: EnvironmentVariablesValidator = validateEnvironmentVariables) {
const environment = new ViteEnvironmentVariables();
validator(environment);
this.instance = environment;
}
}
export type EnvironmentVariablesValidator = typeof validateEnvironmentVariables;

View File

@@ -1,24 +1,24 @@
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
import { IEnvironmentVariables } from './IEnvironmentVariables';
/* Validation is externalized to keep the environment objects simple */
export function validateMetadata(metadata: IAppMetadata): void {
if (!metadata) {
throw new Error('missing metadata');
export function validateEnvironmentVariables(environment: IEnvironmentVariables): void {
if (!environment) {
throw new Error('missing environment');
}
const keyValues = capturePropertyValues(metadata);
const keyValues = capturePropertyValues(environment);
if (!Object.keys(keyValues).length) {
throw new Error('Unable to capture metadata key/value pairs');
throw new Error('Unable to capture key/value pairs');
}
const keysMissingValue = getMissingMetadataKeys(keyValues);
const keysMissingValue = getKeysMissingValues(keyValues);
if (keysMissingValue.length > 0) {
throw new Error(`Metadata keys missing: ${keysMissingValue.join(', ')}`);
throw new Error(`Environment keys missing: ${keysMissingValue.join(', ')}`);
}
}
function getMissingMetadataKeys(keyValuePairs: Record<string, unknown>): string[] {
function getKeysMissingValues(keyValuePairs: Record<string, unknown>): string[] {
return Object.entries(keyValuePairs)
.reduce((acc, [key, value]) => {
if (!value) {
if (!value && typeof value !== 'boolean') {
acc.push(key);
}
return acc;

View File

@@ -1,8 +1,5 @@
/**
* Represents essential metadata about the application.
*
* Designed to decouple the process of retrieving metadata
* (e.g., from the build environment) from the rest of the application.
*/
export interface IAppMetadata {
readonly version: string;

View File

@@ -0,0 +1,9 @@
import { IAppMetadata } from './IAppMetadata';
/**
* Designed to decouple the process of retrieving environment variables
* (e.g., from the build environment) from the rest of the application.
*/
export interface IEnvironmentVariables extends IAppMetadata {
readonly isNonProduction: boolean;
}

View File

@@ -0,0 +1,5 @@
import { IEnvironmentVariables } from './IEnvironmentVariables';
export interface IEnvironmentVariablesFactory {
readonly instance: IEnvironmentVariables;
}

View File

@@ -1,8 +1,13 @@
// Only variables prefixed with VITE_ are exposed to Vite-processed code
export const VITE_ENVIRONMENT_KEYS = {
export const VITE_USER_DEFINED_ENVIRONMENT_KEYS = {
VERSION: 'VITE_APP_VERSION',
NAME: 'VITE_APP_NAME',
SLOGAN: 'VITE_APP_SLOGAN',
REPOSITORY_URL: 'VITE_APP_REPOSITORY_URL',
HOMEPAGE_URL: 'VITE_APP_HOMEPAGE_URL',
} as const;
export const VITE_ENVIRONMENT_KEYS = {
...VITE_USER_DEFINED_ENVIRONMENT_KEYS,
DEV: 'DEV',
} as const;

View File

@@ -1,9 +1,9 @@
import { IAppMetadata } from '../IAppMetadata';
import { IEnvironmentVariables } from '../IEnvironmentVariables';
/**
* Provides the application's metadata using Vite's environment variables.
* Provides the application's environment variables.
*/
export class ViteAppMetadata implements IAppMetadata {
export class ViteEnvironmentVariables implements IEnvironmentVariables {
// Ensure the use of import.meta.env prefix for the following properties.
// Vue will replace these statically during production builds.
@@ -26,4 +26,8 @@ export class ViteAppMetadata implements IAppMetadata {
public get homepageUrl(): string {
return import.meta.env.VITE_APP_HOMEPAGE_URL;
}
public get isNonProduction(): boolean {
return import.meta.env.DEV;
}
}

View File

@@ -0,0 +1,13 @@
import { ILogger } from './ILogger';
export class ConsoleLogger implements ILogger {
constructor(private readonly globalConsole: Partial<Console> = console) {
if (!globalConsole) {
throw new Error('missing console');
}
}
public info(...params: unknown[]): void {
this.globalConsole.info(...params);
}
}

View File

@@ -0,0 +1,12 @@
import { ElectronLog } from 'electron-log';
import { ILogger } from './ILogger';
// Using plain-function rather than class so it can be used in Electron's context-bridging.
export function createElectronLogger(logger: Partial<ElectronLog>): ILogger {
if (!logger) {
throw new Error('missing logger');
}
return {
info: (...params) => logger.info(...params),
};
}

View File

@@ -0,0 +1,3 @@
export interface ILogger {
info (...params: unknown[]): void;
}

View File

@@ -0,0 +1,5 @@
import { ILogger } from './ILogger';
export interface ILoggerFactory {
readonly logger: ILogger;
}

View File

@@ -0,0 +1,5 @@
import { ILogger } from './ILogger';
export class NoopLogger implements ILogger {
public info(): void { /* NOOP */ }
}

View File

@@ -0,0 +1,20 @@
import { WindowVariables } from '../WindowVariables/WindowVariables';
import { ILogger } from './ILogger';
export class WindowInjectedLogger implements ILogger {
private readonly logger: ILogger;
constructor(windowVariables: WindowVariables = window) {
if (!windowVariables) {
throw new Error('missing window');
}
if (!windowVariables.log) {
throw new Error('missing log');
}
this.logger = windowVariables.log;
}
public info(...params: unknown[]): void {
this.logger.info(...params);
}
}

View File

@@ -1,18 +0,0 @@
import { IAppMetadata } from './IAppMetadata';
import { IAppMetadataFactory } from './IAppMetadataFactory';
import { validateMetadata } from './MetadataValidator';
import { ViteAppMetadata } from './Vite/ViteAppMetadata';
export class AppMetadataFactory implements IAppMetadataFactory {
public static readonly Current = new AppMetadataFactory();
public readonly instance: IAppMetadata;
protected constructor(validator: MetadataValidator = validateMetadata) {
const metadata = new ViteAppMetadata();
validator(metadata);
this.instance = metadata;
}
}
export type MetadataValidator = typeof validateMetadata;

View File

@@ -1,5 +0,0 @@
import { IAppMetadata } from './IAppMetadata';
export interface IAppMetadataFactory {
readonly instance: IAppMetadata;
}

View File

@@ -0,0 +1,7 @@
import { OperatingSystem } from '@/domain/OperatingSystem';
export interface IRuntimeEnvironment {
readonly isDesktop: boolean;
readonly os: OperatingSystem | undefined;
readonly isNonProduction: boolean;
}

View File

@@ -1,29 +1,29 @@
import { OperatingSystem } from '@/domain/OperatingSystem';
import { ISystemOperations } from '@/infrastructure/Environment/SystemOperations/ISystemOperations';
import { WindowVariables } from '@/infrastructure/WindowVariables/WindowVariables';
import { IEnvironmentVariables } from '@/infrastructure/EnvironmentVariables/IEnvironmentVariables';
import { EnvironmentVariablesFactory } from '@/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory';
import { BrowserOsDetector } from './BrowserOs/BrowserOsDetector';
import { IBrowserOsDetector } from './BrowserOs/IBrowserOsDetector';
import { IEnvironment } from './IEnvironment';
import { WindowVariables } from './WindowVariables';
import { validateWindowVariables } from './WindowVariablesValidator';
import { IRuntimeEnvironment } from './IRuntimeEnvironment';
export class Environment implements IEnvironment {
public static readonly CurrentEnvironment: IEnvironment = new Environment(window);
export class RuntimeEnvironment implements IRuntimeEnvironment {
public static readonly CurrentEnvironment: IRuntimeEnvironment = new RuntimeEnvironment(window);
public readonly isDesktop: boolean;
public readonly os: OperatingSystem | undefined;
public readonly system: ISystemOperations | undefined;
public readonly isNonProduction: boolean;
protected constructor(
window: Partial<Window>,
environmentVariables: IEnvironmentVariables = EnvironmentVariablesFactory.Current.instance,
browserOsDetector: IBrowserOsDetector = new BrowserOsDetector(),
windowValidator: WindowValidator = validateWindowVariables,
) {
if (!window) {
throw new Error('missing window');
}
windowValidator(window);
this.isNonProduction = environmentVariables.isNonProduction;
this.isDesktop = isDesktop(window);
if (this.isDesktop) {
this.os = window?.os;
@@ -34,7 +34,6 @@ export class Environment implements IEnvironment {
this.os = browserOsDetector.detect(userAgent);
}
}
this.system = window?.system;
}
}
@@ -45,5 +44,3 @@ function getUserAgent(window: Partial<Window>): string {
function isDesktop(window: Partial<WindowVariables>): boolean {
return window?.isDesktop === true;
}
export type WindowValidator = typeof validateWindowVariables;

View File

@@ -1,4 +1,4 @@
export interface ISanityCheckOptions {
readonly validateMetadata: boolean;
readonly validateEnvironment: boolean;
readonly validateEnvironmentVariables: boolean;
readonly validateWindowVariables: boolean;
}

View File

@@ -1,9 +1,9 @@
import { ISanityCheckOptions } from './Common/ISanityCheckOptions';
import { ISanityValidator } from './Common/ISanityValidator';
import { MetadataValidator } from './Validators/MetadataValidator';
import { EnvironmentVariablesValidator } from './Validators/EnvironmentVariablesValidator';
const DefaultSanityValidators: ISanityValidator[] = [
new MetadataValidator(),
new EnvironmentVariablesValidator(),
];
/* Helps to fail-fast on errors */

View File

@@ -1,16 +0,0 @@
import { Environment } from '@/infrastructure/Environment/Environment';
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
export class EnvironmentValidator extends FactoryValidator<IEnvironment> {
constructor(factory: FactoryFunction<IEnvironment> = () => Environment.CurrentEnvironment) {
super(factory);
}
public override name = 'environment';
public override shouldValidate(options: ISanityCheckOptions): boolean {
return options.validateEnvironment;
}
}

View File

@@ -0,0 +1,20 @@
import { IEnvironmentVariables } from '@/infrastructure/EnvironmentVariables/IEnvironmentVariables';
import { EnvironmentVariablesFactory } from '@/infrastructure/EnvironmentVariables/EnvironmentVariablesFactory';
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
export class EnvironmentVariablesValidator extends FactoryValidator<IEnvironmentVariables> {
constructor(
factory: FactoryFunction<IEnvironmentVariables> = () => {
return EnvironmentVariablesFactory.Current.instance;
},
) {
super(factory);
}
public override name = 'environment variables';
public override shouldValidate(options: ISanityCheckOptions): boolean {
return options.validateEnvironmentVariables;
}
}

View File

@@ -1,16 +0,0 @@
import { IAppMetadata } from '@/infrastructure/Metadata/IAppMetadata';
import { AppMetadataFactory } from '@/infrastructure/Metadata/AppMetadataFactory';
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
export class MetadataValidator extends FactoryValidator<IAppMetadata> {
constructor(factory: FactoryFunction<IAppMetadata> = () => AppMetadataFactory.Current.instance) {
super(factory);
}
public override name = 'metadata';
public override shouldValidate(options: ISanityCheckOptions): boolean {
return options.validateMetadata;
}
}

View File

@@ -0,0 +1,15 @@
import { WindowVariables } from '@/infrastructure/WindowVariables/WindowVariables';
import { ISanityCheckOptions } from '../Common/ISanityCheckOptions';
import { FactoryValidator, FactoryFunction } from '../Common/FactoryValidator';
export class WindowVariablesValidator extends FactoryValidator<WindowVariables> {
constructor(factory: FactoryFunction<WindowVariables> = () => window) {
super(factory);
}
public override name = 'window variables';
public override shouldValidate(options: ISanityCheckOptions): boolean {
return options.validateWindowVariables;
}
}

View File

@@ -0,0 +1,14 @@
import { WindowVariables } from '../WindowVariables/WindowVariables';
import { ISystemOperations } from './ISystemOperations';
export function getWindowInjectedSystemOperations(
windowVariables: Partial<WindowVariables> = window,
): ISystemOperations {
if (!windowVariables) {
throw new Error('missing window');
}
if (!windowVariables.system) {
throw new Error('missing system');
}
return windowVariables.system;
}

View File

@@ -0,0 +1,11 @@
import { OperatingSystem } from '@/domain/OperatingSystem';
import { ISystemOperations } from '@/infrastructure/SystemOperations/ISystemOperations';
import { ILogger } from '@/infrastructure/Log/ILogger';
/* Primary entry point for platform-specific injections */
export interface WindowVariables {
readonly system: ISystemOperations;
readonly isDesktop: boolean;
readonly os: OperatingSystem;
readonly log: ILogger;
}

View File

@@ -6,11 +6,8 @@ import { WindowVariables } from './WindowVariables';
* Checks for consistency in runtime environment properties injected by Electron preloader.
*/
export function validateWindowVariables(variables: Partial<WindowVariables>) {
if (!variables) {
throw new Error('missing variables');
}
if (!isObject(variables)) {
throw new Error(`window is not an object but ${typeof variables}`);
throw new Error('window is not an object');
}
const errors = [...testEveryProperty(variables)];
if (errors.length > 0) {
@@ -25,6 +22,7 @@ function* testEveryProperty(variables: Partial<WindowVariables>): Iterable<strin
os: testOperatingSystem(variables.os),
isDesktop: testIsDesktop(variables.isDesktop),
system: testSystem(variables),
log: testLogger(variables),
};
for (const [propertyName, testResult] of Object.entries(tests)) {
@@ -47,11 +45,18 @@ function testOperatingSystem(os: unknown): boolean {
.includes(os);
}
function testLogger(variables: Partial<WindowVariables>): boolean {
if (!variables.isDesktop) {
return true;
}
return isObject(variables.log);
}
function testSystem(variables: Partial<WindowVariables>): boolean {
if (!variables.isDesktop) {
return true;
}
return variables.system !== undefined && isObject(variables.system);
return isObject(variables.system);
}
function testIsDesktop(isDesktop: unknown): boolean {
@@ -70,7 +75,7 @@ function isBoolean(variable: unknown): variable is boolean {
}
function isObject(variable: unknown): variable is object {
return typeof variable === 'object'
&& variable !== null // the data type of null is an object
return Boolean(variable) // the data type of null is an object
&& typeof variable === 'object'
&& !Array.isArray(variable);
}

View File

@@ -0,0 +1,6 @@
import { WindowVariables } from './WindowVariables';
declare global {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Window extends WindowVariables { }
}

View File

@@ -3,6 +3,8 @@ import { IconBootstrapper } from './Modules/IconBootstrapper';
import { VueConstructor, IVueBootstrapper } from './IVueBootstrapper';
import { VueBootstrapper } from './Modules/VueBootstrapper';
import { TooltipBootstrapper } from './Modules/TooltipBootstrapper';
import { RuntimeSanityValidator } from './Modules/RuntimeSanityValidator';
import { AppInitializationLogger } from './Modules/AppInitializationLogger';
export class ApplicationBootstrapper implements IVueBootstrapper {
public bootstrap(vue: VueConstructor): void {
@@ -18,6 +20,8 @@ export class ApplicationBootstrapper implements IVueBootstrapper {
new TreeBootstrapper(),
new VueBootstrapper(),
new TooltipBootstrapper(),
new RuntimeSanityValidator(),
new AppInitializationLogger(),
];
}
}

View File

@@ -0,0 +1,25 @@
import { RuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironment';
import { IRuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/IRuntimeEnvironment';
import { ConsoleLogger } from '@/infrastructure/Log/ConsoleLogger';
import { ILogger } from '@/infrastructure/Log/ILogger';
import { ILoggerFactory } from '@/infrastructure/Log/ILoggerFactory';
import { NoopLogger } from '@/infrastructure/Log/NoopLogger';
import { WindowInjectedLogger } from '@/infrastructure/Log/WindowInjectedLogger';
export class ClientLoggerFactory implements ILoggerFactory {
public static readonly Current: ILoggerFactory = new ClientLoggerFactory();
public readonly logger: ILogger;
protected constructor(environment: IRuntimeEnvironment = RuntimeEnvironment.CurrentEnvironment) {
if (environment.isDesktop) {
this.logger = new WindowInjectedLogger();
return;
}
if (environment.isNonProduction) {
this.logger = new ConsoleLogger();
return;
}
this.logger = new NoopLogger();
}
}

View File

@@ -2,15 +2,15 @@ import { InjectionKey, provide } from 'vue';
import { useCollectionState } from '@/presentation/components/Shared/Hooks/UseCollectionState';
import { useApplication } from '@/presentation/components/Shared/Hooks/UseApplication';
import {
useCollectionStateKey, useApplicationKey, useEnvironmentKey,
useCollectionStateKey, useApplicationKey, useRuntimeEnvironmentKey,
} from '@/presentation/injectionSymbols';
import { IApplicationContext } from '@/application/Context/IApplicationContext';
import { Environment } from '@/infrastructure/Environment/Environment';
import { RuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/RuntimeEnvironment';
export function provideDependencies(context: IApplicationContext) {
registerSingleton(useApplicationKey, useApplication(context.app));
registerTransient(useCollectionStateKey, () => useCollectionState(context));
registerSingleton(useEnvironmentKey, Environment.CurrentEnvironment);
registerSingleton(useRuntimeEnvironmentKey, RuntimeEnvironment.CurrentEnvironment);
}
function registerSingleton<T>(

View File

@@ -0,0 +1,14 @@
import { ILogger } from '@/infrastructure/Log/ILogger';
import { IVueBootstrapper } from '../IVueBootstrapper';
import { ClientLoggerFactory } from '../ClientLoggerFactory';
export class AppInitializationLogger implements IVueBootstrapper {
constructor(
private readonly logger: ILogger = ClientLoggerFactory.Current.logger,
) { }
public bootstrap(): void {
// Do not remove [APP_INIT]; it's a marker used in tests.
this.logger.info('[APP_INIT] Application is initialized.');
}
}

View File

@@ -0,0 +1,15 @@
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
import { IVueBootstrapper } from '../IVueBootstrapper';
export class RuntimeSanityValidator implements IVueBootstrapper {
constructor(private readonly validator = validateRuntimeSanity) {
}
public bootstrap(): void {
this.validator({
validateEnvironmentVariables: true,
validateWindowVariables: true,
});
}
}

View File

@@ -18,7 +18,6 @@ import TheCodeButtons from '@/presentation/components/Code/CodeButtons/TheCodeBu
import TheScriptArea from '@/presentation/components/Scripts/TheScriptArea.vue';
import TheSearchBar from '@/presentation/components/TheSearchBar.vue';
import { buildContext } from '@/application/Context/ApplicationContextFactory';
import { validateRuntimeSanity } from '@/infrastructure/RuntimeSanity/SanityChecks';
import { provideDependencies } from '../bootstrapping/DependencyProvider';
const singletonAppContext = await buildContext();
@@ -33,10 +32,6 @@ export default defineComponent({
},
setup() {
provideDependencies(singletonAppContext); // In Vue 3.0 we can move it to main.ts
validateRuntimeSanity({
validateMetadata: true,
validateEnvironment: true,
});
},
});
</script>

View File

@@ -29,11 +29,10 @@
import {
defineComponent, ref, computed, inject,
} from 'vue';
import { useCollectionStateKey, useEnvironmentKey } from '@/presentation/injectionSymbols';
import { useCollectionStateKey, useRuntimeEnvironmentKey } from '@/presentation/injectionSymbols';
import { SaveFileDialog, FileType } from '@/infrastructure/SaveFileDialog';
import { Clipboard } from '@/infrastructure/Clipboard';
import ModalDialog from '@/presentation/components/Shared/Modal/ModalDialog.vue';
import { Environment } from '@/infrastructure/Environment/Environment';
import { IReadOnlyCategoryCollectionState } from '@/application/Context/State/ICategoryCollectionState';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { IApplicationCode } from '@/application/Context/State/Code/IApplicationCode';
@@ -56,10 +55,10 @@ export default defineComponent({
const {
currentState, currentContext, onStateChange, events,
} = inject(useCollectionStateKey)();
const { isDesktop } = inject(useEnvironmentKey);
const { os, isDesktop } = inject(useRuntimeEnvironmentKey);
const areInstructionsVisible = ref(false);
const canRun = computed<boolean>(() => getCanRunState(currentState.value.os, isDesktop));
const canRun = computed<boolean>(() => getCanRunState(currentState.value.os, isDesktop, os));
const fileName = computed<string>(() => buildFileName(currentState.value.collection.scripting));
const hasCode = ref(false);
const instructions = computed<IInstructionListData | undefined>(() => getDownloadInstructions(
@@ -122,8 +121,12 @@ function getDownloadInstructions(
return getInstructions(os, fileName);
}
function getCanRunState(selectedOs: OperatingSystem, isDesktopVersion: boolean): boolean {
const isRunningOnSelectedOs = selectedOs === Environment.CurrentEnvironment.os;
function getCanRunState(
selectedOs: OperatingSystem,
isDesktopVersion: boolean,
hostOs: OperatingSystem,
): boolean {
const isRunningOnSelectedOs = selectedOs === hostOs;
return isDesktopVersion && isRunningOnSelectedOs;
}

View File

@@ -1,8 +0,0 @@
import { IEnvironment } from '@/infrastructure/Environment/IEnvironment';
export function useEnvironment(environment: IEnvironment) {
if (!environment) {
throw new Error('missing environment');
}
return environment;
}

View File

@@ -0,0 +1,8 @@
import { IRuntimeEnvironment } from '@/infrastructure/RuntimeEnvironment/IRuntimeEnvironment';
export function useRuntimeEnvironment(environment: IRuntimeEnvironment) {
if (!environment) {
throw new Error('missing environment');
}
return environment;
}

View File

@@ -20,7 +20,7 @@
<script lang="ts">
import { defineComponent, inject } from 'vue';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { useEnvironmentKey } from '@/presentation/injectionSymbols';
import { useRuntimeEnvironmentKey } from '@/presentation/injectionSymbols';
import DownloadUrlListItem from './DownloadUrlListItem.vue';
const supportedOperativeSystems: readonly OperatingSystem[] = [
@@ -34,7 +34,7 @@ export default defineComponent({
DownloadUrlListItem,
},
setup() {
const { os: currentOs } = inject(useEnvironmentKey);
const { os: currentOs } = inject(useRuntimeEnvironmentKey);
const supportedDesktops = [
...supportedOperativeSystems,
].sort((os) => (os === currentOs ? 0 : 1));

View File

@@ -14,7 +14,7 @@ import {
defineComponent, PropType, computed,
inject,
} from 'vue';
import { useApplicationKey, useEnvironmentKey } from '@/presentation/injectionSymbols';
import { useApplicationKey, useRuntimeEnvironmentKey } from '@/presentation/injectionSymbols';
import { OperatingSystem } from '@/domain/OperatingSystem';
export default defineComponent({
@@ -26,7 +26,7 @@ export default defineComponent({
},
setup(props) {
const { info } = inject(useApplicationKey);
const { os: currentOs } = inject(useEnvironmentKey);
const { os: currentOs } = inject(useRuntimeEnvironmentKey);
const isCurrentOs = computed<boolean>(() => {
return currentOs === props.operatingSystem;

View File

@@ -42,12 +42,12 @@
<script lang="ts">
import { defineComponent, computed, inject } from 'vue';
import { useApplicationKey, useEnvironmentKey } from '@/presentation/injectionSymbols';
import { useApplicationKey, useRuntimeEnvironmentKey } from '@/presentation/injectionSymbols';
export default defineComponent({
setup() {
const { info } = inject(useApplicationKey);
const { isDesktop } = inject(useEnvironmentKey);
const { isDesktop } = inject(useRuntimeEnvironmentKey);
const repositoryUrl = computed<string>(() => info.repositoryUrl);
const feedbackUrl = computed<string>(() => info.feedbackUrl);

View File

@@ -48,7 +48,7 @@ import {
defineComponent, ref, computed, inject,
} from 'vue';
import ModalDialog from '@/presentation/components/Shared/Modal/ModalDialog.vue';
import { useApplicationKey, useEnvironmentKey } from '@/presentation/injectionSymbols';
import { useApplicationKey, useRuntimeEnvironmentKey } from '@/presentation/injectionSymbols';
import DownloadUrlList from './DownloadUrlList.vue';
import PrivacyPolicy from './PrivacyPolicy.vue';
@@ -60,7 +60,7 @@ export default defineComponent({
},
setup() {
const { info } = inject(useApplicationKey);
const { isDesktop } = inject(useEnvironmentKey);
const { isDesktop } = inject(useRuntimeEnvironmentKey);
const isPrivacyDialogVisible = ref(false);

View File

@@ -6,7 +6,6 @@ import log from 'electron-log';
import fetch from 'cross-fetch';
import { ProjectInformation } from '@/domain/ProjectInformation';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { ViteAppMetadata } from '@/infrastructure/Metadata/Vite/ViteAppMetadata';
import { Version } from '@/domain/Version';
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
import { UpdateProgressBar } from './UpdateProgressBar';
@@ -29,7 +28,7 @@ export async function handleManualUpdate(info: UpdateInfo) {
}
function getTargetProject(targetVersion: string) {
const existingProject = parseProjectInformation(new ViteAppMetadata());
const existingProject = parseProjectInformation();
const targetProject = new ProjectInformation(
existingProject.name,
new Version(targetVersion),

View File

@@ -25,11 +25,11 @@ protocol.registerSchemesAsPrivileged([
setupLogger();
validateRuntimeSanity({
// Metadata is used by manual updates.
validateMetadata: true,
validateEnvironmentVariables: true,
// Environment is populated by the preload script and is in the renderer's context;
// it's not directly accessible from the main process.
validateEnvironment: false,
validateWindowVariables: false,
});
function createWindow() {
@@ -151,7 +151,4 @@ function getWindowSize(idealWidth: number, idealHeight: number) {
function setupLogger(): void {
log.transports.file.level = 'silly';
if (!isDevelopment) {
Object.assign(console, log.functions); // override console.log, console.warn etc.
}
}

View File

@@ -1,14 +1,19 @@
import { createNodeSystemOperations } from '@/infrastructure/Environment/SystemOperations/NodeSystemOperations';
import { WindowVariables } from '@/infrastructure/Environment/WindowVariables';
import log from 'electron-log';
import { createNodeSystemOperations } from '@/infrastructure/SystemOperations/NodeSystemOperations';
import { createElectronLogger } from '@/infrastructure/Log/ElectronLogger';
import { ILogger } from '@/infrastructure/Log/ILogger';
import { WindowVariables } from '@/infrastructure/WindowVariables/WindowVariables';
import { convertPlatformToOs } from './NodeOsMapper';
export function provideWindowVariables(
createSystem = createNodeSystemOperations,
createLogger: () => ILogger = () => createElectronLogger(log),
convertToOs = convertPlatformToOs,
): WindowVariables {
return {
system: createSystem(),
isDesktop: true,
log: createLogger(),
os: convertToOs(process.platform),
};
}

View File

@@ -8,10 +8,10 @@ import { provideWindowVariables } from './WindowVariablesProvider';
validateRuntimeSanity({
// Validate metadata as a preventive measure for fail-fast,
// even if it's not currently used in the preload script.
validateMetadata: true,
validateEnvironmentVariables: true,
// The preload script cannot access variables on the window object.
validateEnvironment: false,
validateWindowVariables: false,
});
const windowVariables = provideWindowVariables();

View File

@@ -1,11 +1,11 @@
import { useCollectionState } from '@/presentation/components/Shared/Hooks/UseCollectionState';
import { useApplication } from '@/presentation/components/Shared/Hooks/UseApplication';
import { useEnvironment } from '@/presentation/components/Shared/Hooks/UseEnvironment';
import { useRuntimeEnvironment } from '@/presentation/components/Shared/Hooks/UseRuntimeEnvironment';
import type { InjectionKey } from 'vue';
export const useCollectionStateKey = defineTransientKey<ReturnType<typeof useCollectionState>>('useCollectionState');
export const useApplicationKey = defineSingletonKey<ReturnType<typeof useApplication>>('useApplication');
export const useEnvironmentKey = defineSingletonKey<ReturnType<typeof useEnvironment>>('useEnvironment');
export const useRuntimeEnvironmentKey = defineSingletonKey<ReturnType<typeof useRuntimeEnvironment>>('useRuntimeEnvironment');
function defineSingletonKey<T>(key: string) {
return Symbol(key) as InjectionKey<T>;