|
1 | | -export default ` |
2 | | -class Variable { |
| 1 | +export function getProceedLanguageExtension( |
| 2 | + variables: { name: string; type: string }[], |
| 3 | + writeVarsIntoGlobalScope = false, |
| 4 | +) { |
| 5 | + const hasVariables = variables.length; |
| 6 | + |
| 7 | + const varNames = hasVariables |
| 8 | + ? variables.map((variable) => `'${variable.name}'`).join(' | ') |
| 9 | + : 'never'; |
| 10 | + |
| 11 | + const withoutLogs = `{ |
| 12 | + ${variables.map((variable) => `'${variable.name}'?: ${variable.type},`).join('\n ')} |
| 13 | + }`; |
| 14 | + |
| 15 | + const withLogs = `{ |
| 16 | + ${variables.map((variable) => `'${variable.name}'?: { value: ${variable.type}, log: { changedTime: number; changedBy?: string; oldValue?: ${variable.type}; }[] },`).join('\n ')} |
| 17 | + }`; |
| 18 | + |
| 19 | + return ` |
| 20 | + type __varNameTypeMap = { |
| 21 | + ${variables.map((variable) => `'${variable.name}': ${variable.type},`).join('\n ')} |
| 22 | + }; |
| 23 | +
|
| 24 | +type Variable = { |
3 | 25 | /** |
4 | | - * Allows setting values for instance variables |
| 26 | + * ${hasVariables ? 'Allows setting values for instance variables' : 'Define a variable to be able to use this function'} |
5 | 27 | **/ |
6 | | - set(varName: string, value: any): void; |
| 28 | + set: ${hasVariables ? `<VariableName extends ${varNames}>(varName: VariableName, value: __varNameTypeMap[VariableName]) => void;` : 'never;'} |
| 29 | + // in theory it is possible to call set on a variable that is not predefined |
| 30 | + // but we prefer users to predefine all variables so we disable this type definition |
| 31 | + // set(varName: string, value: any): void; |
7 | 32 |
|
8 | 33 | /** |
9 | | - * Returns value of an instance variable |
| 34 | + * ${hasVariables ? 'Returns value of an instance variable' : 'Define a variable to be able to use this function'} |
10 | 35 | **/ |
11 | | - get(varName: string): any; |
| 36 | + get: ${hasVariables ? `<VariableName extends ${varNames}>(varName: VariableName) => __varNameTypeMap[VariableName] | undefined;` : 'never;'} |
| 37 | + // in theory it is possible to call get on a variable that is not predefined |
| 38 | + // but we prefer users to predefine all variables so we disable this type definition |
| 39 | + // get(varName: string): any; |
12 | 40 |
|
13 | 41 | /** |
14 | 42 | * Returns all instance variables and their values |
15 | 43 | **/ |
16 | | - getAll(): object; |
| 44 | + getAll: () => ${withoutLogs}; |
17 | 45 |
|
18 | | - // TODO: jsdoc for these |
19 | | - getWithLogs(): any; |
| 46 | + /** |
| 47 | + * Returns all instance variables with their values and information about change events for each variable |
| 48 | + **/ |
| 49 | + getWithLogs: () => ${withLogs}; |
20 | 50 |
|
21 | | - getProcess(name: string): any; |
| 51 | + /** |
| 52 | + * ${hasVariables ? 'Returns the value of an instance variable ignoring changes made earlier in this script' : 'Define a variable to be able to use this function'} |
| 53 | + **/ |
| 54 | + getProcess: ${hasVariables ? `<VariableName extends ${varNames}>(name: VariableName) => __varNameTypeMap[VariableName] | undefined;` : 'never;'} |
22 | 55 |
|
23 | | - getAllProcess(): any; |
| 56 | + /** |
| 57 | + * Returns all instance variables and their values ignoring changes made earlier in this script |
| 58 | + **/ |
| 59 | + getAllProcess: () => ${withoutLogs}; |
24 | 60 |
|
25 | | - getWithLogsProcess(): any; |
| 61 | + /** |
| 62 | + * Returns all instance variables with their values and information about change events for each variable ignoring changes made earlier in this script |
| 63 | + **/ |
| 64 | + getWithLogsProcess: () => ${withLogs}; |
26 | 65 |
|
27 | | - setProcess(name: string, value: any); |
| 66 | + /** |
| 67 | + * ${hasVariables ? 'Allows setting values for instance variables directly in the instance state' : 'Define a variable to be able to use this function'} |
| 68 | + **/ |
| 69 | + setProcess: ${hasVariables ? `<VariableName extends ${varNames}>(name: VariableName, value: __varNameTypeMap[VariableName]) => void;` : 'never;'} |
28 | 70 |
|
29 | | - setGlobal(varPath: string, value: any): void; |
| 71 | + setGlobal: (varPath: string, value: any) => void; |
30 | 72 |
|
31 | | - getGlobal(varPath: string); |
| 73 | + getGlobal: (varPath: string) => any; |
32 | 74 |
|
33 | | - getGlobalFull(varPath: string); |
| 75 | + getGlobalFull: (varPath: string) => any; |
34 | 76 |
|
35 | | - setGlobalOrg(varPath: string, value: any): void; |
| 77 | + setGlobalOrg: (varPath: string, value: any) => void; |
36 | 78 |
|
37 | | - getGlobalOrg(varPath: string); |
| 79 | + getGlobalOrg: (varPath: string) => any; |
38 | 80 |
|
39 | | - getGlobalOrgFull(varPath: string); |
| 81 | + getGlobalOrgFull: (varPath: string) => any; |
40 | 82 | } |
41 | 83 | declare var variable: Variable; |
42 | 84 |
|
| 85 | +${writeVarsIntoGlobalScope ? variables.map((variable) => `declare var ${variable.name}: undefined | ${variable.type};`).join('\n') : ''} |
| 86 | +
|
43 | 87 | declare class Log { |
44 | 88 | trace(message?: any): void; |
45 | 89 | debug(message?: any): void; |
@@ -75,7 +119,7 @@ declare function setInterval(callback: () => any, timeout: number): number; |
75 | 119 | /** |
76 | 120 | * Cancel an interval |
77 | 121 | **/ |
78 | | -declare function clearInterval(intervalId): boolean; |
| 122 | +declare function clearInterval(intervalId: number): boolean; |
79 | 123 |
|
80 | 124 | // declare class CapabilityService { |
81 | 125 | // /** |
@@ -233,7 +277,7 @@ type _Request = { |
233 | 277 | // files?: { name: string; data: any }; |
234 | 278 | }; |
235 | 279 |
|
236 | | -class _Response { |
| 280 | +type _Response = { |
237 | 281 | status(code: number): _Response; |
238 | 282 | send(body: any): void; |
239 | 283 | } |
@@ -264,16 +308,16 @@ declare class NetworkServer { |
264 | 308 | getAsync(path: string): Promise<{ req: _Request; res: _Response }>; |
265 | 309 |
|
266 | 310 | /** Open a POST route listener */ |
267 | | - post(path: string, callback: ({ req: _Request, res: _Response }) => void): void; |
| 311 | + post(path: string, callback: ({ req, res }: { req: _Request, res: _Response }) => void): void; |
268 | 312 |
|
269 | 313 | /** Open a PUT route listener */ |
270 | | - put(path: string, callback: ({ req: _Request, res: _Response }) => void): void; |
| 314 | + put(path: string, callback: ({ req, res }: { req: _Request, res: _Response }) => void): void; |
271 | 315 |
|
272 | 316 | /** Open a DELETE route listener */ |
273 | | - delete(path: string, callback: ({ req: _Request, res: _Response }) => void): void; |
| 317 | + delete(path: string, callback: ({ req, res }: { req: _Request, res: _Response }) => void): void; |
274 | 318 |
|
275 | 319 | /** Open a GET route listener */ |
276 | | - get(path: string, callback: ({ req: _Request, res: _Response }) => void): void; |
| 320 | + get(path: string, callback: ({ req, res }: { req: _Request, res: _Response }) => void): void; |
277 | 321 |
|
278 | 322 | /** |
279 | 323 | * Close the network server. This is necessary for the task script to stop, after a route handler |
@@ -303,3 +347,4 @@ declare function wait(ms: number): void; |
303 | 347 | /** Returns a promise that is resolved after the given amount of milliseconds */ |
304 | 348 | declare function waitAsync(ms: number): Promise<void>; |
305 | 349 | `; |
| 350 | +} |
0 commit comments