-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.ts
More file actions
105 lines (98 loc) · 3 KB
/
cli.ts
File metadata and controls
105 lines (98 loc) · 3 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import fs from "node:fs";
import path from "node:path";
import {
Command,
prettyPath,
wrapAction,
} from "@react-native-node-api/cli-utils";
import { readBindingFile } from "./gyp.js";
import {
bindingGypToCmakeLists,
type GypToCmakeListsOptions,
} from "./transformer.js";
export type TransformOptions = Omit<
GypToCmakeListsOptions,
"gyp" | "projectName"
> & {
disallowUnknownProperties: boolean;
projectName?: string;
};
export function generateProjectName(gypPath: string) {
return path.dirname(gypPath).replaceAll(path.sep, "-");
}
export function transformBindingGypFile(
gypPath: string,
{
disallowUnknownProperties,
projectName = generateProjectName(gypPath),
...restOfOptions
}: TransformOptions,
) {
const parentPath = path.dirname(gypPath);
const cmakeListsPath = path.join(parentPath, "CMakeLists.txt");
console.log(
`Transforming ${prettyPath(gypPath)} → ${prettyPath(cmakeListsPath)}`,
);
const gyp = readBindingFile(gypPath, disallowUnknownProperties);
const result = bindingGypToCmakeLists({
gyp,
projectName,
...restOfOptions,
});
fs.writeFileSync(cmakeListsPath, result, "utf-8");
}
export function transformBindingGypsRecursively(
directoryPath: string,
options: TransformOptions,
) {
const entries = fs.readdirSync(directoryPath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(directoryPath, entry.name);
if (entry.isDirectory()) {
transformBindingGypsRecursively(fullPath, options);
} else if (entry.isFile() && entry.name === "binding.gyp") {
transformBindingGypFile(fullPath, options);
}
}
}
export const program = new Command("gyp-to-cmake")
.description("Transform binding.gyp to CMakeLists.txt")
.option(
"--no-path-transforms",
"Don't transform output from command expansions (replacing '\\' with '/')",
)
.option("--weak-node-api", "Link against the weak-node-api library", false)
.option("--define-napi-version", "Define NAPI_VERSION for all targets", false)
.option("--cpp <version>", "C++ standard version", "17")
.argument(
"[path]",
"Path to the binding.gyp file or directory to traverse recursively",
process.cwd(),
)
.action(
wrapAction(
(
targetPath: string,
{ pathTransforms, cpp, defineNapiVersion, weakNodeApi },
) => {
const options: TransformOptions = {
unsupportedBehaviour: "throw",
disallowUnknownProperties: false,
transformWinPathsToPosix: pathTransforms,
compileFeatures: cpp ? [`cxx_std_${cpp}`] : [],
defineNapiVersion,
weakNodeApi,
};
const stat = fs.statSync(targetPath);
if (stat.isFile()) {
transformBindingGypFile(targetPath, options);
} else if (stat.isDirectory()) {
transformBindingGypsRecursively(targetPath, options);
} else {
throw new Error(
`Expected either a file or a directory: ${targetPath}`,
);
}
},
),
);