node-opcua-convert-nodeset-to-javascript 2.97.0 → 2.98.2

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.
@@ -1,754 +1,754 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.convertTypeToTypescript = exports._convertTypeToTypescript = exports.findUsedImport = exports.extractClassMemberDef = exports.checkIfShouldExposeInnerDefinition = exports.findClassMember = exports.extractClassDefinition = exports.makeTypeName2 = exports.convertDataTypeToTypescript = void 0;
13
- /* eslint-disable max-depth */
14
- const chalk = require("chalk");
15
- const node_opcua_data_model_1 = require("node-opcua-data-model");
16
- const node_opcua_types_1 = require("node-opcua-types");
17
- const node_opcua_utils_1 = require("node-opcua-utils");
18
- const node_opcua_variant_1 = require("node-opcua-variant");
19
- const node_opcua_assert_1 = require("node-opcua-assert");
20
- const node_opcua_debug_1 = require("node-opcua-debug");
21
- const utils_1 = require("./private/utils");
22
- const cache_1 = require("./private/cache");
23
- const to_filename_1 = require("./private/to_filename");
24
- const utils2_1 = require("./utils2");
25
- const _dataType_1 = require("./_dataType");
26
- const get_corresponding_data_type_1 = require("./private/get_corresponding_data_type");
27
- const warningLog = (0, node_opcua_debug_1.make_warningLog)("typescript");
28
- const doDebug = false;
29
- const baseExtension = "_Base";
30
- const m0 = !doDebug ? "" : `/* 0 */`;
31
- const m1 = !doDebug ? "" : `/* 1 */`;
32
- const m2 = !doDebug ? "" : `/* 2 */`;
33
- const m3 = !doDebug ? "" : `/* 3 */`;
34
- const m4 = !doDebug ? "" : `/* 4 */`;
35
- const m5 = !doDebug ? "" : `/* 5 */`;
36
- const m6 = !doDebug ? "" : `/* 6 */`;
37
- const m7 = !doDebug ? "" : `/* 7 */`;
38
- const m8 = !doDebug ? "" : `/* 8 */`;
39
- const m9 = !doDebug ? "" : `/* 9 */`;
40
- const mA = !doDebug ? "" : `/* a */`;
41
- const mB = !doDebug ? "" : `/* b */`;
42
- const mC = !doDebug ? "" : `/* c */`;
43
- function convertDataTypeToTypescript(session, dataTypeId) {
44
- return __awaiter(this, void 0, void 0, function* () {
45
- const definition = yield (0, utils_1.getDefinition)(session, dataTypeId);
46
- const browseName = yield (0, utils_1.getBrowseName)(session, dataTypeId);
47
- const dataTypeTypescriptName = `UA${browseName.name.toString()}`;
48
- const f = new node_opcua_utils_1.LineFile();
49
- if (definition && definition instanceof node_opcua_types_1.StructureDefinition) {
50
- f.write(`interface ${dataTypeTypescriptName} {`);
51
- for (const field of definition.fields || []) {
52
- /** */
53
- }
54
- f.write(`}`);
55
- }
56
- });
57
- }
58
- exports.convertDataTypeToTypescript = convertDataTypeToTypescript;
59
- function makeTypeName2(nodeClass, browseName, suffix) {
60
- (0, node_opcua_assert_1.default)(browseName);
61
- if (nodeClass === node_opcua_data_model_1.NodeClass.Method) {
62
- return { name: "UAMethod", namespace: 0, module: "UAMethod" };
63
- }
64
- return (0, cache_1.makeTypeNameNew)(nodeClass, null, browseName, suffix);
65
- }
66
- exports.makeTypeName2 = makeTypeName2;
67
- function extractClassDefinition(session, nodeId, cache) {
68
- return __awaiter(this, void 0, void 0, function* () {
69
- const extraImports = [];
70
- const _c = cache.classDefCache || {};
71
- cache.classDefCache = _c;
72
- let classDef = _c[nodeId.toString()];
73
- if (classDef) {
74
- return classDef;
75
- }
76
- const nodeClass = yield (0, utils_1.getNodeClass)(session, nodeId);
77
- if (nodeClass !== node_opcua_data_model_1.NodeClass.VariableType && nodeClass !== node_opcua_data_model_1.NodeClass.ObjectType) {
78
- throw new Error("Invalid nodeClass " + node_opcua_data_model_1.NodeClass[nodeClass] + " nodeId " + nodeId.toString());
79
- }
80
- const browseName = yield (0, utils_1.getBrowseName)(session, nodeId);
81
- const isAbstract = yield (0, utils_1.getIsAbstract)(session, nodeId);
82
- const superType = (yield (0, utils_1.getSubtypeNodeIdIfAny)(session, nodeId)) || undefined;
83
- const dataTypeNodeId = yield (0, utils_1.getDataTypeNodeId)(session, nodeId);
84
- let dataTypeName = "";
85
- let dataType = node_opcua_variant_1.DataType.Null;
86
- let dataTypeImport = undefined;
87
- if (nodeClass === node_opcua_data_model_1.NodeClass.VariableType) {
88
- dataType = yield (0, utils_1.extractBasicDataType)(session, dataTypeNodeId);
89
- const importCollector = (i) => {
90
- extraImports.push(i);
91
- cache.ensureImported(i);
92
- };
93
- const { jtype } = yield (0, get_corresponding_data_type_1.getCorrepondingJavascriptType2)(session, nodeId, dataTypeNodeId, cache, importCollector);
94
- dataTypeName = jtype; // with decoration
95
- if (!(dataTypeNodeId === null || dataTypeNodeId === void 0 ? void 0 : dataTypeNodeId.isEmpty())) {
96
- // "DT" + (await getBrowseName(session, dataTypeNodeId!))?.name! || "";
97
- // const bn = await getBrowseName(session, dataTypeNodeId!);
98
- dataTypeImport = extraImports; // makeTypeName2(NodeClass.DataType, bn);
99
- }
100
- }
101
- const baseClassDef = !superType ? null : yield extractClassDefinition(session, superType.nodeId, cache);
102
- const description = yield (0, utils_1.getDescription)(session, nodeId);
103
- // const definition = await getDefinition(session, nodeId);
104
- const interfaceName = makeTypeName2(nodeClass, browseName);
105
- const baseInterfaceName = !superType ? null : makeTypeName2(nodeClass, superType.browseName);
106
- // extract member
107
- const children = yield (0, utils_1.getChildrenOrFolderElements)(session, nodeId);
108
- const members = [];
109
- classDef = {
110
- nodeClass,
111
- browseName,
112
- isAbstract,
113
- dataType,
114
- dataTypeNodeId,
115
- dataTypeName,
116
- dataTypeImport,
117
- superType,
118
- baseClassDef,
119
- description,
120
- // definition,
121
- children,
122
- members,
123
- interfaceName,
124
- baseInterfaceName
125
- };
126
- _c[nodeId.toString()] = classDef;
127
- for (const child of children) {
128
- const c = yield extractClassMemberDef(session, child.nodeId, classDef, cache);
129
- classDef.members.push(c);
130
- }
131
- return classDef;
132
- });
133
- }
134
- exports.extractClassDefinition = extractClassDefinition;
135
- function classify(session, refs, classDef) {
136
- return __awaiter(this, void 0, void 0, function* () {
137
- const r = {
138
- Mandatory: [],
139
- Optional: [],
140
- MandatoryPlaceholder: [],
141
- OptionalPlaceholder: [],
142
- ExposesItsArray: []
143
- };
144
- for (const mm of refs) {
145
- let modellingRule = yield (0, utils_1.getModellingRule)(session, mm.nodeId);
146
- if (modellingRule) {
147
- // if Optional, check that base class member is also Optional or force to Mandatory
148
- if (modellingRule === "Optional" && classDef.baseClassDef) {
149
- const sameMember = classDef.baseClassDef.members.find((m) => m.browseName.name === mm.browseName.name);
150
- if (sameMember) {
151
- if (!sameMember.isOptional) {
152
- console.log(chalk.cyan("Warning: ") +
153
- chalk.yellow(classDef.browseName.toString()) +
154
- " ModellingRule is Optional but base class member is Mandatory");
155
- modellingRule = "Mandatory";
156
- }
157
- }
158
- }
159
- r[modellingRule] = r[modellingRule] || [];
160
- r[modellingRule].push(mm);
161
- }
162
- }
163
- return r;
164
- });
165
- }
166
- function extractAllMembers(session, classDef, cache) {
167
- return __awaiter(this, void 0, void 0, function* () {
168
- const m = [...classDef.children];
169
- let s = classDef;
170
- while (s.baseClassDef) {
171
- s = s.baseClassDef;
172
- // start from top most class
173
- // do not overwrite most top member definition, so we get mandatory stuff
174
- for (const mm of s.children) {
175
- const found = m.findIndex((r) => r.browseName.toString() === mm.browseName.toString());
176
- if (found < 0) {
177
- m.push(mm);
178
- }
179
- }
180
- }
181
- // now separate mandatory and optionals
182
- const members = yield classify(session, m, classDef);
183
- return members;
184
- });
185
- }
186
- function findClassMember(session, browseName, baseParentDef, cache) {
187
- return __awaiter(this, void 0, void 0, function* () {
188
- const childBrowseName = browseName.toString();
189
- const r = baseParentDef.children.find((a) => a.browseName.toString() === childBrowseName);
190
- if (r) {
191
- const d = yield extractClassMemberDef(session, r.nodeId, baseParentDef, cache);
192
- return d;
193
- }
194
- const baseBaseParentDef = baseParentDef.superType && (yield extractClassDefinition(session, baseParentDef.superType.nodeId, cache));
195
- if (!baseBaseParentDef) {
196
- return null;
197
- }
198
- return yield findClassMember(session, browseName, baseBaseParentDef, cache);
199
- });
200
- }
201
- exports.findClassMember = findClassMember;
202
- function hasNewMaterial(referenceMembers, instanceMembers) {
203
- const ref = referenceMembers.map((x) => x.browseName.toString()).sort();
204
- const instance = instanceMembers.map((x) => x.browseName.toString()).sort();
205
- for (const n of instance) {
206
- if (ref.findIndex((x) => x === n) < 0) {
207
- return true;
208
- }
209
- }
210
- return false;
211
- }
212
- // we should expose an inner definition if
213
- // - at least one mandatory in instnace doesn't exist in main.Mandatory
214
- // - at least one optional in instnace doesn't exist in main.Mandatory
215
- function checkIfShouldExposeInnerDefinition(main, instance) {
216
- const hasNewStuff = hasNewMaterial(main.Mandatory, instance.Mandatory) || hasNewMaterial(main.Optional, instance.Optional);
217
- return hasNewStuff;
218
- }
219
- exports.checkIfShouldExposeInnerDefinition = checkIfShouldExposeInnerDefinition;
220
- function dump1(a) {
221
- console.log("Mandatory = ", a.Mandatory.map((x) => x.browseName.toString())
222
- .sort()
223
- .join(" "));
224
- console.log("Optional = ", a.Optional.map((x) => x.browseName.toString())
225
- .sort()
226
- .join(" "));
227
- }
228
- function getSameInBaseClass(parent, browseName) {
229
- if (!parent || !parent.baseClassDef)
230
- return null;
231
- const originalClassMember = parent.baseClassDef.members.find((m) => m.browseName.toString() === browseName.toString());
232
- return originalClassMember || null;
233
- }
234
- function sameInBaseClassIsMandatory(parent, browseName) {
235
- const originalClassMember = getSameInBaseClass(parent, browseName);
236
- if (!originalClassMember)
237
- return false;
238
- return !originalClassMember.isOptional;
239
- }
240
- function _extractLocalMembers(session, parent, classMember, cache) {
241
- var _a;
242
- return __awaiter(this, void 0, void 0, function* () {
243
- const children2 = [];
244
- for (const child of classMember.children) {
245
- const nodeId = child.nodeId;
246
- const browseName = yield (0, utils_1.getBrowseName)(session, nodeId);
247
- const name = (0, utils2_1.toJavascritPropertyName)(browseName.name, { ignoreConflictingName: true });
248
- const description = yield (0, utils_1.getDescription)(session, nodeId);
249
- const modellingRule = yield (0, utils_1.getModellingRule)(session, nodeId);
250
- const isOptional = sameInBaseClassIsMandatory(parent, browseName) ? false : modellingRule === "Optional";
251
- const typeDefinition = yield (0, utils_1.getTypeDefOrBaseType)(session, nodeId);
252
- const childInBase = (_a = classMember.classDef) === null || _a === void 0 ? void 0 : _a.members.find((a) => a.browseName.toString() === browseName.toString());
253
- let childType;
254
- if (childInBase) {
255
- childType = childInBase.childType;
256
- }
257
- else {
258
- const d = typeDefinition.nodeId.isEmpty() ? null : yield extractClassDefinition(session, typeDefinition.nodeId, cache);
259
- childType = (d === null || d === void 0 ? void 0 : d.interfaceName) || { name: "UAMethod", module: "BasicType", namespace: -1 };
260
- }
261
- // may be childType is already
262
- const { suffix, suffixInstantiate, typeToReference, chevrons } = yield extractVariableExtra(session, child.nodeId, cache, classMember);
263
- const c = {
264
- childType,
265
- description,
266
- isOptional,
267
- modellingRule,
268
- name,
269
- suffix,
270
- suffixInstantiate,
271
- typeToReference,
272
- chevrons
273
- };
274
- children2.push(c);
275
- }
276
- return children2;
277
- });
278
- }
279
- function isUnspecifiedDataType(dataType) {
280
- return dataType === node_opcua_variant_1.DataType.Null || dataType === node_opcua_variant_1.DataType.Variant;
281
- }
282
- function extractVariableExtra(session, nodeId, cache, classMember) {
283
- var _a;
284
- return __awaiter(this, void 0, void 0, function* () {
285
- const typeToReference = [];
286
- const importCollector = (i) => {
287
- typeToReference.push(i);
288
- cache.ensureImported(i);
289
- };
290
- const nodeClass = yield (0, utils_1.getNodeClass)(session, nodeId);
291
- if (nodeClass === node_opcua_data_model_1.NodeClass.Variable) {
292
- const dataTypeNodeId = yield (0, utils_1.getDataTypeNodeId)(session, nodeId);
293
- const { dataType, jtype } = yield (0, get_corresponding_data_type_1.getCorrepondingJavascriptType2)(session, nodeId, dataTypeNodeId, cache, importCollector);
294
- cache && cache.referenceBasicType("DataType");
295
- importCollector({ name: "DataType", namespace: -1, module: "BasicType" });
296
- const t = isUnspecifiedDataType((_a = classMember.classDef) === null || _a === void 0 ? void 0 : _a.dataType);
297
- const suffix = jtype === "undefined" || isUnspecifiedDataType(dataType)
298
- ? `<any, any>`
299
- : `<${jtype}${t ? `, ${m0}DataType.${node_opcua_variant_1.DataType[dataType]}` : ""}>`;
300
- // const suffix2 = `<T${t ? `, /DT extends DataType` : ""}>`;
301
- const suffix3 = `<T${t ? `, ${m1}DT` : ""}>`;
302
- const typeDef = yield (0, utils_1.getTypeDefOrBaseType)(session, nodeId);
303
- const typeDefCD = yield extractClassDefinition(session, typeDef.nodeId, cache);
304
- const chevrons = calculateChevrons(typeDefCD, { dataType });
305
- const suffix2 = chevrons.chevronsDef;
306
- /** Suffix for instantiation
307
- * if typeDef.dataType is not null, then we need to add a type argument
308
- */
309
- let suffixInstantiate = "";
310
- if (isUnspecifiedDataType(dataType)) {
311
- if (!isUnspecifiedDataType(typeDefCD.dataType)) {
312
- suffixInstantiate = "<any>";
313
- }
314
- else {
315
- suffixInstantiate = "<any, any>";
316
- }
317
- }
318
- else {
319
- if (!isUnspecifiedDataType(typeDefCD.dataType)) {
320
- suffixInstantiate = `<${jtype}>`;
321
- }
322
- else {
323
- suffixInstantiate = `<${jtype}, DataType.${node_opcua_variant_1.DataType[dataType]}>`;
324
- }
325
- }
326
- return { suffixInstantiate, suffix, suffix2, suffix3, dataTypeNodeId, dataType, jtype, typeToReference, chevrons };
327
- }
328
- return { suffix: "", typeToReference };
329
- });
330
- }
331
- // eslint-disable-next-line max-statements
332
- function extractClassMemberDef(session, nodeId, parentDef, cache) {
333
- return __awaiter(this, void 0, void 0, function* () {
334
- const nodeClass = yield (0, utils_1.getNodeClass)(session, nodeId);
335
- const browseName = yield (0, utils_1.getBrowseName)(session, nodeId);
336
- const name = (0, utils2_1.toJavascritPropertyName)(browseName.name, { ignoreConflictingName: true });
337
- if (nodeClass !== node_opcua_data_model_1.NodeClass.Method && nodeClass !== node_opcua_data_model_1.NodeClass.Object && nodeClass !== node_opcua_data_model_1.NodeClass.Variable) {
338
- throw new Error("Invalid property " + node_opcua_data_model_1.NodeClass[nodeClass] + " " + (browseName === null || browseName === void 0 ? void 0 : browseName.toString()) + " " + nodeId.toString());
339
- }
340
- const description = yield (0, utils_1.getDescription)(session, nodeId);
341
- const children = yield (0, utils_1.getChildrenOrFolderElements)(session, nodeId);
342
- const modellingRule = yield (0, utils_1.getModellingRule)(session, nodeId);
343
- const isOptional = sameInBaseClassIsMandatory(parentDef, browseName) ? false : modellingRule === "Optional";
344
- const typeDefinition = yield (0, utils_1.getTypeDefOrBaseType)(session, nodeId);
345
- if (nodeClass !== node_opcua_data_model_1.NodeClass.Method && (!typeDefinition.browseName || !typeDefinition.browseName.name)) {
346
- warningLog(typeDefinition.toString());
347
- warningLog("cannot find typeDefinition for ", browseName.toString(), "( is the namespace loaded ?)");
348
- }
349
- let childType = makeTypeName2(nodeClass, typeDefinition.browseName);
350
- const classDef = typeDefinition.nodeId.isEmpty() ? null : yield extractClassDefinition(session, typeDefinition.nodeId, cache);
351
- let innerClass = null;
352
- let childBase = null;
353
- let shouldExposeInnerDefinition = false;
354
- // find member exposed by type definition
355
- const baseParentDef = parentDef.superType && (yield extractClassDefinition(session, parentDef.superType.nodeId, cache));
356
- if (classDef && baseParentDef) {
357
- const chevrons1 = calculateChevrons(classDef, baseParentDef);
358
- (0, node_opcua_assert_1.default)(!innerClass);
359
- // let's extract the member that are theorically defined in the member
360
- const membersReference = yield extractAllMembers(session, classDef, cache);
361
- // find member exposed by this member
362
- const membersInstance = yield classify(session, children, classDef);
363
- // if (name==="powerup") {
364
- // dump1(membersReference);
365
- // dump1(membersInstance);
366
- // await extractAllMembers(session, classDef, cache);
367
- // }
368
- shouldExposeInnerDefinition = checkIfShouldExposeInnerDefinition(membersReference, membersInstance);
369
- const sameMemberInBaseClass = baseParentDef && (yield findClassMember(session, browseName, baseParentDef, cache));
370
- if (shouldExposeInnerDefinition) {
371
- if (sameMemberInBaseClass) {
372
- innerClass = {
373
- module: parentDef.interfaceName.module,
374
- name: parentDef.interfaceName.name + "_" + name,
375
- namespace: nodeId.namespace
376
- };
377
- childBase = sameMemberInBaseClass.childType;
378
- childType = innerClass;
379
- }
380
- else {
381
- innerClass = {
382
- module: parentDef.interfaceName.module,
383
- name: parentDef.interfaceName.name + "_" + name,
384
- namespace: nodeId.namespace
385
- };
386
- childBase = childType;
387
- childType = innerClass;
388
- }
389
- (0, node_opcua_assert_1.default)(innerClass);
390
- (0, node_opcua_assert_1.default)(childBase);
391
- }
392
- else {
393
- // may not expose definition but we may want to used the augment typescript class defined in the definition
394
- // we don't need to create an inner class ...
395
- childType = sameMemberInBaseClass ? sameMemberInBaseClass === null || sameMemberInBaseClass === void 0 ? void 0 : sameMemberInBaseClass.childType : childType;
396
- (0, node_opcua_assert_1.default)(!innerClass);
397
- (0, node_opcua_assert_1.default)(!childBase);
398
- }
399
- }
400
- else {
401
- // the lass member has no HasTypeDefinition reference
402
- // may be is a method
403
- (0, node_opcua_assert_1.default)(!innerClass);
404
- (0, node_opcua_assert_1.default)(!childBase);
405
- }
406
- let classMember = {
407
- name,
408
- nodeClass,
409
- browseName,
410
- modellingRule,
411
- isOptional,
412
- classDef,
413
- description,
414
- typeDefinition,
415
- childType,
416
- suffix2: "",
417
- suffix: "",
418
- suffix3: "",
419
- suffixInstantiate: "",
420
- children,
421
- children2: [],
422
- typeToReference: [],
423
- //
424
- innerClass,
425
- childBase,
426
- chevrons: null
427
- };
428
- classMember.children2 = yield _extractLocalMembers(session, classDef, classMember, cache);
429
- if (nodeClass === node_opcua_data_model_1.NodeClass.Variable) {
430
- const extra = yield extractVariableExtra(session, nodeId, cache, classMember);
431
- classMember = Object.assign(Object.assign({}, classMember), extra);
432
- }
433
- return classMember;
434
- });
435
- }
436
- exports.extractClassMemberDef = extractClassMemberDef;
437
- function preDumpChildren(padding, classDef, f, cache) {
438
- return __awaiter(this, void 0, void 0, function* () {
439
- f = f || new node_opcua_utils_1.LineFile();
440
- for (const memberDef of classDef.members) {
441
- const { innerClass, suffix2, suffix3, nodeClass, childBase, chevrons } = memberDef;
442
- if (!innerClass || !childBase) {
443
- continue; // no need to expose inner class
444
- }
445
- cache.ensureImported(childBase);
446
- if (innerClass.name === "UAPubSubDiagnostics_counters" || innerClass.name === "UAProgramStateMachine_currentState") {
447
- // debugger;
448
- }
449
- const baseStuff = getBaseClassWithOmit2(memberDef);
450
- //f.write(`export interface ${innerClass.name}${suffix2} extends ${childBase.name}${suffix3} { // ${NodeClass[nodeClass]}`);
451
- f.write(`export interface ${innerClass.name}${suffix2} extends ${baseStuff} { // ${node_opcua_data_model_1.NodeClass[nodeClass]}`);
452
- yield dumpChildren(padding + " ", memberDef.children2, f, cache);
453
- f.write("}");
454
- }
455
- });
456
- }
457
- const isPlaceHolder = (def) => {
458
- const { modellingRule } = def;
459
- return modellingRule === "MandatoryPlaceholder" || modellingRule === "OptionalPlaceholder";
460
- };
461
- function countExposedChildren(children) {
462
- return children.reduce((p, def) => p + (isPlaceHolder(def) ? 0 : 1), 0);
463
- }
464
- function dumpChildren(padding, children, f, cache) {
465
- f = f || new node_opcua_utils_1.LineFile();
466
- for (const def of children) {
467
- const { suffixInstantiate, name, childType, modellingRule, isOptional, description } = def;
468
- def.typeToReference.forEach((t) => cache.ensureImported(t));
469
- if (isPlaceHolder(def)) {
470
- f.write(" // PlaceHolder for ", name);
471
- continue;
472
- }
473
- cache.ensureImported(childType);
474
- const adjustedName = (0, utils2_1.toJavascritPropertyName)(name, { ignoreConflictingName: true });
475
- if (description.text) {
476
- f.write(`${padding}/**`);
477
- f.write(`${padding} * ${name || ""}`);
478
- f.write((0, utils2_1.toComment)(`${padding} * `, description.text || ""));
479
- f.write(`${padding} */`);
480
- }
481
- f.write(`${padding}${(0, utils2_1.quotifyIfNecessary)(adjustedName)}${isOptional ? "?" : ""}: ${childType.name}${suffixInstantiate || ""};`);
482
- }
483
- }
484
- // now from other namespace
485
- const getSubSymbolList = (s) => {
486
- const subSymbolList = [];
487
- for (const [subSymbol, count] of Object.entries(s.subSymbols)) {
488
- subSymbolList.push(subSymbol);
489
- }
490
- return subSymbolList;
491
- };
492
- function findUsedImport(namespaceIndex, cache) {
493
- const usedImport = [];
494
- // from standard types
495
- for (const imp of Object.keys(cache.imports)) {
496
- const symbolToImport = Object.keys(cache.imports[imp]).filter((f) => Object.prototype.hasOwnProperty.call(cache.requestedBasicTypes, f));
497
- if (symbolToImport.length == 0) {
498
- continue;
499
- }
500
- usedImport.push(imp);
501
- }
502
- // from namespace
503
- for (let ns = 0; ns < cache.requestedSymbols.namespace.length; ns++) {
504
- if (ns === namespaceIndex) {
505
- continue; // avoid self reference
506
- }
507
- const module = cache.namespace[ns].module;
508
- if (cache.requestedSymbols.namespace[ns] && Object.values(cache.requestedSymbols.namespace[ns]).length > 0) {
509
- usedImport.push(module);
510
- }
511
- }
512
- return usedImport;
513
- }
514
- exports.findUsedImport = findUsedImport;
515
- function dumpUsedExport(currentType, namespaceIndex, cache, f) {
516
- f = f || new node_opcua_utils_1.LineFile();
517
- for (const imp of Object.keys(cache.imports)) {
518
- const symbolToImport = Object.keys(cache.imports[imp]).filter((f) => f !== currentType && Object.prototype.hasOwnProperty.call(cache.requestedBasicTypes, f));
519
- if (symbolToImport.length == 0) {
520
- continue;
521
- }
522
- f.write(`import { ${symbolToImport.join(", ")} } from "${imp}"`);
523
- }
524
- for (let ns = 0; ns < cache.requestedSymbols.namespace.length; ns++) {
525
- const n = cache.namespace[ns];
526
- const sourceFolder = n.sourceFolder;
527
- const module = n.module;
528
- if (ns === namespaceIndex) {
529
- // include individuals stuff
530
- for (const [symbol, s] of Object.entries(cache.requestedSymbols.namespace[ns].symbols).filter((a) => a[0] !== currentType)) {
531
- const filename = (0, to_filename_1.toFilename)(symbol);
532
- const subSymbolList = getSubSymbolList(s);
533
- f.write(`import { ${subSymbolList.join(", ")} } from "./${filename}"`);
534
- }
535
- }
536
- else {
537
- if (cache.requestedSymbols.namespace[ns]) {
538
- for (const [symbol, s] of Object.entries(cache.requestedSymbols.namespace[ns].symbols)) {
539
- const subSymbolList = getSubSymbolList(s);
540
- const filename = (0, to_filename_1.toFilename)(symbol);
541
- f.write(`import { ${subSymbolList.join(", ")} } from "${module}/source/${filename}"`);
542
- }
543
- }
544
- }
545
- }
546
- return f.toString();
547
- }
548
- function calculateChevrons(classDef, classDefDerived) {
549
- const { nodeClass, dataType, dataTypeName } = classDef;
550
- let chevronsDef = "";
551
- let chevronsUse = "";
552
- let chevronsExtend = "";
553
- if (nodeClass !== node_opcua_data_model_1.NodeClass.VariableType) {
554
- return { chevronsDef, chevronsUse, chevronsExtend };
555
- }
556
- if (!isUnspecifiedDataType(dataType)) {
557
- chevronsDef = `<T extends ${dataTypeName}${m3}>`;
558
- chevronsUse = `<T${m4}>`;
559
- if (classDefDerived) {
560
- if (isUnspecifiedDataType(classDefDerived.dataType)) {
561
- chevronsExtend = `<T, ${m5}DataType.${node_opcua_variant_1.DataType[dataType]}>`;
562
- }
563
- else {
564
- chevronsExtend = `<T${m6}>`;
565
- }
566
- }
567
- }
568
- else {
569
- // classDef.dataType === DataType.Null
570
- chevronsDef = `<T, DT extends DataType>`;
571
- if (classDefDerived) {
572
- if (isUnspecifiedDataType(classDefDerived.dataType)) {
573
- chevronsUse = `<T, ${m7}DT>`;
574
- chevronsExtend = `<T${m8}, DT>`;
575
- }
576
- else {
577
- chevronsUse = `<T, ${m9}DataType.${node_opcua_variant_1.DataType[classDefDerived.dataType]}>`;
578
- chevronsExtend = `<T, ${mA}DataType.${node_opcua_variant_1.DataType[classDefDerived.dataType]}>`;
579
- }
580
- }
581
- }
582
- return { chevronsDef, chevronsUse, chevronsExtend };
583
- }
584
- // find the structure member that are already in base structure definition and that are replicated here
585
- // we will have to use the Omit<Base,"member1" |"member2"> typescript pattern to avoid issues
586
- function extractMembersRecursively(classDef) {
587
- if (!classDef) {
588
- return [];
589
- }
590
- const m = classDef.members.map((m) => m.name);
591
- if (classDef.baseClassDef) {
592
- const m2 = extractMembersRecursively(classDef.baseClassDef);
593
- return m.concat(m2);
594
- }
595
- return m;
596
- }
597
- function getBaseClassWithOmit(classDef) {
598
- const { baseInterfaceName, members } = classDef;
599
- const allMembers = extractMembersRecursively(classDef.baseClassDef);
600
- const conflictingMembers = members.filter((m) => allMembers.indexOf(m.name) !== -1);
601
- //console.log(allMembers.join(" "));
602
- if (conflictingMembers.length) {
603
- // console.log("conflictingMembers = ", conflictingMembers.map((a) => a.name).join(" "));
604
- }
605
- const chBase = calculateChevrons(classDef.baseClassDef, classDef);
606
- let baseStuff = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${chBase.chevronsUse}`;
607
- if (conflictingMembers.length) {
608
- baseStuff = `Omit<${baseStuff}, ${conflictingMembers.map((a) => `"${a.name}"`).join("|")}>`;
609
- }
610
- return baseStuff;
611
- }
612
- function getBaseClassWithOmit2(classMember) {
613
- const members = classMember.children2;
614
- const allMembers = extractMembersRecursively(classMember.classDef);
615
- const conflictingMembers = members.filter((m) => allMembers.indexOf(m.name) !== -1);
616
- //console.log(allMembers.join(" "));
617
- if (conflictingMembers.length) {
618
- doDebug && console.log("conflictingMembers = ", conflictingMembers.map((a) => a.name).join(" "));
619
- }
620
- const childBase = classMember.childBase;
621
- let baseStuff = `${childBase === null || childBase === void 0 ? void 0 : childBase.name}${classMember.suffix3}`;
622
- if (conflictingMembers.length) {
623
- baseStuff = `Omit<${baseStuff}, ${conflictingMembers.map((a) => `"${a.name}"`).join("|")}>`;
624
- }
625
- return baseStuff;
626
- }
627
- /**
628
- * nodeId : a DataType, ReferenceType,AObjectType, VariableType node
629
- */
630
- // eslint-disable-next-line max-statements
631
- function _convertTypeToTypescript(session, nodeId, cache, f) {
632
- return __awaiter(this, void 0, void 0, function* () {
633
- f = f || new node_opcua_utils_1.LineFile();
634
- const nodeClass = yield (0, utils_1.getNodeClass)(session, nodeId);
635
- if (nodeClass === node_opcua_data_model_1.NodeClass.DataType) {
636
- return yield (0, _dataType_1._exportDataTypeToTypescript)(session, nodeId, cache, f);
637
- }
638
- const classDef = yield extractClassDefinition(session, nodeId, cache);
639
- // if (classDef.browseName.toString().match(/PubSubDiagnosticsWriterGroupType/)) {
640
- // debugger;
641
- // }
642
- const { dataTypeName, dataType, dataTypeNodeId, interfaceName, baseInterfaceName, browseName, description, isAbstract, members } = classDef;
643
- yield preDumpChildren(" ", classDef, f, cache);
644
- // f.write(superType.toString());
645
- f.write(`/**`);
646
- if (description.text) {
647
- f.write((0, utils2_1.toComment)(" * ", description.text || ""));
648
- f.write(` *`);
649
- }
650
- f.write(` * | |${(0, utils2_1.f1)(" ")}|`);
651
- f.write(` * |----------------|${(0, utils2_1.f2)("-")}|`);
652
- f.write(` * |namespace |${(0, utils2_1.f1)(cache.namespace[nodeId.namespace].namespaceUri)}|`);
653
- f.write(` * |nodeClass |${(0, utils2_1.f1)(node_opcua_data_model_1.NodeClass[nodeClass])}|`);
654
- f.write(` * |typedDefinition |${(0, utils2_1.f1)(browseName.toString() + " " + nodeId.toString())}|`);
655
- if (nodeClass === node_opcua_data_model_1.NodeClass.VariableType) {
656
- f.write(` * |dataType |${(0, utils2_1.f1)(node_opcua_variant_1.DataType[dataType])}|`);
657
- f.write(` * |dataType Name |${(0, utils2_1.f1)(dataTypeName + " " + (dataTypeNodeId === null || dataTypeNodeId === void 0 ? void 0 : dataTypeNodeId.toString()))}|`);
658
- }
659
- f.write(` * |isAbstract |${(0, utils2_1.f1)(isAbstract.toString())}|`);
660
- f.write(` */`);
661
- // if (interfaceName.name === "UAOperationLimits") {
662
- // debugger;
663
- // }
664
- cache.ensureImported(baseInterfaceName);
665
- cache.ensureImported(Object.assign(Object.assign({}, baseInterfaceName), { name: baseInterfaceName.name + `${baseExtension}` }));
666
- const ch = calculateChevrons(classDef);
667
- {
668
- if (nodeClass === node_opcua_data_model_1.NodeClass.VariableType) {
669
- if (classDef.dataTypeImport) {
670
- if (doDebug) {
671
- console.log(chalk.red(" ----------------> ", classDef.browseName));
672
- classDef.dataTypeImport.forEach((a) => {
673
- console.log(chalk.red(" ------------------------> ", a.module, a.name, a.namespace));
674
- });
675
- }
676
- classDef.dataTypeImport.forEach(cache.ensureImported.bind(cache));
677
- }
678
- cache.referenceBasicType("DataType");
679
- const chevrons = calculateChevrons(classDef);
680
- const chevronsBase = calculateChevrons(classDef.baseClassDef, classDef);
681
- // Shall we cache.ensureImported({ ...baseInterfaceName!, module: dataTypeName, name: dataTypeName });
682
- const classBaseName = `${interfaceName.name}${baseExtension}${chevrons.chevronsDef}`;
683
- if (countExposedChildren(members) === 0 && (baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name) !== "UAVariableT") {
684
- //
685
- const baseName = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${baseExtension}${chevronsBase.chevronsExtend}`;
686
- f.write(`export type ${classBaseName} = ${baseName};`);
687
- }
688
- else {
689
- if ((baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name) === "UAVariableT") {
690
- f.write(`export interface ${classBaseName} {`);
691
- }
692
- else {
693
- const baseName = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${baseExtension}${chevronsBase.chevronsExtend}`;
694
- f.write(`export interface ${classBaseName} extends ${baseName} {`);
695
- }
696
- yield dumpChildren(" ", members, f, cache);
697
- f.write(`}`);
698
- }
699
- }
700
- else {
701
- const classBaseName = `${interfaceName.name}${baseExtension}`;
702
- if (countExposedChildren(members) === 0 && (baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name) !== "UAObject") {
703
- const baseName = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${baseExtension}`;
704
- f.write(`export type ${classBaseName} = ${baseName};`);
705
- }
706
- else {
707
- if ((baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name) === "UAObject") {
708
- f.write(`export interface ${classBaseName} {`);
709
- }
710
- else {
711
- const baseName = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${baseExtension}`;
712
- f.write(`export interface ${classBaseName} extends ${baseName} {`);
713
- }
714
- yield dumpChildren(" ", members, f, cache);
715
- f.write(`}`);
716
- }
717
- }
718
- }
719
- const baseStuff = getBaseClassWithOmit(classDef);
720
- if (nodeClass === node_opcua_data_model_1.NodeClass.VariableType) {
721
- cache.referenceBasicType("DataType");
722
- // if dataType is a extension object we can simpligy by forcing DT
723
- const className = `${interfaceName.name}${ch.chevronsDef}`;
724
- const c = isUnspecifiedDataType(dataType) ? `<T, DT${mB}>` : `<T${mC}>`;
725
- const classBaseName = `${interfaceName.name}${baseExtension}${c}`;
726
- f.write(`export interface ${className} extends ${baseStuff}, ${classBaseName} {`);
727
- }
728
- else {
729
- const className = `${interfaceName.name}`;
730
- const classBaseName = `${interfaceName.name}${baseExtension}`;
731
- f.write(`export interface ${className} extends ${baseStuff}, ${classBaseName} {`);
732
- }
733
- f.write(`}`);
734
- return { type: "ua", content: f.toString(), typeName: interfaceName.name };
735
- });
736
- }
737
- exports._convertTypeToTypescript = _convertTypeToTypescript;
738
- function convertTypeToTypescript(session, nodeId, options, cache, f) {
739
- return __awaiter(this, void 0, void 0, function* () {
740
- f = new node_opcua_utils_1.LineFile();
741
- cache = cache || (yield (0, cache_1.constructCache)(session, options));
742
- const { type, content, typeName } = yield _convertTypeToTypescript(session, nodeId, cache);
743
- f.write(`// ----- this file has been automatically generated - do not edit`);
744
- f.write(dumpUsedExport(typeName, nodeId.namespace, cache));
745
- f.write(content);
746
- const folder = cache.namespace[nodeId.namespace].sourceFolder;
747
- const module = cache.namespace[nodeId.namespace].module;
748
- const filename = (0, to_filename_1.toFilename)(typeName);
749
- const dependencies = findUsedImport(nodeId.namespace, cache);
750
- return { type, content: f.toString(), folder, module, filename, dependencies };
751
- });
752
- }
753
- exports.convertTypeToTypescript = convertTypeToTypescript;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.convertTypeToTypescript = exports._convertTypeToTypescript = exports.findUsedImport = exports.extractClassMemberDef = exports.checkIfShouldExposeInnerDefinition = exports.findClassMember = exports.extractClassDefinition = exports.makeTypeName2 = exports.convertDataTypeToTypescript = void 0;
13
+ /* eslint-disable max-depth */
14
+ const chalk = require("chalk");
15
+ const node_opcua_data_model_1 = require("node-opcua-data-model");
16
+ const node_opcua_types_1 = require("node-opcua-types");
17
+ const node_opcua_utils_1 = require("node-opcua-utils");
18
+ const node_opcua_variant_1 = require("node-opcua-variant");
19
+ const node_opcua_assert_1 = require("node-opcua-assert");
20
+ const node_opcua_debug_1 = require("node-opcua-debug");
21
+ const utils_1 = require("./private/utils");
22
+ const cache_1 = require("./private/cache");
23
+ const to_filename_1 = require("./private/to_filename");
24
+ const utils2_1 = require("./utils2");
25
+ const _dataType_1 = require("./_dataType");
26
+ const get_corresponding_data_type_1 = require("./private/get_corresponding_data_type");
27
+ const warningLog = (0, node_opcua_debug_1.make_warningLog)("typescript");
28
+ const doDebug = false;
29
+ const baseExtension = "_Base";
30
+ const m0 = !doDebug ? "" : `/* 0 */`;
31
+ const m1 = !doDebug ? "" : `/* 1 */`;
32
+ const m2 = !doDebug ? "" : `/* 2 */`;
33
+ const m3 = !doDebug ? "" : `/* 3 */`;
34
+ const m4 = !doDebug ? "" : `/* 4 */`;
35
+ const m5 = !doDebug ? "" : `/* 5 */`;
36
+ const m6 = !doDebug ? "" : `/* 6 */`;
37
+ const m7 = !doDebug ? "" : `/* 7 */`;
38
+ const m8 = !doDebug ? "" : `/* 8 */`;
39
+ const m9 = !doDebug ? "" : `/* 9 */`;
40
+ const mA = !doDebug ? "" : `/* a */`;
41
+ const mB = !doDebug ? "" : `/* b */`;
42
+ const mC = !doDebug ? "" : `/* c */`;
43
+ function convertDataTypeToTypescript(session, dataTypeId) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const definition = yield (0, utils_1.getDefinition)(session, dataTypeId);
46
+ const browseName = yield (0, utils_1.getBrowseName)(session, dataTypeId);
47
+ const dataTypeTypescriptName = `UA${browseName.name.toString()}`;
48
+ const f = new node_opcua_utils_1.LineFile();
49
+ if (definition && definition instanceof node_opcua_types_1.StructureDefinition) {
50
+ f.write(`interface ${dataTypeTypescriptName} {`);
51
+ for (const field of definition.fields || []) {
52
+ /** */
53
+ }
54
+ f.write(`}`);
55
+ }
56
+ });
57
+ }
58
+ exports.convertDataTypeToTypescript = convertDataTypeToTypescript;
59
+ function makeTypeName2(nodeClass, browseName, suffix) {
60
+ (0, node_opcua_assert_1.default)(browseName);
61
+ if (nodeClass === node_opcua_data_model_1.NodeClass.Method) {
62
+ return { name: "UAMethod", namespace: 0, module: "UAMethod" };
63
+ }
64
+ return (0, cache_1.makeTypeNameNew)(nodeClass, null, browseName, suffix);
65
+ }
66
+ exports.makeTypeName2 = makeTypeName2;
67
+ function extractClassDefinition(session, nodeId, cache) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ const extraImports = [];
70
+ const _c = cache.classDefCache || {};
71
+ cache.classDefCache = _c;
72
+ let classDef = _c[nodeId.toString()];
73
+ if (classDef) {
74
+ return classDef;
75
+ }
76
+ const nodeClass = yield (0, utils_1.getNodeClass)(session, nodeId);
77
+ if (nodeClass !== node_opcua_data_model_1.NodeClass.VariableType && nodeClass !== node_opcua_data_model_1.NodeClass.ObjectType) {
78
+ throw new Error("Invalid nodeClass " + node_opcua_data_model_1.NodeClass[nodeClass] + " nodeId " + nodeId.toString());
79
+ }
80
+ const browseName = yield (0, utils_1.getBrowseName)(session, nodeId);
81
+ const isAbstract = yield (0, utils_1.getIsAbstract)(session, nodeId);
82
+ const superType = (yield (0, utils_1.getSubtypeNodeIdIfAny)(session, nodeId)) || undefined;
83
+ const dataTypeNodeId = yield (0, utils_1.getDataTypeNodeId)(session, nodeId);
84
+ let dataTypeName = "";
85
+ let dataType = node_opcua_variant_1.DataType.Null;
86
+ let dataTypeImport = undefined;
87
+ if (nodeClass === node_opcua_data_model_1.NodeClass.VariableType) {
88
+ dataType = yield (0, utils_1.extractBasicDataType)(session, dataTypeNodeId);
89
+ const importCollector = (i) => {
90
+ extraImports.push(i);
91
+ cache.ensureImported(i);
92
+ };
93
+ const { jtype } = yield (0, get_corresponding_data_type_1.getCorrepondingJavascriptType2)(session, nodeId, dataTypeNodeId, cache, importCollector);
94
+ dataTypeName = jtype; // with decoration
95
+ if (!(dataTypeNodeId === null || dataTypeNodeId === void 0 ? void 0 : dataTypeNodeId.isEmpty())) {
96
+ // "DT" + (await getBrowseName(session, dataTypeNodeId!))?.name! || "";
97
+ // const bn = await getBrowseName(session, dataTypeNodeId!);
98
+ dataTypeImport = extraImports; // makeTypeName2(NodeClass.DataType, bn);
99
+ }
100
+ }
101
+ const baseClassDef = !superType ? null : yield extractClassDefinition(session, superType.nodeId, cache);
102
+ const description = yield (0, utils_1.getDescription)(session, nodeId);
103
+ // const definition = await getDefinition(session, nodeId);
104
+ const interfaceName = makeTypeName2(nodeClass, browseName);
105
+ const baseInterfaceName = !superType ? null : makeTypeName2(nodeClass, superType.browseName);
106
+ // extract member
107
+ const children = yield (0, utils_1.getChildrenOrFolderElements)(session, nodeId);
108
+ const members = [];
109
+ classDef = {
110
+ nodeClass,
111
+ browseName,
112
+ isAbstract,
113
+ dataType,
114
+ dataTypeNodeId,
115
+ dataTypeName,
116
+ dataTypeImport,
117
+ superType,
118
+ baseClassDef,
119
+ description,
120
+ // definition,
121
+ children,
122
+ members,
123
+ interfaceName,
124
+ baseInterfaceName
125
+ };
126
+ _c[nodeId.toString()] = classDef;
127
+ for (const child of children) {
128
+ const c = yield extractClassMemberDef(session, child.nodeId, classDef, cache);
129
+ classDef.members.push(c);
130
+ }
131
+ return classDef;
132
+ });
133
+ }
134
+ exports.extractClassDefinition = extractClassDefinition;
135
+ function classify(session, refs, classDef) {
136
+ return __awaiter(this, void 0, void 0, function* () {
137
+ const r = {
138
+ Mandatory: [],
139
+ Optional: [],
140
+ MandatoryPlaceholder: [],
141
+ OptionalPlaceholder: [],
142
+ ExposesItsArray: []
143
+ };
144
+ for (const mm of refs) {
145
+ let modellingRule = yield (0, utils_1.getModellingRule)(session, mm.nodeId);
146
+ if (modellingRule) {
147
+ // if Optional, check that base class member is also Optional or force to Mandatory
148
+ if (modellingRule === "Optional" && classDef.baseClassDef) {
149
+ const sameMember = classDef.baseClassDef.members.find((m) => m.browseName.name === mm.browseName.name);
150
+ if (sameMember) {
151
+ if (!sameMember.isOptional) {
152
+ console.log(chalk.cyan("Warning: ") +
153
+ chalk.yellow(classDef.browseName.toString()) +
154
+ " ModellingRule is Optional but base class member is Mandatory");
155
+ modellingRule = "Mandatory";
156
+ }
157
+ }
158
+ }
159
+ r[modellingRule] = r[modellingRule] || [];
160
+ r[modellingRule].push(mm);
161
+ }
162
+ }
163
+ return r;
164
+ });
165
+ }
166
+ function extractAllMembers(session, classDef, cache) {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ const m = [...classDef.children];
169
+ let s = classDef;
170
+ while (s.baseClassDef) {
171
+ s = s.baseClassDef;
172
+ // start from top most class
173
+ // do not overwrite most top member definition, so we get mandatory stuff
174
+ for (const mm of s.children) {
175
+ const found = m.findIndex((r) => r.browseName.toString() === mm.browseName.toString());
176
+ if (found < 0) {
177
+ m.push(mm);
178
+ }
179
+ }
180
+ }
181
+ // now separate mandatory and optionals
182
+ const members = yield classify(session, m, classDef);
183
+ return members;
184
+ });
185
+ }
186
+ function findClassMember(session, browseName, baseParentDef, cache) {
187
+ return __awaiter(this, void 0, void 0, function* () {
188
+ const childBrowseName = browseName.toString();
189
+ const r = baseParentDef.children.find((a) => a.browseName.toString() === childBrowseName);
190
+ if (r) {
191
+ const d = yield extractClassMemberDef(session, r.nodeId, baseParentDef, cache);
192
+ return d;
193
+ }
194
+ const baseBaseParentDef = baseParentDef.superType && (yield extractClassDefinition(session, baseParentDef.superType.nodeId, cache));
195
+ if (!baseBaseParentDef) {
196
+ return null;
197
+ }
198
+ return yield findClassMember(session, browseName, baseBaseParentDef, cache);
199
+ });
200
+ }
201
+ exports.findClassMember = findClassMember;
202
+ function hasNewMaterial(referenceMembers, instanceMembers) {
203
+ const ref = referenceMembers.map((x) => x.browseName.toString()).sort();
204
+ const instance = instanceMembers.map((x) => x.browseName.toString()).sort();
205
+ for (const n of instance) {
206
+ if (ref.findIndex((x) => x === n) < 0) {
207
+ return true;
208
+ }
209
+ }
210
+ return false;
211
+ }
212
+ // we should expose an inner definition if
213
+ // - at least one mandatory in instnace doesn't exist in main.Mandatory
214
+ // - at least one optional in instnace doesn't exist in main.Mandatory
215
+ function checkIfShouldExposeInnerDefinition(main, instance) {
216
+ const hasNewStuff = hasNewMaterial(main.Mandatory, instance.Mandatory) || hasNewMaterial(main.Optional, instance.Optional);
217
+ return hasNewStuff;
218
+ }
219
+ exports.checkIfShouldExposeInnerDefinition = checkIfShouldExposeInnerDefinition;
220
+ function dump1(a) {
221
+ console.log("Mandatory = ", a.Mandatory.map((x) => x.browseName.toString())
222
+ .sort()
223
+ .join(" "));
224
+ console.log("Optional = ", a.Optional.map((x) => x.browseName.toString())
225
+ .sort()
226
+ .join(" "));
227
+ }
228
+ function getSameInBaseClass(parent, browseName) {
229
+ if (!parent || !parent.baseClassDef)
230
+ return null;
231
+ const originalClassMember = parent.baseClassDef.members.find((m) => m.browseName.toString() === browseName.toString());
232
+ return originalClassMember || null;
233
+ }
234
+ function sameInBaseClassIsMandatory(parent, browseName) {
235
+ const originalClassMember = getSameInBaseClass(parent, browseName);
236
+ if (!originalClassMember)
237
+ return false;
238
+ return !originalClassMember.isOptional;
239
+ }
240
+ function _extractLocalMembers(session, parent, classMember, cache) {
241
+ var _a;
242
+ return __awaiter(this, void 0, void 0, function* () {
243
+ const children2 = [];
244
+ for (const child of classMember.children) {
245
+ const nodeId = child.nodeId;
246
+ const browseName = yield (0, utils_1.getBrowseName)(session, nodeId);
247
+ const name = (0, utils2_1.toJavascritPropertyName)(browseName.name, { ignoreConflictingName: true });
248
+ const description = yield (0, utils_1.getDescription)(session, nodeId);
249
+ const modellingRule = yield (0, utils_1.getModellingRule)(session, nodeId);
250
+ const isOptional = sameInBaseClassIsMandatory(parent, browseName) ? false : modellingRule === "Optional";
251
+ const typeDefinition = yield (0, utils_1.getTypeDefOrBaseType)(session, nodeId);
252
+ const childInBase = (_a = classMember.classDef) === null || _a === void 0 ? void 0 : _a.members.find((a) => a.browseName.toString() === browseName.toString());
253
+ let childType;
254
+ if (childInBase) {
255
+ childType = childInBase.childType;
256
+ }
257
+ else {
258
+ const d = typeDefinition.nodeId.isEmpty() ? null : yield extractClassDefinition(session, typeDefinition.nodeId, cache);
259
+ childType = (d === null || d === void 0 ? void 0 : d.interfaceName) || { name: "UAMethod", module: "BasicType", namespace: -1 };
260
+ }
261
+ // may be childType is already
262
+ const { suffix, suffixInstantiate, typeToReference, chevrons } = yield extractVariableExtra(session, child.nodeId, cache, classMember);
263
+ const c = {
264
+ childType,
265
+ description,
266
+ isOptional,
267
+ modellingRule,
268
+ name,
269
+ suffix,
270
+ suffixInstantiate,
271
+ typeToReference,
272
+ chevrons
273
+ };
274
+ children2.push(c);
275
+ }
276
+ return children2;
277
+ });
278
+ }
279
+ function isUnspecifiedDataType(dataType) {
280
+ return dataType === node_opcua_variant_1.DataType.Null || dataType === node_opcua_variant_1.DataType.Variant;
281
+ }
282
+ function extractVariableExtra(session, nodeId, cache, classMember) {
283
+ var _a;
284
+ return __awaiter(this, void 0, void 0, function* () {
285
+ const typeToReference = [];
286
+ const importCollector = (i) => {
287
+ typeToReference.push(i);
288
+ cache.ensureImported(i);
289
+ };
290
+ const nodeClass = yield (0, utils_1.getNodeClass)(session, nodeId);
291
+ if (nodeClass === node_opcua_data_model_1.NodeClass.Variable) {
292
+ const dataTypeNodeId = yield (0, utils_1.getDataTypeNodeId)(session, nodeId);
293
+ const { dataType, jtype } = yield (0, get_corresponding_data_type_1.getCorrepondingJavascriptType2)(session, nodeId, dataTypeNodeId, cache, importCollector);
294
+ cache && cache.referenceBasicType("DataType");
295
+ importCollector({ name: "DataType", namespace: -1, module: "BasicType" });
296
+ const t = isUnspecifiedDataType((_a = classMember.classDef) === null || _a === void 0 ? void 0 : _a.dataType);
297
+ const suffix = jtype === "undefined" || isUnspecifiedDataType(dataType)
298
+ ? `<any, any>`
299
+ : `<${jtype}${t ? `, ${m0}DataType.${node_opcua_variant_1.DataType[dataType]}` : ""}>`;
300
+ // const suffix2 = `<T${t ? `, /DT extends DataType` : ""}>`;
301
+ const suffix3 = `<T${t ? `, ${m1}DT` : ""}>`;
302
+ const typeDef = yield (0, utils_1.getTypeDefOrBaseType)(session, nodeId);
303
+ const typeDefCD = yield extractClassDefinition(session, typeDef.nodeId, cache);
304
+ const chevrons = calculateChevrons(typeDefCD, { dataType });
305
+ const suffix2 = chevrons.chevronsDef;
306
+ /** Suffix for instantiation
307
+ * if typeDef.dataType is not null, then we need to add a type argument
308
+ */
309
+ let suffixInstantiate = "";
310
+ if (isUnspecifiedDataType(dataType)) {
311
+ if (!isUnspecifiedDataType(typeDefCD.dataType)) {
312
+ suffixInstantiate = "<any>";
313
+ }
314
+ else {
315
+ suffixInstantiate = "<any, any>";
316
+ }
317
+ }
318
+ else {
319
+ if (!isUnspecifiedDataType(typeDefCD.dataType)) {
320
+ suffixInstantiate = `<${jtype}>`;
321
+ }
322
+ else {
323
+ suffixInstantiate = `<${jtype}, DataType.${node_opcua_variant_1.DataType[dataType]}>`;
324
+ }
325
+ }
326
+ return { suffixInstantiate, suffix, suffix2, suffix3, dataTypeNodeId, dataType, jtype, typeToReference, chevrons };
327
+ }
328
+ return { suffix: "", typeToReference };
329
+ });
330
+ }
331
+ // eslint-disable-next-line max-statements
332
+ function extractClassMemberDef(session, nodeId, parentDef, cache) {
333
+ return __awaiter(this, void 0, void 0, function* () {
334
+ const nodeClass = yield (0, utils_1.getNodeClass)(session, nodeId);
335
+ const browseName = yield (0, utils_1.getBrowseName)(session, nodeId);
336
+ const name = (0, utils2_1.toJavascritPropertyName)(browseName.name, { ignoreConflictingName: true });
337
+ if (nodeClass !== node_opcua_data_model_1.NodeClass.Method && nodeClass !== node_opcua_data_model_1.NodeClass.Object && nodeClass !== node_opcua_data_model_1.NodeClass.Variable) {
338
+ throw new Error("Invalid property " + node_opcua_data_model_1.NodeClass[nodeClass] + " " + (browseName === null || browseName === void 0 ? void 0 : browseName.toString()) + " " + nodeId.toString());
339
+ }
340
+ const description = yield (0, utils_1.getDescription)(session, nodeId);
341
+ const children = yield (0, utils_1.getChildrenOrFolderElements)(session, nodeId);
342
+ const modellingRule = yield (0, utils_1.getModellingRule)(session, nodeId);
343
+ const isOptional = sameInBaseClassIsMandatory(parentDef, browseName) ? false : modellingRule === "Optional";
344
+ const typeDefinition = yield (0, utils_1.getTypeDefOrBaseType)(session, nodeId);
345
+ if (nodeClass !== node_opcua_data_model_1.NodeClass.Method && (!typeDefinition.browseName || !typeDefinition.browseName.name)) {
346
+ warningLog(typeDefinition.toString());
347
+ warningLog("cannot find typeDefinition for ", browseName.toString(), "( is the namespace loaded ?)");
348
+ }
349
+ let childType = makeTypeName2(nodeClass, typeDefinition.browseName);
350
+ const classDef = typeDefinition.nodeId.isEmpty() ? null : yield extractClassDefinition(session, typeDefinition.nodeId, cache);
351
+ let innerClass = null;
352
+ let childBase = null;
353
+ let shouldExposeInnerDefinition = false;
354
+ // find member exposed by type definition
355
+ const baseParentDef = parentDef.superType && (yield extractClassDefinition(session, parentDef.superType.nodeId, cache));
356
+ if (classDef && baseParentDef) {
357
+ const chevrons1 = calculateChevrons(classDef, baseParentDef);
358
+ (0, node_opcua_assert_1.default)(!innerClass);
359
+ // let's extract the member that are theorically defined in the member
360
+ const membersReference = yield extractAllMembers(session, classDef, cache);
361
+ // find member exposed by this member
362
+ const membersInstance = yield classify(session, children, classDef);
363
+ // if (name==="powerup") {
364
+ // dump1(membersReference);
365
+ // dump1(membersInstance);
366
+ // await extractAllMembers(session, classDef, cache);
367
+ // }
368
+ shouldExposeInnerDefinition = checkIfShouldExposeInnerDefinition(membersReference, membersInstance);
369
+ const sameMemberInBaseClass = baseParentDef && (yield findClassMember(session, browseName, baseParentDef, cache));
370
+ if (shouldExposeInnerDefinition) {
371
+ if (sameMemberInBaseClass) {
372
+ innerClass = {
373
+ module: parentDef.interfaceName.module,
374
+ name: parentDef.interfaceName.name + "_" + name,
375
+ namespace: nodeId.namespace
376
+ };
377
+ childBase = sameMemberInBaseClass.childType;
378
+ childType = innerClass;
379
+ }
380
+ else {
381
+ innerClass = {
382
+ module: parentDef.interfaceName.module,
383
+ name: parentDef.interfaceName.name + "_" + name,
384
+ namespace: nodeId.namespace
385
+ };
386
+ childBase = childType;
387
+ childType = innerClass;
388
+ }
389
+ (0, node_opcua_assert_1.default)(innerClass);
390
+ (0, node_opcua_assert_1.default)(childBase);
391
+ }
392
+ else {
393
+ // may not expose definition but we may want to used the augment typescript class defined in the definition
394
+ // we don't need to create an inner class ...
395
+ childType = sameMemberInBaseClass ? sameMemberInBaseClass === null || sameMemberInBaseClass === void 0 ? void 0 : sameMemberInBaseClass.childType : childType;
396
+ (0, node_opcua_assert_1.default)(!innerClass);
397
+ (0, node_opcua_assert_1.default)(!childBase);
398
+ }
399
+ }
400
+ else {
401
+ // the lass member has no HasTypeDefinition reference
402
+ // may be is a method
403
+ (0, node_opcua_assert_1.default)(!innerClass);
404
+ (0, node_opcua_assert_1.default)(!childBase);
405
+ }
406
+ let classMember = {
407
+ name,
408
+ nodeClass,
409
+ browseName,
410
+ modellingRule,
411
+ isOptional,
412
+ classDef,
413
+ description,
414
+ typeDefinition,
415
+ childType,
416
+ suffix2: "",
417
+ suffix: "",
418
+ suffix3: "",
419
+ suffixInstantiate: "",
420
+ children,
421
+ children2: [],
422
+ typeToReference: [],
423
+ //
424
+ innerClass,
425
+ childBase,
426
+ chevrons: null
427
+ };
428
+ classMember.children2 = yield _extractLocalMembers(session, classDef, classMember, cache);
429
+ if (nodeClass === node_opcua_data_model_1.NodeClass.Variable) {
430
+ const extra = yield extractVariableExtra(session, nodeId, cache, classMember);
431
+ classMember = Object.assign(Object.assign({}, classMember), extra);
432
+ }
433
+ return classMember;
434
+ });
435
+ }
436
+ exports.extractClassMemberDef = extractClassMemberDef;
437
+ function preDumpChildren(padding, classDef, f, cache) {
438
+ return __awaiter(this, void 0, void 0, function* () {
439
+ f = f || new node_opcua_utils_1.LineFile();
440
+ for (const memberDef of classDef.members) {
441
+ const { innerClass, suffix2, suffix3, nodeClass, childBase, chevrons } = memberDef;
442
+ if (!innerClass || !childBase) {
443
+ continue; // no need to expose inner class
444
+ }
445
+ cache.ensureImported(childBase);
446
+ if (innerClass.name === "UAPubSubDiagnostics_counters" || innerClass.name === "UAProgramStateMachine_currentState") {
447
+ // debugger;
448
+ }
449
+ const baseStuff = getBaseClassWithOmit2(memberDef);
450
+ //f.write(`export interface ${innerClass.name}${suffix2} extends ${childBase.name}${suffix3} { // ${NodeClass[nodeClass]}`);
451
+ f.write(`export interface ${innerClass.name}${suffix2} extends ${baseStuff} { // ${node_opcua_data_model_1.NodeClass[nodeClass]}`);
452
+ yield dumpChildren(padding + " ", memberDef.children2, f, cache);
453
+ f.write("}");
454
+ }
455
+ });
456
+ }
457
+ const isPlaceHolder = (def) => {
458
+ const { modellingRule } = def;
459
+ return modellingRule === "MandatoryPlaceholder" || modellingRule === "OptionalPlaceholder";
460
+ };
461
+ function countExposedChildren(children) {
462
+ return children.reduce((p, def) => p + (isPlaceHolder(def) ? 0 : 1), 0);
463
+ }
464
+ function dumpChildren(padding, children, f, cache) {
465
+ f = f || new node_opcua_utils_1.LineFile();
466
+ for (const def of children) {
467
+ const { suffixInstantiate, name, childType, modellingRule, isOptional, description } = def;
468
+ def.typeToReference.forEach((t) => cache.ensureImported(t));
469
+ if (isPlaceHolder(def)) {
470
+ f.write(" // PlaceHolder for ", name);
471
+ continue;
472
+ }
473
+ cache.ensureImported(childType);
474
+ const adjustedName = (0, utils2_1.toJavascritPropertyName)(name, { ignoreConflictingName: true });
475
+ if (description.text) {
476
+ f.write(`${padding}/**`);
477
+ f.write(`${padding} * ${name || ""}`);
478
+ f.write((0, utils2_1.toComment)(`${padding} * `, description.text || ""));
479
+ f.write(`${padding} */`);
480
+ }
481
+ f.write(`${padding}${(0, utils2_1.quotifyIfNecessary)(adjustedName)}${isOptional ? "?" : ""}: ${childType.name}${suffixInstantiate || ""};`);
482
+ }
483
+ }
484
+ // now from other namespace
485
+ const getSubSymbolList = (s) => {
486
+ const subSymbolList = [];
487
+ for (const [subSymbol, count] of Object.entries(s.subSymbols)) {
488
+ subSymbolList.push(subSymbol);
489
+ }
490
+ return subSymbolList;
491
+ };
492
+ function findUsedImport(namespaceIndex, cache) {
493
+ const usedImport = [];
494
+ // from standard types
495
+ for (const imp of Object.keys(cache.imports)) {
496
+ const symbolToImport = Object.keys(cache.imports[imp]).filter((f) => Object.prototype.hasOwnProperty.call(cache.requestedBasicTypes, f));
497
+ if (symbolToImport.length == 0) {
498
+ continue;
499
+ }
500
+ usedImport.push(imp);
501
+ }
502
+ // from namespace
503
+ for (let ns = 0; ns < cache.requestedSymbols.namespace.length; ns++) {
504
+ if (ns === namespaceIndex) {
505
+ continue; // avoid self reference
506
+ }
507
+ const module = cache.namespace[ns].module;
508
+ if (cache.requestedSymbols.namespace[ns] && Object.values(cache.requestedSymbols.namespace[ns]).length > 0) {
509
+ usedImport.push(module);
510
+ }
511
+ }
512
+ return usedImport;
513
+ }
514
+ exports.findUsedImport = findUsedImport;
515
+ function dumpUsedExport(currentType, namespaceIndex, cache, f) {
516
+ f = f || new node_opcua_utils_1.LineFile();
517
+ for (const imp of Object.keys(cache.imports)) {
518
+ const symbolToImport = Object.keys(cache.imports[imp]).filter((f) => f !== currentType && Object.prototype.hasOwnProperty.call(cache.requestedBasicTypes, f));
519
+ if (symbolToImport.length == 0) {
520
+ continue;
521
+ }
522
+ f.write(`import { ${symbolToImport.join(", ")} } from "${imp}"`);
523
+ }
524
+ for (let ns = 0; ns < cache.requestedSymbols.namespace.length; ns++) {
525
+ const n = cache.namespace[ns];
526
+ const sourceFolder = n.sourceFolder;
527
+ const module = n.module;
528
+ if (ns === namespaceIndex) {
529
+ // include individuals stuff
530
+ for (const [symbol, s] of Object.entries(cache.requestedSymbols.namespace[ns].symbols).filter((a) => a[0] !== currentType)) {
531
+ const filename = (0, to_filename_1.toFilename)(symbol);
532
+ const subSymbolList = getSubSymbolList(s);
533
+ f.write(`import { ${subSymbolList.join(", ")} } from "./${filename}"`);
534
+ }
535
+ }
536
+ else {
537
+ if (cache.requestedSymbols.namespace[ns]) {
538
+ for (const [symbol, s] of Object.entries(cache.requestedSymbols.namespace[ns].symbols)) {
539
+ const subSymbolList = getSubSymbolList(s);
540
+ const filename = (0, to_filename_1.toFilename)(symbol);
541
+ f.write(`import { ${subSymbolList.join(", ")} } from "${module}/source/${filename}"`);
542
+ }
543
+ }
544
+ }
545
+ }
546
+ return f.toString();
547
+ }
548
+ function calculateChevrons(classDef, classDefDerived) {
549
+ const { nodeClass, dataType, dataTypeName } = classDef;
550
+ let chevronsDef = "";
551
+ let chevronsUse = "";
552
+ let chevronsExtend = "";
553
+ if (nodeClass !== node_opcua_data_model_1.NodeClass.VariableType) {
554
+ return { chevronsDef, chevronsUse, chevronsExtend };
555
+ }
556
+ if (!isUnspecifiedDataType(dataType)) {
557
+ chevronsDef = `<T extends ${dataTypeName}${m3}>`;
558
+ chevronsUse = `<T${m4}>`;
559
+ if (classDefDerived) {
560
+ if (isUnspecifiedDataType(classDefDerived.dataType)) {
561
+ chevronsExtend = `<T, ${m5}DataType.${node_opcua_variant_1.DataType[dataType]}>`;
562
+ }
563
+ else {
564
+ chevronsExtend = `<T${m6}>`;
565
+ }
566
+ }
567
+ }
568
+ else {
569
+ // classDef.dataType === DataType.Null
570
+ chevronsDef = `<T, DT extends DataType>`;
571
+ if (classDefDerived) {
572
+ if (isUnspecifiedDataType(classDefDerived.dataType)) {
573
+ chevronsUse = `<T, ${m7}DT>`;
574
+ chevronsExtend = `<T${m8}, DT>`;
575
+ }
576
+ else {
577
+ chevronsUse = `<T, ${m9}DataType.${node_opcua_variant_1.DataType[classDefDerived.dataType]}>`;
578
+ chevronsExtend = `<T, ${mA}DataType.${node_opcua_variant_1.DataType[classDefDerived.dataType]}>`;
579
+ }
580
+ }
581
+ }
582
+ return { chevronsDef, chevronsUse, chevronsExtend };
583
+ }
584
+ // find the structure member that are already in base structure definition and that are replicated here
585
+ // we will have to use the Omit<Base,"member1" |"member2"> typescript pattern to avoid issues
586
+ function extractMembersRecursively(classDef) {
587
+ if (!classDef) {
588
+ return [];
589
+ }
590
+ const m = classDef.members.map((m) => m.name);
591
+ if (classDef.baseClassDef) {
592
+ const m2 = extractMembersRecursively(classDef.baseClassDef);
593
+ return m.concat(m2);
594
+ }
595
+ return m;
596
+ }
597
+ function getBaseClassWithOmit(classDef) {
598
+ const { baseInterfaceName, members } = classDef;
599
+ const allMembers = extractMembersRecursively(classDef.baseClassDef);
600
+ const conflictingMembers = members.filter((m) => allMembers.indexOf(m.name) !== -1);
601
+ //console.log(allMembers.join(" "));
602
+ if (conflictingMembers.length) {
603
+ // console.log("conflictingMembers = ", conflictingMembers.map((a) => a.name).join(" "));
604
+ }
605
+ const chBase = calculateChevrons(classDef.baseClassDef, classDef);
606
+ let baseStuff = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${chBase.chevronsUse}`;
607
+ if (conflictingMembers.length) {
608
+ baseStuff = `Omit<${baseStuff}, ${conflictingMembers.map((a) => `"${a.name}"`).join("|")}>`;
609
+ }
610
+ return baseStuff;
611
+ }
612
+ function getBaseClassWithOmit2(classMember) {
613
+ const members = classMember.children2;
614
+ const allMembers = extractMembersRecursively(classMember.classDef);
615
+ const conflictingMembers = members.filter((m) => allMembers.indexOf(m.name) !== -1);
616
+ //console.log(allMembers.join(" "));
617
+ if (conflictingMembers.length) {
618
+ doDebug && console.log("conflictingMembers = ", conflictingMembers.map((a) => a.name).join(" "));
619
+ }
620
+ const childBase = classMember.childBase;
621
+ let baseStuff = `${childBase === null || childBase === void 0 ? void 0 : childBase.name}${classMember.suffix3}`;
622
+ if (conflictingMembers.length) {
623
+ baseStuff = `Omit<${baseStuff}, ${conflictingMembers.map((a) => `"${a.name}"`).join("|")}>`;
624
+ }
625
+ return baseStuff;
626
+ }
627
+ /**
628
+ * nodeId : a DataType, ReferenceType,AObjectType, VariableType node
629
+ */
630
+ // eslint-disable-next-line max-statements
631
+ function _convertTypeToTypescript(session, nodeId, cache, f) {
632
+ return __awaiter(this, void 0, void 0, function* () {
633
+ f = f || new node_opcua_utils_1.LineFile();
634
+ const nodeClass = yield (0, utils_1.getNodeClass)(session, nodeId);
635
+ if (nodeClass === node_opcua_data_model_1.NodeClass.DataType) {
636
+ return yield (0, _dataType_1._exportDataTypeToTypescript)(session, nodeId, cache, f);
637
+ }
638
+ const classDef = yield extractClassDefinition(session, nodeId, cache);
639
+ // if (classDef.browseName.toString().match(/PubSubDiagnosticsWriterGroupType/)) {
640
+ // debugger;
641
+ // }
642
+ const { dataTypeName, dataType, dataTypeNodeId, interfaceName, baseInterfaceName, browseName, description, isAbstract, members } = classDef;
643
+ yield preDumpChildren(" ", classDef, f, cache);
644
+ // f.write(superType.toString());
645
+ f.write(`/**`);
646
+ if (description.text) {
647
+ f.write((0, utils2_1.toComment)(" * ", description.text || ""));
648
+ f.write(` *`);
649
+ }
650
+ f.write(` * | |${(0, utils2_1.f1)(" ")}|`);
651
+ f.write(` * |----------------|${(0, utils2_1.f2)("-")}|`);
652
+ f.write(` * |namespace |${(0, utils2_1.f1)(cache.namespace[nodeId.namespace].namespaceUri)}|`);
653
+ f.write(` * |nodeClass |${(0, utils2_1.f1)(node_opcua_data_model_1.NodeClass[nodeClass])}|`);
654
+ f.write(` * |typedDefinition |${(0, utils2_1.f1)(browseName.toString() + " " + nodeId.toString())}|`);
655
+ if (nodeClass === node_opcua_data_model_1.NodeClass.VariableType) {
656
+ f.write(` * |dataType |${(0, utils2_1.f1)(node_opcua_variant_1.DataType[dataType])}|`);
657
+ f.write(` * |dataType Name |${(0, utils2_1.f1)(dataTypeName + " " + (dataTypeNodeId === null || dataTypeNodeId === void 0 ? void 0 : dataTypeNodeId.toString()))}|`);
658
+ }
659
+ f.write(` * |isAbstract |${(0, utils2_1.f1)(isAbstract.toString())}|`);
660
+ f.write(` */`);
661
+ // if (interfaceName.name === "UAOperationLimits") {
662
+ // debugger;
663
+ // }
664
+ cache.ensureImported(baseInterfaceName);
665
+ cache.ensureImported(Object.assign(Object.assign({}, baseInterfaceName), { name: baseInterfaceName.name + `${baseExtension}` }));
666
+ const ch = calculateChevrons(classDef);
667
+ {
668
+ if (nodeClass === node_opcua_data_model_1.NodeClass.VariableType) {
669
+ if (classDef.dataTypeImport) {
670
+ if (doDebug) {
671
+ console.log(chalk.red(" ----------------> ", classDef.browseName));
672
+ classDef.dataTypeImport.forEach((a) => {
673
+ console.log(chalk.red(" ------------------------> ", a.module, a.name, a.namespace));
674
+ });
675
+ }
676
+ classDef.dataTypeImport.forEach(cache.ensureImported.bind(cache));
677
+ }
678
+ cache.referenceBasicType("DataType");
679
+ const chevrons = calculateChevrons(classDef);
680
+ const chevronsBase = calculateChevrons(classDef.baseClassDef, classDef);
681
+ // Shall we cache.ensureImported({ ...baseInterfaceName!, module: dataTypeName, name: dataTypeName });
682
+ const classBaseName = `${interfaceName.name}${baseExtension}${chevrons.chevronsDef}`;
683
+ if (countExposedChildren(members) === 0 && (baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name) !== "UAVariableT") {
684
+ //
685
+ const baseName = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${baseExtension}${chevronsBase.chevronsExtend}`;
686
+ f.write(`export type ${classBaseName} = ${baseName};`);
687
+ }
688
+ else {
689
+ if ((baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name) === "UAVariableT") {
690
+ f.write(`export interface ${classBaseName} {`);
691
+ }
692
+ else {
693
+ const baseName = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${baseExtension}${chevronsBase.chevronsExtend}`;
694
+ f.write(`export interface ${classBaseName} extends ${baseName} {`);
695
+ }
696
+ yield dumpChildren(" ", members, f, cache);
697
+ f.write(`}`);
698
+ }
699
+ }
700
+ else {
701
+ const classBaseName = `${interfaceName.name}${baseExtension}`;
702
+ if (countExposedChildren(members) === 0 && (baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name) !== "UAObject") {
703
+ const baseName = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${baseExtension}`;
704
+ f.write(`export type ${classBaseName} = ${baseName};`);
705
+ }
706
+ else {
707
+ if ((baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name) === "UAObject") {
708
+ f.write(`export interface ${classBaseName} {`);
709
+ }
710
+ else {
711
+ const baseName = `${baseInterfaceName === null || baseInterfaceName === void 0 ? void 0 : baseInterfaceName.name}${baseExtension}`;
712
+ f.write(`export interface ${classBaseName} extends ${baseName} {`);
713
+ }
714
+ yield dumpChildren(" ", members, f, cache);
715
+ f.write(`}`);
716
+ }
717
+ }
718
+ }
719
+ const baseStuff = getBaseClassWithOmit(classDef);
720
+ if (nodeClass === node_opcua_data_model_1.NodeClass.VariableType) {
721
+ cache.referenceBasicType("DataType");
722
+ // if dataType is a extension object we can simpligy by forcing DT
723
+ const className = `${interfaceName.name}${ch.chevronsDef}`;
724
+ const c = isUnspecifiedDataType(dataType) ? `<T, DT${mB}>` : `<T${mC}>`;
725
+ const classBaseName = `${interfaceName.name}${baseExtension}${c}`;
726
+ f.write(`export interface ${className} extends ${baseStuff}, ${classBaseName} {`);
727
+ }
728
+ else {
729
+ const className = `${interfaceName.name}`;
730
+ const classBaseName = `${interfaceName.name}${baseExtension}`;
731
+ f.write(`export interface ${className} extends ${baseStuff}, ${classBaseName} {`);
732
+ }
733
+ f.write(`}`);
734
+ return { type: "ua", content: f.toString(), typeName: interfaceName.name };
735
+ });
736
+ }
737
+ exports._convertTypeToTypescript = _convertTypeToTypescript;
738
+ function convertTypeToTypescript(session, nodeId, options, cache, f) {
739
+ return __awaiter(this, void 0, void 0, function* () {
740
+ f = new node_opcua_utils_1.LineFile();
741
+ cache = cache || (yield (0, cache_1.constructCache)(session, options));
742
+ const { type, content, typeName } = yield _convertTypeToTypescript(session, nodeId, cache);
743
+ f.write(`// ----- this file has been automatically generated - do not edit`);
744
+ f.write(dumpUsedExport(typeName, nodeId.namespace, cache));
745
+ f.write(content);
746
+ const folder = cache.namespace[nodeId.namespace].sourceFolder;
747
+ const module = cache.namespace[nodeId.namespace].module;
748
+ const filename = (0, to_filename_1.toFilename)(typeName);
749
+ const dependencies = findUsedImport(nodeId.namespace, cache);
750
+ return { type, content: f.toString(), folder, module, filename, dependencies };
751
+ });
752
+ }
753
+ exports.convertTypeToTypescript = convertTypeToTypescript;
754
754
  //# sourceMappingURL=convert_to_typescript.js.map