@tsplc/typegen 0.5.0 → 0.5.1
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 +59 -43
- package/dist/cli.js.map +7 -6
- package/dist/codegen.js +14 -7
- package/dist/codegen.js.map +4 -4
- package/dist/declarations.js +417 -0
- package/dist/declarations.js.map +13 -0
- package/dist/index.js +57 -39
- package/dist/index.js.map +7 -6
- package/package.json +8 -3
- package/src/codegen/declarations.ts +73 -0
- package/src/codegen/generate.ts +1 -1
- package/src/codegen/parser.ts +29 -14
- package/src/project.ts +2 -29
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
3
|
// packages/typegen/src/cli.ts
|
|
4
|
-
import { mkdir as mkdir2, readFile as
|
|
4
|
+
import { mkdir as mkdir2, readFile as readFile5, rename as rename2, rm, writeFile as writeFile2 } from "node:fs/promises";
|
|
5
5
|
import { dirname as dirname2, resolve as resolve3 } from "node:path";
|
|
6
6
|
import { parseArgs } from "node:util";
|
|
7
7
|
|
|
@@ -313,10 +313,10 @@ class Parser {
|
|
|
313
313
|
}
|
|
314
314
|
return { kind: "named", name };
|
|
315
315
|
}
|
|
316
|
-
members(end) {
|
|
316
|
+
members(end, { retain }) {
|
|
317
317
|
const members = [];
|
|
318
318
|
while (this.peek() !== end) {
|
|
319
|
-
const
|
|
319
|
+
const symbol = this.tokens.slice(this.position, this.nextIndex()).flatMap((token) => token.kind === "attribute" ? symbolAccess(token.text) : []).at(-1);
|
|
320
320
|
const description = this.documentation();
|
|
321
321
|
const names = [this.identifier()];
|
|
322
322
|
while (this.accept(",")) {
|
|
@@ -341,7 +341,8 @@ class Parser {
|
|
|
341
341
|
description,
|
|
342
342
|
type,
|
|
343
343
|
...initial === undefined ? {} : { initial },
|
|
344
|
-
...
|
|
344
|
+
...symbol ? { symbol } : {},
|
|
345
|
+
...retain ? { retain } : {}
|
|
345
346
|
});
|
|
346
347
|
}
|
|
347
348
|
}
|
|
@@ -361,7 +362,7 @@ class Parser {
|
|
|
361
362
|
declarations.push({
|
|
362
363
|
...common,
|
|
363
364
|
kind: "struct",
|
|
364
|
-
members: this.members("END_STRUCT")
|
|
365
|
+
members: this.members("END_STRUCT", { retain: false })
|
|
365
366
|
});
|
|
366
367
|
this.accept(";");
|
|
367
368
|
} else if (this.accept("(")) {
|
|
@@ -424,11 +425,17 @@ class Parser {
|
|
|
424
425
|
}
|
|
425
426
|
written.add(qualifier);
|
|
426
427
|
}
|
|
427
|
-
const members = this.members("END_VAR"
|
|
428
|
+
const members = this.members("END_VAR", {
|
|
429
|
+
retain: written.has("RETAIN") || written.has("PERSISTENT")
|
|
430
|
+
});
|
|
428
431
|
this.accept(";");
|
|
429
432
|
return { constant: written.has("CONSTANT"), members };
|
|
430
433
|
}
|
|
431
434
|
}
|
|
435
|
+
var symbolAccess = (attribute) => {
|
|
436
|
+
const access = /^\{\s*attribute\s+'symbol'\s*:=\s*'(read|write|readwrite)'\s*\}$/i.exec(attribute)?.[1]?.toLowerCase();
|
|
437
|
+
return access === "read" || access === "write" || access === "readwrite" ? [access] : [];
|
|
438
|
+
};
|
|
432
439
|
function parseDeclarations(source) {
|
|
433
440
|
return new Parser(source).declarations();
|
|
434
441
|
}
|
|
@@ -512,7 +519,7 @@ function published(globals, publication) {
|
|
|
512
519
|
members: list.members.filter((member) => publication ? [
|
|
513
520
|
...publication.production,
|
|
514
521
|
...publication.development
|
|
515
|
-
].includes(`${list.name}.${member.name}`) : member.
|
|
522
|
+
].includes(`${list.name}.${member.name}`) : member.symbol !== undefined)
|
|
516
523
|
}).filter((list) => list.constant || list.members.length);
|
|
517
524
|
}
|
|
518
525
|
function emit(declarations, globals, {
|
|
@@ -740,7 +747,7 @@ ${list.members.map((member) => `${documentation(member.description)}${quote(memb
|
|
|
740
747
|
}
|
|
741
748
|
|
|
742
749
|
// packages/typegen/src/project.ts
|
|
743
|
-
import { mkdir, readFile as
|
|
750
|
+
import { mkdir, readFile as readFile4, rename, writeFile } from "node:fs/promises";
|
|
744
751
|
import { dirname, resolve as resolve2 } from "node:path";
|
|
745
752
|
import {
|
|
746
753
|
ioSymbolsSchema,
|
|
@@ -753,11 +760,45 @@ import {
|
|
|
753
760
|
emittedStDirectory,
|
|
754
761
|
projectSnapshotFile
|
|
755
762
|
} from "@tsplc/config/project-files";
|
|
756
|
-
import { blockCommentEnd as blockCommentEnd2, lineCommentEnd as lineCommentEnd2 } from "@tsplc/iec/st";
|
|
757
763
|
import { format } from "prettier";
|
|
758
764
|
|
|
765
|
+
// packages/typegen/src/codegen/declarations.ts
|
|
766
|
+
import { readFile as readFile2 } from "node:fs/promises";
|
|
767
|
+
import { blockCommentEnd as blockCommentEnd2, lineCommentEnd as lineCommentEnd2 } from "@tsplc/iec/st";
|
|
768
|
+
var codeStart = (text, offset) => {
|
|
769
|
+
const start = offset + text.slice(offset).search(/\S|$/);
|
|
770
|
+
if (text.startsWith("(*", start)) {
|
|
771
|
+
const end = blockCommentEnd2(text, start);
|
|
772
|
+
return end === null ? start : codeStart(text, end);
|
|
773
|
+
}
|
|
774
|
+
if (text.startsWith("//", start)) {
|
|
775
|
+
return codeStart(text, lineCommentEnd2(text, start));
|
|
776
|
+
}
|
|
777
|
+
if (text.startsWith("{", start)) {
|
|
778
|
+
const end = text.indexOf("}", start);
|
|
779
|
+
return end < 0 ? start : codeStart(text, end + 1);
|
|
780
|
+
}
|
|
781
|
+
return start;
|
|
782
|
+
};
|
|
783
|
+
var declarationFiles = async (files) => {
|
|
784
|
+
const sorted = {
|
|
785
|
+
types: [],
|
|
786
|
+
globals: []
|
|
787
|
+
};
|
|
788
|
+
for (const file of files) {
|
|
789
|
+
const text = await readFile2(file, "utf8");
|
|
790
|
+
const code = text.slice(codeStart(text, 0));
|
|
791
|
+
if (/^TYPE\b/i.test(code)) {
|
|
792
|
+
sorted.types.push(file);
|
|
793
|
+
} else if (/^VAR_GLOBAL\b/i.test(code)) {
|
|
794
|
+
sorted.globals.push(file);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return sorted;
|
|
798
|
+
};
|
|
799
|
+
|
|
759
800
|
// packages/typegen/src/hardware.ts
|
|
760
|
-
import { readdir as readdir2, readFile as
|
|
801
|
+
import { readdir as readdir2, readFile as readFile3 } from "node:fs/promises";
|
|
761
802
|
import { resolve } from "node:path";
|
|
762
803
|
|
|
763
804
|
// packages/typegen/src/hardware/channels.ts
|
|
@@ -1382,7 +1423,7 @@ async function readDescriptors(config, root) {
|
|
|
1382
1423
|
for (const device of Object.values(config.devices)) {
|
|
1383
1424
|
files.add(resolve(root, device.file));
|
|
1384
1425
|
}
|
|
1385
|
-
return Promise.all([...files].map(async (file) => parseEds(await
|
|
1426
|
+
return Promise.all([...files].map(async (file) => parseEds(await readFile3(file, "utf8"), file)));
|
|
1386
1427
|
}
|
|
1387
1428
|
async function loadHardware(config, root) {
|
|
1388
1429
|
const descriptors = await readDescriptors(config, root);
|
|
@@ -1464,7 +1505,7 @@ function resolveIo(config, catalog) {
|
|
|
1464
1505
|
|
|
1465
1506
|
// packages/typegen/src/project.ts
|
|
1466
1507
|
var formatTypeScript = (source) => format(source, { parser: "typescript" });
|
|
1467
|
-
var readPublication = async (root) => publicationSchema.parse(JSON.parse(await
|
|
1508
|
+
var readPublication = async (root) => publicationSchema.parse(JSON.parse(await readFile4(resolve2(root, emittedStDirectory, emittedFiles.publication), "utf8")));
|
|
1468
1509
|
var generatedFiles = {
|
|
1469
1510
|
hardware: "hardware.ts",
|
|
1470
1511
|
contract: "plc.ts",
|
|
@@ -1472,7 +1513,7 @@ var generatedFiles = {
|
|
|
1472
1513
|
};
|
|
1473
1514
|
async function readIoSymbols(root) {
|
|
1474
1515
|
const file = resolve2(root, emittedStDirectory, emittedFiles.ioSymbols);
|
|
1475
|
-
const text = await
|
|
1516
|
+
const text = await readFile4(file, "utf8").catch((error) => {
|
|
1476
1517
|
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
1477
1518
|
return "{}";
|
|
1478
1519
|
}
|
|
@@ -1511,32 +1552,7 @@ async function contractSources(config, root, settings) {
|
|
|
1511
1552
|
if (!files.length) {
|
|
1512
1553
|
throw new Error(`no .st declarations found in ${sourceRoot}`);
|
|
1513
1554
|
}
|
|
1514
|
-
|
|
1515
|
-
for (const file of files) {
|
|
1516
|
-
const text = await readFile3(file, "utf8");
|
|
1517
|
-
const code = text.slice(codeStart(text, 0));
|
|
1518
|
-
if (/^TYPE\b/i.test(code)) {
|
|
1519
|
-
sources.types.push(file);
|
|
1520
|
-
} else if (/^VAR_GLOBAL\b/i.test(code)) {
|
|
1521
|
-
sources.globals.push(file);
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
return sources;
|
|
1525
|
-
}
|
|
1526
|
-
function codeStart(text, offset) {
|
|
1527
|
-
const start = offset + text.slice(offset).search(/\S|$/);
|
|
1528
|
-
if (text.startsWith("(*", start)) {
|
|
1529
|
-
const end = blockCommentEnd2(text, start);
|
|
1530
|
-
return end === null ? start : codeStart(text, end);
|
|
1531
|
-
}
|
|
1532
|
-
if (text.startsWith("//", start)) {
|
|
1533
|
-
return codeStart(text, lineCommentEnd2(text, start));
|
|
1534
|
-
}
|
|
1535
|
-
if (text.startsWith("{", start)) {
|
|
1536
|
-
const end = text.indexOf("}", start);
|
|
1537
|
-
return end < 0 ? start : codeStart(text, end + 1);
|
|
1538
|
-
}
|
|
1539
|
-
return start;
|
|
1555
|
+
return { ...await declarationFiles(files), explicit: false };
|
|
1540
1556
|
}
|
|
1541
1557
|
async function filesIn(directories) {
|
|
1542
1558
|
const files = [];
|
|
@@ -1562,7 +1578,7 @@ async function writeOutputs(outputs) {
|
|
|
1562
1578
|
}
|
|
1563
1579
|
async function checkOutputs(outputs) {
|
|
1564
1580
|
for (const output of outputs) {
|
|
1565
|
-
const actual = await
|
|
1581
|
+
const actual = await readFile4(output.path, "utf8").catch(() => {
|
|
1566
1582
|
return;
|
|
1567
1583
|
});
|
|
1568
1584
|
const normalized = actual !== undefined && /\.ts$/i.test(output.path) ? await formatTypeScript(actual) : actual;
|
|
@@ -1611,7 +1627,7 @@ async function generateProject(file = "tsplc.config.ts", options = {}) {
|
|
|
1611
1627
|
checkHmiTargets(config.hmi.controls, (await readPublication(root)).production);
|
|
1612
1628
|
}
|
|
1613
1629
|
if (config.compiler && !options.hardwareOnly) {
|
|
1614
|
-
const machine = await
|
|
1630
|
+
const machine = await readFile4(resolve2(root, emittedStDirectory, emittedFiles.machines), "utf8").catch(() => {
|
|
1615
1631
|
return;
|
|
1616
1632
|
});
|
|
1617
1633
|
if (machine) {
|
|
@@ -1663,7 +1679,7 @@ Generate TypeScript types and runtime field metadata from CODESYS ST declaration
|
|
|
1663
1679
|
globals: values.globals ? resolve3(values.globals) : undefined
|
|
1664
1680
|
});
|
|
1665
1681
|
if (values.check) {
|
|
1666
|
-
const existing = await
|
|
1682
|
+
const existing = await readFile5(out, "utf8").catch((error) => {
|
|
1667
1683
|
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
|
|
1668
1684
|
return;
|
|
1669
1685
|
}
|
|
@@ -1690,5 +1706,5 @@ main().catch((error) => {
|
|
|
1690
1706
|
process.exitCode = 1;
|
|
1691
1707
|
});
|
|
1692
1708
|
|
|
1693
|
-
//# debugId=
|
|
1709
|
+
//# debugId=99EBC0D16F6CE99B64756E2164756E21
|
|
1694
1710
|
//# sourceMappingURL=cli.js.map
|