improve shebang script writing

This commit is contained in:
Meghan Denny
2024-03-20 01:30:06 -07:00
parent 5332a17551
commit b3e53e1eb5
3 changed files with 46 additions and 22 deletions

View File

@@ -2592,22 +2592,30 @@ pub const PackageManager = struct {
};
defer node_gyp_file.close();
var bytes: string = switch (Environment.os) {
else => "#!/usr/bin/env node\nrequire(\"child_process\").spawnSync(\"bun\",[\"x\",\"node-gyp\",...process.argv.slice(2)],{stdio:\"inherit\"})",
.windows => "@node -e \"require('child_process').spawnSync('bun',['x','node-gyp',...process.argv.slice(2)],{stdio:'inherit'})\"",
const shebang = switch (Environment.os) {
.windows =>
\\0</* :{
\\ @echo off
\\ node %~f0 %*
\\ exit /b %errorlevel%
\\:} */0;
\\
,
else =>
\\#!/usr/bin/env node
\\
,
};
const content =
\\const child_process = require("child_process");
\\child_process.spawnSync("bun", ["x", "node-gyp", ...process.argv.slice(2)], { stdio: "inherit" });
\\
;
node_gyp_file.writeAll(shebang ++ content) catch |err| {
Output.prettyErrorln("<r><red>error<r>: <b><red>{s}<r> writing to " ++ file_name ++ " file", .{@errorName(err)});
Global.crash();
};
var index: usize = 0;
while (index < bytes.len) {
switch (bun.sys.write(bun.toFD(node_gyp_file.handle), bytes[index..])) {
.result => |written| {
index += written;
},
.err => |err| {
Output.prettyErrorln("<r><red>error<r>: <b><red>{s}<r> writing to " ++ file_name ++ " file", .{@tagName(err.getErrno())});
Global.crash();
},
}
}
// Add our node-gyp tempdir to the path
const existing_path = this.env.get("PATH") orelse "";

View File

@@ -4447,12 +4447,10 @@ test("it should be able to find binary in node_modules/.bin from parent director
await cp(join(packageDir, "bunfig.toml"), join(packageDir, "morePackageDir", "bunfig.toml"));
await await writeFile(
await writeShebangScript(
join(packageDir, "node_modules", ".bin", "missing-bin"),
`#!/usr/bin/env node
require("fs").writeFileSync("missing-bin.txt", "missing-bin@WHAT");
`,
{ mode: 0o777 },
"node",
`require("fs").writeFileSync("missing-bin.txt", "missing-bin@WHAT");`,
);
const { stdout, stderr, exited } = spawn({

View File

@@ -1,7 +1,7 @@
import { gc as bunGC, unsafe, which } from "bun";
import { describe, test, expect, afterAll, beforeAll } from "bun:test";
import { readlink, readFile } from "fs/promises";
import { isAbsolute } from "path";
import { readlink, readFile, writeFile } from "fs/promises";
import { isAbsolute, sep } from "path";
import { openSync, closeSync } from "node:fs";
export const isMacOS = process.platform === "darwin";
@@ -557,3 +557,21 @@ export function toTOMLString(opts: object) {
}
return ret;
}
const shebang_posix = (program: string) => `#!/usr/bin/env ${program}
`;
const shebang_windows = (program: string) => `0</* :{
@echo off
${program} %~f0 %*
exit /b %errorlevel%
:} */0;
`;
export function writeShebangScript(path: string, program: string, data: string) {
if (!isWindows) {
return writeFile(path, shebang_posix(program) + "\n" + data, { mode: 0o777 });
} else {
return writeFile(path + ".cmd", shebang_windows(program) + "\n" + data);
}
}