Skip to content

Commit 2241cdb

Browse files
committed
Explicit clang-format dependency and prioritize brew
1 parent d1a9164 commit 2241cdb

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@tsconfig/node22": "^22.0.0",
6565
"@tsconfig/react-native": "3.0.6",
6666
"@types/node": "^22",
67+
"clang-format": "1.8.0",
6768
"depcheck": "^1.4.7",
6869
"eslint": "^9.32.0",
6970
"eslint-config-prettier": "^10.1.8",

packages/host/scripts/generate-injector.mts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from "node:assert/strict";
12
import fs from "node:fs";
23
import path from "node:path";
34
import cp from "node:child_process";
@@ -22,6 +23,32 @@ const IMPLEMENTED_RUNTIME_FUNCTIONS = [
2223
"napi_get_version",
2324
];
2425

26+
const { PATH } = process.env;
27+
const BREW_BIN_PATH = "/opt/homebrew/bin";
28+
29+
function getBrewPrioritizedPath() {
30+
if (process.platform === "darwin" && PATH && fs.existsSync(BREW_BIN_PATH)) {
31+
return [BREW_BIN_PATH, ...PATH.split(path.delimiter)].join(path.delimiter);
32+
} else {
33+
return PATH;
34+
}
35+
}
36+
37+
function clangFormat(filePath: string) {
38+
const { status, stderr = "No error output" } = cp.spawnSync(
39+
"clang-format",
40+
["-i", filePath],
41+
{
42+
encoding: "utf8",
43+
env: {
44+
...process.env,
45+
PATH: getBrewPrioritizedPath(),
46+
},
47+
},
48+
);
49+
assert.equal(status, 0, `Failed to format ${filePath}: ${stderr}`);
50+
}
51+
2552
/**
2653
* Generates source code which injects the Node API functions from the host.
2754
*/
@@ -80,7 +107,7 @@ async function run() {
80107
const source = generateSource(nodeApiFunctions);
81108
const sourcePath = path.join(CPP_SOURCE_PATH, "WeakNodeApiInjector.cpp");
82109
await fs.promises.writeFile(sourcePath, source, "utf-8");
83-
cp.spawnSync("clang-format", ["-i", sourcePath], { stdio: "inherit" });
110+
clangFormat(sourcePath);
84111
}
85112

86113
run().catch((err) => {

packages/weak-node-api/scripts/generate.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,32 @@ import * as hostGenerator from "./generators/NodeApiHost.js";
1313

1414
export const OUTPUT_PATH = path.join(import.meta.dirname, "../generated");
1515

16+
const { PATH } = process.env;
17+
const BREW_BIN_PATH = "/opt/homebrew/bin";
18+
19+
function getBrewPrioritizedPath() {
20+
if (process.platform === "darwin" && PATH && fs.existsSync(BREW_BIN_PATH)) {
21+
return [BREW_BIN_PATH, ...PATH.split(path.delimiter)].join(path.delimiter);
22+
} else {
23+
return PATH;
24+
}
25+
}
26+
27+
function clangFormat(filePath: string) {
28+
const { status, stderr = "No error output" } = cp.spawnSync(
29+
"clang-format",
30+
["-i", filePath],
31+
{
32+
encoding: "utf8",
33+
env: {
34+
...process.env,
35+
PATH: getBrewPrioritizedPath(),
36+
},
37+
},
38+
);
39+
assert.equal(status, 0, `Failed to format ${filePath}: ${stderr}`);
40+
}
41+
1642
type GenerateFileOptions = {
1743
functions: FunctionDecl[];
1844
fileName: string;
@@ -43,14 +69,7 @@ async function generateFile({
4369
`;
4470
const outputPath = path.join(OUTPUT_PATH, fileName);
4571
await fs.promises.writeFile(outputPath, output.trim(), "utf-8");
46-
const { status, stderr = "No error output" } = cp.spawnSync(
47-
"clang-format",
48-
["-i", outputPath],
49-
{
50-
encoding: "utf8",
51-
},
52-
);
53-
assert.equal(status, 0, `Failed to format ${fileName}: ${stderr}`);
72+
clangFormat(outputPath);
5473
}
5574

5675
async function run() {

0 commit comments

Comments
 (0)