-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathheaders.ts
More file actions
62 lines (58 loc) · 1.81 KB
/
headers.ts
File metadata and controls
62 lines (58 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { createRequire } from "node:module";
import path from "node:path";
import fs from "node:fs";
import assert from "node:assert/strict";
const require = createRequire(import.meta.url);
/**
* @returns path of the directory containing the headers which provide the Node-API C API (node_api.h and js_native_api.h)
*/
export function getNodeApiHeadersPath(): string {
try {
const packagePath = path.dirname(
require.resolve("node-api-headers/package.json"),
);
const result = path.join(packagePath, "include");
const stat = fs.statSync(packagePath);
assert(stat.isDirectory(), `Expected ${packagePath} to be a directory`);
return result;
} catch (error) {
throw new Error(
`Failed resolve Node-API headers: Did you install the 'node-api-headers' package?`,
{
cause: error,
},
);
}
}
/**
* @returns path of the directory containing the headers which provide the Node-API C++ wrapper (napi.h)
*/
export function getNodeAddonHeadersPath(): string {
try {
const packagePath = path.dirname(
require.resolve("node-addon-api/package.json"),
);
return packagePath;
} catch (error) {
throw new Error(
`Failed resolve Node-API addon headers: Did you install the 'node-addon-api' package?`,
{
cause: error,
},
);
}
}
/**
* @returns A semicolon-separated list of include directories for Node-API headers.
* @note Using semicolon as a path separator for CMake regardless of platform
*/
export function getNodeApiIncludeDirectories(): string {
const includePaths = [getNodeApiHeadersPath(), getNodeAddonHeadersPath()];
for (const includePath of includePaths) {
assert(
!includePath.includes(";"),
`Include path with a ';' is not supported: ${includePath}`,
);
}
return includePaths.join(";");
}