@itwin/ecschema-locaters 4.0.0-dev.48 → 4.0.0-dev.51

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.
Files changed (31) hide show
  1. package/lib/cjs/SchemaFileLocater.d.ts +82 -82
  2. package/lib/cjs/SchemaFileLocater.js +212 -212
  3. package/lib/cjs/SchemaFileLocater.js.map +1 -1
  4. package/lib/cjs/SchemaFileUtility.d.ts +17 -17
  5. package/lib/cjs/SchemaFileUtility.js +48 -48
  6. package/lib/cjs/SchemaFileUtility.js.map +1 -1
  7. package/lib/cjs/SchemaJsonFileLocater.d.ts +33 -33
  8. package/lib/cjs/SchemaJsonFileLocater.js +90 -90
  9. package/lib/cjs/SchemaJsonFileLocater.js.map +1 -1
  10. package/lib/cjs/SchemaXmlFileLocater.d.ts +33 -33
  11. package/lib/cjs/SchemaXmlFileLocater.js +93 -93
  12. package/lib/cjs/SchemaXmlFileLocater.js.map +1 -1
  13. package/lib/cjs/StubSchemaXmlFileLocater.d.ts +69 -69
  14. package/lib/cjs/StubSchemaXmlFileLocater.js +177 -177
  15. package/lib/cjs/StubSchemaXmlFileLocater.js.map +1 -1
  16. package/lib/cjs/ecschema-locaters.d.ts +17 -17
  17. package/lib/cjs/ecschema-locaters.js +33 -33
  18. package/lib/cjs/ecschema-locaters.js.map +1 -1
  19. package/lib/cjs/test/SchemaFileUtility.test.d.ts +1 -1
  20. package/lib/cjs/test/SchemaFileUtility.test.js +56 -56
  21. package/lib/cjs/test/SchemaFileUtility.test.js.map +1 -1
  22. package/lib/cjs/test/SchemaJsonFileLocator.test.d.ts +1 -1
  23. package/lib/cjs/test/SchemaJsonFileLocator.test.js +119 -119
  24. package/lib/cjs/test/SchemaJsonFileLocator.test.js.map +1 -1
  25. package/lib/cjs/test/SchemaXmlFileLocator.test.d.ts +1 -1
  26. package/lib/cjs/test/SchemaXmlFileLocator.test.js +180 -180
  27. package/lib/cjs/test/SchemaXmlFileLocator.test.js.map +1 -1
  28. package/lib/cjs/test/StubSchemaXmlFileLocater.test.d.ts +1 -1
  29. package/lib/cjs/test/StubSchemaXmlFileLocater.test.js +170 -170
  30. package/lib/cjs/test/StubSchemaXmlFileLocater.test.js.map +1 -1
  31. package/package.json +4 -4
