node-opcua-generator 2.177.0 → 2.179.0

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.
@@ -2,9 +2,6 @@
2
2
  * @module node-opcua-generator
3
3
  */
4
4
 
5
- // tslint:disable:max-line-length
6
- // tslint:disable:max-depth
7
-
8
5
  /* c8 ignore start */
9
6
 
10
7
  import fs from "node:fs";
@@ -38,23 +35,21 @@ function makeFieldType(field: FieldType) {
38
35
  return `{${field.fieldType}${field.isArray ? "[" : ""}${field.isArray ? "]" : ""}}`;
39
36
  }
40
37
 
41
- function _convertToJavascriptCode(obj: any): string {
38
+ function _convertToJavascriptCode(obj: unknown): string {
42
39
  const lines: string[] = [];
43
40
 
44
- if (typeof obj === "object" && !(obj instanceof Array)) {
41
+ if (typeof obj === "object" && obj !== null && !Array.isArray(obj)) {
45
42
  lines.push("{");
46
43
  for (const prop of Object.keys(obj)) {
47
- lines.push(prop, ": ", _convertToJavascriptCode(obj[prop]), ",");
44
+ lines.push(prop, ": ", _convertToJavascriptCode((obj as Record<string, unknown>)[prop]), ",");
48
45
  }
49
46
  lines.push("}");
50
- } else if (obj instanceof Array) {
47
+ } else if (Array.isArray(obj)) {
51
48
  lines.push("[");
52
49
  for (const prop of obj) {
53
50
  lines.push(_convertToJavascriptCode(prop), ",");
54
51
  }
55
52
  lines.push("]");
56
-
57
- // tslint:disable:no-empty
58
53
  } else if (typeof obj === "function") {
59
54
  /** */
60
55
  } else {
@@ -64,7 +59,7 @@ function _convertToJavascriptCode(obj: any): string {
64
59
  }
65
60
 
66
61
  function get_class_folder(_schemaName: string, optionalFolder?: string): string {
67
- let folder : string ="";
62
+ let folder: string = "";
68
63
  if (optionalFolder) {
69
64
  if (!fs.existsSync(optionalFolder)) {
70
65
  fs.mkdirSync(optionalFolder);
@@ -142,10 +137,10 @@ function write_enumeration_setter(write: WriteFunc, _schema: IStructuredTypeSche
142
137
 
143
138
  function write_enumeration_fast_init(
144
139
  write: WriteFunc,
145
- schema: IStructuredTypeSchema,
140
+ _schema: IStructuredTypeSchema,
146
141
  field: FieldType,
147
142
  member: string,
148
- i: number
143
+ _i: number
149
144
  ): void {
150
145
  if (field.isArray) {
151
146
  write(` this.${member} = [];`);
@@ -154,7 +149,7 @@ function write_enumeration_fast_init(
154
149
  write(` this.${member} = 0 as ${field.fieldType};`);
155
150
  }
156
151
 
157
- function write_enumeration(write: WriteFunc, schema: IStructuredTypeSchema, field: FieldType, member: string, i: number): void {
152
+ function write_enumeration(write: WriteFunc, _schema: IStructuredTypeSchema, field: FieldType, member: string, i: number): void {
158
153
  const capMember = capitalizeFirstLetter(member);
159
154
  if (field.isArray) {
160
155
  write(
@@ -165,7 +160,7 @@ function write_enumeration(write: WriteFunc, schema: IStructuredTypeSchema, fiel
165
160
  write(` this.${field.name} = this.set${capMember}(initialize_field(schema.fields[${i}], options?.${field.name}));`);
166
161
  }
167
162
 
168
- function write_complex_fast_init(write: WriteFunc, schema: IStructuredTypeSchema, field: FieldType, member: string) {
163
+ function write_complex_fast_init(write: WriteFunc, _schema: IStructuredTypeSchema, field: FieldType, member: string) {
169
164
  if (field.isArray) {
170
165
  write(` this.${member} = null; /* null array */`);
171
166
  } else {
@@ -175,7 +170,7 @@ function write_complex_fast_init(write: WriteFunc, schema: IStructuredTypeSchema
175
170
 
176
171
  function write_complex(write: WriteFunc, schema: IStructuredTypeSchema, field: FieldType, member: string /*, i*/) {
177
172
  if (field.isArray) {
178
- if (Object.prototype.hasOwnProperty.call(field, "defaultValue")) {
173
+ if (Object.hasOwn(field, "defaultValue")) {
179
174
  // todo: fix me => should call field defaultValue in the live version
180
175
  write(` this.${member} = []; // should default`);
181
176
  } else {
@@ -197,7 +192,7 @@ function write_complex(write: WriteFunc, schema: IStructuredTypeSchema, field: F
197
192
  }
198
193
  }
199
194
 
200
- function write_basic(write: WriteFunc, schema: IStructuredTypeSchema, field: FieldType, member: string, i: number): void {
195
+ function write_basic(write: WriteFunc, _schema: IStructuredTypeSchema, field: FieldType, member: string, i: number): void {
201
196
  assert(field.category === FieldCategory.basic);
202
197
 
203
198
  if (field.isArray) {
@@ -211,7 +206,13 @@ function write_basic(write: WriteFunc, schema: IStructuredTypeSchema, field: Fie
211
206
  write(` this.${member} = initialize_field(schema.fields[${i}], options?.${field.name});`);
212
207
  }
213
208
  }
214
- function write_basic_fast_init(write: WriteFunc, schema: IStructuredTypeSchema, field: FieldType, member: string, i: number): void {
209
+ function write_basic_fast_init(
210
+ write: WriteFunc,
211
+ _schema: IStructuredTypeSchema,
212
+ field: FieldType,
213
+ member: string,
214
+ _i: number
215
+ ): void {
215
216
  if (field.isArray) {
216
217
  // write(`this.${member} = [];`);
217
218
  // write(`if (options.${member}) {`);
@@ -304,20 +305,20 @@ function write_constructor(write: WriteFunc, schema: IStructuredTypeSchema): voi
304
305
  if (schema.documentation && schema.documentation.length > 0) {
305
306
  write(` * ${schema.documentation}`);
306
307
  }
307
- let def = "";
308
+ let _def = "";
308
309
  for (let i = 0; i < n; i++) {
309
310
  const field = schema.fields[i];
310
- const fieldType = field.fieldType;
311
- const documentation = field.documentation ? field.documentation : "";
312
- def = "";
311
+ const _fieldType = field.fieldType;
312
+ const _documentation = field.documentation ? field.documentation : "";
313
+ _def = "";
313
314
  if (field.defaultValue !== undefined) {
314
315
  if (typeof field.defaultValue === "function") {
315
- def = ` = ${field.defaultValue()}`;
316
+ _def = ` = ${field.defaultValue()}`;
316
317
  } else {
317
- def = ` = ${field.defaultValue}`;
318
+ _def = ` = ${field.defaultValue}`;
318
319
  }
319
320
  }
320
- const ft = makeFieldType(field);
321
+ const _ft = makeFieldType(field);
321
322
  // xx write(" * @param [options." + field.name + def + "] " + ft + " " + documentation);
322
323
  }
323
324
 
@@ -380,7 +381,7 @@ function write_constructor(write: WriteFunc, schema: IStructuredTypeSchema): voi
380
381
  write(` * @type ${makeFieldType(field)}`);
381
382
  }
382
383
  if (field.defaultValue !== undefined && typeof field.defaultValue !== "function") {
383
- write(" * @default ", field.defaultValue);
384
+ write(" * @default ", String(field.defaultValue));
384
385
  }
385
386
 
386
387
  write(" */");
@@ -401,9 +402,9 @@ function write_constructor(write: WriteFunc, schema: IStructuredTypeSchema): voi
401
402
  write(" }");
402
403
  }
403
404
 
404
- function write_possible_fields(write: WriteFunc, className: string, possibleFields: string[]): void {
405
+ function write_possible_fields(write: WriteFunc, _className: string, possibleFields: string[]): void {
405
406
  write(" public static possibleFields: string[] = [");
406
- write(` ${possibleFields.map(quotify).join("," + os.EOL + " ")}`);
407
+ write(` ${possibleFields.map(quotify).join(`,${os.EOL} `)}`);
407
408
  write(" ];");
408
409
  }
409
410
 
@@ -535,7 +536,7 @@ function write_decode(write: WriteFunc, schema: IStructuredTypeSchema): void {
535
536
  const n = schema.fields.length;
536
537
  for (let i = 0; i < n; i++) {
537
538
  const field = schema.fields[i];
538
- const fieldType = field.fieldType;
539
+ const _fieldType = field.fieldType;
539
540
  const member = field.name;
540
541
  write_field(field, member, i);
541
542
  }
@@ -552,7 +553,7 @@ function hasEnumeration(schema: IStructuredTypeSchema): boolean {
552
553
  return false;
553
554
  }
554
555
 
555
- function hasComplex(schema: IStructuredTypeSchema): boolean {
556
+ function _hasComplex(schema: IStructuredTypeSchema): boolean {
556
557
  for (const field of schema.fields) {
557
558
  if (field.category === FieldCategory.complex) {
558
559
  return true;
@@ -665,7 +666,7 @@ function write_expose_encoder_decoder(write: WriteFunc, schema: IStructuredTypeS
665
666
  write('import { ExpandedNodeId, NodeId } from "node-opcua-nodeid";');
666
667
 
667
668
  const n = schema.fields.length;
668
- const done: any = {};
669
+ const done: Record<string, FieldType> = {};
669
670
  for (let i = 0; i < n; i++) {
670
671
  const field = schema.fields[i];
671
672
  const fieldType = field.fieldType;
@@ -678,33 +679,33 @@ function write_expose_encoder_decoder(write: WriteFunc, schema: IStructuredTypeS
678
679
  write(`const _enumeration${field.fieldType} = getEnumeration("${field.fieldType}");`);
679
680
  write(
680
681
  "const encode" +
681
- field.fieldType +
682
- ': (value: any, stream: OutputBinaryStream) => void = getEnumeration("' +
683
- field.fieldType +
684
- '").encode;'
682
+ field.fieldType +
683
+ ': (value: any, stream: OutputBinaryStream) => void = getEnumeration("' +
684
+ field.fieldType +
685
+ '").encode;'
685
686
  );
686
687
  write(
687
688
  "const decode" +
688
- field.fieldType +
689
- ': (stream: BinaryStream) => void = getEnumeration("' +
690
- field.fieldType +
691
- '").decode;'
689
+ field.fieldType +
690
+ ': (stream: BinaryStream) => void = getEnumeration("' +
691
+ field.fieldType +
692
+ '").decode;'
692
693
  );
693
694
  break;
694
695
  case FieldCategory.complex:
695
696
  write(
696
697
  "const encode" +
697
- field.fieldType +
698
- ': (value: any, stream: OutputBinaryStream) => void = getBuiltInType("' +
699
- field.fieldType +
700
- '").encode;'
698
+ field.fieldType +
699
+ ': (value: any, stream: OutputBinaryStream) => void = getBuiltInType("' +
700
+ field.fieldType +
701
+ '").encode;'
701
702
  );
702
703
  write(
703
704
  "const decode" +
704
- field.fieldType +
705
- ': (stream: BinaryStream) => void = getBuiltInType("' +
706
- field.fieldType +
707
- '").decode;'
705
+ field.fieldType +
706
+ ': (stream: BinaryStream) => void = getBuiltInType("' +
707
+ field.fieldType +
708
+ '").decode;'
708
709
  );
709
710
  break;
710
711
  }
@@ -721,16 +722,13 @@ export function writeStructuredType(write: WriteFunc, schema: IStructuredTypeSch
721
722
  const encodingXmlNodeId = getEncodingXmlId(schema);
722
723
  const encodingJsonNodeId = getEncodingJsonId(schema);
723
724
 
724
-
725
725
  // ----------------------------------------------- Options
726
726
  if (baseClass === "BaseUAObject" || baseClass === "ExtensionObject" || baseClass === "DataTypeDefinition") {
727
727
  write(`export interface ${className}Options {`);
728
728
  } else {
729
729
  write(`export interface ${className}Options extends ${baseClass}Options {`);
730
730
  }
731
- {
732
- write_class_constructor_options(write, schema);
733
- }
731
+ write_class_constructor_options(write, schema);
734
732
  write(`}`);
735
733
 
736
734
  write(`export class ${className} extends ${baseClass} {`);
@@ -791,7 +789,7 @@ export function writeStructuredType(write: WriteFunc, schema: IStructuredTypeSch
791
789
  write(`${className}.schema.encodingDefaultJson = ${className}.encodingDefaultJson;`);
792
790
  }
793
791
 
794
- const needRegistration = true;// encodingBinaryNodeId.value !== 0;
792
+ const needRegistration = true; // encodingBinaryNodeId.value !== 0;
795
793
 
796
794
  if (needRegistration) {
797
795
  write(`registerClassDefinition( ${className}.dataTypeNodeId, "${className}", ${className});`);
@@ -800,23 +798,23 @@ export function writeStructuredType(write: WriteFunc, schema: IStructuredTypeSch
800
798
 
801
799
  function getDataTypeNodeId(schema: IStructuredTypeSchema): NodeId {
802
800
  const className = schema.name;
803
- const encodingBinaryId = (DataTypeIds as any)[className];
801
+ const encodingBinaryId = (DataTypeIds as unknown as Record<string, number>)[className];
804
802
  return coerceNodeId(encodingBinaryId);
805
803
  }
806
804
  function getEncodingBinaryId(schema: IStructuredTypeSchema): NodeId {
807
805
  const className = schema.name;
808
- const encodingBinaryId = (ObjectIds as any)[className + "_Encoding_DefaultBinary"];
806
+ const encodingBinaryId = (ObjectIds as unknown as Record<string, number>)[`${className}_Encoding_DefaultBinary`];
809
807
  return coerceNodeId(encodingBinaryId);
810
808
  }
811
809
 
812
810
  function getEncodingXmlId(schema: IStructuredTypeSchema): NodeId {
813
811
  const className = schema.name;
814
- const encodingXmlId = (ObjectIds as any)[className + "_Encoding_DefaultXml"];
812
+ const encodingXmlId = (ObjectIds as unknown as Record<string, number>)[`${className}_Encoding_DefaultXml`];
815
813
  return coerceNodeId(encodingXmlId);
816
814
  }
817
815
  function getEncodingJsonId(schema: IStructuredTypeSchema): NodeId {
818
816
  const className = schema.name;
819
- const encodingXmlId = (ObjectIds as any)[className + "_Encoding_DefaultJson"];
817
+ const encodingXmlId = (ObjectIds as unknown as Record<string, number>)[`${className}_Encoding_DefaultJson`];
820
818
  return coerceNodeId(encodingXmlId);
821
819
  }
822
820
 
@@ -881,7 +879,7 @@ export function produce_TScript_code(
881
879
  // -------------------------------------------------------------------------
882
880
  // - insert definition of complex type used by this class
883
881
  // -------------------------------------------------------------------------
884
- const tmpMap: any = {};
882
+ const tmpMap: Record<string, number> = {};
885
883
  for (const field of complexTypes) {
886
884
  if (Object.hasOwn(tmpMap, field.fieldType)) {
887
885
  continue;
@@ -1,13 +1,12 @@
1
- /* eslint-disable max-statements */
2
1
  /* c8 ignore start */
3
2
  /**
4
3
  * @module node-opcua-generator
5
4
  */
6
- // tslint:disable:max-line-length
7
- // tslint:disable:no-inner-declarations
8
5
  //
9
- import fs from "fs";
6
+
7
+ import fs from "node:fs";
10
8
  import chalk from "chalk";
9
+
11
10
  // node 14 onward : import { readFile } from "fs/promises";
12
11
  const { readFile } = fs.promises;
13
12
 
@@ -17,14 +16,14 @@ import { assert } from "node-opcua-assert";
17
16
  import { DataTypeIds, ObjectIds } from "node-opcua-constants";
18
17
  import { checkDebugFlag, make_debugLog } from "node-opcua-debug";
19
18
  import {
20
- EnumerationDefinitionSchema,
19
+ DataTypeFactory,
20
+ type EnumerationDefinitionSchema,
21
21
  FieldCategory,
22
22
  getStandardDataTypeFactory,
23
- DataTypeFactory,
24
- IStructuredTypeSchema
23
+ type IStructuredTypeSchema
25
24
  } from "node-opcua-factory";
26
25
  import { NodeId } from "node-opcua-nodeid";
27
- import { DataTypeAndEncodingId, MapDataTypeAndEncodingIdProvider, parseBinaryXSD } from "node-opcua-schemas";
26
+ import { type DataTypeAndEncodingId, type MapDataTypeAndEncodingIdProvider, parseBinaryXSD } from "node-opcua-schemas";
28
27
 
29
28
  import { writeStructuredType } from "./factory_code_generator";
30
29
  import { LineFile1 } from "./utils/line_file";
@@ -37,7 +36,6 @@ const f = new LineFile1();
37
36
 
38
37
  const write = makeWrite(f);
39
38
 
40
- // eslint-disable-next-line max-statements
41
39
  function writeEnumeratedType(enumerationSchema: EnumerationDefinitionSchema): void {
42
40
  const arrayValues = Object.keys(enumerationSchema.enumValues)
43
41
  .filter((a: string) => a.match("[0-9]+"))
@@ -52,9 +50,11 @@ function writeEnumeratedType(enumerationSchema: EnumerationDefinitionSchema): vo
52
50
  const maxEnumValue = Math.max.apply(null, arrayValues);
53
51
 
54
52
  // make sure there is a Invalid key in the enum => else insert one (but only if not flaggable)
55
- const hasInvalid = Object.prototype.hasOwnProperty.call(enumerationSchema.enumValues, "Invalid");
53
+ const hasInvalid = Object.hasOwn(enumerationSchema.enumValues, "Invalid");
56
54
  if (!hasInvalid && !isFlaggable) {
57
- enumerationSchema.enumValues[(enumerationSchema.enumValues.Invalid = 0xffffffff)] = "Invalid";
55
+ // bidirectional enum map: name -> value and value -> name
56
+ enumerationSchema.enumValues.Invalid = 0xffffffff;
57
+ enumerationSchema.enumValues[0xffffffff] = "Invalid";
58
58
  }
59
59
 
60
60
  write("");
@@ -63,7 +63,7 @@ function writeEnumeratedType(enumerationSchema: EnumerationDefinitionSchema): vo
63
63
 
64
64
  const str = [];
65
65
 
66
- const values = Object.keys(enumerationSchema.enumValues).filter((a: any) => a.match(/^[0-9]+$/));
66
+ const values = Object.keys(enumerationSchema.enumValues).filter((a: string) => a.match(/^[0-9]+$/));
67
67
 
68
68
  for (const value of values) {
69
69
  str.push(` ${enumerationSchema.enumValues[value]} = ${value}`);
@@ -143,10 +143,10 @@ export async function generate(filename: string, generatedTypescriptFilename: st
143
143
 
144
144
  const idProvider: MapDataTypeAndEncodingIdProvider = {
145
145
  getDataTypeAndEncodingId(name: string): DataTypeAndEncodingId | null {
146
- const dataType = (DataTypeIds as any)[name] || 0;
147
- const binEncoding = (ObjectIds as any)[name + "_Encoding_DefaultBinary"] || 0;
148
- const xmlEncoding = (ObjectIds as any)[name + "_Encoding_DefaultXml"] || 0;
149
- const jsonEncoding = (ObjectIds as any)[name + "_Encoding_DefaultJson"] || 0;
146
+ const dataType = (DataTypeIds as unknown as Record<string, number>)[name] || 0;
147
+ const binEncoding = (ObjectIds as unknown as Record<string, number>)[`${name}_Encoding_DefaultBinary`] || 0;
148
+ const xmlEncoding = (ObjectIds as unknown as Record<string, number>)[`${name}_Encoding_DefaultXml`] || 0;
149
+ const jsonEncoding = (ObjectIds as unknown as Record<string, number>)[`${name}_Encoding_DefaultJson`] || 0;
150
150
  if (dataType === undefined) {
151
151
  return null;
152
152
  }
@@ -269,30 +269,29 @@ import {
269
269
  // write(`}`);
270
270
  // write(``);
271
271
 
272
- const alreadyDone: { [key: string]: any } = {};
273
- /* tslint:disable:no-string-literal */
274
- alreadyDone["ExtensionObject"] = true;
275
- alreadyDone["NodeId"] = true;
276
-
277
- alreadyDone["ExpandedNodeId"] = true;
278
- alreadyDone["Variant"] = true;
279
- alreadyDone["XmlElement"] = true;
280
-
281
- alreadyDone["NodeIdType"] = true;
282
- alreadyDone["TwoByteNodeId"] = true;
283
- alreadyDone["FourByteNodeId"] = true;
284
- alreadyDone["NumericNodeId"] = true;
285
- alreadyDone["StringNodeId"] = true;
286
- alreadyDone["GuidNodeId"] = true;
287
- alreadyDone["ByteStringNodeId"] = true;
288
-
289
- alreadyDone["DiagnosticInfo"] = true;
290
- alreadyDone["Variant"] = true;
291
- alreadyDone["DataValue"] = true;
292
- alreadyDone["LocalizedText"] = true;
293
- alreadyDone["QualifiedName"] = true;
294
- alreadyDone["BrowseDirection"] = true;
295
- alreadyDone["TimestampsToReturn"] = true;
272
+ const alreadyDone: { [key: string]: boolean | EnumerationDefinitionSchema | IStructuredTypeSchema } = {};
273
+ alreadyDone.ExtensionObject = true;
274
+ alreadyDone.NodeId = true;
275
+
276
+ alreadyDone.ExpandedNodeId = true;
277
+ alreadyDone.Variant = true;
278
+ alreadyDone.XmlElement = true;
279
+
280
+ alreadyDone.NodeIdType = true;
281
+ alreadyDone.TwoByteNodeId = true;
282
+ alreadyDone.FourByteNodeId = true;
283
+ alreadyDone.NumericNodeId = true;
284
+ alreadyDone.StringNodeId = true;
285
+ alreadyDone.GuidNodeId = true;
286
+ alreadyDone.ByteStringNodeId = true;
287
+
288
+ alreadyDone.DiagnosticInfo = true;
289
+ alreadyDone.Variant = true;
290
+ alreadyDone.DataValue = true;
291
+ alreadyDone.LocalizedText = true;
292
+ alreadyDone.QualifiedName = true;
293
+ alreadyDone.BrowseDirection = true;
294
+ alreadyDone.TimestampsToReturn = true;
296
295
 
297
296
  function processEnumeratedType(enumerationSchema: EnumerationDefinitionSchema): void {
298
297
  if (alreadyDone[enumerationSchema.name]) {
@@ -318,7 +317,10 @@ import {
318
317
  processStructuredType(fieldSchema);
319
318
  }
320
319
  if (field.category === FieldCategory.enumeration) {
321
- const fieldSchema = dataTypeFactory.getEnumeration(field.fieldType)!;
320
+ const fieldSchema = dataTypeFactory.getEnumeration(field.fieldType);
321
+ if (!fieldSchema) {
322
+ throw new Error(`Cannot find enumeration ${field.fieldType}`);
323
+ }
322
324
  processEnumeratedType(fieldSchema);
323
325
  }
324
326
  }
@@ -352,4 +354,4 @@ import {
352
354
  */
353
355
  });
354
356
  }
355
- /* c8 ignore stop */
357
+ /* c8 ignore stop */
@@ -2,18 +2,16 @@
2
2
  /**
3
3
  * @module node-opcua-generator
4
4
  */
5
- // tslint:disable:max-line-length
6
- // tslint:disable:no-console
7
- import fs from "fs";
5
+ import fs from "node:fs";
6
+
8
7
  // node 14 onward : import { mkdir } from "fs/promises";
9
8
  const { mkdir } = fs.promises;
10
9
 
11
- import path from "path";
12
- import ts from "typescript";
13
-
10
+ import path from "node:path";
14
11
  import { assert } from "node-opcua-assert";
15
12
  import { checkDebugFlag, make_debugLog } from "node-opcua-debug";
16
- import { ConstructorFunc } from "node-opcua-factory";
13
+ import type { ConstructorFunc } from "node-opcua-factory";
14
+ import ts from "typescript";
17
15
 
18
16
  import { get_class_TScript_filename, produce_TScript_code } from "./factory_code_generator";
19
17
 
@@ -25,7 +23,7 @@ doDebug;
25
23
  * @module opcua.miscellaneous
26
24
  */
27
25
 
28
- function compileTScriptCode(typescriptFilename: string): string {
26
+ function _compileTScriptCode(typescriptFilename: string): string {
29
27
  const content = fs.readFileSync(typescriptFilename, "utf-8");
30
28
 
31
29
  const compilerOptions = {
@@ -48,11 +46,14 @@ function compileTScriptCode(typescriptFilename: string): string {
48
46
  const sourceMapFilename = typescriptFilename.replace(/\.ts$/, ".js.map");
49
47
 
50
48
  fs.writeFileSync(javascriptFilename, res1.outputText, "utf-8");
51
- fs.writeFileSync(sourceMapFilename, res1.sourceMapText!, "utf-8");
49
+ if (!res1.sourceMapText) {
50
+ throw new Error("transpileModule did not produce a source map (sourceMap: true is set above)");
51
+ }
52
+ fs.writeFileSync(sourceMapFilename, res1.sourceMapText, "utf-8");
52
53
 
53
54
  return res1.outputText;
54
55
  }
55
- compileTScriptCode;
56
+ _compileTScriptCode;
56
57
 
57
58
  export const verbose = false;
58
59
 
@@ -81,7 +82,7 @@ function get_caller_source_filename() {
81
82
  }
82
83
 
83
84
  export async function generateCode(schemaName: string, localSchemaFile: string, generatedCodeFolder?: string): Promise<void> {
84
- const schemaTypescriptFile = schemaName + "_Schema.ts";
85
+ const schemaTypescriptFile = `${schemaName}_Schema.ts`;
85
86
 
86
87
  const currentFolder = process.cwd();
87
88
  //
@@ -100,7 +101,7 @@ export async function generateCode(schemaName: string, localSchemaFile: string,
100
101
  await mkdir(generatedCodeFolder);
101
102
  }
102
103
 
103
- const generatedTypescriptSource = path.join(generatedCodeFolder, "_" + schemaName + ".ts");
104
+ const generatedTypescriptSource = path.join(generatedCodeFolder, `_${schemaName}.ts`);
104
105
 
105
106
  const generatedSourceExists = fs.existsSync(generatedTypescriptSource);
106
107
 
@@ -119,7 +120,7 @@ export async function generateCode(schemaName: string, localSchemaFile: string,
119
120
  codeGeneratorScript = path.join(__dirname, "factory_code_generator.js");
120
121
  }
121
122
 
122
- assert(fs.existsSync(codeGeneratorScript), "cannot get code factory_code_generator" + codeGeneratorScript);
123
+ assert(fs.existsSync(codeGeneratorScript), `cannot get code factory_code_generator${codeGeneratorScript}`);
123
124
  const codeGeneratorScriptMtime = new Date(fs.statSync(codeGeneratorScript).mtime).getTime();
124
125
 
125
126
  codeGeneratorIsNewer = generatedSourceMtime <= codeGeneratorScriptMtime;
@@ -128,24 +129,25 @@ export async function generateCode(schemaName: string, localSchemaFile: string,
128
129
 
129
130
  if (generatedSourceIsOutdated) {
130
131
  const module = await import(localSchemaFile);
131
- const schema = module[schemaName + "_Schema"];
132
+ const schema = module[`${schemaName}_Schema`];
132
133
 
133
134
  if (!schema) {
134
135
  throw new Error(`module must export a Schema with name ${schemaName}_Schema in ${generatedTypescriptSource}`);
135
136
  }
136
137
 
137
- debugLog(" generated_source_is_outdated ", schemaName, " to ", generatedTypescriptSource);
138
+ // c8 ignore next
139
+ doDebug && debugLog(" generated_source_is_outdated ", schemaName, " to ", generatedTypescriptSource);
138
140
  if (exports.verbose) {
139
141
  console.log(" generating ", schemaName, " in ", generatedTypescriptSource);
140
142
  }
141
- const localSchemaFile1 = path.join("../schemas", schemaName + "_schema");
143
+ const localSchemaFile1 = path.join("../schemas", `${schemaName}_schema`);
142
144
  produce_TScript_code(schema, localSchemaFile1, generatedTypescriptSource);
143
145
  }
144
146
  }
145
147
 
146
148
  export async function generateTypeScriptCodeFromSchema(schemaName: string): Promise<void> {
147
149
  const currentFolder = process.cwd();
148
- const schemaFilename = path.join(currentFolder, "schemas", schemaName + "_schema.ts");
150
+ const schemaFilename = path.join(currentFolder, "schemas", `${schemaName}_schema.ts`);
149
151
  const generatedCodeFolder = path.join(process.cwd(), "_generated_");
150
152
  await generateCode(schemaName, schemaFilename, generatedCodeFolder);
151
153
  }
@@ -169,11 +171,11 @@ export async function registerObject(schema: string, generateCodeFolder?: string
169
171
  const folderHint = hintSchema[0];
170
172
  schema = hintSchema[1];
171
173
 
172
- const schemaName = schema + "_Schema";
173
- const schemaFile = path.join(folderHint, schema + "_schema.ts");
174
+ const schemaName = `${schema}_Schema`;
175
+ const schemaFile = path.join(folderHint, `${schema}_schema.ts`);
174
176
  const module = await import(schemaFile);
175
177
  if (!module) {
176
- throw new Error("cannot find " + schemaFile);
178
+ throw new Error(`cannot find ${schemaFile}`);
177
179
  }
178
180
 
179
181
  await generateCode(schemaName, schemaFile, generateCodeFolder);
package/source/index.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  /**
3
3
  * @module node-opcua-generator
4
4
  */
5
- export { generateTypeScriptCodeFromSchema, registerObject, unregisterObject } from "./generator";
5
+
6
6
  export { generate } from "./generate_extension_object_code";
7
+ export { generateTypeScriptCodeFromSchema, registerObject, unregisterObject } from "./generator";
7
8
  /* c8 ignore stop */
@@ -2,7 +2,6 @@
2
2
  /**
3
3
  * @module node-opcua-generator
4
4
  */
5
- // tslint:disable:no-console
6
5
  import { generateTypeScriptCodeFromSchema } from "./generator";
7
6
 
8
7
  console.log(process.argv);
@@ -1,13 +1,13 @@
1
1
  /**
2
2
  * @module node-opcua-utils
3
3
  */
4
- import { writeFileSync } from "fs";
4
+ import { writeFileSync } from "node:fs";
5
5
  import { LineFile } from "node-opcua-utils";
6
6
 
7
7
  export class LineFile1 extends LineFile {
8
8
  constructor() {
9
9
  super();
10
- this.write("// --------- This code has been automatically generated !!! " + new Date().toISOString());
10
+ this.write(`// --------- This code has been automatically generated !!! ${new Date().toISOString()}`);
11
11
  this.write("/**");
12
12
  this.write(" * @module node-opcua-types");
13
13
  this.write(" */");
@@ -1,4 +1,4 @@
1
- import path from "path";
1
+ import path from "node:path";
2
2
  // ---------------------------------------------------------------------------------------------------------------------
3
3
  /**
4
4
 
@@ -13,7 +13,7 @@ export function normalize_require_file(baseFolder: string, fullPathToFile: strin
13
13
  let localFile = path.relative(baseFolder, fullPathToFile).replace(/\\/g, "/");
14
14
  // append ./ if necessary
15
15
  if (localFile.substring(0, 1) !== ".") {
16
- localFile = "./" + localFile;
16
+ localFile = `./${localFile}`;
17
17
  }
18
18
  // remove extension
19
19
  localFile = localFile.substring(0, localFile.length - path.extname(localFile).length);
@@ -1,4 +1,4 @@
1
- import { LineFile1 } from "./line_file";
1
+ import type { LineFile1 } from "./line_file";
2
2
 
3
3
  export interface WriteFunc {
4
4
  (...args: string[]): void;