@tsplc/config 0.2.17 → 0.2.18
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 +2 -2
- package/dist/build.js +164 -133
- package/dist/build.js.map +7 -7
- package/dist/index.js +291 -208
- package/dist/index.js.map +8 -7
- package/dist/schema.js +249 -204
- package/dist/schema.js.map +5 -5
- package/package.json +50 -51
- package/src/build-schema.ts +220 -197
- package/src/build.ts +73 -63
- package/src/compiler.ts +140 -122
- package/src/environment.ts +72 -0
- package/src/index.ts +52 -1
- package/src/loader.ts +33 -15
- package/src/schema.ts +429 -356
- package/templates/provenance.json +6 -6
package/src/compiler.ts
CHANGED
|
@@ -1,168 +1,186 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
|
|
3
3
|
interface Diagnostic {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
origin: "config";
|
|
5
|
+
code: string;
|
|
6
|
+
severity: "error";
|
|
7
|
+
message: string;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
export const nanosecondsPattern = /^[1-9][0-9]*$/;
|
|
10
11
|
export const durationSchema = z
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
.object({
|
|
13
|
+
kind: z.literal("duration"),
|
|
14
|
+
nanoseconds: z.string().regex(nanosecondsPattern),
|
|
15
|
+
})
|
|
16
|
+
.strict();
|
|
16
17
|
|
|
17
18
|
export type Duration = z.infer<typeof durationSchema>;
|
|
18
19
|
|
|
19
20
|
function duration(value: number, scale: number): Duration {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
22
|
+
throw new Error("Task duration must be finite and positive.");
|
|
23
|
+
}
|
|
24
|
+
// Keep task periods exact before serializing them for the compiler/backend.
|
|
25
|
+
const nanoseconds = value * scale;
|
|
26
|
+
if (!Number.isSafeInteger(nanoseconds)) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
"Task duration must be an exact, safe number of nanoseconds.",
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return { kind: "duration", nanoseconds: String(nanoseconds) };
|
|
29
32
|
}
|
|
30
33
|
|
|
31
34
|
export const ns = (value: number): Duration => duration(value, 1);
|
|
32
35
|
export const ms = (value: number): Duration => duration(value, 1_000_000);
|
|
33
|
-
export const seconds = (value: number): Duration =>
|
|
36
|
+
export const seconds = (value: number): Duration =>
|
|
37
|
+
duration(value, 1_000_000_000);
|
|
34
38
|
|
|
35
39
|
const sourceExportSchema = z
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
40
|
+
.object({ module: z.string().min(1), export: z.string().min(1) })
|
|
41
|
+
.strict();
|
|
42
|
+
// Names and priorities are only bounded here; a backend narrows them to what its scheduler accepts.
|
|
43
|
+
export const taskSchema = z.strictObject({
|
|
44
|
+
name: z.string().nonempty(),
|
|
45
|
+
schedule: z.strictObject({
|
|
46
|
+
kind: z.literal("cyclic"),
|
|
47
|
+
interval: durationSchema,
|
|
48
|
+
}),
|
|
49
|
+
priority: z.number().int().nonnegative(),
|
|
50
|
+
programs: z.array(z.string().nonempty()).nonempty(),
|
|
51
|
+
});
|
|
46
52
|
|
|
47
53
|
// Shared configuration validates the target envelope. Each backend owns its
|
|
48
54
|
// options schema and hardware-specific scheduling/addressing constraints.
|
|
49
55
|
const targetSchema = z
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
.object({
|
|
57
|
+
kind: z.string().min(1),
|
|
58
|
+
options: z.unknown().refine((value) => value !== undefined, {
|
|
59
|
+
message: "Target options are required.",
|
|
60
|
+
}),
|
|
61
|
+
})
|
|
62
|
+
.strict();
|
|
57
63
|
const common = {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
name: z.string().min(1),
|
|
65
|
+
tsconfig: z.string().min(1),
|
|
66
|
+
tasks: z.array(taskSchema).min(1),
|
|
67
|
+
target: targetSchema,
|
|
62
68
|
};
|
|
63
|
-
const entryConfigSchema = z
|
|
69
|
+
const entryConfigSchema = z
|
|
70
|
+
.object({ ...common, entry: sourceExportSchema })
|
|
71
|
+
.strict();
|
|
64
72
|
const programsConfigSchema = z
|
|
65
|
-
|
|
66
|
-
|
|
73
|
+
.object({
|
|
74
|
+
...common,
|
|
75
|
+
programs: z.record(z.string().min(1), sourceExportSchema),
|
|
76
|
+
})
|
|
77
|
+
.strict();
|
|
67
78
|
|
|
68
79
|
export interface TargetDescriptor<Options = unknown> {
|
|
69
|
-
|
|
70
|
-
|
|
80
|
+
kind: string;
|
|
81
|
+
options: Options;
|
|
71
82
|
}
|
|
72
83
|
export type SourceExport = z.infer<typeof sourceExportSchema>;
|
|
73
84
|
export type TaskConfig = Omit<z.infer<typeof taskSchema>, "programs"> & {
|
|
74
|
-
|
|
85
|
+
programs: readonly string[];
|
|
75
86
|
};
|
|
76
87
|
|
|
77
88
|
export type PlcConfig<Target extends TargetDescriptor = TargetDescriptor> = {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
name: string;
|
|
90
|
+
tsconfig: string;
|
|
91
|
+
tasks: readonly TaskConfig[];
|
|
92
|
+
target: Target;
|
|
82
93
|
} & (
|
|
83
|
-
|
|
84
|
-
|
|
94
|
+
| { entry: SourceExport; programs?: never }
|
|
95
|
+
| { programs: Readonly<Record<string, SourceExport>>; entry?: never }
|
|
85
96
|
);
|
|
86
97
|
|
|
87
98
|
export interface NormalizedConfig {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
99
|
+
name: string;
|
|
100
|
+
tsconfig: string;
|
|
101
|
+
programs: Readonly<Record<string, SourceExport>>;
|
|
102
|
+
tasks: readonly TaskConfig[];
|
|
103
|
+
target: TargetDescriptor;
|
|
93
104
|
}
|
|
94
105
|
|
|
95
106
|
export function definePlcConfig<const C extends PlcConfig>(config: C): C {
|
|
96
|
-
|
|
107
|
+
return config;
|
|
97
108
|
}
|
|
98
109
|
|
|
99
110
|
export function validateConfig(value: unknown): {
|
|
100
|
-
|
|
101
|
-
|
|
111
|
+
config?: NormalizedConfig;
|
|
112
|
+
diagnostics: readonly Diagnostic[];
|
|
102
113
|
} {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
114
|
+
const multiplePrograms =
|
|
115
|
+
typeof value === "object" &&
|
|
116
|
+
value !== null &&
|
|
117
|
+
Object.hasOwn(value, "programs") &&
|
|
118
|
+
!Object.hasOwn(value, "entry");
|
|
119
|
+
const parsed = (
|
|
120
|
+
multiplePrograms ? programsConfigSchema : entryConfigSchema
|
|
121
|
+
).safeParse(value);
|
|
122
|
+
if (!parsed.success) {
|
|
123
|
+
return {
|
|
124
|
+
diagnostics: parsed.error.issues.map((issue) => ({
|
|
125
|
+
origin: "config",
|
|
126
|
+
code: "PLC5001",
|
|
127
|
+
severity: "error",
|
|
128
|
+
message: `${issue.path.join(".") || "config"}: ${issue.message}`,
|
|
129
|
+
})),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
119
132
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
133
|
+
const { data } = parsed;
|
|
134
|
+
const programs = "entry" in data ? { main: data.entry } : data.programs;
|
|
135
|
+
const diagnostics: Diagnostic[] = [];
|
|
136
|
+
const report = (message: string): void => {
|
|
137
|
+
diagnostics.push({
|
|
138
|
+
origin: "config",
|
|
139
|
+
code: "PLC5002",
|
|
140
|
+
severity: "error",
|
|
141
|
+
message,
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
if (Object.keys(programs).length === 0) {
|
|
145
|
+
report("At least one program is required.");
|
|
146
|
+
}
|
|
129
147
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
148
|
+
// A program has one scan owner. Preserve task call order while rejecting
|
|
149
|
+
// unknown, duplicated, or unassigned program keys.
|
|
150
|
+
const taskNames = new Set<string>();
|
|
151
|
+
const assigned = new Set<string>();
|
|
152
|
+
for (const task of data.tasks) {
|
|
153
|
+
if (taskNames.has(task.name)) {
|
|
154
|
+
report(`Duplicate task name: ${task.name}.`);
|
|
155
|
+
}
|
|
156
|
+
taskNames.add(task.name);
|
|
157
|
+
for (const program of task.programs) {
|
|
158
|
+
if (!Object.hasOwn(programs, program)) {
|
|
159
|
+
report(`Task ${task.name} references unknown program ${program}.`);
|
|
160
|
+
}
|
|
161
|
+
if (assigned.has(program)) {
|
|
162
|
+
report(`Program ${program} must belong to exactly one task.`);
|
|
163
|
+
}
|
|
164
|
+
assigned.add(program);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
for (const program of Object.keys(programs)) {
|
|
168
|
+
if (!assigned.has(program)) {
|
|
169
|
+
report(`Program ${program} is not assigned to a task.`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
154
172
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
173
|
+
if (diagnostics.length) {
|
|
174
|
+
return { diagnostics };
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
config: {
|
|
178
|
+
name: data.name,
|
|
179
|
+
tsconfig: data.tsconfig,
|
|
180
|
+
programs,
|
|
181
|
+
tasks: data.tasks,
|
|
182
|
+
target: data.target,
|
|
183
|
+
},
|
|
184
|
+
diagnostics,
|
|
185
|
+
};
|
|
168
186
|
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ResolvedTsplcConfig } from "./schema.ts";
|
|
3
|
+
|
|
4
|
+
/** Environment variables by name, as `process.env` holds them. */
|
|
5
|
+
export type Environment = Readonly<Record<string, string | undefined>>;
|
|
6
|
+
|
|
7
|
+
/** The longest secret value the PLC holds. */
|
|
8
|
+
const secretLength = 512;
|
|
9
|
+
|
|
10
|
+
/** A secret's value, which the PLC sends as a bearer token: no line break or NUL. */
|
|
11
|
+
export const secretValue = z
|
|
12
|
+
.string()
|
|
13
|
+
.nonempty("is empty")
|
|
14
|
+
.max(secretLength, `is longer than ${secretLength} characters`)
|
|
15
|
+
.regex(/^[^\r\n\0]*$/, "holds a line break or NUL");
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* What a release for the plant agent carries in its ST instead of secret `name`; the agent puts
|
|
19
|
+
* the value in its place. It is as long as the longest value, so whatever holds it fits the value.
|
|
20
|
+
*/
|
|
21
|
+
export const secretPlaceholder = (name: string) =>
|
|
22
|
+
`{{tsplc-secret:${name}:`.padEnd(secretLength - 2, "-") + "}}";
|
|
23
|
+
|
|
24
|
+
export interface EnvironmentOptions {
|
|
25
|
+
env: Environment;
|
|
26
|
+
/** The config's target to take values from; with one target configured, that one. */
|
|
27
|
+
target: string | undefined;
|
|
28
|
+
/** A release for the plant agent: it gets placeholders for the secrets, and several targets need a choice. */
|
|
29
|
+
forAgent: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const targetValues = (
|
|
33
|
+
config: ResolvedTsplcConfig,
|
|
34
|
+
{ target, forAgent }: EnvironmentOptions,
|
|
35
|
+
) => {
|
|
36
|
+
const names = Object.keys(config.targets);
|
|
37
|
+
if (target !== undefined) {
|
|
38
|
+
const selected = config.targets[target];
|
|
39
|
+
if (!selected) {
|
|
40
|
+
throw new Error(
|
|
41
|
+
`The config has no target ${target}; its targets are: ${names.join(", ") || "none"}.`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return selected.values;
|
|
45
|
+
}
|
|
46
|
+
if (forAgent && names.length > 1) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
`Choose the target to deploy to with --target: ${names.join(", ")}.`,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
const [only, ...others] = Object.values(config.targets);
|
|
52
|
+
return only && !others.length ? only.values : {};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The environment the build reads the config's variables from, such as `server.baseUrlEnv`, the
|
|
57
|
+
* HTTP credentials and the program's `env()` calls: `env`, with the target's values over it.
|
|
58
|
+
*/
|
|
59
|
+
export const projectEnvironment = (
|
|
60
|
+
config: ResolvedTsplcConfig,
|
|
61
|
+
options: EnvironmentOptions,
|
|
62
|
+
): Environment => ({
|
|
63
|
+
...options.env,
|
|
64
|
+
...targetValues(config, options),
|
|
65
|
+
...(options.forAgent &&
|
|
66
|
+
Object.fromEntries(
|
|
67
|
+
(config.secrets?.names ?? []).map((name) => [
|
|
68
|
+
name,
|
|
69
|
+
secretPlaceholder(name),
|
|
70
|
+
]),
|
|
71
|
+
)),
|
|
72
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,53 @@
|
|
|
1
|
-
export
|
|
1
|
+
export {
|
|
2
|
+
ns,
|
|
3
|
+
ms,
|
|
4
|
+
seconds,
|
|
5
|
+
definePlcConfig,
|
|
6
|
+
validateConfig,
|
|
7
|
+
type Duration,
|
|
8
|
+
type NormalizedConfig,
|
|
9
|
+
type PlcConfig,
|
|
10
|
+
type SourceExport,
|
|
11
|
+
type TargetDescriptor,
|
|
12
|
+
type TaskConfig,
|
|
13
|
+
} from "./compiler.ts";
|
|
14
|
+
export type {
|
|
15
|
+
CodesysBuildConfig,
|
|
16
|
+
Controller,
|
|
17
|
+
DeviceTree,
|
|
18
|
+
} from "./build-schema.ts";
|
|
19
|
+
export {
|
|
20
|
+
httpOptionsSchema,
|
|
21
|
+
telemetrySchema,
|
|
22
|
+
typegenSchema,
|
|
23
|
+
buildTargetSchema,
|
|
24
|
+
publicationSchema,
|
|
25
|
+
hmiSchema,
|
|
26
|
+
tsplcConfigSchema,
|
|
27
|
+
buildTargetFor,
|
|
28
|
+
controllerFor,
|
|
29
|
+
serverTokenEnv,
|
|
30
|
+
simulationConfig,
|
|
31
|
+
defineConfig,
|
|
32
|
+
rawBuildConfig,
|
|
33
|
+
compilerConfig,
|
|
34
|
+
type HttpOptions,
|
|
35
|
+
type BuildTarget,
|
|
36
|
+
type Publication,
|
|
37
|
+
type HmiConfig,
|
|
38
|
+
type ResolvedHmiConfig,
|
|
39
|
+
type TsplcConfig,
|
|
40
|
+
type ResolvedTsplcConfig,
|
|
41
|
+
type HardwareDevice,
|
|
42
|
+
type ResolvedTelemetryConfig,
|
|
43
|
+
type ResolvedDebugConfig,
|
|
44
|
+
type LibraryDependency,
|
|
45
|
+
} from "./schema.ts";
|
|
46
|
+
export {
|
|
47
|
+
type Environment,
|
|
48
|
+
type EnvironmentOptions,
|
|
49
|
+
projectEnvironment,
|
|
50
|
+
secretPlaceholder,
|
|
51
|
+
secretValue,
|
|
52
|
+
} from "./environment.ts";
|
|
2
53
|
export { loadTsplcConfig } from "./loader.ts";
|
package/src/loader.ts
CHANGED
|
@@ -1,20 +1,38 @@
|
|
|
1
1
|
import { readFile } from "node:fs/promises";
|
|
2
2
|
import { dirname, resolve } from "node:path";
|
|
3
3
|
import { createJiti } from "jiti";
|
|
4
|
+
import { type EnvironmentOptions, projectEnvironment } from "./environment.ts";
|
|
4
5
|
import { tsplcConfigSchema } from "./schema.ts";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
|
|
7
|
+
/** The config, and the environment its variables resolve in: `process.env` and the only target, unless `environment` says otherwise. */
|
|
8
|
+
export async function loadTsplcConfig(
|
|
9
|
+
file = "tsplc.config.ts",
|
|
10
|
+
environment: EnvironmentOptions = {
|
|
11
|
+
env: process.env,
|
|
12
|
+
target: undefined,
|
|
13
|
+
forAgent: false,
|
|
14
|
+
},
|
|
15
|
+
) {
|
|
16
|
+
const configFile = resolve(file);
|
|
17
|
+
await readFile(configFile);
|
|
18
|
+
const jiti = createJiti(import.meta.url, {
|
|
19
|
+
moduleCache: false,
|
|
20
|
+
fsCache: false,
|
|
21
|
+
interopDefault: false,
|
|
22
|
+
// Bun can import TS natively, but its module cache would hide config edits.
|
|
23
|
+
tryNative: false,
|
|
24
|
+
});
|
|
25
|
+
const module = await jiti.import<{ default?: unknown; config?: unknown }>(
|
|
26
|
+
configFile,
|
|
27
|
+
);
|
|
28
|
+
const config = tsplcConfigSchema.parse(module.default ?? module.config);
|
|
29
|
+
if (config.compiler && !config.tasks) {
|
|
30
|
+
throw new Error("TypeScript programs require a tasks configuration.");
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
configFile,
|
|
34
|
+
root: dirname(configFile),
|
|
35
|
+
config,
|
|
36
|
+
env: projectEnvironment(config, environment),
|
|
37
|
+
};
|
|
20
38
|
}
|