@tsplc/build 0.5.9 → 0.5.12

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.
package/dist/cli.js CHANGED
@@ -557,7 +557,7 @@ import {
557
557
  } from "node:fs/promises";
558
558
  import { resolve as resolve7 } from "node:path";
559
559
  import { setTimeout as delay } from "node:timers/promises";
560
- import { z as z7 } from "zod";
560
+ import { z as z8 } from "zod";
561
561
 
562
562
  // packages/build/src/controller-protocol.ts
563
563
  import {
@@ -565,200 +565,19 @@ import {
565
565
  sha256Digest,
566
566
  tcpPort
567
567
  } from "@tsplc/config/build";
568
- import { z as z5 } from "zod";
569
- var imageIdSchema = z5.string().regex(/^sha256:[a-f0-9]{64}$/);
570
- var endpointSchema = z5.strictObject({
571
- host: z5.string().nonempty(),
572
- port: tcpPort
573
- });
574
- var controllerResultSchema = z5.strictObject({
575
- schemaVersion: z5.literal(1),
576
- operationId: z5.uuid(),
577
- operation: z5.enum(["download", "online-change", "inspect"]),
578
- imageId: imageIdSchema,
579
- engineering: sha256Digest.optional(),
580
- templateHash: sha256Digest,
581
- target: z5.strictObject({
582
- network: z5.string().nonempty(),
583
- gateway: endpointSchema,
584
- device: endpointSchema,
585
- controller: controllerSchema.optional(),
586
- deviceTree: sha256Digest.optional()
587
- }),
588
- generation: z5.string().nonempty(),
589
- application: z5.string().nonempty(),
590
- applicationGuid: z5.string().nonempty(),
591
- controllerIds: z5.strictObject({
592
- codeId: z5.string().nonempty(),
593
- dataId: z5.string().nonempty()
594
- }),
595
- state: z5.string().nonempty(),
596
- values: z5.record(z5.string(), z5.string())
597
- });
598
- var controllerCredentialsSchema = z5.strictObject({
599
- username: z5.string().nonempty(),
600
- password: z5.string().nonempty()
601
- });
602
- var warmOperationSchema = z5.strictObject({
603
- operation: z5.enum(["download", "online-change"]),
604
- operationId: z5.uuid(),
605
- request: z5.string().nonempty(),
606
- initializeUser: z5.boolean(),
607
- credentials: controllerCredentialsSchema.optional()
608
- });
609
- var tracebackFailure = (traceback) => {
610
- const text = traceback.trim();
611
- const lastFrame = text.lastIndexOf(`
612
- File `);
613
- if (lastFrame === -1) {
614
- return text;
615
- }
616
- const [, ...rest] = text.slice(lastFrame + 1).split(`
617
- `);
618
- return ((rest[0]?.startsWith(" ")) ? rest.slice(1) : rest).join(`
619
- `);
620
- };
621
- var controllerTarget = (config, online) => ({
622
- network: online.network,
623
- gateway: online.gateway,
624
- device: online.device,
625
- controller: config.controller,
626
- ...config.deviceTree && {
627
- deviceTree: deviceTreeStructure(config.deviceTree)
628
- }
629
- });
630
- var sameBuilder = (state, expected) => state.engineering && expected.engineering ? state.engineering === expected.engineering : state.imageId === expected.imageId;
631
- var sameBaseline = (state, expected) => sameBuilder(state, expected) && state.templateHash === expected.templateHash && JSON.stringify(state.target) === JSON.stringify(expected.target);
632
- var rollbackSchema = z5.enum([
633
- "intact",
634
- "restored",
635
- "failed",
636
- "unavailable"
637
- ]);
638
- var loginCommandSchema = z5.discriminatedUnion("command", [
639
- z5.strictObject({ command: z5.literal("state") }),
640
- z5.strictObject({
641
- command: z5.literal("read"),
642
- expressions: z5.array(z5.string().nonempty()).nonempty()
643
- }),
644
- z5.strictObject({
645
- command: z5.enum(["write", "force"]),
646
- values: z5.record(z5.string().nonempty(), z5.string().nonempty())
647
- }),
648
- z5.strictObject({
649
- command: z5.literal("unforce"),
650
- expressions: z5.array(z5.string().nonempty()),
651
- restore: z5.boolean()
652
- }),
653
- z5.strictObject({ command: z5.enum(["start", "stop"]) }),
654
- z5.strictObject({
655
- command: z5.literal("reset"),
656
- kind: z5.enum(["Warm", "Cold"])
657
- })
658
- ]);
659
- var loginResultSchema = z5.union([
660
- z5.strictObject({ values: z5.record(z5.string(), z5.string()) }),
661
- z5.strictObject({ state: z5.string(), forced: z5.array(z5.string()) })
662
- ]);
663
-
664
- // packages/build/src/host/codesys-command.ts
665
- var codesysCommand = [
666
- "@echo off",
667
- "setlocal DisableDelayedExpansion",
668
- '"%CODESYS_BUILD_EXE%" --profile="%CODESYS_BUILD_PROFILE%" --noUI --runscript="%CODESYS_BUILD_SCRIPT%"',
669
- 'set "CODESYS_BUILD_STATUS=%errorlevel%"',
670
- 'if not "%CODESYS_BUILD_STATUS%"=="0" echo CODESYS Windows exit status: %CODESYS_BUILD_STATUS%',
671
- "exit /b %CODESYS_BUILD_STATUS%",
672
- ""
673
- ].join(`\r
674
- `);
675
-
676
- // packages/build/src/host/codesys-host.ts
677
- import { execFile as execFile3, spawn as spawn2 } from "node:child_process";
678
- import { promisify as promisify3 } from "node:util";
679
568
  import { z as z6 } from "zod";
680
569
 
681
- // packages/build/src/host/windows-path.ts
682
- import { execFile as execFile2 } from "node:child_process";
683
- import { promisify as promisify2 } from "node:util";
684
- import {
685
- codesysTimeouts,
686
- commandArgumentSchema
687
- } from "@tsplc/config/build";
688
- var execute2 = promisify2(execFile2);
689
-
690
- // packages/build/src/host/codesys-host.ts
691
- var execute3 = promisify3(execFile3);
692
- var exitSchema = z6.object({
693
- pid: z6.int().positive(),
694
- code: z6.int().nullable(),
695
- signal: z6.string().nullable()
696
- });
697
-
698
- // packages/build/src/host/codesys-worker.ts
699
- var pythonDirectory = resolve7(packageDirectory, "python");
700
- var sessionSchema = z7.object({
701
- directory: z7.string(),
702
- fingerprint: z7.string(),
703
- pid: z7.int().positive()
704
- });
705
- var phaseSchema = z7.enum(["prepare", "transfer"]);
706
- var hasCode = (error, code) => error instanceof Error && ("code" in error) && error.code === code;
707
- var isProcessAlive = (pid) => {
708
- try {
709
- process.kill(pid, 0);
710
- return true;
711
- } catch (error) {
712
- if (hasCode(error, "ESRCH")) {
713
- return false;
714
- }
715
- throw error;
716
- }
717
- };
718
- var withWorkerLock = async (config, run) => {
719
- await mkdir5(resolve7(config.outDir, ".codesys"), { recursive: true });
720
- const path = resolve7(config.outDir, ".codesys/command.lock");
721
- const deadline = Date.now() + config.timeouts.build + config.timeouts.startup;
722
- while (true) {
723
- const lock = await open(path, "wx").catch((error) => {
724
- if (hasCode(error, "EEXIST")) {
725
- return;
726
- }
727
- throw error;
728
- });
729
- if (lock) {
730
- try {
731
- await lock.writeFile(String(process.pid));
732
- return await run();
733
- } finally {
734
- await lock.close();
735
- await unlink(path);
736
- }
737
- }
738
- const owner = Number(await readFile5(path, "utf8").catch((error) => {
739
- if (hasCode(error, "ENOENT")) {
740
- return "";
741
- }
742
- throw error;
743
- }));
744
- if (owner > 0 && !isProcessAlive(owner)) {
745
- throw new Error(`An interrupted command left ${path}. Remove that lock file and retry.`);
746
- }
747
- if (Date.now() >= deadline) {
748
- throw new Error(`Timed out waiting for another CODESYS command (${path})`);
749
- }
750
- await delay(200);
751
- }
752
- };
753
-
754
- // packages/build/src/sources.ts
755
- import { readdir as readdir2, readFile as readFile6 } from "node:fs/promises";
756
- import { basename as basename4, resolve as resolve8 } from "node:path";
757
- import { identifierPattern as identifierPattern3 } from "@tsplc/iec/st";
570
+ // packages/build/src/online-change-check.ts
571
+ import { elementaryType } from "@tsplc/iec";
572
+ import { z as z5 } from "zod";
758
573
 
759
- // packages/build/src/io.ts
760
- import { ioArray, ioScalarBits, sizePrefix } from "@tsplc/iec";
761
- import { qualifiers, variableSections } from "@tsplc/iec/st";
574
+ // packages/build/src/st/declarations.ts
575
+ import {
576
+ conditionalDirective,
577
+ modifiers,
578
+ qualifiers,
579
+ variableSections
580
+ } from "@tsplc/iec/st";
762
581
 
763
582
  // packages/build/src/st/lexer.ts
764
583
  import { blockCommentEnd, lineCommentEnd, stringEnd } from "@tsplc/iec/st";
@@ -853,122 +672,6 @@ var tokenize = (source, filename = "<source>") => {
853
672
  return tokens;
854
673
  };
855
674
 
856
- // packages/build/src/io.ts
857
- var span = (type) => ioArray(type) ?? { element: type, length: 1 };
858
- var addressSize = (type) => {
859
- const bits = ioScalarBits(type);
860
- return bits === undefined || bits === 64 ? undefined : sizePrefix(bits);
861
- };
862
- function bindStructuredTextIo(objects, bindings) {
863
- const declarations = [];
864
- const visit = (object) => {
865
- const tokens = tokenize(object.declaration, object.source);
866
- let section = false;
867
- let start = 0;
868
- for (const [index, token] of tokens.entries()) {
869
- if (variableSections.has(token.value)) {
870
- section = true;
871
- start = index + 1;
872
- continue;
873
- }
874
- if (token.value === "END_VAR") {
875
- section = false;
876
- continue;
877
- }
878
- if (!section || token.value !== ";") {
879
- continue;
880
- }
881
- const part = tokens.slice(start, index).filter((t) => t.kind !== "pragma");
882
- start = index + 1;
883
- const colon = part.find((t) => t.value === ":");
884
- if (!colon) {
885
- continue;
886
- }
887
- const colonIndex = part.indexOf(colon);
888
- if (colonIndex < 1) {
889
- continue;
890
- }
891
- const names = part.slice(0, colonIndex).filter((t) => t.kind === "word" && !qualifiers.has(t.value) && t.value !== "AT");
892
- const at = part.find((t) => t.value === "AT");
893
- const variables = at ? names.filter((t) => t.start < at.start) : names;
894
- const initializer = part.findIndex((t, index) => index > colonIndex && t.value === ":=");
895
- const typeTokens = part.slice(colonIndex + 1, initializer < 0 ? undefined : initializer);
896
- const [first] = typeTokens;
897
- const last = typeTokens.at(-1);
898
- const type = first && last ? object.declaration.slice(first.start, last.end).replace(/\s+/g, " ").toUpperCase() : "";
899
- for (const name of variables) {
900
- declarations.push({
901
- object,
902
- name: name.value,
903
- names: variables,
904
- colon,
905
- type
906
- });
907
- }
908
- }
909
- object.children.forEach(visit);
910
- };
911
- objects.forEach(visit);
912
- const edits = new Map;
913
- for (const [symbol, binding] of Object.entries(bindings)) {
914
- const parts = symbol.toUpperCase().split(".");
915
- if (parts.length > 2) {
916
- throw new Error(`${symbol}: direct IO maps a scalar declaration, not a nested field.`);
917
- }
918
- const matches = declarations.filter((d) => d.name === parts.at(-1) && (parts.length === 1 || d.object.name.toUpperCase() === parts[0]));
919
- const [declaration, ...others] = matches;
920
- if (!declaration || others.length) {
921
- throw new Error(`${symbol}: expected one ST variable declaration, found ${matches.length}. Use Object.Variable to disambiguate.`);
922
- }
923
- const [variable, ...siblings] = declaration.names;
924
- if (!variable || siblings.length) {
925
- throw new Error(`${symbol}: declare mapped IO variables separately.`);
926
- }
927
- const declared = span(declaration.type);
928
- const size = addressSize(declared.element);
929
- if (!size || declared.length > 1 && declared.element === "BOOL" || binding.address[2] !== size) {
930
- throw new Error(`${symbol}: ${declaration.type} is incompatible with ${binding.address}.`);
931
- }
932
- const expected = binding.type && span(binding.type.replace(/\s+/g, " ").toUpperCase());
933
- if (expected && (expected.element !== declared.element || expected.length !== declared.length)) {
934
- throw new Error(`${symbol}: expected ${binding.type}, found ${declaration.type}.`);
935
- }
936
- const list = edits.get(declaration.object) ?? [];
937
- const start = variable.end;
938
- if (list.some((e) => e.start === start)) {
939
- throw new Error(`${symbol}: the variable has more than one IO binding.`);
940
- }
941
- list.push({
942
- start,
943
- end: declaration.colon.start,
944
- text: ` AT ${binding.address} `
945
- });
946
- edits.set(declaration.object, list);
947
- }
948
- for (const [object, list] of edits) {
949
- for (const edit of list.sort((a, b) => b.start - a.start)) {
950
- object.declaration = object.declaration.slice(0, edit.start) + edit.text + object.declaration.slice(edit.end);
951
- }
952
- }
953
- }
954
-
955
- // packages/build/src/st/parse.ts
956
- import { basename as basename3 } from "node:path";
957
- import {
958
- globalSections,
959
- identifierPattern as identifierPattern2,
960
- qualifiers as qualifiers3,
961
- variableSections as variableSections3
962
- } from "@tsplc/iec/st";
963
-
964
- // packages/build/src/st/declarations.ts
965
- import {
966
- conditionalDirective,
967
- modifiers,
968
- qualifiers as qualifiers2,
969
- variableSections as variableSections2
970
- } from "@tsplc/iec/st";
971
-
972
675
  // packages/build/src/st/reader.ts
973
676
  var createTokenReader = (source, filename) => {
974
677
  const tokens = tokenize(source, filename);
@@ -1028,7 +731,7 @@ var closingObjects = new Set(Object.keys(objectKinds).map((kind) => `END_${kind}
1028
731
  var declarationKeywords = new Set([
1029
732
  ...Object.keys(objectKinds),
1030
733
  ...closingObjects,
1031
- ...variableSections2,
734
+ ...variableSections,
1032
735
  "END_VAR",
1033
736
  "TYPE",
1034
737
  "END_TYPE",
@@ -1132,7 +835,7 @@ var readVariableSection = (reader) => {
1132
835
  const conditionals = [];
1133
836
  while (reader.peek()?.value !== "END_VAR") {
1134
837
  const token = reader.take();
1135
- if (variableSections2.has(token.value) || closingObjects.has(token.value)) {
838
+ if (variableSections.has(token.value) || closingObjects.has(token.value)) {
1136
839
  reader.fail("Missing END_VAR", token);
1137
840
  }
1138
841
  const directive = token.kind === "pragma" && conditionalDirective(token.value);
@@ -1169,7 +872,7 @@ var readDeclarationSections = (reader) => {
1169
872
  ahead++;
1170
873
  }
1171
874
  const next = reader.peek(ahead)?.value ?? "";
1172
- if (variableSections2.has(next)) {
875
+ if (variableSections.has(next)) {
1173
876
  readDeclarationPragmas(reader);
1174
877
  readVariableSection(reader);
1175
878
  continue;
@@ -1195,7 +898,335 @@ var keptSections = new Set([
1195
898
  "VAR_GLOBAL"
1196
899
  ]);
1197
900
 
901
+ // packages/build/src/online-change-check.ts
902
+ var committedObjectSchema = z5.lazy(() => z5.looseObject({
903
+ kind: z5.string(),
904
+ name: z5.string(),
905
+ declaration: z5.string(),
906
+ children: z5.array(committedObjectSchema).optional()
907
+ }));
908
+ var onlineChangeFindingSchema = z5.strictObject({
909
+ kind: z5.enum(["type", "tasks", "restart"]),
910
+ refuses: z5.boolean(),
911
+ object: z5.string(),
912
+ variable: z5.string().nullable(),
913
+ before: z5.string().nullable(),
914
+ after: z5.string().nullable(),
915
+ advice: z5.string()
916
+ });
917
+
918
+ // packages/build/src/controller-protocol.ts
919
+ var imageIdSchema = z6.string().regex(/^sha256:[a-f0-9]{64}$/);
920
+ var endpointSchema = z6.strictObject({
921
+ host: z6.string().nonempty(),
922
+ port: tcpPort
923
+ });
924
+ var controllerResultSchema = z6.strictObject({
925
+ schemaVersion: z6.literal(1),
926
+ operationId: z6.uuid(),
927
+ operation: z6.enum(["download", "online-change", "inspect"]),
928
+ imageId: imageIdSchema,
929
+ engineering: sha256Digest.optional(),
930
+ templateHash: sha256Digest,
931
+ target: z6.strictObject({
932
+ network: z6.string().nonempty(),
933
+ gateway: endpointSchema,
934
+ device: endpointSchema,
935
+ controller: controllerSchema.optional(),
936
+ deviceTree: sha256Digest.optional()
937
+ }),
938
+ generation: z6.string().nonempty(),
939
+ application: z6.string().nonempty(),
940
+ applicationGuid: z6.string().nonempty(),
941
+ controllerIds: z6.strictObject({
942
+ codeId: z6.string().nonempty(),
943
+ dataId: z6.string().nonempty()
944
+ }),
945
+ state: z6.string().nonempty(),
946
+ values: z6.record(z6.string(), z6.string())
947
+ });
948
+ var controllerCredentialsSchema = z6.strictObject({
949
+ username: z6.string().nonempty(),
950
+ password: z6.string().nonempty()
951
+ });
952
+ var resetKindSchema = z6.enum(["Warm", "Cold"]);
953
+ var warmRequest = {
954
+ operationId: z6.uuid(),
955
+ request: z6.string().nonempty(),
956
+ credentials: controllerCredentialsSchema.optional()
957
+ };
958
+ var warmOperationSchema = z6.discriminatedUnion("operation", [
959
+ z6.strictObject({
960
+ operation: z6.enum(["download", "online-change", "review"]),
961
+ ...warmRequest,
962
+ initializeUser: z6.boolean()
963
+ }),
964
+ z6.strictObject({
965
+ operation: z6.literal("restart"),
966
+ ...warmRequest,
967
+ reset: resetKindSchema
968
+ })
969
+ ]);
970
+ var tracebackFailure = (traceback) => {
971
+ const text = traceback.trim();
972
+ const lastFrame = text.lastIndexOf(`
973
+ File `);
974
+ if (lastFrame === -1) {
975
+ return text;
976
+ }
977
+ const [, ...rest] = text.slice(lastFrame + 1).split(`
978
+ `);
979
+ return ((rest[0]?.startsWith(" ")) ? rest.slice(1) : rest).join(`
980
+ `);
981
+ };
982
+ var controllerTarget = (config, online) => ({
983
+ network: online.network,
984
+ gateway: online.gateway,
985
+ device: online.device,
986
+ controller: config.controller,
987
+ ...config.deviceTree && {
988
+ deviceTree: deviceTreeStructure(config.deviceTree)
989
+ }
990
+ });
991
+ var sameBuilder = (state, expected) => state.engineering && expected.engineering ? state.engineering === expected.engineering : state.imageId === expected.imageId;
992
+ var sameBaseline = (state, expected) => sameBuilder(state, expected) && state.templateHash === expected.templateHash && JSON.stringify(state.target) === JSON.stringify(expected.target);
993
+ var restartReplySchema = z6.strictObject({ state: z6.string() });
994
+ var reviewReplySchema = z6.strictObject({
995
+ findings: z6.array(onlineChangeFindingSchema)
996
+ });
997
+ var rollbackSchema = z6.enum([
998
+ "intact",
999
+ "restored",
1000
+ "failed",
1001
+ "unavailable"
1002
+ ]);
1003
+ var loginCommandSchema = z6.discriminatedUnion("command", [
1004
+ z6.strictObject({ command: z6.literal("state") }),
1005
+ z6.strictObject({
1006
+ command: z6.literal("read"),
1007
+ expressions: z6.array(z6.string().nonempty()).nonempty()
1008
+ }),
1009
+ z6.strictObject({
1010
+ command: z6.enum(["write", "force"]),
1011
+ values: z6.record(z6.string().nonempty(), z6.string().nonempty())
1012
+ }),
1013
+ z6.strictObject({
1014
+ command: z6.literal("unforce"),
1015
+ expressions: z6.array(z6.string().nonempty()),
1016
+ restore: z6.boolean()
1017
+ }),
1018
+ z6.strictObject({ command: z6.enum(["start", "stop"]) }),
1019
+ z6.strictObject({ command: z6.literal("reset"), kind: resetKindSchema })
1020
+ ]);
1021
+ var loginResultSchema = z6.union([
1022
+ z6.strictObject({ values: z6.record(z6.string(), z6.string()) }),
1023
+ z6.strictObject({ state: z6.string(), forced: z6.array(z6.string()) })
1024
+ ]);
1025
+
1026
+ // packages/build/src/host/codesys-command.ts
1027
+ var codesysCommand = [
1028
+ "@echo off",
1029
+ "setlocal DisableDelayedExpansion",
1030
+ '"%CODESYS_BUILD_EXE%" --profile="%CODESYS_BUILD_PROFILE%" --noUI --runscript="%CODESYS_BUILD_SCRIPT%"',
1031
+ 'set "CODESYS_BUILD_STATUS=%errorlevel%"',
1032
+ 'if not "%CODESYS_BUILD_STATUS%"=="0" echo CODESYS Windows exit status: %CODESYS_BUILD_STATUS%',
1033
+ "exit /b %CODESYS_BUILD_STATUS%",
1034
+ ""
1035
+ ].join(`\r
1036
+ `);
1037
+
1038
+ // packages/build/src/host/codesys-host.ts
1039
+ import { execFile as execFile3, spawn as spawn2 } from "node:child_process";
1040
+ import { promisify as promisify3 } from "node:util";
1041
+ import { z as z7 } from "zod";
1042
+
1043
+ // packages/build/src/host/windows-path.ts
1044
+ import { execFile as execFile2 } from "node:child_process";
1045
+ import { promisify as promisify2 } from "node:util";
1046
+ import {
1047
+ codesysTimeouts,
1048
+ commandArgumentSchema
1049
+ } from "@tsplc/config/build";
1050
+ var execute2 = promisify2(execFile2);
1051
+
1052
+ // packages/build/src/host/codesys-host.ts
1053
+ var execute3 = promisify3(execFile3);
1054
+ var exitSchema = z7.object({
1055
+ pid: z7.int().positive(),
1056
+ code: z7.int().nullable(),
1057
+ signal: z7.string().nullable()
1058
+ });
1059
+
1060
+ // packages/build/src/host/codesys-worker.ts
1061
+ var pythonDirectory = resolve7(packageDirectory, "python");
1062
+ var sessionSchema = z8.object({
1063
+ directory: z8.string(),
1064
+ fingerprint: z8.string(),
1065
+ pid: z8.int().positive()
1066
+ });
1067
+ var phaseSchema = z8.enum(["prepare", "transfer"]);
1068
+ var hasCode = (error, code) => error instanceof Error && ("code" in error) && error.code === code;
1069
+ var isProcessAlive = (pid) => {
1070
+ try {
1071
+ process.kill(pid, 0);
1072
+ return true;
1073
+ } catch (error) {
1074
+ if (hasCode(error, "ESRCH")) {
1075
+ return false;
1076
+ }
1077
+ throw error;
1078
+ }
1079
+ };
1080
+ var withWorkerLock = async (config, run) => {
1081
+ await mkdir5(resolve7(config.outDir, ".codesys"), { recursive: true });
1082
+ const path = resolve7(config.outDir, ".codesys/command.lock");
1083
+ const deadline = Date.now() + config.timeouts.build + config.timeouts.startup;
1084
+ while (true) {
1085
+ const lock = await open(path, "wx").catch((error) => {
1086
+ if (hasCode(error, "EEXIST")) {
1087
+ return;
1088
+ }
1089
+ throw error;
1090
+ });
1091
+ if (lock) {
1092
+ try {
1093
+ await lock.writeFile(String(process.pid));
1094
+ return await run();
1095
+ } finally {
1096
+ await lock.close();
1097
+ await unlink(path);
1098
+ }
1099
+ }
1100
+ const owner = Number(await readFile5(path, "utf8").catch((error) => {
1101
+ if (hasCode(error, "ENOENT")) {
1102
+ return "";
1103
+ }
1104
+ throw error;
1105
+ }));
1106
+ if (owner > 0 && !isProcessAlive(owner)) {
1107
+ throw new Error(`An interrupted command left ${path}. Remove that lock file and retry.`);
1108
+ }
1109
+ if (Date.now() >= deadline) {
1110
+ throw new Error(`Timed out waiting for another CODESYS command (${path})`);
1111
+ }
1112
+ await delay(200);
1113
+ }
1114
+ };
1115
+
1116
+ // packages/build/src/sources.ts
1117
+ import { readdir as readdir2, readFile as readFile6 } from "node:fs/promises";
1118
+ import { basename as basename4, resolve as resolve8 } from "node:path";
1119
+ import { identifierPattern as identifierPattern3 } from "@tsplc/iec/st";
1120
+
1121
+ // packages/build/src/io.ts
1122
+ import { ioArray, ioScalarBits, sizePrefix } from "@tsplc/iec";
1123
+ import { qualifiers as qualifiers2, variableSections as variableSections2 } from "@tsplc/iec/st";
1124
+ var span = (type) => ioArray(type) ?? { element: type, length: 1 };
1125
+ var addressSize = (type) => {
1126
+ const bits = ioScalarBits(type);
1127
+ return bits === undefined || bits === 64 ? undefined : sizePrefix(bits);
1128
+ };
1129
+ function bindStructuredTextIo(objects, bindings) {
1130
+ const declarations = [];
1131
+ const visit = (object) => {
1132
+ const tokens = tokenize(object.declaration, object.source);
1133
+ let section = false;
1134
+ let start = 0;
1135
+ for (const [index, token] of tokens.entries()) {
1136
+ if (variableSections2.has(token.value)) {
1137
+ section = true;
1138
+ start = index + 1;
1139
+ continue;
1140
+ }
1141
+ if (token.value === "END_VAR") {
1142
+ section = false;
1143
+ continue;
1144
+ }
1145
+ if (!section || token.value !== ";") {
1146
+ continue;
1147
+ }
1148
+ const part = tokens.slice(start, index).filter((t) => t.kind !== "pragma");
1149
+ start = index + 1;
1150
+ const colon = part.find((t) => t.value === ":");
1151
+ if (!colon) {
1152
+ continue;
1153
+ }
1154
+ const colonIndex = part.indexOf(colon);
1155
+ if (colonIndex < 1) {
1156
+ continue;
1157
+ }
1158
+ const names = part.slice(0, colonIndex).filter((t) => t.kind === "word" && !qualifiers2.has(t.value) && t.value !== "AT");
1159
+ const at = part.find((t) => t.value === "AT");
1160
+ const variables = at ? names.filter((t) => t.start < at.start) : names;
1161
+ const initializer = part.findIndex((t, index) => index > colonIndex && t.value === ":=");
1162
+ const typeTokens = part.slice(colonIndex + 1, initializer < 0 ? undefined : initializer);
1163
+ const [first] = typeTokens;
1164
+ const last = typeTokens.at(-1);
1165
+ const type = first && last ? object.declaration.slice(first.start, last.end).replace(/\s+/g, " ").toUpperCase() : "";
1166
+ for (const name of variables) {
1167
+ declarations.push({
1168
+ object,
1169
+ name: name.value,
1170
+ names: variables,
1171
+ colon,
1172
+ type
1173
+ });
1174
+ }
1175
+ }
1176
+ object.children.forEach(visit);
1177
+ };
1178
+ objects.forEach(visit);
1179
+ const edits = new Map;
1180
+ for (const [symbol, binding] of Object.entries(bindings)) {
1181
+ const parts = symbol.toUpperCase().split(".");
1182
+ if (parts.length > 2) {
1183
+ throw new Error(`${symbol}: direct IO maps a scalar declaration, not a nested field.`);
1184
+ }
1185
+ const matches = declarations.filter((d) => d.name === parts.at(-1) && (parts.length === 1 || d.object.name.toUpperCase() === parts[0]));
1186
+ const [declaration, ...others] = matches;
1187
+ if (!declaration || others.length) {
1188
+ throw new Error(`${symbol}: expected one ST variable declaration, found ${matches.length}. Use Object.Variable to disambiguate.`);
1189
+ }
1190
+ const [variable, ...siblings] = declaration.names;
1191
+ if (!variable || siblings.length) {
1192
+ throw new Error(`${symbol}: declare mapped IO variables separately.`);
1193
+ }
1194
+ const declared = span(declaration.type);
1195
+ const size = addressSize(declared.element);
1196
+ if (!size || declared.length > 1 && declared.element === "BOOL" || binding.address[2] !== size) {
1197
+ throw new Error(`${symbol}: ${declaration.type} is incompatible with ${binding.address}.`);
1198
+ }
1199
+ const expected = binding.type && span(binding.type.replace(/\s+/g, " ").toUpperCase());
1200
+ if (expected && (expected.element !== declared.element || expected.length !== declared.length)) {
1201
+ throw new Error(`${symbol}: expected ${binding.type}, found ${declaration.type}.`);
1202
+ }
1203
+ const list = edits.get(declaration.object) ?? [];
1204
+ const start = variable.end;
1205
+ if (list.some((e) => e.start === start)) {
1206
+ throw new Error(`${symbol}: the variable has more than one IO binding.`);
1207
+ }
1208
+ list.push({
1209
+ start,
1210
+ end: declaration.colon.start,
1211
+ text: ` AT ${binding.address} `
1212
+ });
1213
+ edits.set(declaration.object, list);
1214
+ }
1215
+ for (const [object, list] of edits) {
1216
+ for (const edit of list.sort((a, b) => b.start - a.start)) {
1217
+ object.declaration = object.declaration.slice(0, edit.start) + edit.text + object.declaration.slice(edit.end);
1218
+ }
1219
+ }
1220
+ }
1221
+
1198
1222
  // packages/build/src/st/parse.ts
1223
+ import { basename as basename3 } from "node:path";
1224
+ import {
1225
+ globalSections,
1226
+ identifierPattern as identifierPattern2,
1227
+ qualifiers as qualifiers3,
1228
+ variableSections as variableSections3
1229
+ } from "@tsplc/iec/st";
1199
1230
  var dutTypes = {
1200
1231
  STRUCT: "structure",
1201
1232
  UNION: "union",
@@ -1563,18 +1594,6 @@ import {
1563
1594
  sha256Digest as sha256Digest2
1564
1595
  } from "@tsplc/config/build";
1565
1596
  import { z as z9 } from "zod";
1566
-
1567
- // packages/build/src/online-change-check.ts
1568
- import { elementaryType } from "@tsplc/iec";
1569
- import { z as z8 } from "zod";
1570
- var committedObjectSchema = z8.lazy(() => z8.looseObject({
1571
- kind: z8.string(),
1572
- name: z8.string(),
1573
- declaration: z8.string(),
1574
- children: z8.array(committedObjectSchema).optional()
1575
- }));
1576
-
1577
- // packages/build/src/controller-session.ts
1578
1597
  var guid = z9.string().regex(/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i);
1579
1598
  var pointerSchema = z9.strictObject({
1580
1599
  schemaVersion: z9.literal(1),
@@ -1678,6 +1697,8 @@ var loginReplySchema = z10.discriminatedUnion("ok", [
1678
1697
  ok: z10.literal(false),
1679
1698
  error: z10.string(),
1680
1699
  downloadRequired: z10.literal(true).optional(),
1700
+ findings: z10.array(onlineChangeFindingSchema).optional(),
1701
+ messages: z10.array(z10.string()).optional(),
1681
1702
  rollback: rollbackSchema.optional()
1682
1703
  })
1683
1704
  ]);
@@ -2195,6 +2216,8 @@ var loginReplySchema2 = z13.discriminatedUnion("ok", [
2195
2216
  ok: z13.literal(false),
2196
2217
  error: z13.string(),
2197
2218
  downloadRequired: z13.literal(true).optional(),
2219
+ findings: z13.array(onlineChangeFindingSchema).optional(),
2220
+ messages: z13.array(z13.string()).optional(),
2198
2221
  rollback: rollbackSchema.optional()
2199
2222
  })
2200
2223
  ]);
@@ -2402,5 +2425,5 @@ try {
2402
2425
  process.exitCode = 1;
2403
2426
  }
2404
2427
 
2405
- //# debugId=8A39677BBD3E872964756E2164756E21
2428
+ //# debugId=B5952F23DC80EDB464756E2164756E21
2406
2429
  //# sourceMappingURL=cli.js.map