@tsplc/compiler 0.2.17 → 0.2.19
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/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AgentOperationError,
|
|
3
|
+
type AgentClient,
|
|
4
|
+
createAgentClient,
|
|
5
|
+
} from "@tsplc/agent/client";
|
|
6
|
+
import {
|
|
7
|
+
ask,
|
|
8
|
+
askCredentials,
|
|
9
|
+
type Credentials,
|
|
10
|
+
type Target,
|
|
11
|
+
type Variable,
|
|
12
|
+
} from "@tsplc/agent";
|
|
13
|
+
import type { LoginCommand, LoginResult } from "@tsplc/build";
|
|
14
|
+
import type { Environment, loadTsplcConfig } from "@tsplc/config";
|
|
15
|
+
import { LoginUnavailable, noLogin } from "@tsplc/debug";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { prepareRelease } from "./release.ts";
|
|
18
|
+
import { choose, confirm, progress, timedProgress } from "./terminal.ts";
|
|
19
|
+
|
|
20
|
+
const plantCommands = [
|
|
21
|
+
"deploy",
|
|
22
|
+
"target",
|
|
23
|
+
"status",
|
|
24
|
+
"login",
|
|
25
|
+
"logout",
|
|
26
|
+
"vars",
|
|
27
|
+
"read",
|
|
28
|
+
"write",
|
|
29
|
+
"force",
|
|
30
|
+
"unforce",
|
|
31
|
+
"start",
|
|
32
|
+
"stop",
|
|
33
|
+
"reset",
|
|
34
|
+
] as const;
|
|
35
|
+
|
|
36
|
+
export type PlantCommand = (typeof plantCommands)[number];
|
|
37
|
+
|
|
38
|
+
export const plantCommand = (name: string) =>
|
|
39
|
+
plantCommands.find((command) => command === name);
|
|
40
|
+
|
|
41
|
+
export interface PlantFlags {
|
|
42
|
+
download?: boolean;
|
|
43
|
+
"initialize-user"?: boolean;
|
|
44
|
+
restore?: boolean;
|
|
45
|
+
select?: boolean;
|
|
46
|
+
simulate?: boolean;
|
|
47
|
+
yes?: boolean;
|
|
48
|
+
"frozen-lockfile"?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type LoadedConfig = Awaited<ReturnType<typeof loadTsplcConfig>>;
|
|
52
|
+
|
|
53
|
+
/** The plant agent named by TSPLC_AGENT_URL and TSPLC_AGENT_TOKEN in `env`. */
|
|
54
|
+
export const connect = (env: Record<string, string | undefined>) => {
|
|
55
|
+
const environment = z
|
|
56
|
+
.object({
|
|
57
|
+
TSPLC_AGENT_URL: z.url({
|
|
58
|
+
error:
|
|
59
|
+
"Set TSPLC_AGENT_URL to the plant agent, such as http://line-pc:4850",
|
|
60
|
+
}),
|
|
61
|
+
TSPLC_AGENT_TOKEN: z.string({
|
|
62
|
+
error: "Set TSPLC_AGENT_TOKEN to the plant agent's token",
|
|
63
|
+
}),
|
|
64
|
+
})
|
|
65
|
+
.parse(env);
|
|
66
|
+
return createAgentClient({
|
|
67
|
+
url: environment.TSPLC_AGENT_URL,
|
|
68
|
+
token: environment.TSPLC_AGENT_TOKEN,
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Reads through the agent's open login, as `tsplc read` does, without opening one: the
|
|
74
|
+
* debugger has no controller credentials.
|
|
75
|
+
*/
|
|
76
|
+
export const readThroughAgent =
|
|
77
|
+
(agent: AgentClient) =>
|
|
78
|
+
async ([first, ...rest]: string[]) => {
|
|
79
|
+
if (!first) {
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const result = await agent.command({
|
|
84
|
+
command: "read",
|
|
85
|
+
expressions: [first, ...rest],
|
|
86
|
+
});
|
|
87
|
+
if (!("values" in result)) {
|
|
88
|
+
throw new Error("The agent answered a read without values");
|
|
89
|
+
}
|
|
90
|
+
return result.values;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
const { login } = await agent.status();
|
|
93
|
+
if (!login) {
|
|
94
|
+
throw new LoginUnavailable(noLogin, { cause: error });
|
|
95
|
+
}
|
|
96
|
+
if ("error" in login) {
|
|
97
|
+
throw new Error(login.error, { cause: error });
|
|
98
|
+
}
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const selectTarget = async (agent: AgentClient): Promise<Target> => {
|
|
104
|
+
const interfaces = await agent.interfaces();
|
|
105
|
+
if (!interfaces.length) {
|
|
106
|
+
throw new Error("The plant PC has no IPv4 network.");
|
|
107
|
+
}
|
|
108
|
+
const network =
|
|
109
|
+
interfaces.length === 1 && interfaces[0]
|
|
110
|
+
? interfaces[0]
|
|
111
|
+
: await choose(
|
|
112
|
+
"Which network is the controller on?",
|
|
113
|
+
interfaces,
|
|
114
|
+
(item) => `${item.name} ${item.address}/${item.prefixLength}`,
|
|
115
|
+
);
|
|
116
|
+
console.log(
|
|
117
|
+
`Scanning ${network.address}/${network.prefixLength} for CODESYS controllers…`,
|
|
118
|
+
);
|
|
119
|
+
const found = await agent.discover(network.name);
|
|
120
|
+
const manual = { address: "", port: 11740, hostname: null };
|
|
121
|
+
const choice = await choose(
|
|
122
|
+
"Which controller?",
|
|
123
|
+
[...found, manual],
|
|
124
|
+
(item) =>
|
|
125
|
+
item === manual
|
|
126
|
+
? "Enter an address"
|
|
127
|
+
: `${item.address}${item.hostname ? ` (${item.hostname})` : ""}`,
|
|
128
|
+
);
|
|
129
|
+
const host =
|
|
130
|
+
choice === manual ? await ask("Controller address: ") : choice.address;
|
|
131
|
+
return agent.setTarget({ host, port: choice.port });
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The controller user to send: TSPLC_CONTROLLER_USERNAME and TSPLC_CONTROLLER_PASSWORD from
|
|
136
|
+
* `env`, else null for the user stored on the agent, else what `prompt` asks for. Initializing
|
|
137
|
+
* the controller's user creates the one sent, so it never leaves it to the stored one.
|
|
138
|
+
*/
|
|
139
|
+
export const controllerCredentials = async (
|
|
140
|
+
env: Environment,
|
|
141
|
+
agent: { stored: boolean; initializeUser: boolean },
|
|
142
|
+
prompt: () => Promise<Credentials>,
|
|
143
|
+
): Promise<Credentials | null> => {
|
|
144
|
+
const username = env["TSPLC_CONTROLLER_USERNAME"];
|
|
145
|
+
const password = env["TSPLC_CONTROLLER_PASSWORD"];
|
|
146
|
+
if (username && password) {
|
|
147
|
+
return { username, password };
|
|
148
|
+
}
|
|
149
|
+
if (username || password) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
"Set both TSPLC_CONTROLLER_USERNAME and TSPLC_CONTROLLER_PASSWORD, or neither.",
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return agent.stored && !agent.initializeUser ? null : prompt();
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const deploy = async (
|
|
158
|
+
agent: AgentClient,
|
|
159
|
+
file: string,
|
|
160
|
+
loaded: LoadedConfig,
|
|
161
|
+
flags: PlantFlags,
|
|
162
|
+
env: Environment,
|
|
163
|
+
) => {
|
|
164
|
+
const status = await agent.status();
|
|
165
|
+
if (status.login) {
|
|
166
|
+
throw new Error("A login is open; log out before deploying: tsplc logout");
|
|
167
|
+
}
|
|
168
|
+
const target =
|
|
169
|
+
!status.target || flags.select ? await selectTarget(agent) : status.target;
|
|
170
|
+
console.log(`Building the release for ${target.host}…`);
|
|
171
|
+
const building = timedProgress();
|
|
172
|
+
const { release, diagnostics } = await prepareRelease(file, loaded, flags);
|
|
173
|
+
if (!release) {
|
|
174
|
+
return { diagnostics };
|
|
175
|
+
}
|
|
176
|
+
building("Built");
|
|
177
|
+
if (
|
|
178
|
+
flags.download &&
|
|
179
|
+
!(await confirm(
|
|
180
|
+
"A full download stops the running application while it loads. Continue?",
|
|
181
|
+
flags,
|
|
182
|
+
))
|
|
183
|
+
) {
|
|
184
|
+
throw new Error("Cancelled.");
|
|
185
|
+
}
|
|
186
|
+
const credentials = await controllerCredentials(
|
|
187
|
+
env,
|
|
188
|
+
{
|
|
189
|
+
stored: status.credentialsStored,
|
|
190
|
+
initializeUser: flags["initialize-user"] ?? false,
|
|
191
|
+
},
|
|
192
|
+
askCredentials,
|
|
193
|
+
);
|
|
194
|
+
const settings = {
|
|
195
|
+
credentials,
|
|
196
|
+
download: flags.download ?? false,
|
|
197
|
+
initializeUser: flags["initialize-user"] ?? false,
|
|
198
|
+
};
|
|
199
|
+
console.log(
|
|
200
|
+
release.manifest.server
|
|
201
|
+
? "Deploying the server and the program…"
|
|
202
|
+
: "Deploying the program…",
|
|
203
|
+
);
|
|
204
|
+
const deploying = timedProgress();
|
|
205
|
+
deploying("Uploading");
|
|
206
|
+
try {
|
|
207
|
+
return await agent.deploy(release, settings, deploying);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
if (
|
|
210
|
+
!(error instanceof AgentOperationError && error.downloadRequired) ||
|
|
211
|
+
!(await confirm(
|
|
212
|
+
`${error.message}. A full download stops the running application while it loads. Download now?`,
|
|
213
|
+
flags,
|
|
214
|
+
))
|
|
215
|
+
) {
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
return agent.deploy(
|
|
219
|
+
release,
|
|
220
|
+
{ ...settings, download: true },
|
|
221
|
+
timedProgress(),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/** Opens a login when none is working; it stays open across commands until `tsplc logout`. */
|
|
227
|
+
const ensureLogin = async (agent: AgentClient, env: Environment) => {
|
|
228
|
+
const { login, credentialsStored } = await agent.status();
|
|
229
|
+
if (login && "state" in login) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (login) {
|
|
233
|
+
console.log(`The open login failed (${login.error}); replacing it.`);
|
|
234
|
+
await agent.logout();
|
|
235
|
+
}
|
|
236
|
+
const credentials = await controllerCredentials(
|
|
237
|
+
env,
|
|
238
|
+
{ stored: credentialsStored, initializeUser: false },
|
|
239
|
+
askCredentials,
|
|
240
|
+
);
|
|
241
|
+
console.log(
|
|
242
|
+
"Logging in. CODESYS takes a few minutes to start; the login stays open until tsplc logout.",
|
|
243
|
+
);
|
|
244
|
+
await agent.login(credentials, progress);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/** The children of `prefix` in the variable tree, with array indexes folded into one range. */
|
|
248
|
+
export const variableChildren = (
|
|
249
|
+
variables: readonly Variable[],
|
|
250
|
+
prefix: string,
|
|
251
|
+
) => {
|
|
252
|
+
const children = new Map<string, { type?: string; leaves: number }>();
|
|
253
|
+
const start = prefix.toLowerCase();
|
|
254
|
+
for (const { path, type } of variables) {
|
|
255
|
+
const lower = path.toLowerCase();
|
|
256
|
+
if (
|
|
257
|
+
prefix &&
|
|
258
|
+
!(
|
|
259
|
+
lower === start ||
|
|
260
|
+
lower.startsWith(`${start}.`) ||
|
|
261
|
+
lower.startsWith(`${start}[`)
|
|
262
|
+
)
|
|
263
|
+
) {
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const rest = path.slice(prefix.length).replace(/^\./, "");
|
|
267
|
+
if (rest === "") {
|
|
268
|
+
return [{ name: path, type, leaves: 1 }];
|
|
269
|
+
}
|
|
270
|
+
const name = /^(\[[^\]]*\]|[^.[]+)/.exec(rest)?.[0] ?? rest;
|
|
271
|
+
const child = children.get(name) ?? { leaves: 0 };
|
|
272
|
+
children.set(name, {
|
|
273
|
+
type: rest === name ? type : undefined,
|
|
274
|
+
leaves: child.leaves + 1,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
const indexes = [...children.keys()]
|
|
278
|
+
.filter((name) => /^\[\d+\]$/.test(name))
|
|
279
|
+
.map((name) => Number(name.slice(1, -1)));
|
|
280
|
+
const listed = [...children].filter(([name]) => !/^\[\d+\]$/.test(name));
|
|
281
|
+
if (indexes.length) {
|
|
282
|
+
const first = children.get(`[${Math.min(...indexes)}]`);
|
|
283
|
+
listed.unshift([
|
|
284
|
+
`[${Math.min(...indexes)}..${Math.max(...indexes)}]`,
|
|
285
|
+
first ?? { leaves: 0 },
|
|
286
|
+
]);
|
|
287
|
+
}
|
|
288
|
+
return listed.map(([name, child]) => ({ name, ...child }));
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const printStatus = async (agent: AgentClient) => {
|
|
292
|
+
const status = await agent.status();
|
|
293
|
+
console.log(
|
|
294
|
+
`Controller ${status.target ? `${status.target.host}:${status.target.port}` : "not selected (tsplc target)"}`,
|
|
295
|
+
);
|
|
296
|
+
console.log(
|
|
297
|
+
`Release ${status.release ? `${status.release.id}, ${status.release.operation} at ${status.release.deployedAt}` : "none"}`,
|
|
298
|
+
);
|
|
299
|
+
console.log(
|
|
300
|
+
`Server ${status.server ? `${status.server.running ? "running" : "restarting"} (${status.server.executable})` : "none"}`,
|
|
301
|
+
);
|
|
302
|
+
const { login } = status;
|
|
303
|
+
if (!login) {
|
|
304
|
+
console.log("Login closed");
|
|
305
|
+
} else if ("error" in login) {
|
|
306
|
+
console.log(`Login failed: ${login.error} (tsplc logout)`);
|
|
307
|
+
} else {
|
|
308
|
+
console.log(
|
|
309
|
+
`Login open; application ${login.state}${login.forced.length ? `; forced: ${login.forced.join(", ")}` : ""}`,
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
/** Lists the deployed release's published variables under `path`, or its roots. */
|
|
315
|
+
const printVariables = async (agent: AgentClient, path: string | undefined) => {
|
|
316
|
+
const children = variableChildren(await agent.variables(), path ?? "");
|
|
317
|
+
if (!children.length) {
|
|
318
|
+
throw new Error(`No published variable ${path}.`);
|
|
319
|
+
}
|
|
320
|
+
for (const child of children) {
|
|
321
|
+
console.log(
|
|
322
|
+
`${child.name.padEnd(32)} ${child.type ?? `${child.leaves} variables`}`,
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const printLogin = (result: LoginResult) => {
|
|
328
|
+
if ("values" in result) {
|
|
329
|
+
for (const [expression, value] of Object.entries(result.values)) {
|
|
330
|
+
console.log(`${expression} = ${value}`);
|
|
331
|
+
}
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
console.log(`Application ${result.state}`);
|
|
335
|
+
if (result.forced.length) {
|
|
336
|
+
console.log(`Forced: ${result.forced.join(", ")}`);
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Runs a command against the plant agent. Returns a deployment's compiler diagnostics when the
|
|
342
|
+
* program doesn't compile.
|
|
343
|
+
*/
|
|
344
|
+
export const runPlantCommand = async (
|
|
345
|
+
agent: AgentClient,
|
|
346
|
+
command: PlantCommand,
|
|
347
|
+
args: string[],
|
|
348
|
+
flags: PlantFlags,
|
|
349
|
+
project: { file: string; load: () => Promise<LoadedConfig> },
|
|
350
|
+
env: Environment,
|
|
351
|
+
) => {
|
|
352
|
+
const expect = (count: number, usage: string) => {
|
|
353
|
+
if (args.length !== count) {
|
|
354
|
+
throw new Error(`Usage: tsplc ${command} ${usage}`);
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
const send = async (loginCommand: LoginCommand) => {
|
|
358
|
+
await ensureLogin(agent, env);
|
|
359
|
+
printLogin(await agent.command(loginCommand));
|
|
360
|
+
};
|
|
361
|
+
switch (command) {
|
|
362
|
+
case "deploy": {
|
|
363
|
+
expect(
|
|
364
|
+
0,
|
|
365
|
+
"[--target name] [--download] [--select] [--initialize-user] [--simulate]",
|
|
366
|
+
);
|
|
367
|
+
const deployed = await deploy(
|
|
368
|
+
agent,
|
|
369
|
+
project.file,
|
|
370
|
+
await project.load(),
|
|
371
|
+
flags,
|
|
372
|
+
env,
|
|
373
|
+
);
|
|
374
|
+
if ("diagnostics" in deployed) {
|
|
375
|
+
return deployed;
|
|
376
|
+
}
|
|
377
|
+
console.log(`Deployed release ${deployed.id} (${deployed.operation}).`);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
case "target": {
|
|
381
|
+
expect(0, "");
|
|
382
|
+
const target = await selectTarget(agent);
|
|
383
|
+
console.log(`Deployments now go to ${target.host}:${target.port}.`);
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
case "status":
|
|
387
|
+
expect(0, "");
|
|
388
|
+
return printStatus(agent);
|
|
389
|
+
case "login":
|
|
390
|
+
expect(0, "");
|
|
391
|
+
await ensureLogin(agent, env);
|
|
392
|
+
printLogin(await agent.command({ command: "state" }));
|
|
393
|
+
return;
|
|
394
|
+
case "logout": {
|
|
395
|
+
expect(0, "");
|
|
396
|
+
const { login } = await agent.status();
|
|
397
|
+
const forced = login && "forced" in login ? login.forced : [];
|
|
398
|
+
if (
|
|
399
|
+
forced.length &&
|
|
400
|
+
!(await confirm(
|
|
401
|
+
`Forced: ${forced.join(", ")}. Forces stay on the controller after logout. Log out anyway?`,
|
|
402
|
+
flags,
|
|
403
|
+
))
|
|
404
|
+
) {
|
|
405
|
+
throw new Error("Cancelled.");
|
|
406
|
+
}
|
|
407
|
+
const { failure } = await agent.logout();
|
|
408
|
+
console.log(
|
|
409
|
+
failure
|
|
410
|
+
? `The login couldn't log out (${failure}); its container was removed.`
|
|
411
|
+
: "Logged out.",
|
|
412
|
+
);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
case "vars":
|
|
416
|
+
if (args.length > 1) {
|
|
417
|
+
throw new Error("Usage: tsplc vars [path]");
|
|
418
|
+
}
|
|
419
|
+
return printVariables(agent, args[0]);
|
|
420
|
+
case "read": {
|
|
421
|
+
const [first, ...rest] = args;
|
|
422
|
+
if (!first) {
|
|
423
|
+
throw new Error("Usage: tsplc read <expression>...");
|
|
424
|
+
}
|
|
425
|
+
return send({ command: "read", expressions: [first, ...rest] });
|
|
426
|
+
}
|
|
427
|
+
case "write":
|
|
428
|
+
case "force": {
|
|
429
|
+
const [expression, value, ...extra] = args;
|
|
430
|
+
if (!expression || !value || extra.length) {
|
|
431
|
+
throw new Error(`Usage: tsplc ${command} <expression> <value>`);
|
|
432
|
+
}
|
|
433
|
+
return send({ command, values: { [expression]: value } });
|
|
434
|
+
}
|
|
435
|
+
case "unforce":
|
|
436
|
+
return send({
|
|
437
|
+
command: "unforce",
|
|
438
|
+
expressions: args,
|
|
439
|
+
restore: flags.restore ?? false,
|
|
440
|
+
});
|
|
441
|
+
case "start":
|
|
442
|
+
expect(0, "");
|
|
443
|
+
return send({ command: "start" });
|
|
444
|
+
case "stop":
|
|
445
|
+
expect(0, "");
|
|
446
|
+
if (!(await confirm("Stop the application on the controller?", flags))) {
|
|
447
|
+
throw new Error("Cancelled.");
|
|
448
|
+
}
|
|
449
|
+
return send({ command: "stop" });
|
|
450
|
+
case "reset": {
|
|
451
|
+
expect(1, "warm|cold");
|
|
452
|
+
const kind = z.enum(["warm", "cold"]).parse(args[0]);
|
|
453
|
+
if (
|
|
454
|
+
!(await confirm(
|
|
455
|
+
`Reset the application (${kind})? It stops and reinitializes its variables.`,
|
|
456
|
+
flags,
|
|
457
|
+
))
|
|
458
|
+
) {
|
|
459
|
+
throw new Error("Cancelled.");
|
|
460
|
+
}
|
|
461
|
+
return send({
|
|
462
|
+
command: "reset",
|
|
463
|
+
kind: kind === "warm" ? "Warm" : "Cold",
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
};
|