Skip to content

Commit f979e54

Browse files
committed
Fix CLI native binding test global typing
1 parent 9f6d7ed commit f979e54

File tree

2 files changed

+40
-46
lines changed

2 files changed

+40
-46
lines changed

cli/env.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
/**
66
* CLI version injected at build time from package.json
77
*/
8-
declare const __CLI_VERSION__: string
8+
declare const __CLI_VERSION__: string;
99

1010
/**
1111
* CLI package name injected at build time from package.json
1212
*/
13-
declare const __CLI_PACKAGE_NAME__: string
13+
declare const __CLI_PACKAGE_NAME__: string;
1414

1515
/**
1616
* Kiro global powers registry JSON string injected at build time
1717
*/
18-
declare const __KIRO_GLOBAL_POWERS_REGISTRY__: string
18+
declare const __KIRO_GLOBAL_POWERS_REGISTRY__: string;
19+
20+
interface GlobalThis {
21+
__TNMSC_TEST_NATIVE_BINDING__?: object;
22+
}

cli/src/core/native-binding.ts

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,63 @@
1-
import {createRequire} from 'node:module'
2-
import process from 'node:process'
3-
4-
declare global {
5-
interface GlobalThis {
6-
__TNMSC_TEST_NATIVE_BINDING__?: object
7-
}
8-
}
1+
import { createRequire } from "node:module";
2+
import process from "node:process";
93

104
function shouldSkipNativeBinding(): boolean {
11-
if (process.env['TNMSC_FORCE_NATIVE_BINDING'] === '1') return false
12-
if (process.env['TNMSC_DISABLE_NATIVE_BINDING'] === '1') return true
5+
if (process.env["TNMSC_FORCE_NATIVE_BINDING"] === "1") return false;
6+
if (process.env["TNMSC_DISABLE_NATIVE_BINDING"] === "1") return true;
137

14-
return process.env['NODE_ENV'] === 'test'
15-
|| process.env['VITEST'] != null
16-
|| process.env['VITEST_WORKER_ID'] != null
8+
return process.env["NODE_ENV"] === "test" || process.env["VITEST"] != null || process.env["VITEST_WORKER_ID"] != null;
179
}
1810

1911
export function tryLoadNativeBinding<T extends object>(): T | undefined {
20-
const testBinding: unknown = globalThis.__TNMSC_TEST_NATIVE_BINDING__
21-
if (testBinding != null && typeof testBinding === 'object') return testBinding as T
22-
if (shouldSkipNativeBinding()) return void 0
12+
const testGlobals = globalThis as typeof globalThis & { __TNMSC_TEST_NATIVE_BINDING__?: object };
13+
const testBinding: unknown = testGlobals.__TNMSC_TEST_NATIVE_BINDING__;
14+
if (testBinding != null && typeof testBinding === "object") return testBinding as T;
15+
if (shouldSkipNativeBinding()) return void 0;
2316

2417
const suffixMap: Readonly<Record<string, string>> = {
25-
'win32-x64': 'win32-x64-msvc',
26-
'linux-x64': 'linux-x64-gnu',
27-
'linux-arm64': 'linux-arm64-gnu',
28-
'darwin-arm64': 'darwin-arm64',
29-
'darwin-x64': 'darwin-x64'
30-
}
31-
const suffix = suffixMap[`${process.platform}-${process.arch}`]
32-
if (suffix == null) return void 0
18+
"win32-x64": "win32-x64-msvc",
19+
"linux-x64": "linux-x64-gnu",
20+
"linux-arm64": "linux-arm64-gnu",
21+
"darwin-arm64": "darwin-arm64",
22+
"darwin-x64": "darwin-x64",
23+
};
24+
const suffix = suffixMap[`${process.platform}-${process.arch}`];
25+
if (suffix == null) return void 0;
3326

3427
try {
35-
const _require = createRequire(import.meta.url)
36-
const packageName = `@truenine/memory-sync-cli-${suffix}`
37-
const binaryFile = `napi-memory-sync-cli.${suffix}.node`
28+
const _require = createRequire(import.meta.url);
29+
const packageName = `@truenine/memory-sync-cli-${suffix}`;
30+
const binaryFile = `napi-memory-sync-cli.${suffix}.node`;
3831
const candidates = [
3932
packageName,
4033
`${packageName}/${binaryFile}`,
4134
`./${binaryFile}`,
4235
`../npm/${suffix}`,
4336
`../npm/${suffix}/${binaryFile}`,
4437
`../../npm/${suffix}`,
45-
`../../npm/${suffix}/${binaryFile}`
46-
]
38+
`../../npm/${suffix}/${binaryFile}`,
39+
];
4740

4841
for (const specifier of candidates) {
4942
try {
50-
const loaded = _require(specifier) as unknown
43+
const loaded = _require(specifier) as unknown;
5144
const possibleBindings = [
52-
(loaded as {config?: unknown})?.config,
53-
(loaded as {default?: {config?: unknown}})?.default?.config,
54-
(loaded as {default?: unknown})?.default,
55-
loaded
56-
]
45+
(loaded as { config?: unknown })?.config,
46+
(loaded as { default?: { config?: unknown } })?.default?.config,
47+
(loaded as { default?: unknown })?.default,
48+
loaded,
49+
];
5750

5851
for (const candidate of possibleBindings) {
59-
if (candidate != null && typeof candidate === 'object') return candidate as T
52+
if (candidate != null && typeof candidate === "object") return candidate as T;
6053
}
61-
}
62-
catch {}
54+
} catch {}
6355
}
64-
}
65-
catch {
66-
}
56+
} catch {}
6757

68-
return void 0
58+
return void 0;
6959
}
7060

7161
export function getNativeBinding<T extends object>(): T | undefined {
72-
return tryLoadNativeBinding<T>()
62+
return tryLoadNativeBinding<T>();
7363
}

0 commit comments

Comments
 (0)