Skip to content

Commit

Permalink
chore: move stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed May 14, 2024
1 parent 5238c1c commit 5744fda
Show file tree
Hide file tree
Showing 32 changed files with 708 additions and 736 deletions.
4 changes: 2 additions & 2 deletions packages/midnight-smoker/src/component/reporter/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ReifiedComponent} from '#component';
import {fromUnknownError} from '#error';
import {ReporterError} from '#error/reporter-error';
import {type EventData, type EventName} from '#event/events';
import {type DataForEvent, type EventName} from '#event/events';
import {type PluginMetadata} from '#plugin';
import {
type ReporterContext,
Expand Down Expand Up @@ -42,7 +42,7 @@ export class Reporter<Ctx = unknown> extends ReifiedComponent<
return this.def.description;
}

public async invokeListener<T extends EventName>(data: EventData<T>) {
public async invokeListener<T extends EventName>(data: DataForEvent<T>) {
// await Promise.resolve();
// XXX don't like these casts
if (this.hasListener(data.type)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/midnight-smoker/src/component/schema/reporter-def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* @packageDocumentation
*/
import {type EventData, type EventName, type Events} from '#event';
import {type DataForEvent, type EventData, type EventName} from '#event';
import {BaseSmokerOptionsSchema, type SmokerOptions} from '#options/options';
import {type StaticPluginMetadata} from '#plugin/static-metadata';
import {
Expand Down Expand Up @@ -48,7 +48,7 @@ export type SomeReporterContext = ReporterContext<object>;
export type ReporterListener<Evt extends EventName, Ctx = unknown> = (
this: void,
ctx: ReporterContext<Ctx>,
data: EventData<Evt>,
data: DataForEvent<Evt>,
) => void | Promise<void>;

export type ReporterSetupFn<Ctx = unknown> = (
Expand All @@ -65,7 +65,7 @@ export type ReporterTeardownFn<Ctx = unknown> = (
*/

export type ReporterListeners<Ctx = unknown> = {
[K in keyof Events as `on${K}`]: ReporterListener<K, Ctx>;
[K in keyof EventData as `on${K}`]: ReporterListener<K, Ctx>;
};

/**
Expand Down
11 changes: 6 additions & 5 deletions packages/midnight-smoker/src/event/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {type SmokerEventData} from './smoker-events';
/**
* Describes the data emitted by each event.
*/
export type Events = InstallEventData &
export type EventData = InstallEventData &
PackEventData &
LintEventData &
ScriptEventData &
Expand All @@ -16,13 +16,14 @@ export type Events = InstallEventData &
/**
* Names of all events emitted by `midnight-smoker`
*/
export type EventName = keyof Events;
export type EventName = keyof EventData;

/**
* Data associated with a specific event
* Data associated with a specific event with an additional `type` field
* containing the event name
*
* @template T - The event name
*/
export type EventData<T extends EventName> = {
[K in T]: {type: K} & Events[K];
export type DataForEvent<T extends EventName> = {
[K in T]: {type: K} & EventData[K];
}[T];
19 changes: 10 additions & 9 deletions packages/midnight-smoker/src/event/script-events.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {
ExecResult,
RunScriptManifest,
RunScriptResult,
ScriptError,
StaticPkgManagerSpec,
} from '../component';
import type {ScriptEvent} from './event-constants';
import type {PkgManagerEventBase} from './pkg-manager-events';
import {
type ExecResult,
type RunScriptManifest,
type RunScriptResult,
type ScriptError,
type StaticPkgManagerSpec,
} from '#schema';
import {type ScriptEvent} from './event-constants';
import {type PkgManagerEventBase} from './pkg-manager-events';

export interface ScriptEventData {
[ScriptEvent.PkgManagerRunScriptsBegin]: PkgManagerRunScriptsBeginEventData;
Expand Down Expand Up @@ -99,6 +99,7 @@ export interface RunScriptOkEventData extends RunScriptEventDataBase {

export interface RunScriptFailedEventData extends RunScriptEventDataBase {
error: ScriptError;
rawResult?: ExecResult;
}

export interface RunScriptSkippedEventData extends RunScriptEventDataBase {
Expand Down
3 changes: 0 additions & 3 deletions packages/midnight-smoker/src/machine/bus/bus-machine.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/midnight-smoker/src/machine/bus/index.ts

This file was deleted.

107 changes: 107 additions & 0 deletions packages/midnight-smoker/src/machine/control/control-machine-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {type DataForEvent, type EventData, type SmokerEvent} from '#event';
import {type LoaderMachineOutput} from '#machine/loader';
import {type PkgManagerMachineOutput} from '#machine/pkg-manager';
import {type ReporterMachineOutput} from '#machine/reporter';
import {type AnyInstallEvent, type CtrlInstallEvents} from './install-events';
import {type AnyLintEvent, type CtrlLintEvents} from './lint-events';
import {type AnyPackEvent, type CtrlPackEvents} from './pack-events';
import {type AnyScriptEvent, type CtrlScriptEvents} from './script-events';

export type ComputedPkgEventFields = 'currentPkg' | 'totalPkgs';

export type EventMap = {
'SCRIPT.RUN_SCRIPT_BEGIN': typeof SmokerEvent.RunScriptBegin;
'SCRIPT.RUN_SCRIPT_FAILED': typeof SmokerEvent.RunScriptFailed;
'SCRIPT.RUN_SCRIPT_OK': typeof SmokerEvent.RunScriptOk;
'SCRIPT.RUN_SCRIPT_SKIPPED': typeof SmokerEvent.RunScriptSkipped;
'SCRIPT.PKG_MANAGER_RUN_SCRIPTS_BEGIN': typeof SmokerEvent.PkgManagerRunScriptsBegin;
'SCRIPT.PKG_MANAGER_RUN_SCRIPTS_FAILED': typeof SmokerEvent.PkgManagerRunScriptsFailed;
'SCRIPT.PKG_MANAGER_RUN_SCRIPTS_OK': typeof SmokerEvent.PkgManagerRunScriptsOk;
'PACK.PKG_PACK_BEGIN': typeof SmokerEvent.PkgPackBegin;
'PACK.PKG_PACK_FAILED': typeof SmokerEvent.PkgPackFailed;
'PACK.PKG_PACK_OK': typeof SmokerEvent.PkgPackOk;
'PACK.PKG_MANAGER_PACK_BEGIN': typeof SmokerEvent.PkgManagerPackBegin;
'PACK.PKG_MANAGER_PACK_FAILED': typeof SmokerEvent.PkgManagerPackFailed;
'PACK.PKG_MANAGER_PACK_OK': typeof SmokerEvent.PkgManagerPackOk;
'INSTALL.PKG_MANAGER_INSTALL_BEGIN': typeof SmokerEvent.PkgManagerInstallBegin;
'INSTALL.PKG_MANAGER_INSTALL_FAILED': typeof SmokerEvent.PkgManagerInstallFailed;
'INSTALL.PKG_MANAGER_INSTALL_OK': typeof SmokerEvent.PkgManagerInstallOk;
'INSTALL.PKG_INSTALL_BEGIN': typeof SmokerEvent.PkgInstallBegin;
'INSTALL.PKG_INSTALL_FAILED': typeof SmokerEvent.PkgInstallFailed;
'INSTALL.PKG_INSTALL_OK': typeof SmokerEvent.PkgInstallOk;
'LINT.RULE_BEGIN': typeof SmokerEvent.RuleBegin;
'LINT.RULE_FAILED': typeof SmokerEvent.RuleFailed;
'LINT.RULE_OK': typeof SmokerEvent.RuleOk;
'LINT.RULE_ERROR': typeof SmokerEvent.RuleError;
'LINT.PKG_MANAGER_LINT_BEGIN': typeof SmokerEvent.PkgManagerLintBegin;
'LINT.PKG_MANAGER_LINT_FAILED': typeof SmokerEvent.PkgManagerLintFailed;
'LINT.PKG_MANAGER_LINT_OK': typeof SmokerEvent.PkgManagerLintOk;
};

export type BusEvent =
| CtrlInstallEvents
| CtrlLintEvents
| CtrlPackEvents
| CtrlScriptEvents;

export type ToSmokerEvent<T extends BusEvent> = DataForEvent<
EventMap[T['type']]
>;

export type CtrlEvents =
| AnyInstallEvent
| AnyLintEvent
| AnyPackEvent
| AnyScriptEvent
| CtrlHaltEvent
| CtrlInstallEvents
| CtrlLingeredEvent
| CtrlLintEvents
| CtrlLoaderMachineDoneEvent
| CtrlPackEvents
| CtrlPkgManagerMachineDoneEvent
| CtrlReporterDoneEvent
| CtrlScriptEvents
| VerbatimEventData;

export type CtrlMachineEmitted = DataForEvent<keyof EventData>;

/**
* These events are emitted by the bus machines, and are identical to the "real"
* events emitted by midnight-smoker.
*/
export type VerbatimEventData = DataForEvent<VerbatimEventNames>;

export type VerbatimEventNames =
| typeof SmokerEvent.LintOk
| typeof SmokerEvent.LintFailed
| typeof SmokerEvent.PackOk
| typeof SmokerEvent.PackFailed
| typeof SmokerEvent.InstallOk
| typeof SmokerEvent.InstallFailed
| typeof SmokerEvent.RunScriptsFailed
| typeof SmokerEvent.RunScriptsOk;

export interface CtrlHaltEvent {
type: 'HALT';
}

export interface CtrlLingeredEvent {
directory: string;
type: 'LINGERED';
}

export interface CtrlLoaderMachineDoneEvent {
output: LoaderMachineOutput;
type: 'xstate.done.actor.LoaderMachine.*';
}

export interface CtrlPkgManagerMachineDoneEvent {
output: PkgManagerMachineOutput;
type: 'xstate.done.actor.PkgManagerMachine.*';
}

export interface CtrlReporterDoneEvent {
output: ReporterMachineOutput;
type: 'xstate.done.actor.ReporterMachine.*';
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {DEFAULT_EXECUTOR_ID, SYSTEM_EXECUTOR_ID} from '#constants';
import {fromUnknownError} from '#error';
import {SmokerEvent, type EventData} from '#event';
import {SmokerEvent, type DataForEvent} from '#event';
import {
LoadableComponents,
LoaderMachine,
type LoaderMachineOutputOk,
type PkgManagerInitPayload,
type ReporterInitPayload,
type RuleInitPayload,
} from '#machine/loader-machine';
} from '#machine/loader';
import {PkgManagerMachine} from '#machine/pkg-manager';
import {
ReporterMachine,
Expand All @@ -26,7 +26,7 @@ import {
type WorkspaceInfo,
} from '#schema';
import {FileManager} from '#util/filemanager';
import {isEmpty} from 'lodash';
import {isEmpty, map, uniqBy} from 'lodash';
import assert from 'node:assert';
import {type PackageJson} from 'type-fest';
import {
Expand Down Expand Up @@ -139,7 +139,7 @@ function delta(startTime: number): string {
export const ControlMachine = setup({
types: {
context: {} as CtrlMachineContext,
emitted: {} as Event.ControlMachineEmitted,
emitted: {} as Event.CtrlMachineEmitted,
events: {} as Event.CtrlEvents,
input: {} as CtrlMachineInput,
output: {} as CtrlMachineOutput,
Expand Down Expand Up @@ -211,7 +211,7 @@ export const ControlMachine = setup({
assignWorkspaceInfo: assign({
workspaceInfo: (_, workspaceInfo: WorkspaceInfo[]) => workspaceInfo,
uniquePkgNames: (_, workspaceInfo: WorkspaceInfo[]) =>
MachineUtil.uniquePkgNames(workspaceInfo),
map(uniqBy(workspaceInfo, 'pkgName'), 'pkgName'),
}),

/**
Expand Down Expand Up @@ -413,7 +413,7 @@ export const ControlMachine = setup({
report: enqueueActions(
(
{enqueue, context: {reporterMachineRefs}},
event: Event.ControlMachineEmitted,
event: Event.CtrlMachineEmitted,
) => {
for (const reporterMachineRef of Object.values(reporterMachineRefs)) {
enqueue.sendTo(reporterMachineRef, {type: 'EVENT', event});
Expand Down Expand Up @@ -1004,14 +1004,14 @@ export const ControlMachine = setup({
type: 'report',
params: ({
context: {pluginRegistry, smokerOptions},
}): EventData<typeof SmokerEvent.SmokeBegin> => ({
}): DataForEvent<typeof SmokerEvent.SmokeBegin> => ({
type: SmokerEvent.SmokeBegin,
plugins: pluginRegistry.plugins.map((plugin) => plugin.toJSON()),
opts: smokerOptions,
}),
},
],
type: 'parallel',
type: MachineUtil.PARALLEL,
states: {
packing: {
initial: 'working',
Expand Down Expand Up @@ -1273,7 +1273,7 @@ export const ControlMachine = setup({
type: 'report',
params: ({
context: {lingered},
}): EventData<typeof SmokerEvent.Lingered> => {
}): DataForEvent<typeof SmokerEvent.Lingered> => {
assert.ok(lingered);
return {type: SmokerEvent.Lingered, directories: lingered};
},
Expand Down Expand Up @@ -1312,21 +1312,21 @@ export const ControlMachine = setup({
`complete (with error) in ${delta(startTime)}s`,
),
],
type: 'final',
type: MachineUtil.FINAL,
},
complete: {
entry: [
log(({context: {startTime}}) => `complete in ${delta(startTime)}s`),
],
type: 'final',
type: MachineUtil.FINAL,
},
},
onDone: {
target: 'stopped',
},
},
stopped: {
type: 'final',
type: MachineUtil.FINAL,
},
},
output: ({
Expand Down
Loading

0 comments on commit 5744fda

Please sign in to comment.