@tsplc/tmp-compiler 0.0.0-stage → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +41 -2
  3. package/dist/cli.js +2325 -0
  4. package/dist/cli.js.map +26 -0
  5. package/dist/clock.js +11 -0
  6. package/dist/clock.js.map +11 -0
  7. package/dist/codesys.js +6563 -0
  8. package/dist/codesys.js.map +51 -0
  9. package/dist/collections.js +399 -0
  10. package/dist/collections.js.map +10 -0
  11. package/dist/index.js +16198 -0
  12. package/dist/index.js.map +62 -0
  13. package/dist/ir.js +3948 -0
  14. package/dist/ir.js.map +35 -0
  15. package/dist/retain.js +10 -0
  16. package/dist/retain.js.map +10 -0
  17. package/dist/signals.js +115 -0
  18. package/dist/signals.js.map +10 -0
  19. package/dist/telemetry.js +121 -0
  20. package/dist/telemetry.js.map +12 -0
  21. package/package.json +122 -4
  22. package/src/codesys/config.ts +50 -0
  23. package/src/codesys/debug.ts +655 -0
  24. package/src/codesys/emit.ts +185 -0
  25. package/src/codesys/endpoints.ts +111 -0
  26. package/src/codesys/engineering.ts +96 -0
  27. package/src/codesys/explain/expressions.ts +445 -0
  28. package/src/codesys/explain/flags.ts +134 -0
  29. package/src/codesys/explain/parameters.ts +53 -0
  30. package/src/codesys/explain/rows.ts +182 -0
  31. package/src/codesys/explain/tables.ts +79 -0
  32. package/src/codesys/explain/tasks.ts +68 -0
  33. package/src/codesys/explain/writes.ts +497 -0
  34. package/src/codesys/explain.ts +149 -0
  35. package/src/codesys/expressions.ts +100 -0
  36. package/src/codesys/global-lists.ts +284 -0
  37. package/src/codesys/global-names.ts +66 -0
  38. package/src/codesys/http.ts +1090 -0
  39. package/src/codesys/index.ts +13 -0
  40. package/src/codesys/instrumentation.ts +574 -0
  41. package/src/codesys/io-paths.ts +149 -0
  42. package/src/codesys/loops.ts +101 -0
  43. package/src/codesys/manifest.ts +684 -0
  44. package/src/codesys/names.ts +58 -0
  45. package/src/codesys/numeric.ts +516 -0
  46. package/src/codesys/plan.ts +688 -0
  47. package/src/codesys/programs.ts +138 -0
  48. package/src/codesys/publication.ts +99 -0
  49. package/src/codesys/required.ts +8 -0
  50. package/src/codesys/routines.ts +247 -0
  51. package/src/codesys/scan-clock.ts +103 -0
  52. package/src/codesys/scheduling.ts +96 -0
  53. package/src/codesys/service-channels.ts +24 -0
  54. package/src/codesys/sources.ts +118 -0
  55. package/src/codesys/st-files.ts +219 -0
  56. package/src/codesys/statements.ts +409 -0
  57. package/src/codesys/storage.ts +150 -0
  58. package/src/codesys/symbols.ts +240 -0
  59. package/src/codesys/task-access.ts +186 -0
  60. package/src/codesys/telemetry-settings.ts +19 -0
  61. package/src/codesys/telemetry.ts +852 -0
  62. package/src/codesys/types.ts +310 -0
  63. package/src/codesys/vendor.ts +113 -0
  64. package/src/frontend/analyzer.ts +779 -0
  65. package/src/frontend/async.ts +530 -0
  66. package/src/frontend/blocks.ts +578 -0
  67. package/src/frontend/configuration.ts +83 -0
  68. package/src/frontend/const-enums.ts +196 -0
  69. package/src/frontend/const-functions.ts +204 -0
  70. package/src/frontend/control-flow.ts +787 -0
  71. package/src/frontend/cooperative.ts +114 -0
  72. package/src/frontend/coroutines.ts +1344 -0
  73. package/src/frontend/dialect-names.ts +104 -0
  74. package/src/frontend/elaborate/array-searches.ts +224 -0
  75. package/src/frontend/elaborate/assignments.ts +343 -0
  76. package/src/frontend/elaborate/calls.ts +1342 -0
  77. package/src/frontend/elaborate/capture-protocol.ts +237 -0
  78. package/src/frontend/elaborate/capture.ts +388 -0
  79. package/src/frontend/elaborate/declarations.ts +124 -0
  80. package/src/frontend/elaborate/dialect.ts +473 -0
  81. package/src/frontend/elaborate/expressions.ts +2177 -0
  82. package/src/frontend/elaborate/filtered-arrays.ts +643 -0
  83. package/src/frontend/elaborate/frame.ts +145 -0
  84. package/src/frontend/elaborate/generics.ts +123 -0
  85. package/src/frontend/elaborate/graph.ts +141 -0
  86. package/src/frontend/elaborate/imports.ts +105 -0
  87. package/src/frontend/elaborate/index.ts +851 -0
  88. package/src/frontend/elaborate/initializers.ts +235 -0
  89. package/src/frontend/elaborate/iterators.ts +1541 -0
  90. package/src/frontend/elaborate/layouts.ts +1025 -0
  91. package/src/frontend/elaborate/model.ts +478 -0
  92. package/src/frontend/elaborate/names.ts +134 -0
  93. package/src/frontend/elaborate/reduce.ts +900 -0
  94. package/src/frontend/elaborate/references.ts +727 -0
  95. package/src/frontend/elaborate/retention.ts +284 -0
  96. package/src/frontend/elaborate/specialize.ts +1137 -0
  97. package/src/frontend/elaborate/statements.ts +1220 -0
  98. package/src/frontend/elaborate/storage.ts +836 -0
  99. package/src/frontend/elaborate/time-constructors.ts +34 -0
  100. package/src/frontend/elaborate/values.ts +200 -0
  101. package/src/frontend/elaborate/writes.ts +415 -0
  102. package/src/frontend/expressions.ts +1382 -0
  103. package/src/frontend/generated-names.ts +95 -0
  104. package/src/frontend/intrinsics.ts +382 -0
  105. package/src/frontend/lowering-key.ts +23 -0
  106. package/src/frontend/module-graph.ts +156 -0
  107. package/src/frontend/positions.ts +359 -0
  108. package/src/frontend/promise-chains.ts +872 -0
  109. package/src/frontend/promises.ts +419 -0
  110. package/src/frontend/routines.ts +239 -0
  111. package/src/frontend/sdk.ts +187 -0
  112. package/src/frontend/services.ts +612 -0
  113. package/src/frontend/statements.ts +871 -0
  114. package/src/frontend/tasks.ts +526 -0
  115. package/src/frontend/telemetry.ts +174 -0
  116. package/src/frontend/types.ts +1073 -0
  117. package/src/frontend/typescript.ts +659 -0
  118. package/src/index.ts +9 -0
  119. package/src/ir/aggregates.ts +291 -0
  120. package/src/ir/allocation.ts +96 -0
  121. package/src/ir/conditions.ts +855 -0
  122. package/src/ir/core-model.ts +289 -0
  123. package/src/ir/declarations.ts +281 -0
  124. package/src/ir/diagnostics.ts +68 -0
  125. package/src/ir/domains.ts +151 -0
  126. package/src/ir/fold.ts +279 -0
  127. package/src/ir/identity.ts +19 -0
  128. package/src/ir/index.ts +137 -0
  129. package/src/ir/job-errors.ts +69 -0
  130. package/src/ir/layouts.ts +211 -0
  131. package/src/ir/link.ts +302 -0
  132. package/src/ir/model.ts +394 -0
  133. package/src/ir/normalization-error.ts +10 -0
  134. package/src/ir/normalize.ts +52 -0
  135. package/src/ir/numeric.ts +163 -0
  136. package/src/ir/operands.ts +160 -0
  137. package/src/ir/ranges.ts +420 -0
  138. package/src/ir/routine.ts +1116 -0
  139. package/src/ir/scalars.ts +453 -0
  140. package/src/ir/share.ts +790 -0
  141. package/src/ir/short-circuit.ts +140 -0
  142. package/src/ir/tables.ts +111 -0
  143. package/src/ir/temporaries.ts +138 -0
  144. package/src/ir/types.ts +151 -0
  145. package/src/project/artifacts.ts +424 -0
  146. package/src/project/cli.ts +211 -0
  147. package/src/project/debugger.ts +111 -0
  148. package/src/project/deployments.ts +387 -0
  149. package/src/project/flags.ts +14 -0
  150. package/src/project/git.ts +35 -0
  151. package/src/project/plant.ts +629 -0
  152. package/src/project/project-commands.ts +507 -0
  153. package/src/project/project.ts +559 -0
  154. package/src/project/release.ts +152 -0
  155. package/src/project/server-check.ts +54 -0
  156. package/src/project/server.ts +165 -0
  157. package/src/project/terminal.ts +45 -0
  158. package/src/runtime/async.ts +207 -0
  159. package/src/runtime/launch.ts +59 -0
  160. package/src/runtime/main.ts +59 -0
  161. package/src/runtime/missing-file.ts +3 -0
  162. package/src/runtime/process.ts +177 -0
  163. package/src/runtime/program.ts +109 -0
  164. package/src/runtime/retain.ts +108 -0
  165. package/src/runtime/runtime.ts +324 -0
  166. package/src/runtime/settings.ts +27 -0
  167. package/src/runtime/storage.ts +456 -0
  168. package/src/runtime/symbols.ts +97 -0
  169. package/src/runtime/telemetry.ts +154 -0
  170. package/src/runtime/transport.ts +168 -0
  171. package/stdlib/clock.ts +8 -0
  172. package/stdlib/collections.ts +534 -0
  173. package/stdlib/plc-time.ts +2 -0
  174. package/stdlib/retain.ts +4 -0
  175. package/stdlib/signals.ts +142 -0
  176. package/stdlib/telemetry.ts +89 -0
  177. package/types/async.d.ts +87 -0
  178. package/types/clock.d.ts +8 -0
  179. package/types/config.d.ts +2 -0
  180. package/types/data-types.d.ts +31 -0
  181. package/types/dialect.d.ts +49 -0
  182. package/types/globals.d.ts +10 -0
  183. package/types/index.d.ts +212 -0
  184. package/types/retain.d.ts +12 -0
  185. package/types/telemetry.d.ts +47 -0
