Skip to content

Commit 74640b9

Browse files
committed
feat: template overriding
1 parent e63014c commit 74640b9

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@synaptic-simulations/mach",
3-
"version": "1.3.0-rc1",
3+
"version": "1.3.0-rc2",
44
"description": "The last MSFS instrument bundler you'll ever need.",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/plugins.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,23 @@ export const writePackageSources = (args: MachArgs, instrument: Instrument): Plu
157157
if (instrument.simulatorPackage.type === "react") {
158158
await fs.writeFile(
159159
instrumentPath,
160-
await renderFile(path.join(__dirname, "./templates/instrument.cjs"), templateParams),
160+
await renderFile(
161+
instrument.simulatorPackage.jsTemplate
162+
? path.resolve(instrument.simulatorPackage.jsTemplate)
163+
: path.join(__dirname, "./templates/instrument.cjs"),
164+
templateParams,
165+
),
161166
);
162167
}
163168

164169
await fs.writeFile(
165170
path.join(packageTarget, `${fileName}.html`),
166-
await renderFile(path.join(__dirname, "./templates/index.html"), templateParams),
171+
await renderFile(
172+
instrument.simulatorPackage.htmlTemplate
173+
? path.resolve(instrument.simulatorPackage.htmlTemplate)
174+
: path.join(__dirname, "./templates/index.html"),
175+
templateParams,
176+
),
167177
);
168178
}
169179
});

src/templates/instrument.cjs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33
/* global registerInstrument */
44

55
class _MachInstrument_{{ templateId }} extends BaseInstrument {
6-
constructor() {
7-
super();
8-
let lastTime = this._lastTime;
9-
this.getDeltaTime = () => {
10-
const nowTime = Date.now();
11-
const deltaTime = nowTime - lastTime;
12-
lastTime = nowTime;
13-
return deltaTime;
14-
};
15-
}
16-
176
get templateID() {
187
return '{{ templateId }}';
198
}
@@ -33,13 +22,11 @@ class _MachInstrument_{{ templateId }} extends BaseInstrument {
3322

3423
Update() {
3524
super.Update();
36-
this.dispatchEvent(new CustomEvent("update", { detail: this.getDeltaTime() }));
25+
document.dispatchEvent(new CustomEvent("update"));
3726
}
3827

3928
onInteractionEvent(event) {
40-
const eventName = String(event);
41-
this.dispatchEvent(new CustomEvent(eventName));
42-
this.dispatchEvent(new CustomEvent("*", { detail: eventName }));
29+
document.dispatchEvent(new CustomEvent(event));
4330
}
4431
}
4532

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ interface PackageSettings {
1919
fileName?: string;
2020
/** Simulator packages to import in the HTML template. */
2121
imports?: string[];
22+
/** Paths to custom HTML template files */
23+
htmlTemplate?: string;
2224
}
2325

2426
interface ReactInstrumentPackageSettings extends PackageSettings {
@@ -27,6 +29,8 @@ interface ReactInstrumentPackageSettings extends PackageSettings {
2729
templateId?: string;
2830
/** Whether the instrument is interactive or not. Defaults to `true`. */
2931
isInteractive?: boolean;
32+
/** Paths to custom JavaScript template files */
33+
jsTemplate?: string;
3034
}
3135

3236
interface BaseInstrumentPackageSettings extends PackageSettings {
@@ -115,6 +119,7 @@ export const InstrumentSchema: z.ZodType<Instrument> = z.lazy(() =>
115119
imports: z.array(z.string()).optional(),
116120
templateId: z.string().optional(),
117121
isInteractive: z.boolean().optional(),
122+
htmlTemplate: z.string().optional(),
118123
}),
119124
z.object({
120125
type: z.literal("baseInstrument"),
@@ -123,6 +128,8 @@ export const InstrumentSchema: z.ZodType<Instrument> = z.lazy(() =>
123128
imports: z.array(z.string()).optional(),
124129
templateId: z.string(),
125130
mountElementId: z.string(),
131+
htmlTemplate: z.string().optional(),
132+
jsTemplate: z.string().optional(),
126133
}),
127134
])
128135
.optional(),

0 commit comments

Comments
 (0)