@@ -1,83 +1,83 @@
1
- import { Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
2
- /**
3
- * A SchemaKey implementation that aids in identifying Schema files via the
4
- * addition of two properties: fileName and schemaText. The fileName contains the
5
- * full path to the file on disk and schemaText is the full string representation
6
- * of the Schema.
7
- * @beta
8
- */
9
- export declare class FileSchemaKey extends SchemaKey {
10
- fileName: string;
11
- schemaText?: string;
12
- /**
13
- * Initializes a new FileSchemaKey object.
14
- * @param key The EC SchemaKey identifying the Schema.
15
- * @param fileName The full path to the Schema file.
16
- * @param schemaText The string representation of the Schema
17
- * loaded from disk. Optional.
18
- */
19
- constructor(key: SchemaKey, fileName: string, schemaJson?: string);
20
- }
21
- /**
22
- * Abstract class to hold common/overlapping functionality between SchemaJsonFileLocater and SchemaXmlFileLocater
23
- * @beta - Needs further testing and possibly moved to a separate package.
24
- */
25
- export declare abstract class SchemaFileLocater {
26
- searchPaths: string[];
27
- constructor();
28
- readUtf8FileToString(filePath: string): Promise<string | undefined>;
29
- readUtf8FileToStringSync(filePath: string): string | undefined;
30
- fileExists(filePath: string): Promise<boolean | undefined>;
31
- fileExistsSync(filePath: string): boolean | undefined;
32
- /**
33
- * Adds more search paths used by this locator to find the
34
- * Schema files.
35
- * @param schemaPaths An array of search paths to add
36
- */
37
- addSchemaSearchPaths(schemaPaths: string[]): void;
38
- /**
39
- * Add one search path used by this locator to find the
40
- * Schema files.
41
- * @param schemaPath A search path to add
42
- */
43
- addSchemaSearchPath(schemaPath: string): void;
44
- protected abstract getSchemaKey(data: string): SchemaKey;
45
- /**
46
- * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey. This method
47
- * only attempts to find schema files that have no version in the file name.
48
- * @param foundFiles The collection of SchemaKeys found in the given directory.
49
- * @param schemaPath The directory in which to search for the Schemas.
50
- * @param schemaName The short name of the Schema (without version).
51
- * @param desiredKey The SchemaKey used to find matching Schema files.
52
- * @param matchType The SchemaMatchType to use when comparing the desiredKey and the keys found during the search.
53
- * @param format The type of file that the schema key refers to. json or xml
54
- */
55
- private addCandidateNoExtSchemaKey;
56
- /**
57
- * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey
58
- * @param foundFiles The collection of SchemaKeys found in the given directory
59
- * @param schemaPath The directory in which to search for the Schemas
60
- * @param fileFilter The file filter, potentially with wildcards, used to locate the Schema files.
61
- * @param desiredKey The schemaKey used to find matching Schema files
62
- * @param matchType The SchemaMatchType to use when comparing the desired Key and the keys found during the search.
63
- * @param format The type of file that the schema key refers to. json or xml
64
- */
65
- private addCandidateSchemaKeys;
66
- /**
67
- * Attempts to find all Schema files in the configurable search paths that match
68
- * the desired SchemaKey.
69
- * @param desiredKey The SchemaKey to match.
70
- * @param matchType The SchemaMatchType.
71
- * @param format The type of file that the schema key refers to. json or xml
72
- */
73
- protected findEligibleSchemaKeys(desiredKey: SchemaKey, matchType: SchemaMatchType, format: string): FileSchemaKey[];
74
- abstract getSchema<T extends Schema>(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<T | undefined>;
75
- /**
76
- * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
77
- * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
78
- * @param lhs The 'left-hand' FileSchemaKey.
79
- * @param rhs The 'right-hand' FileSchemaKey.
80
- */
81
- compareSchemaKeyByVersion: (lhs: FileSchemaKey, rhs: FileSchemaKey) => number;
82
- }
1
+ import { Schema, SchemaContext, SchemaKey, SchemaMatchType } from "@itwin/ecschema-metadata";
2
+ /**
3
+ * A SchemaKey implementation that aids in identifying Schema files via the
4
+ * addition of two properties: fileName and schemaText. The fileName contains the
5
+ * full path to the file on disk and schemaText is the full string representation
6
+ * of the Schema.
7
+ * @beta
8
+ */
9
+ export declare class FileSchemaKey extends SchemaKey {
10
+ fileName: string;
11
+ schemaText?: string;
12
+ /**
13
+ * Initializes a new FileSchemaKey object.
14
+ * @param key The EC SchemaKey identifying the Schema.
15
+ * @param fileName The full path to the Schema file.
16
+ * @param schemaText The string representation of the Schema
17
+ * loaded from disk. Optional.
18
+ */
19
+ constructor(key: SchemaKey, fileName: string, schemaJson?: string);
20
+ }
21
+ /**
22
+ * Abstract class to hold common/overlapping functionality between SchemaJsonFileLocater and SchemaXmlFileLocater
23
+ * @beta - Needs further testing and possibly moved to a separate package.
24
+ */
25
+ export declare abstract class SchemaFileLocater {
26
+ searchPaths: string[];
27
+ constructor();
28
+ readUtf8FileToString(filePath: string): Promise<string | undefined>;
29
+ readUtf8FileToStringSync(filePath: string): string | undefined;
30
+ fileExists(filePath: string): Promise<boolean | undefined>;
31
+ fileExistsSync(filePath: string): boolean | undefined;
32
+ /**
33
+ * Adds more search paths used by this locator to find the
34
+ * Schema files.
35
+ * @param schemaPaths An array of search paths to add
36
+ */
37
+ addSchemaSearchPaths(schemaPaths: string[]): void;
38
+ /**
39
+ * Add one search path used by this locator to find the
40
+ * Schema files.
41
+ * @param schemaPath A search path to add
42
+ */
43
+ addSchemaSearchPath(schemaPath: string): void;
44
+ protected abstract getSchemaKey(data: string): SchemaKey;
45
+ /**
46
+ * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey. This method
47
+ * only attempts to find schema files that have no version in the file name.
48
+ * @param foundFiles The collection of SchemaKeys found in the given directory.
49
+ * @param schemaPath The directory in which to search for the Schemas.
50
+ * @param schemaName The short name of the Schema (without version).
51
+ * @param desiredKey The SchemaKey used to find matching Schema files.
52
+ * @param matchType The SchemaMatchType to use when comparing the desiredKey and the keys found during the search.
53
+ * @param format The type of file that the schema key refers to. json or xml
54
+ */
55
+ private addCandidateNoExtSchemaKey;
56
+ /**
57
+ * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey
58
+ * @param foundFiles The collection of SchemaKeys found in the given directory
59
+ * @param schemaPath The directory in which to search for the Schemas
60
+ * @param fileFilter The file filter, potentially with wildcards, used to locate the Schema files.
61
+ * @param desiredKey The schemaKey used to find matching Schema files
62
+ * @param matchType The SchemaMatchType to use when comparing the desired Key and the keys found during the search.
63
+ * @param format The type of file that the schema key refers to. json or xml
64
+ */
65
+ private addCandidateSchemaKeys;
66
+ /**
67
+ * Attempts to find all Schema files in the configurable search paths that match
68
+ * the desired SchemaKey.
69
+ * @param desiredKey The SchemaKey to match.
70
+ * @param matchType The SchemaMatchType.
71
+ * @param format The type of file that the schema key refers to. json or xml
72
+ */
73
+ protected findEligibleSchemaKeys(desiredKey: SchemaKey, matchType: SchemaMatchType, format: string): FileSchemaKey[];
74
+ abstract getSchema<T extends Schema>(key: SchemaKey, matchType: SchemaMatchType, context: SchemaContext): Promise<T | undefined>;
75
+ /**
76
+ * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
77
+ * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
78
+ * @param lhs The 'left-hand' FileSchemaKey.
79
+ * @param rhs The 'right-hand' FileSchemaKey.
80
+ */
81
+ compareSchemaKeyByVersion: (lhs: FileSchemaKey, rhs: FileSchemaKey) => number;
82
+ }
83
83
  //# sourceMappingURL=SchemaFileLocater.d.ts.map
@@ -1,213 +1,213 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.SchemaFileLocater = exports.FileSchemaKey = void 0;
8
- const fs = require("fs");
9
- const glob = require("glob");
10
- const path = require("path");
11
- const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
12
- /** @packageDocumentation
13
- * @module Locaters
14
- */
15
- const formatString = (format, ...args) => {
16
- return format.replace(/{(\d+)}/g, (match, theNumber) => {
17
- return typeof args[theNumber] !== "undefined"
18
- ? args[theNumber]
19
- : match;
20
- });
21
- };
22
- const padStartEx = (str, targetLength, padString) => {
23
- targetLength = targetLength >> 0; // truncate if number or convert non-number to 0;
24
- padString = String((typeof padString !== "undefined" ? padString : " "));
25
- if (str.length > targetLength) {
26
- return String(str);
27
- }
28
- else {
29
- targetLength = targetLength - str.length;
30
- if (targetLength > padString.length) {
31
- padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed
32
- }
33
- return padString.slice(0, targetLength) + str;
34
- }
35
- };
36
- /**
37
- * A SchemaKey implementation that aids in identifying Schema files via the
38
- * addition of two properties: fileName and schemaText. The fileName contains the
39
- * full path to the file on disk and schemaText is the full string representation
40
- * of the Schema.
41
- * @beta
42
- */
43
- class FileSchemaKey extends ecschema_metadata_1.SchemaKey {
44
- /**
45
- * Initializes a new FileSchemaKey object.
46
- * @param key The EC SchemaKey identifying the Schema.
47
- * @param fileName The full path to the Schema file.
48
- * @param schemaText The string representation of the Schema
49
- * loaded from disk. Optional.
50
- */
51
- constructor(key, fileName, schemaJson) {
52
- super(key.name, key.version);
53
- this.fileName = fileName;
54
- this.schemaText = schemaJson;
55
- }
56
- }
57
- exports.FileSchemaKey = FileSchemaKey;
58
- /**
59
- * Abstract class to hold common/overlapping functionality between SchemaJsonFileLocater and SchemaXmlFileLocater
60
- * @beta - Needs further testing and possibly moved to a separate package.
61
- */
62
- class SchemaFileLocater {
63
- constructor() {
64
- /**
65
- * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
66
- * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
67
- * @param lhs The 'left-hand' FileSchemaKey.
68
- * @param rhs The 'right-hand' FileSchemaKey.
69
- */
70
- this.compareSchemaKeyByVersion = (lhs, rhs) => {
71
- return lhs.compareByVersion(rhs);
72
- };
73
- this.searchPaths = [];
74
- }
75
- async readUtf8FileToString(filePath) {
76
- return new Promise((resolve, reject) => {
77
- fs.readFile(filePath, "utf-8", (err, data) => {
78
- if (err)
79
- reject(err);
80
- else
81
- resolve(data);
82
- });
83
- });
84
- }
85
- readUtf8FileToStringSync(filePath) {
86
- return fs.readFileSync(filePath, "utf-8");
87
- }
88
- async fileExists(filePath) {
89
- return new Promise((resolve) => {
90
- fs.access(filePath, fs.constants.F_OK, (err) => {
91
- resolve(err ? false : true);
92
- });
93
- });
94
- }
95
- fileExistsSync(filePath) {
96
- return fs.existsSync(filePath);
97
- }
98
- /**
99
- * Adds more search paths used by this locator to find the
100
- * Schema files.
101
- * @param schemaPaths An array of search paths to add
102
- */
103
- addSchemaSearchPaths(schemaPaths) {
104
- // If the path is not in the schemaPaths array, add it
105
- for (const schemaPath of schemaPaths)
106
- this.addSchemaSearchPath(schemaPath);
107
- }
108
- /**
109
- * Add one search path used by this locator to find the
110
- * Schema files.
111
- * @param schemaPath A search path to add
112
- */
113
- addSchemaSearchPath(schemaPath) {
114
- // If the path is not in the schemaPaths array, add it
115
- if (!this.searchPaths.find((entry) => entry === schemaPath))
116
- this.searchPaths.push(schemaPath);
117
- }
118
- /**
119
- * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey. This method
120
- * only attempts to find schema files that have no version in the file name.
121
- * @param foundFiles The collection of SchemaKeys found in the given directory.
122
- * @param schemaPath The directory in which to search for the Schemas.
123
- * @param schemaName The short name of the Schema (without version).
124
- * @param desiredKey The SchemaKey used to find matching Schema files.
125
- * @param matchType The SchemaMatchType to use when comparing the desiredKey and the keys found during the search.
126
- * @param format The type of file that the schema key refers to. json or xml
127
- */
128
- addCandidateNoExtSchemaKey(foundFiles, schemaPath, schemaName, desiredKey, matchType, format) {
129
- const fullPath = path.join(schemaPath, `${schemaName}.ecschema.${format}`);
130
- // If the file does not exist, end
131
- if (!fs.existsSync(fullPath))
132
- return;
133
- // Read the file
134
- const file = fs.readFileSync(fullPath);
135
- if (!file)
136
- return;
137
- // Get the schema key
138
- const key = this.getSchemaKey(file.toString());
139
- // If the key matches, put it in foundFiles
140
- if (key.matches(desiredKey, matchType))
141
- foundFiles.push(new FileSchemaKey(key, fullPath, file.toString()));
142
- }
143
- /**
144
- * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey
145
- * @param foundFiles The collection of SchemaKeys found in the given directory
146
- * @param schemaPath The directory in which to search for the Schemas
147
- * @param fileFilter The file filter, potentially with wildcards, used to locate the Schema files.
148
- * @param desiredKey The schemaKey used to find matching Schema files
149
- * @param matchType The SchemaMatchType to use when comparing the desired Key and the keys found during the search.
150
- * @param format The type of file that the schema key refers to. json or xml
151
- */
152
- addCandidateSchemaKeys(foundFiles, schemaPath, fileFilter, desiredKey, matchType, format) {
153
- const fullPath = path.join(schemaPath, fileFilter);
154
- const result = new glob.GlobSync(fullPath, { sync: true });
155
- for (const match of result.found) {
156
- let fileName = path.basename(match, (`.ecschema.${format}`));
157
- // TODO: should this be moved or handled elsewhere?
158
- // Handles two version file names - SchemaKey.parseString supports only 3 version names.
159
- if (/[^\d]\.\d?\d\.\d?\d$/.test(fileName)) {
160
- const parts = fileName.split(".");
161
- parts.splice(2, 0, "00");
162
- fileName = parts.join(".");
163
- }
164
- const file = fs.readFileSync(match);
165
- if (!file)
166
- continue;
167
- const schemaKey = ecschema_metadata_1.SchemaKey.parseString(fileName);
168
- if (schemaKey.matches(desiredKey, matchType))
169
- foundFiles.push(new FileSchemaKey(schemaKey, match, file.toString()));
170
- }
171
- }
172
- /**
173
- * Attempts to find all Schema files in the configurable search paths that match
174
- * the desired SchemaKey.
175
- * @param desiredKey The SchemaKey to match.
176
- * @param matchType The SchemaMatchType.
177
- * @param format The type of file that the schema key refers to. json or xml
178
- */
179
- findEligibleSchemaKeys(desiredKey, matchType, format) {
180
- const foundFiles = new Array();
181
- let twoVersionSuffix;
182
- let threeVersionSuffix;
183
- const readVersion = desiredKey.readVersion.toString();
184
- const writeVersion = desiredKey.writeVersion.toString();
185
- const minorVersion = desiredKey.minorVersion.toString();
186
- if (matchType === ecschema_metadata_1.SchemaMatchType.Latest) {
187
- twoVersionSuffix = (`.*.*.ecschema.${format}`);
188
- threeVersionSuffix = (`.*.*.*.ecschema.${format}`);
189
- }
190
- else if (matchType === ecschema_metadata_1.SchemaMatchType.LatestWriteCompatible) {
191
- twoVersionSuffix = formatString(`.{0}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
192
- threeVersionSuffix = formatString(`.{0}.{1}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"));
193
- }
194
- else if (matchType === ecschema_metadata_1.SchemaMatchType.LatestReadCompatible) {
195
- twoVersionSuffix = formatString(`.{0}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
196
- threeVersionSuffix = formatString(`.{0}.*.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
197
- }
198
- else {
199
- twoVersionSuffix = formatString(`.{0}.{1}.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"));
200
- threeVersionSuffix = formatString(`.{0}.{1}.{2}.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"), padStartEx(minorVersion, 2, "0"));
201
- }
202
- const twoVersionExpression = desiredKey.name + twoVersionSuffix;
203
- const threeVersionExpression = desiredKey.name + threeVersionSuffix;
204
- for (const searchPath of this.searchPaths) {
205
- this.addCandidateNoExtSchemaKey(foundFiles, searchPath, desiredKey.name, desiredKey, matchType, format);
206
- this.addCandidateSchemaKeys(foundFiles, searchPath, twoVersionExpression, desiredKey, matchType, format);
207
- this.addCandidateSchemaKeys(foundFiles, searchPath, threeVersionExpression, desiredKey, matchType, format);
208
- }
209
- return foundFiles;
210
- }
211
- }
212
- exports.SchemaFileLocater = SchemaFileLocater;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.SchemaFileLocater = exports.FileSchemaKey = void 0;
8
+ const fs = require("fs");
9
+ const glob = require("glob");
10
+ const path = require("path");
11
+ const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
12
+ /** @packageDocumentation
13
+ * @module Locaters
14
+ */
15
+ const formatString = (format, ...args) => {
16
+ return format.replace(/{(\d+)}/g, (match, theNumber) => {
17
+ return typeof args[theNumber] !== "undefined"
18
+ ? args[theNumber]
19
+ : match;
20
+ });
21
+ };
22
+ const padStartEx = (str, targetLength, padString) => {
23
+ targetLength = targetLength >> 0; // truncate if number or convert non-number to 0;
24
+ padString = String((typeof padString !== "undefined" ? padString : " "));
25
+ if (str.length > targetLength) {
26
+ return String(str);
27
+ }
28
+ else {
29
+ targetLength = targetLength - str.length;
30
+ if (targetLength > padString.length) {
31
+ padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed
32
+ }
33
+ return padString.slice(0, targetLength) + str;
34
+ }
35
+ };
36
+ /**
37
+ * A SchemaKey implementation that aids in identifying Schema files via the
38
+ * addition of two properties: fileName and schemaText. The fileName contains the
39
+ * full path to the file on disk and schemaText is the full string representation
40
+ * of the Schema.
41
+ * @beta
42
+ */
43
+ class FileSchemaKey extends ecschema_metadata_1.SchemaKey {
44
+ /**
45
+ * Initializes a new FileSchemaKey object.
46
+ * @param key The EC SchemaKey identifying the Schema.
47
+ * @param fileName The full path to the Schema file.
48
+ * @param schemaText The string representation of the Schema
49
+ * loaded from disk. Optional.
50
+ */
51
+ constructor(key, fileName, schemaJson) {
52
+ super(key.name, key.version);
53
+ this.fileName = fileName;
54
+ this.schemaText = schemaJson;
55
+ }
56
+ }
57
+ exports.FileSchemaKey = FileSchemaKey;
58
+ /**
59
+ * Abstract class to hold common/overlapping functionality between SchemaJsonFileLocater and SchemaXmlFileLocater
60
+ * @beta - Needs further testing and possibly moved to a separate package.
61
+ */
62
+ class SchemaFileLocater {
63
+ constructor() {
64
+ /**
65
+ * Compares two Schema versions. If the left-hand version is greater, 1 is returned. If the
66
+ * left-hand version is less, -1 us returned. If the versions are an exact match, 0 is returned.
67
+ * @param lhs The 'left-hand' FileSchemaKey.
68
+ * @param rhs The 'right-hand' FileSchemaKey.
69
+ */
70
+ this.compareSchemaKeyByVersion = (lhs, rhs) => {
71
+ return lhs.compareByVersion(rhs);
72
+ };
73
+ this.searchPaths = [];
74
+ }
75
+ async readUtf8FileToString(filePath) {
76
+ return new Promise((resolve, reject) => {
77
+ fs.readFile(filePath, "utf-8", (err, data) => {
78
+ if (err)
79
+ reject(err);
80
+ else
81
+ resolve(data);
82
+ });
83
+ });
84
+ }
85
+ readUtf8FileToStringSync(filePath) {
86
+ return fs.readFileSync(filePath, "utf-8");
87
+ }
88
+ async fileExists(filePath) {
89
+ return new Promise((resolve) => {
90
+ fs.access(filePath, fs.constants.F_OK, (err) => {
91
+ resolve(err ? false : true);
92
+ });
93
+ });
94
+ }
95
+ fileExistsSync(filePath) {
96
+ return fs.existsSync(filePath);
97
+ }
98
+ /**
99
+ * Adds more search paths used by this locator to find the
100
+ * Schema files.
101
+ * @param schemaPaths An array of search paths to add
102
+ */
103
+ addSchemaSearchPaths(schemaPaths) {
104
+ // If the path is not in the schemaPaths array, add it
105
+ for (const schemaPath of schemaPaths)
106
+ this.addSchemaSearchPath(schemaPath);
107
+ }
108
+ /**
109
+ * Add one search path used by this locator to find the
110
+ * Schema files.
111
+ * @param schemaPath A search path to add
112
+ */
113
+ addSchemaSearchPath(schemaPath) {
114
+ // If the path is not in the schemaPaths array, add it
115
+ if (!this.searchPaths.find((entry) => entry === schemaPath))
116
+ this.searchPaths.push(schemaPath);
117
+ }
118
+ /**
119
+ * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey. This method
120
+ * only attempts to find schema files that have no version in the file name.
121
+ * @param foundFiles The collection of SchemaKeys found in the given directory.
122
+ * @param schemaPath The directory in which to search for the Schemas.
123
+ * @param schemaName The short name of the Schema (without version).
124
+ * @param desiredKey The SchemaKey used to find matching Schema files.
125
+ * @param matchType The SchemaMatchType to use when comparing the desiredKey and the keys found during the search.
126
+ * @param format The type of file that the schema key refers to. json or xml
127
+ */
128
+ addCandidateNoExtSchemaKey(foundFiles, schemaPath, schemaName, desiredKey, matchType, format) {
129
+ const fullPath = path.join(schemaPath, `${schemaName}.ecschema.${format}`);
130
+ // If the file does not exist, end
131
+ if (!fs.existsSync(fullPath))
132
+ return;
133
+ // Read the file
134
+ const file = fs.readFileSync(fullPath);
135
+ if (!file)
136
+ return;
137
+ // Get the schema key
138
+ const key = this.getSchemaKey(file.toString());
139
+ // If the key matches, put it in foundFiles
140
+ if (key.matches(desiredKey, matchType))
141
+ foundFiles.push(new FileSchemaKey(key, fullPath, file.toString()));
142
+ }
143
+ /**
144
+ * Adds SchemaKeys to the provided foundFiles collection that match the desired SchemaKey
145
+ * @param foundFiles The collection of SchemaKeys found in the given directory
146
+ * @param schemaPath The directory in which to search for the Schemas
147
+ * @param fileFilter The file filter, potentially with wildcards, used to locate the Schema files.
148
+ * @param desiredKey The schemaKey used to find matching Schema files
149
+ * @param matchType The SchemaMatchType to use when comparing the desired Key and the keys found during the search.
150
+ * @param format The type of file that the schema key refers to. json or xml
151
+ */
152
+ addCandidateSchemaKeys(foundFiles, schemaPath, fileFilter, desiredKey, matchType, format) {
153
+ const fullPath = path.join(schemaPath, fileFilter);
154
+ const result = new glob.GlobSync(fullPath, { sync: true });
155
+ for (const match of result.found) {
156
+ let fileName = path.basename(match, (`.ecschema.${format}`));
157
+ // TODO: should this be moved or handled elsewhere?
158
+ // Handles two version file names - SchemaKey.parseString supports only 3 version names.
159
+ if (/[^\d]\.\d?\d\.\d?\d$/.test(fileName)) {
160
+ const parts = fileName.split(".");
161
+ parts.splice(2, 0, "00");
162
+ fileName = parts.join(".");
163
+ }
164
+ const file = fs.readFileSync(match);
165
+ if (!file)
166
+ continue;
167
+ const schemaKey = ecschema_metadata_1.SchemaKey.parseString(fileName);
168
+ if (schemaKey.matches(desiredKey, matchType))
169
+ foundFiles.push(new FileSchemaKey(schemaKey, match, file.toString()));
170
+ }
171
+ }
172
+ /**
173
+ * Attempts to find all Schema files in the configurable search paths that match
174
+ * the desired SchemaKey.
175
+ * @param desiredKey The SchemaKey to match.
176
+ * @param matchType The SchemaMatchType.
177
+ * @param format The type of file that the schema key refers to. json or xml
178
+ */
179
+ findEligibleSchemaKeys(desiredKey, matchType, format) {
180
+ const foundFiles = new Array();
181
+ let twoVersionSuffix;
182
+ let threeVersionSuffix;
183
+ const readVersion = desiredKey.readVersion.toString();
184
+ const writeVersion = desiredKey.writeVersion.toString();
185
+ const minorVersion = desiredKey.minorVersion.toString();
186
+ if (matchType === ecschema_metadata_1.SchemaMatchType.Latest) {
187
+ twoVersionSuffix = (`.*.*.ecschema.${format}`);
188
+ threeVersionSuffix = (`.*.*.*.ecschema.${format}`);
189
+ }
190
+ else if (matchType === ecschema_metadata_1.SchemaMatchType.LatestWriteCompatible) {
191
+ twoVersionSuffix = formatString(`.{0}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
192
+ threeVersionSuffix = formatString(`.{0}.{1}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"));
193
+ }
194
+ else if (matchType === ecschema_metadata_1.SchemaMatchType.LatestReadCompatible) {
195
+ twoVersionSuffix = formatString(`.{0}.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
196
+ threeVersionSuffix = formatString(`.{0}.*.*.ecschema.${format}`, padStartEx(readVersion, 2, "0"));
197
+ }
198
+ else {
199
+ twoVersionSuffix = formatString(`.{0}.{1}.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"));
200
+ threeVersionSuffix = formatString(`.{0}.{1}.{2}.ecschema.${format}`, padStartEx(readVersion, 2, "0"), padStartEx(writeVersion, 2, "0"), padStartEx(minorVersion, 2, "0"));
201
+ }
202
+ const twoVersionExpression = desiredKey.name + twoVersionSuffix;
203
+ const threeVersionExpression = desiredKey.name + threeVersionSuffix;
204
+ for (const searchPath of this.searchPaths) {
205
+ this.addCandidateNoExtSchemaKey(foundFiles, searchPath, desiredKey.name, desiredKey, matchType, format);
206
+ this.addCandidateSchemaKeys(foundFiles, searchPath, twoVersionExpression, desiredKey, matchType, format);
207
+ this.addCandidateSchemaKeys(foundFiles, searchPath, threeVersionExpression, desiredKey, matchType, format);
208
+ }
209
+ return foundFiles;
210
+ }
211
+ }
212
+ exports.SchemaFileLocater = SchemaFileLocater;
213
213
  //# sourceMappingURL=SchemaFileLocater.js.map