@@ -0,0 +1,507 @@
1
+ import { resolve } from "node:path";
2
+ import type { ParseArgsOptionsConfig } from "node:util";
3
+ import {
4
+ checkRawSources,
5
+ compileStructuredText,
6
+ configureDocker,
7
+ configureRuntime,
8
+ inspectProjectController,
9
+ installLibraries,
10
+ type ProgressStream,
11
+ rawEngineeringSettings,
12
+ runProject,
13
+ updateController,
14
+ } from "@tsplc/build";
15
+ import { checkHostConfig } from "@tsplc/compiler";
16
+ import { type Diagnostic, hasErrors } from "@tsplc/compiler/ir";
17
+ import {
18
+ buildTargetFor,
19
+ controllerFor,
20
+ type Environment,
21
+ type LoadedConfig,
22
+ loadTsplcConfig,
23
+ rawBuildConfig,
24
+ } from "@tsplc/config";
25
+ import { resolveConfig } from "@tsplc/config/build";
26
+ import { generateProject } from "@tsplc/typegen";
27
+ import { runInBun } from "../runtime/process";
28
+ import { type Flags, flag, type OutputFormat } from "./flags";
29
+ import {
30
+ buildConfigs,
31
+ buildTypeScriptProject,
32
+ checkProject,
33
+ cleanProject,
34
+ projectTargets,
35
+ } from "./project";
36
+ import { buildServer, checkServer, startServerCheck } from "./server";
37
+
38
+ /** The flags of the project commands. */
39
+ export const projectOptions = {
40
+ "emit-only": flag,
41
+ read: { type: "string", multiple: true },
42
+ "initialize-user": flag,
43
+ "frozen-lockfile": flag,
44
+ check: flag,
45
+ plc: flag,
46
+ server: flag,
47
+ simulate: flag,
48
+ runtime: flag,
49
+ bun: flag,
50
+ foreground: flag,
51
+ } as const satisfies ParseArgsOptionsConfig;
52
+
53
+ export type ProjectFlags = Flags<typeof projectOptions> & {
54
+ format?: OutputFormat;
55
+ target?: string;
56
+ };
57
+
58
+ /**
59
+ * Every command result; one whose diagnostics have errors fails the command. `values` are the
60
+ * `--read` expressions' values, which text output prints too.
61
+ */
62
+ export type CommandResult = object & {
63
+ diagnostics?: readonly Diagnostic[];
64
+ values?: Readonly<Record<string, string>>;
65
+ };
66
+
67
+ /** A result whose diagnostics fail the command; the CLI reports the whole result. */
68
+ export class DiagnosticFailure extends Error {
69
+ constructor(readonly result: CommandResult) {
70
+ super("PLC validation failed.");
71
+ }
72
+ }
73
+
74
+ /**
75
+ * The flags only some commands take, in the order they are checked, with what using one
76
+ * elsewhere is told. `--simulate`, `--frozen-lockfile` and `--initialize-user` are deploy's too, a
77
+ * plant command; the last group belongs to plant commands and the debugger only.
78
+ */
79
+ const restrictedFlags = [
80
+ ["emit-only", "--emit-only is supported by build only."],
81
+ ["target", "--target applies only to commands that read the config."],
82
+ ["runtime", "--runtime applies only to configure."],
83
+ [
84
+ "bun",
85
+ "--bun applies only to run --simulate: the Bun runtime has no fieldbus, so a plant simulator writes its inputs.",
86
+ ],
87
+ ["foreground", "--foreground applies only to run --simulate --bun."],
88
+ ["plc", "--plc and --server apply only to build."],
89
+ ["server", "--plc and --server apply only to build."],
90
+ [
91
+ "simulate",
92
+ "--simulate applies only to run, download, online-change and deploy.",
93
+ ],
94
+ ["check", "--check applies only to typegen."],
95
+ ["read", "--read applies only to inspect, download and online-change."],
96
+ [
97
+ "frozen-lockfile",
98
+ "--frozen-lockfile applies only to install, build, run, download, online-change and deploy.",
99
+ ],
100
+ ["initialize-user", "--initialize-user applies only to download and deploy."],
101
+ ["download", "--download applies only to deploy."],
102
+ ["select", "--select applies only to deploy."],
103
+ ["review", "--review applies only to deploy and redeploy."],
104
+ ["wait", "--wait applies only to deploy, rollback and redeploy."],
105
+ ["restore", "--restore applies only to unforce."],
106
+ ["yes", "-y applies only to plant agent commands."],
107
+ ["port", "--port applies only to debug."],
108
+ ["server-url", "--server-url applies only to debug."],
109
+ ["opcua", "--opcua applies only to debug."],
110
+ ] as const;
111
+ type RestrictedFlag = (typeof restrictedFlags)[number][0];
112
+
113
+ /** A flag that means something only beside another one. */
114
+ const prerequisites: Partial<Record<RestrictedFlag, RestrictedFlag>> = {
115
+ bun: "simulate",
116
+ foreground: "bun",
117
+ };
118
+
119
+ interface ProjectContext {
120
+ file: string;
121
+ flags: ProjectFlags;
122
+ environment: Environment;
123
+ progress: ProgressStream;
124
+ /** The config with the target the flags name, in `environment`. */
125
+ load: () => Promise<LoadedConfig>;
126
+ }
127
+
128
+ interface ProjectCommandEntry {
129
+ /** The restricted flags it takes. */
130
+ takes: readonly RestrictedFlag[];
131
+ /** Rejects flags that don't go together, before the config loads. */
132
+ check?: (flags: ProjectFlags) => void;
133
+ run: (context: ProjectContext) => Promise<CommandResult>;
134
+ }
135
+
136
+ const projectCommands = {
137
+ install: {
138
+ takes: ["target", "frozen-lockfile"],
139
+ run: async ({ load, flags }) => install(await load(), flags),
140
+ },
141
+ typegen: {
142
+ takes: ["target", "check"],
143
+ run: async ({ load, flags }) => typegen(await load(), flags),
144
+ },
145
+ check: {
146
+ takes: ["target"],
147
+ run: async ({ file, load }) => check(await loadCheckedConfig(file, load)),
148
+ },
149
+ build: {
150
+ takes: ["target", "emit-only", "plc", "server", "frozen-lockfile"],
151
+ check: (flags) => {
152
+ if (flags.plc && flags.server) {
153
+ throw new Error(
154
+ "Choose --plc or --server, or omit both for a full build.",
155
+ );
156
+ }
157
+ if (flags.server && flags["emit-only"]) {
158
+ throw new Error("--emit-only applies to PLC emission only.");
159
+ }
160
+ },
161
+ run: async ({ file, load, flags, progress }) =>
162
+ build(await loadCheckedConfig(file, load), flags, progress),
163
+ },
164
+ run: {
165
+ takes: ["target", "simulate", "bun", "foreground", "frozen-lockfile"],
166
+ run: async ({ file, load, flags, progress }) =>
167
+ run(await loadCheckedConfig(file, load), flags, progress),
168
+ },
169
+ download: {
170
+ takes: ["target", "simulate", "read", "frozen-lockfile", "initialize-user"],
171
+ run: async ({ file, load, flags, progress }) =>
172
+ updateProgram(
173
+ "download",
174
+ await loadCheckedConfig(file, load),
175
+ flags,
176
+ progress,
177
+ ),
178
+ },
179
+ "online-change": {
180
+ takes: ["target", "simulate", "read", "frozen-lockfile"],
181
+ run: async ({ file, load, flags, progress }) =>
182
+ updateProgram(
183
+ "online-change",
184
+ await loadCheckedConfig(file, load),
185
+ flags,
186
+ progress,
187
+ ),
188
+ },
189
+ inspect: {
190
+ takes: ["target", "read"],
191
+ run: async ({ load, flags, progress }) =>
192
+ inspectProjectController(await load(), flags.read, { progress }),
193
+ },
194
+ clean: { takes: [], run: ({ file }) => cleanProject(file) },
195
+ configure: {
196
+ takes: ["runtime"],
197
+ run: ({ flags, progress, environment }) =>
198
+ configure(flags, progress, environment),
199
+ },
200
+ } satisfies Record<string, ProjectCommandEntry>;
201
+
202
+ export type ProjectCommand = keyof typeof projectCommands;
203
+
204
+ export const isProjectCommand = (name: string): name is ProjectCommand =>
205
+ Object.hasOwn(projectCommands, name);
206
+
207
+ /** Rejects the flags that don't apply to `command`, among every flag the command line parsed. */
208
+ export const checkProjectFlags = (
209
+ command: ProjectCommand,
210
+ flags: ProjectFlags & Partial<Record<RestrictedFlag, unknown>>,
211
+ ) => {
212
+ const entry: ProjectCommandEntry = projectCommands[command];
213
+ for (const [name, message] of restrictedFlags) {
214
+ const prerequisite = prerequisites[name];
215
+ const applies =
216
+ entry.takes.includes(name) &&
217
+ (prerequisite === undefined || flags[prerequisite]);
218
+ if (flags[name] && !applies) {
219
+ throw new Error(message);
220
+ }
221
+ }
222
+ entry.check?.(flags);
223
+ };
224
+
225
+ /** Runs `command` in this process's `environment`, which the config's target values overlay. */
226
+ export const runProjectCommand = (
227
+ command: ProjectCommand,
228
+ file: string,
229
+ flags: ProjectFlags,
230
+ environment: Environment,
231
+ ): Promise<CommandResult> =>
232
+ projectCommands[command].run({
233
+ file,
234
+ flags,
235
+ environment,
236
+ // A JSON result owns standard output, so progress goes to standard error.
237
+ progress: flags.format === "json" ? "stderr" : "stdout",
238
+ load: () =>
239
+ loadTsplcConfig(file, {
240
+ env: environment,
241
+ target: flags.target,
242
+ forAgent: false,
243
+ }),
244
+ });
245
+
246
+ /** A config loaded after its host config check, whose diagnostics the compiler reports. */
247
+ interface CheckedConfig {
248
+ loaded: LoadedConfig;
249
+ hostConfigDiagnostics: readonly Diagnostic[];
250
+ }
251
+
252
+ /** Loads the config after type-checking it, so a type error fails before the config runs. */
253
+ const loadCheckedConfig = async (
254
+ file: string,
255
+ load: () => Promise<LoadedConfig>,
256
+ ): Promise<CheckedConfig> => {
257
+ const hostConfigDiagnostics = checkHostConfig(resolve(file));
258
+ if (hasErrors(hostConfigDiagnostics)) {
259
+ throw new DiagnosticFailure({ diagnostics: hostConfigDiagnostics });
260
+ }
261
+ return { loaded: await load(), hostConfigDiagnostics };
262
+ };
263
+
264
+ /** Builds the Docker image every build uses, or with `--runtime` the local PLC runtime's. */
265
+ const configure = async (
266
+ flags: Pick<ProjectFlags, "runtime">,
267
+ progress: ProgressStream,
268
+ environment: Environment,
269
+ ) => {
270
+ await (flags.runtime
271
+ ? configureRuntime({ progress })
272
+ : configureDocker({
273
+ progress,
274
+ winetricksCache: environment.CODESYS_WINETRICKS_CACHE || null,
275
+ }));
276
+ return { ok: true };
277
+ };
278
+
279
+ const install = async (
280
+ { config, root }: LoadedConfig,
281
+ flags: Pick<ProjectFlags, "frozen-lockfile">,
282
+ ) => ({
283
+ libraries: await installLibraries(config.dependencies, root, {
284
+ frozen: flags["frozen-lockfile"],
285
+ }),
286
+ });
287
+
288
+ const typegen = async (
289
+ loaded: LoadedConfig,
290
+ flags: Pick<ProjectFlags, "check">,
291
+ ) => {
292
+ const targets = await projectTargets(loaded.configFile, loaded);
293
+ // `--check` compares the committed files with this source's contract, so it emits too.
294
+ if (loaded.config.compiler) {
295
+ if (!flags.check) {
296
+ await generateProject(loaded, { hardwareOnly: true, targets });
297
+ }
298
+ const emitted = await buildTypeScriptProject(loaded.configFile, {
299
+ emitOnly: true,
300
+ loaded,
301
+ hostConfigDiagnostics: checkHostConfig(loaded.configFile),
302
+ });
303
+ if (hasErrors(emitted.diagnostics)) {
304
+ throw new DiagnosticFailure(emitted);
305
+ }
306
+ }
307
+ const { catalog, io, files } = await generateProject(loaded, {
308
+ check: flags.check,
309
+ targets,
310
+ });
311
+ return { catalog, io, files };
312
+ };
313
+
314
+ /** The check's verdict; the plan and the loaded config stay out, since they hold the environment. */
315
+ const check = async (checked: CheckedConfig) => {
316
+ if (checked.loaded.config.compiler) {
317
+ const { configFile, diagnostics } = await checkProject(
318
+ checked.loaded.configFile,
319
+ checked,
320
+ );
321
+ return { configFile, diagnostics };
322
+ }
323
+ return { objects: (await checkRawSources(checked.loaded)).objects.length };
324
+ };
325
+
326
+ /**
327
+ * The PLC program `build` and `run` compile: TypeScript through the compiler, or raw
328
+ * Structured Text straight to the vendor build. Either refreshes the generated types.
329
+ */
330
+ const buildPlc = async (
331
+ command: "build" | "run",
332
+ { loaded, hostConfigDiagnostics }: CheckedConfig,
333
+ flags: Pick<ProjectFlags, "emit-only" | "frozen-lockfile" | "simulate">,
334
+ progress: ProgressStream,
335
+ ) => {
336
+ const { config, configFile } = loaded;
337
+ if (config.compiler) {
338
+ // The build's analysis already read the targets; `--format json` reports the rest.
339
+ const { targets, services, ...built } = await buildTypeScriptProject(
340
+ configFile,
341
+ {
342
+ progress,
343
+ emitOnly: flags["emit-only"],
344
+ frozenLockfile: flags["frozen-lockfile"],
345
+ ...buildConfigs(loaded, flags),
346
+ hostConfigDiagnostics,
347
+ controller: controllerFor(config, command),
348
+ buildTarget: buildTargetFor(command),
349
+ },
350
+ );
351
+ if (hasErrors(built.diagnostics)) {
352
+ throw new DiagnosticFailure(built);
353
+ }
354
+ await generateProject(loaded, { targets });
355
+ return { built, services };
356
+ }
357
+ if (flags["emit-only"]) {
358
+ throw new Error("Raw Structured Text is already emitted.");
359
+ }
360
+ const built = await compileStructuredText(
361
+ await rawEngineeringSettings(loaded, {
362
+ command,
363
+ frozenLockfile: flags["frozen-lockfile"],
364
+ simulate: flags.simulate,
365
+ }),
366
+ { progress },
367
+ );
368
+ await generateProject(loaded);
369
+ return { built, services: undefined };
370
+ };
371
+
372
+ /** The PLC program and, unless `--plc` or `--emit-only` restricts it, the configured server. */
373
+ const build = async (
374
+ checked: CheckedConfig,
375
+ flags: Pick<
376
+ ProjectFlags,
377
+ "emit-only" | "frozen-lockfile" | "plc" | "server" | "simulate"
378
+ >,
379
+ progress: ProgressStream,
380
+ ) => {
381
+ const { config, root } = checked.loaded;
382
+ if (flags.server) {
383
+ return buildServer(await checkServer(config, root), config, root);
384
+ }
385
+ const withServer =
386
+ !flags.plc && !flags["emit-only"] && config.server?.runtime;
387
+ const serverCheck = withServer ? startServerCheck(config, root) : undefined;
388
+ const { built: plc } = await buildPlc("build", checked, flags, progress);
389
+ if (!serverCheck) {
390
+ return plc;
391
+ }
392
+ return { ...plc, ...(await buildServer(await serverCheck, config, root)) };
393
+ };
394
+
395
+ const run = async (
396
+ checked: CheckedConfig,
397
+ flags: Pick<
398
+ ProjectFlags,
399
+ "frozen-lockfile" | "simulate" | "bun" | "foreground" | "target"
400
+ >,
401
+ progress: ProgressStream,
402
+ ) => {
403
+ if (flags.bun) {
404
+ return runInBunRuntime(checked, flags, progress);
405
+ }
406
+ const { config, configFile } = checked.loaded;
407
+ const { built } = await buildPlc("run", checked, flags, progress);
408
+ if (!config.build) {
409
+ throw new Error("Configure build to run this project.");
410
+ }
411
+ await runProject(
412
+ resolveConfig(
413
+ config.compiler ? config.build : rawBuildConfig(config),
414
+ configFile,
415
+ ),
416
+ config.name,
417
+ { progress },
418
+ );
419
+ return built;
420
+ };
421
+
422
+ /** Emits the program and runs its TypeScript in the Bun runtime instead of CODESYS. */
423
+ const runInBunRuntime = async (
424
+ checked: CheckedConfig,
425
+ flags: Pick<
426
+ ProjectFlags,
427
+ "frozen-lockfile" | "simulate" | "foreground" | "target"
428
+ >,
429
+ progress: ProgressStream,
430
+ ) => {
431
+ const { config, configFile, env, root } = checked.loaded;
432
+ if (!config.compiler) {
433
+ throw new Error(
434
+ "The Bun runtime runs a TypeScript program; this project builds raw Structured Text.",
435
+ );
436
+ }
437
+ const { built, services } = await buildPlc(
438
+ "run",
439
+ checked,
440
+ { ...flags, "emit-only": true },
441
+ progress,
442
+ );
443
+ const runtime = await runInBun({
444
+ root,
445
+ launch: {
446
+ config: configFile,
447
+ target: flags.target ?? null,
448
+ services: services ?? {},
449
+ },
450
+ environment: env,
451
+ foreground: flags.foreground ?? false,
452
+ progress,
453
+ });
454
+ return { ...built, runtime };
455
+ };
456
+
457
+ /** Downloads or online-changes the program on the configured controller. */
458
+ const updateProgram = async (
459
+ operation: "download" | "online-change",
460
+ checked: CheckedConfig,
461
+ flags: Pick<
462
+ ProjectFlags,
463
+ "frozen-lockfile" | "simulate" | "initialize-user" | "read"
464
+ >,
465
+ progress: ProgressStream,
466
+ ) => {
467
+ const { config, env } = checked.loaded;
468
+ if (!config.build) {
469
+ throw new Error("Configure build settings and controller connection.");
470
+ }
471
+ const settings = config.compiler
472
+ ? await emittedControllerSettings(operation, checked, flags)
473
+ : await rawEngineeringSettings(checked.loaded, {
474
+ command: operation,
475
+ frozenLockfile: flags["frozen-lockfile"],
476
+ simulate: flags.simulate,
477
+ });
478
+ return updateController(settings, operation, {
479
+ initializeUser: flags["initialize-user"],
480
+ read: flags.read,
481
+ progress,
482
+ environment: env,
483
+ });
484
+ };
485
+
486
+ const emittedControllerSettings = async (
487
+ operation: "download" | "online-change",
488
+ { loaded, hostConfigDiagnostics }: CheckedConfig,
489
+ flags: Pick<ProjectFlags, "frozen-lockfile" | "simulate">,
490
+ ) => {
491
+ const emitted = await buildTypeScriptProject(loaded.configFile, {
492
+ emitOnly: true,
493
+ forController: true,
494
+ frozenLockfile: flags["frozen-lockfile"],
495
+ ...buildConfigs(loaded, flags),
496
+ hostConfigDiagnostics,
497
+ controller: controllerFor(loaded.config, operation),
498
+ buildTarget: buildTargetFor(operation),
499
+ });
500
+ if (hasErrors(emitted.diagnostics)) {
501
+ throw new DiagnosticFailure(emitted);
502
+ }
503
+ if (!emitted.controllerConfig) {
504
+ throw new Error("Configure build settings for deployment.");
505
+ }
506
+ return emitted.controllerConfig;
507
+ };