-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcli.ts
More file actions
80 lines (74 loc) · 2.4 KB
/
cli.ts
File metadata and controls
80 lines (74 loc) · 2.4 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
import fs from "node:fs";
import path from "node:path";
import { Command } from "@commander-js/extra-typings";
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
) {
console.log("Transforming", gypPath);
const gyp = readBindingFile(gypPath, disallowUnknownProperties);
const parentPath = path.dirname(gypPath);
const result = bindingGypToCmakeLists({
gyp,
projectName,
...restOfOptions,
});
const cmakeListsPath = path.join(parentPath, "CMakeLists.txt");
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 '/')")
.argument(
"[path]",
"Path to the binding.gyp file or directory to traverse recursively",
process.cwd()
)
.action((targetPath: string, { pathTransforms }) => {
const options: TransformOptions = {
unsupportedBehaviour: "throw",
disallowUnknownProperties: false,
transformWinPathsToPosix: pathTransforms,
};
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: ${path}`);
}
});