@ya-modbus/driver-loader 0.7.2 → 0.8.1

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.
@@ -0,0 +1,43 @@
1
+ export interface PackageInfo {
2
+ version: string;
3
+ description: string;
4
+ }
5
+ type ReadTextFile = (path: string) => string;
6
+ /**
7
+ * Read the version and description from the closest package.json above a file
8
+ *
9
+ * package.json files without a version are skipped. This passes over the
10
+ * `{"type": "module"}` and `{"type": "commonjs"}` markers in `dist/esm` and `dist/cjs`.
11
+ *
12
+ * @param file - Path or `file:` URL of a module inside the package
13
+ * @param readFile - Reads a file as text and throws if it does not exist (replaceable in tests)
14
+ * @throws Error if no package.json with a version is found
15
+ */
16
+ export declare function readPackageInfo(file: string, readFile?: ReadTextFile): PackageInfo;
17
+ /**
18
+ * Pick the calling module from the file names of a stack, innermost first
19
+ *
20
+ * The first frame is this module. The caller is the first frame from another file that is a
21
+ * real module path or `file:` URL, which skips frames such as `node:internal/timers`.
22
+ *
23
+ * @throws Error if the stack has no such frame
24
+ */
25
+ export declare function findCallerFile(files: ReadonlyArray<string | undefined>): string;
26
+ /**
27
+ * Get the version and description of the package that contains the calling module
28
+ *
29
+ * Lets a CLI report its own package.json values without hardcoding them. The caller's
30
+ * location comes from the call stack, so it works the same in ES module and CommonJS
31
+ * builds, where `import.meta.url` and `__dirname` are each available in only one.
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * const { version } = getCallerPackageInfo()
36
+ * program.version(version)
37
+ * ```
38
+ *
39
+ * @throws Error if the calling module cannot be determined or has no package.json with a version
40
+ */
41
+ export declare function getCallerPackageInfo(): PackageInfo;
42
+ export {};
43
+ //# sourceMappingURL=package-info.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package-info.d.ts","sourceRoot":"","sources":["../../../src/package-info.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,EAAE,MAAM,CAAA;CACpB;AAwBD,KAAK,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAA;AAiB5C;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,YAA2B,GAAG,WAAW,CAehG;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,aAAa,CAAC,MAAM,GAAG,SAAS,CAAC,GAAG,MAAM,CAY/E;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,IAAI,WAAW,CAIlD"}
@@ -0,0 +1,96 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { dirname, isAbsolute, join } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ const MIN_STACK_TRACE_LIMIT = 10;
5
+ /**
6
+ * Call sites of the current stack, innermost first.
7
+ *
8
+ * V8 formats `stack` lazily, so it must be read while the formatter is replaced.
9
+ */
10
+ function getCallSites() {
11
+ // eslint-disable-next-line @typescript-eslint/unbound-method -- only restored, never called
12
+ const originalPrepare = Error.prepareStackTrace;
13
+ const originalLimit = Error.stackTraceLimit;
14
+ try {
15
+ Error.prepareStackTrace = (_error, callSites) => callSites;
16
+ // An application can lower the limit so far that the caller is not on the stack
17
+ Error.stackTraceLimit = Math.max(originalLimit, MIN_STACK_TRACE_LIMIT);
18
+ return new Error().stack;
19
+ }
20
+ finally {
21
+ Error.prepareStackTrace = originalPrepare;
22
+ Error.stackTraceLimit = originalLimit;
23
+ }
24
+ }
25
+ const readUtf8File = (path) => readFileSync(path, 'utf-8');
26
+ function readPackageJson(path, readFile) {
27
+ let content;
28
+ try {
29
+ content = readFile(path);
30
+ }
31
+ catch {
32
+ return undefined;
33
+ }
34
+ return JSON.parse(content);
35
+ }
36
+ /**
37
+ * Read the version and description from the closest package.json above a file
38
+ *
39
+ * package.json files without a version are skipped. This passes over the
40
+ * `{"type": "module"}` and `{"type": "commonjs"}` markers in `dist/esm` and `dist/cjs`.
41
+ *
42
+ * @param file - Path or `file:` URL of a module inside the package
43
+ * @param readFile - Reads a file as text and throws if it does not exist (replaceable in tests)
44
+ * @throws Error if no package.json with a version is found
45
+ */
46
+ export function readPackageInfo(file, readFile = readUtf8File) {
47
+ const path = file.startsWith('file:') ? fileURLToPath(file) : file;
48
+ for (let dir = dirname(path);; dir = dirname(dir)) {
49
+ const packageJson = readPackageJson(join(dir, 'package.json'), readFile);
50
+ if (typeof packageJson?.version === 'string') {
51
+ return {
52
+ version: packageJson.version,
53
+ description: typeof packageJson.description === 'string' ? packageJson.description : '',
54
+ };
55
+ }
56
+ if (dirname(dir) === dir) {
57
+ throw new Error(`No package.json with a version found for ${path}`);
58
+ }
59
+ }
60
+ }
61
+ /**
62
+ * Pick the calling module from the file names of a stack, innermost first
63
+ *
64
+ * The first frame is this module. The caller is the first frame from another file that is a
65
+ * real module path or `file:` URL, which skips frames such as `node:internal/timers`.
66
+ *
67
+ * @throws Error if the stack has no such frame
68
+ */
69
+ export function findCallerFile(files) {
70
+ const [ownFile] = files;
71
+ const callerFile = files.find((file) => file !== undefined && file !== ownFile && (file.startsWith('file:') || isAbsolute(file)));
72
+ if (callerFile === undefined) {
73
+ throw new Error('Cannot determine the calling module');
74
+ }
75
+ return callerFile;
76
+ }
77
+ /**
78
+ * Get the version and description of the package that contains the calling module
79
+ *
80
+ * Lets a CLI report its own package.json values without hardcoding them. The caller's
81
+ * location comes from the call stack, so it works the same in ES module and CommonJS
82
+ * builds, where `import.meta.url` and `__dirname` are each available in only one.
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * const { version } = getCallerPackageInfo()
87
+ * program.version(version)
88
+ * ```
89
+ *
90
+ * @throws Error if the calling module cannot be determined or has no package.json with a version
91
+ */
92
+ export function getCallerPackageInfo() {
93
+ const files = getCallSites().map((callSite) => callSite.getFileName() ?? undefined);
94
+ return readPackageInfo(findCallerFile(files));
95
+ }
96
+ //# sourceMappingURL=package-info.js.map
@@ -0,0 +1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