@tsplc/typegen 0.5.13 → 0.5.15
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/README.md +29 -3
- package/dist/cli.js +609 -433
- package/dist/cli.js.map +21 -16
- package/dist/codegen.js +242 -192
- package/dist/codegen.js.map +8 -6
- package/dist/declarations.js +277 -138
- package/dist/declarations.js.map +8 -7
- package/dist/hardware.js +383 -114
- package/dist/hardware.js.map +13 -10
- package/dist/index.js +652 -468
- package/dist/index.js.map +21 -17
- package/dist/project-snapshot.js +17 -19
- package/dist/project-snapshot.js.map +5 -4
- package/package.json +3 -3
- package/src/cli.ts +25 -33
- package/src/codegen/constants.ts +19 -25
- package/src/codegen/declarations.ts +12 -39
- package/src/codegen/generate.ts +118 -179
- package/src/codegen/parser.ts +121 -63
- package/src/codegen/resolution.ts +68 -0
- package/src/device-tree.ts +14 -97
- package/src/files.ts +40 -0
- package/src/hardware/catalog.ts +49 -0
- package/src/hardware/channels.ts +12 -5
- package/src/hardware/eds.ts +229 -45
- package/src/hardware/mapping-types.ts +58 -21
- package/src/hardware/modbus.ts +42 -28
- package/src/hardware/process-image.ts +57 -24
- package/src/hardware/selection.ts +25 -9
- package/src/hardware.ts +56 -77
- package/src/index.ts +4 -7
- package/src/outputs.ts +50 -0
- package/src/project-snapshot.ts +13 -25
- package/src/project.ts +108 -136
|
@@ -1,4 +1,22 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
elementaryTypes,
|
|
3
|
+
ioArray,
|
|
4
|
+
ioScalar,
|
|
5
|
+
isIntegerValued,
|
|
6
|
+
isWholeValue,
|
|
7
|
+
sizePrefix,
|
|
8
|
+
sizePrefixes,
|
|
9
|
+
type WholeValueType,
|
|
10
|
+
} from "@tsplc/iec";
|
|
11
|
+
|
|
12
|
+
const scalarLayout = (scalar: WholeValueType) =>
|
|
13
|
+
isIntegerValued(scalar) ? `INT${scalar.bits}` : scalar.name;
|
|
14
|
+
|
|
15
|
+
const scalarLayoutBits = new Map(
|
|
16
|
+
elementaryTypes
|
|
17
|
+
.filter(isWholeValue)
|
|
18
|
+
.map((scalar) => [scalarLayout(scalar), scalar.bits]),
|
|
19
|
+
);
|
|
2
20
|
|
|
3
21
|
/**
|
|
4
22
|
* Binding classes group integers and bit strings by width, BOOL, floats and time types
|
|
@@ -13,12 +31,13 @@ export function ioLayout(type: string): string | undefined {
|
|
|
13
31
|
: undefined;
|
|
14
32
|
}
|
|
15
33
|
const scalar = ioScalar(type);
|
|
16
|
-
|
|
17
|
-
return `INT${scalar.bits}`;
|
|
18
|
-
}
|
|
19
|
-
return scalar?.name;
|
|
34
|
+
return scalar && scalarLayout(scalar);
|
|
20
35
|
}
|
|
21
36
|
|
|
37
|
+
/** The width each element of an `ioLayout` binds at. */
|
|
38
|
+
export const layoutBits = (layout: string) =>
|
|
39
|
+
scalarLayoutBits.get(layout.replace(/^\d+x/, ""));
|
|
40
|
+
|
|
22
41
|
/** The size prefix a `bits`-wide scalar binds at; tsPLC binds nothing at 64-bit addresses. */
|
|
23
42
|
export const addressSize = (bits: number) =>
|
|
24
43
|
bits === 64 ? undefined : sizePrefix(bits);
|
|
@@ -44,15 +63,37 @@ export function directAddress(
|
|
|
44
63
|
return `%${region}${size}${byte / (bits / 8)}`;
|
|
45
64
|
}
|
|
46
65
|
|
|
47
|
-
/** The
|
|
48
|
-
export
|
|
66
|
+
/** The region and bits a direct address names: `directAddress`'s inverse at base 0. */
|
|
67
|
+
export function addressBits(
|
|
68
|
+
address: string,
|
|
69
|
+
): { region: "I" | "Q"; offsetBits: number; bits: number } | undefined {
|
|
70
|
+
const [, region, size, index, bit] =
|
|
71
|
+
/^%([IQ])([A-Z])(\d+)(?:\.([0-7]))?$/.exec(address) ?? [];
|
|
72
|
+
const bits = size === undefined ? undefined : sizePrefixes.get(size)?.bits;
|
|
73
|
+
if (
|
|
74
|
+
(region !== "I" && region !== "Q") ||
|
|
75
|
+
bits === undefined ||
|
|
76
|
+
addressSize(bits) !== size ||
|
|
77
|
+
(bits === 1) !== (bit !== undefined)
|
|
78
|
+
) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
const offsetBits =
|
|
82
|
+
bits === 1 ? Number(index) * 8 + Number(bit) : Number(index) * bits;
|
|
83
|
+
return { region, offsetBits, bits };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const regionOf = (direction: "input" | "output"): "I" | "Q" =>
|
|
87
|
+
direction === "input" ? "I" : "Q";
|
|
88
|
+
|
|
89
|
+
export const isOutputAddress = (address: string) => address.startsWith("%Q");
|
|
49
90
|
|
|
50
|
-
/** A device's byte region of the %I or %Q process image, at `
|
|
91
|
+
/** A device's byte region of the %I or %Q process image, at byte `offset` or still to be placed. */
|
|
51
92
|
export interface ProcessImageRegion {
|
|
52
93
|
id: string;
|
|
53
94
|
region: "I" | "Q";
|
|
54
95
|
size: number;
|
|
55
|
-
|
|
96
|
+
offset?: number;
|
|
56
97
|
}
|
|
57
98
|
|
|
58
99
|
/**
|
|
@@ -62,23 +103,15 @@ export interface ProcessImageRegion {
|
|
|
62
103
|
export function regionPlacer(
|
|
63
104
|
regions: readonly ProcessImageRegion[],
|
|
64
105
|
{ generatedTree }: { generatedTree: boolean },
|
|
65
|
-
): (region: ProcessImageRegion) =>
|
|
66
|
-
const taken = regions.flatMap(({ region, size,
|
|
67
|
-
|
|
68
|
-
? [
|
|
69
|
-
{
|
|
70
|
-
region,
|
|
71
|
-
start: byteOffset(address),
|
|
72
|
-
end: byteOffset(address) + size,
|
|
73
|
-
},
|
|
74
|
-
]
|
|
75
|
-
: [],
|
|
106
|
+
): (region: ProcessImageRegion) => number {
|
|
107
|
+
const taken = regions.flatMap(({ region, size, offset }) =>
|
|
108
|
+
offset === undefined ? [] : [{ region, start: offset, end: offset + size }],
|
|
76
109
|
);
|
|
77
110
|
const cursors = { I: 0, Q: 0 };
|
|
78
111
|
const aligned = (offset: number) => Math.ceil(offset / 4) * 4;
|
|
79
|
-
return ({ id, region, size,
|
|
80
|
-
if (
|
|
81
|
-
return
|
|
112
|
+
return ({ id, region, size, offset }) => {
|
|
113
|
+
if (offset !== undefined) {
|
|
114
|
+
return offset;
|
|
82
115
|
}
|
|
83
116
|
if (!generatedTree) {
|
|
84
117
|
throw new Error(
|
|
@@ -97,6 +130,6 @@ export function regionPlacer(
|
|
|
97
130
|
const start = clear(aligned(cursors[region]));
|
|
98
131
|
taken.push({ region, start, end: start + size });
|
|
99
132
|
cursors[region] = start + size;
|
|
100
|
-
return
|
|
133
|
+
return start;
|
|
101
134
|
};
|
|
102
135
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { HardwareDevice } from "@tsplc/config";
|
|
2
2
|
import type { EdsAssembly, EdsConnection, HardwareDescriptor } from "./eds";
|
|
3
|
+
import { addressBits, regionOf } from "./process-image";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* An explicit connection must carry the selected assemblies. Otherwise the one
|
|
@@ -60,8 +61,24 @@ export interface AssemblySelection {
|
|
|
60
61
|
instance: number;
|
|
61
62
|
assembly: EdsAssembly;
|
|
62
63
|
size: number;
|
|
63
|
-
/** The configured base; without one the device tree places the assembly. */
|
|
64
|
-
|
|
64
|
+
/** The configured base byte; without one the device tree places the assembly. */
|
|
65
|
+
offset: number | undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The byte a configured assembly address names, which must lie in its direction's region. */
|
|
69
|
+
function configuredByte(
|
|
70
|
+
id: string,
|
|
71
|
+
direction: "input" | "output",
|
|
72
|
+
address: string,
|
|
73
|
+
): number {
|
|
74
|
+
const region = regionOf(direction);
|
|
75
|
+
const location = addressBits(address);
|
|
76
|
+
if (location?.region !== region || location.bits !== 8) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`${id}: ${direction} assembly needs a %${region}B address.`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return location.offsetBits / 8;
|
|
65
82
|
}
|
|
66
83
|
|
|
67
84
|
/**
|
|
@@ -94,12 +111,11 @@ export function selectAssemblies(
|
|
|
94
111
|
`${id}: assembly ${instance} is not present in ${settings.file}.`,
|
|
95
112
|
);
|
|
96
113
|
}
|
|
97
|
-
const region = direction
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
114
|
+
const region = regionOf(direction);
|
|
115
|
+
const offset =
|
|
116
|
+
selection?.address === undefined
|
|
117
|
+
? undefined
|
|
118
|
+
: configuredByte(id, direction, selection.address);
|
|
103
119
|
const declaredSize =
|
|
104
120
|
direction === "input" ? connection?.inputSize : connection?.outputSize;
|
|
105
121
|
const payloadSize = selection?.size ?? declaredSize?.value ?? assembly.size;
|
|
@@ -129,7 +145,7 @@ export function selectAssemblies(
|
|
|
129
145
|
instance,
|
|
130
146
|
assembly,
|
|
131
147
|
size: payloadSize,
|
|
132
|
-
|
|
148
|
+
offset,
|
|
133
149
|
},
|
|
134
150
|
];
|
|
135
151
|
});
|
package/src/hardware.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
import { readdir, readFile } from "node:fs/promises";
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
3
2
|
import { resolve } from "node:path";
|
|
4
3
|
import type { ResolvedTsplcConfig } from "@tsplc/config";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from "./hardware/
|
|
11
|
-
import {
|
|
12
|
-
import { byteOffset, regionPlacer } from "./hardware/process-image";
|
|
4
|
+
import { tsplcConfigSchema } from "@tsplc/config/schema";
|
|
5
|
+
import { filesUnder, isMissingFile } from "./files";
|
|
6
|
+
import { boundChannel, type HardwareCatalog } from "./hardware/catalog";
|
|
7
|
+
import { assemblyChannels } from "./hardware/channels";
|
|
8
|
+
import { type HardwareDescriptor, parseEds } from "./hardware/eds";
|
|
9
|
+
import { placeModbusRegisters } from "./hardware/modbus";
|
|
10
|
+
import { directAddress, regionPlacer } from "./hardware/process-image";
|
|
13
11
|
import { selectAssemblies, selectConnection } from "./hardware/selection";
|
|
14
12
|
|
|
15
13
|
export { ioScalarBits } from "@tsplc/iec";
|
|
16
|
-
export
|
|
14
|
+
export { deviceTree } from "./device-tree";
|
|
15
|
+
export {
|
|
16
|
+
deviceChannel,
|
|
17
|
+
type HardwareCatalog,
|
|
18
|
+
type SelectedAssembly,
|
|
19
|
+
} from "./hardware/catalog";
|
|
20
|
+
export { type IoChannel, isPayloadByte } from "./hardware/channels";
|
|
17
21
|
export {
|
|
18
22
|
type EdsAssembly,
|
|
19
23
|
type EdsConnection,
|
|
@@ -29,73 +33,49 @@ export {
|
|
|
29
33
|
type IoTarget,
|
|
30
34
|
type ProjectTargets,
|
|
31
35
|
} from "./hardware/mapping-types";
|
|
32
|
-
export type
|
|
33
|
-
export {
|
|
36
|
+
export { type ModbusRegister, modbusServerName } from "./hardware/modbus";
|
|
37
|
+
export {
|
|
38
|
+
addressBits,
|
|
39
|
+
ioLayout,
|
|
40
|
+
isOutputAddress,
|
|
41
|
+
regionOf,
|
|
42
|
+
} from "./hardware/process-image";
|
|
34
43
|
|
|
35
|
-
|
|
36
|
-
direction: "input" | "output";
|
|
37
|
-
instance: number;
|
|
38
|
-
name: string;
|
|
39
|
-
address: string;
|
|
40
|
-
size: number;
|
|
41
|
-
capacity: number;
|
|
42
|
-
}
|
|
43
|
-
export interface HardwareCatalog {
|
|
44
|
-
descriptors: HardwareDescriptor[];
|
|
45
|
-
devices: {
|
|
46
|
-
id: string;
|
|
47
|
-
descriptor: string;
|
|
48
|
-
connection?: Pick<EdsConnection, "id" | "name" | "path">;
|
|
49
|
-
assemblies: SelectedAssembly[];
|
|
50
|
-
channels: IoChannel[];
|
|
51
|
-
statusSymbols: Record<string, string>;
|
|
52
|
-
modbus?: { port: number; registers: ModbusRegister[] };
|
|
53
|
-
}[];
|
|
54
|
-
}
|
|
44
|
+
const defaultEds = tsplcConfigSchema.shape.eds.parse(undefined);
|
|
55
45
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
) {
|
|
46
|
+
/**
|
|
47
|
+
* The EDS files under a directory in locale order, the order generated
|
|
48
|
+
* hardware.ts lists them in; only the default directory may be absent.
|
|
49
|
+
*/
|
|
50
|
+
const edsFiles = (config: ResolvedTsplcConfig, directory: string) =>
|
|
51
|
+
filesUnder(directory, "eds", (a, b) => a.localeCompare(b)).catch(
|
|
52
|
+
(error: unknown) => {
|
|
53
|
+
if (config.eds === defaultEds && isMissingFile(error)) {
|
|
65
54
|
return [];
|
|
66
55
|
}
|
|
67
|
-
throw
|
|
56
|
+
throw error;
|
|
68
57
|
},
|
|
69
58
|
);
|
|
70
|
-
const files: string[] = [];
|
|
71
|
-
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
72
|
-
const path = resolve(directory, entry.name);
|
|
73
|
-
if (entry.isDirectory()) {
|
|
74
|
-
files.push(...(await edsFiles(path)));
|
|
75
|
-
} else if (entry.isFile() && /\.eds$/i.test(entry.name)) {
|
|
76
|
-
files.push(path);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return files;
|
|
80
|
-
}
|
|
81
59
|
|
|
82
|
-
/** Every descriptor under the `eds` directories, then each device's own file. */
|
|
83
60
|
async function readDescriptors(
|
|
84
61
|
config: ResolvedTsplcConfig,
|
|
85
62
|
root: string,
|
|
86
|
-
): Promise<HardwareDescriptor
|
|
63
|
+
): Promise<Map<string, HardwareDescriptor>> {
|
|
87
64
|
const files = new Set<string>();
|
|
88
65
|
for (const directory of [config.eds].flat()) {
|
|
89
|
-
for (const file of await edsFiles(resolve(root, directory))) {
|
|
66
|
+
for (const file of await edsFiles(config, resolve(root, directory))) {
|
|
90
67
|
files.add(file);
|
|
91
68
|
}
|
|
92
69
|
}
|
|
93
70
|
for (const device of Object.values(config.devices)) {
|
|
94
71
|
files.add(resolve(root, device.file));
|
|
95
72
|
}
|
|
96
|
-
return
|
|
97
|
-
|
|
98
|
-
|
|
73
|
+
return new Map(
|
|
74
|
+
await Promise.all(
|
|
75
|
+
[...files].map(
|
|
76
|
+
async (file) =>
|
|
77
|
+
[file, parseEds(await readFile(file, "utf8"), file)] as const,
|
|
78
|
+
),
|
|
99
79
|
),
|
|
100
80
|
);
|
|
101
81
|
}
|
|
@@ -106,9 +86,10 @@ export async function loadHardware(
|
|
|
106
86
|
): Promise<HardwareCatalog> {
|
|
107
87
|
const descriptors = await readDescriptors(config, root);
|
|
108
88
|
const selected = Object.entries(config.devices).map(([id, settings]) => {
|
|
109
|
-
const descriptor = descriptors.
|
|
110
|
-
|
|
111
|
-
|
|
89
|
+
const descriptor = descriptors.get(resolve(root, settings.file));
|
|
90
|
+
if (!descriptor) {
|
|
91
|
+
throw new Error(`${id}: ${settings.file} was not read.`);
|
|
92
|
+
}
|
|
112
93
|
const connection = selectConnection(id, settings, descriptor);
|
|
113
94
|
const selections = selectAssemblies(id, settings, descriptor, connection);
|
|
114
95
|
return { id, settings, descriptor, connection, selections };
|
|
@@ -119,10 +100,14 @@ export async function loadHardware(
|
|
|
119
100
|
);
|
|
120
101
|
const placed = selected.map((device) => ({
|
|
121
102
|
...device,
|
|
122
|
-
assemblies: device.selections.map((selection) =>
|
|
123
|
-
|
|
124
|
-
address
|
|
125
|
-
|
|
103
|
+
assemblies: device.selections.map((selection) => {
|
|
104
|
+
const offset = place(selection);
|
|
105
|
+
const address = directAddress(selection.region, offset, 0, 8);
|
|
106
|
+
if (!address) {
|
|
107
|
+
throw new Error(`${device.id}: byte ${offset} has no direct address.`);
|
|
108
|
+
}
|
|
109
|
+
return { ...selection, offset, address };
|
|
110
|
+
}),
|
|
126
111
|
}));
|
|
127
112
|
// Registers are placed after every assembly, so adding one leaves the existing process image in place.
|
|
128
113
|
const devices = placed.map(
|
|
@@ -153,13 +138,13 @@ export async function loadHardware(
|
|
|
153
138
|
channels: [
|
|
154
139
|
...registers.flatMap((register) => register.channels),
|
|
155
140
|
...assemblies.flatMap(
|
|
156
|
-
({ direction, instance, assembly, size,
|
|
141
|
+
({ direction, instance, assembly, size, offset }) =>
|
|
157
142
|
assemblyChannels({
|
|
158
143
|
device: id,
|
|
159
144
|
direction,
|
|
160
145
|
instance,
|
|
161
146
|
members: assembly.members,
|
|
162
|
-
base:
|
|
147
|
+
base: offset,
|
|
163
148
|
payloadSize: size,
|
|
164
149
|
}),
|
|
165
150
|
),
|
|
@@ -174,7 +159,7 @@ export async function loadHardware(
|
|
|
174
159
|
};
|
|
175
160
|
},
|
|
176
161
|
);
|
|
177
|
-
return { descriptors, devices };
|
|
162
|
+
return { descriptors: [...descriptors.values()], devices };
|
|
178
163
|
}
|
|
179
164
|
|
|
180
165
|
export function resolveIo(
|
|
@@ -186,13 +171,7 @@ export function resolveIo(
|
|
|
186
171
|
if ("address" in binding) {
|
|
187
172
|
return [symbol, binding];
|
|
188
173
|
}
|
|
189
|
-
const
|
|
190
|
-
const channel = device?.channels.find((c) => c.id === binding.channel);
|
|
191
|
-
if (!channel?.address) {
|
|
192
|
-
throw new Error(
|
|
193
|
-
`${symbol}: unknown or unaddressable channel ${binding.device}.${binding.channel}.`,
|
|
194
|
-
);
|
|
195
|
-
}
|
|
174
|
+
const channel = boundChannel(catalog, symbol, binding);
|
|
196
175
|
return [
|
|
197
176
|
symbol,
|
|
198
177
|
{
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
export { ioScalarBits } from "@tsplc/iec";
|
|
2
2
|
export { generateTypes } from "./codegen/generate";
|
|
3
|
-
export { deviceTree
|
|
4
|
-
export {
|
|
5
|
-
|
|
6
|
-
loadHardware,
|
|
7
|
-
resolveIo,
|
|
8
|
-
type SelectedAssembly,
|
|
9
|
-
} from "./hardware";
|
|
3
|
+
export { deviceTree } from "./device-tree";
|
|
4
|
+
export { loadHardware, resolveIo } from "./hardware";
|
|
5
|
+
export type { HardwareCatalog, SelectedAssembly } from "./hardware/catalog";
|
|
10
6
|
export type { IoChannel } from "./hardware/channels";
|
|
11
7
|
export {
|
|
12
8
|
type EdsAssembly,
|
|
@@ -15,6 +11,7 @@ export {
|
|
|
15
11
|
type EdsParameter,
|
|
16
12
|
type EdsSize,
|
|
17
13
|
type HardwareDescriptor,
|
|
14
|
+
normalizeEds,
|
|
18
15
|
parseEds,
|
|
19
16
|
readEdsRecords,
|
|
20
17
|
} from "./hardware/eds";
|
package/src/outputs.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { mkdir, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
import { format } from "prettier";
|
|
5
|
+
import { readIfPresent } from "./files";
|
|
6
|
+
|
|
7
|
+
// Prettier's format() never reads a Prettier config, so generated files keep Prettier's
|
|
8
|
+
// defaults in whichever repository typegen runs.
|
|
9
|
+
export const formatTypeScript = (source: string) =>
|
|
10
|
+
format(source, { parser: "typescript" });
|
|
11
|
+
|
|
12
|
+
/** A file typegen generates, or with `content: null` one that must not exist. */
|
|
13
|
+
export interface Output {
|
|
14
|
+
path: string;
|
|
15
|
+
content: string | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Writes each output through a temporary file, so a reader never sees half of one. */
|
|
19
|
+
export async function writeOutputs(outputs: readonly Output[]) {
|
|
20
|
+
for (const { path, content } of outputs) {
|
|
21
|
+
if (content === null) {
|
|
22
|
+
await rm(path, { force: true });
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
await mkdir(dirname(path), { recursive: true });
|
|
26
|
+
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
27
|
+
try {
|
|
28
|
+
await writeFile(temporary, content);
|
|
29
|
+
await rename(temporary, path);
|
|
30
|
+
} finally {
|
|
31
|
+
await rm(temporary, { force: true });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function checkOutputs(outputs: readonly Output[]) {
|
|
37
|
+
for (const { path, content } of outputs) {
|
|
38
|
+
const actual = await readIfPresent(path);
|
|
39
|
+
// Consumers may format generated TS with their own formatter. Compare the
|
|
40
|
+
// same normalized syntax while preserving changes to values and comments;
|
|
41
|
+
// text Prettier cannot parse is compared as it is, and so is stale.
|
|
42
|
+
const normalized =
|
|
43
|
+
actual !== null && /\.ts$/i.test(path)
|
|
44
|
+
? await formatTypeScript(actual).catch(() => actual)
|
|
45
|
+
: actual;
|
|
46
|
+
if (normalized !== content) {
|
|
47
|
+
throw new Error(`Generated file is missing or stale: ${path}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/project-snapshot.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import { dirname, resolve } from "node:path";
|
|
3
|
-
import {
|
|
4
|
-
|
|
3
|
+
import {
|
|
4
|
+
ioSymbolsSchema,
|
|
5
|
+
projectSnapshotFile,
|
|
6
|
+
} from "@tsplc/config/project-files";
|
|
7
|
+
import { tsplcConfigSchema } from "@tsplc/config/schema";
|
|
5
8
|
import { z } from "zod";
|
|
9
|
+
import { isMissingFile } from "./files";
|
|
6
10
|
|
|
7
11
|
/** What `tsplc typegen` materializes at `projectSnapshotFile` for readers that never execute the config. */
|
|
8
12
|
export const projectSnapshotSchema = z.object({
|
|
@@ -22,35 +26,19 @@ export type SnapshotRead =
|
|
|
22
26
|
| { kind: "absent" | "stale" }
|
|
23
27
|
| { kind: "invalid"; reason: string };
|
|
24
28
|
|
|
25
|
-
const missing = (error: unknown) =>
|
|
26
|
-
error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
27
|
-
|
|
28
|
-
const parseJson = (text: string): { json: unknown } | { error: unknown } => {
|
|
29
|
-
try {
|
|
30
|
-
return { json: JSON.parse(text) };
|
|
31
|
-
} catch (error) {
|
|
32
|
-
return { error };
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
|
|
36
29
|
export async function readProjectSnapshot(
|
|
37
30
|
configFile: string,
|
|
38
31
|
): Promise<SnapshotRead> {
|
|
39
32
|
const file = resolve(dirname(configFile), projectSnapshotFile);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
return missing(read.error)
|
|
33
|
+
let json: unknown;
|
|
34
|
+
try {
|
|
35
|
+
json = JSON.parse(await readFile(file, "utf8"));
|
|
36
|
+
} catch (error) {
|
|
37
|
+
return isMissingFile(error)
|
|
46
38
|
? { kind: "absent" }
|
|
47
|
-
: { kind: "invalid", reason: `${file}: ${String(
|
|
48
|
-
}
|
|
49
|
-
const decoded = parseJson(read.text);
|
|
50
|
-
if ("error" in decoded) {
|
|
51
|
-
return { kind: "invalid", reason: `${file}: ${String(decoded.error)}` };
|
|
39
|
+
: { kind: "invalid", reason: `${file}: ${String(error)}` };
|
|
52
40
|
}
|
|
53
|
-
const parsed = projectSnapshotSchema.safeParse(
|
|
41
|
+
const parsed = projectSnapshotSchema.safeParse(json);
|
|
54
42
|
if (!parsed.success) {
|
|
55
43
|
return {
|
|
56
44
|
kind: "invalid",
|