Skip to content

Commit

Permalink
add .path support so we can use Bun.file as stdin/stdout/stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Jan 27, 2024
1 parent 1accffa commit f5f71e5
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/bun.js/api/bun/subprocess.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3223,22 +3223,43 @@ pub const Subprocess = struct {
.data = .{ .fd = bun.uvfdcast(_fd) },
},
.path => |pathlike| {
_ = pathlike;
@panic("TODO");
// var file_path: [bun.MAX_PATH_BYTES]u8 = undefined;
// const path_fd = try bun.sys.open(
// pathlike.path.sliceZ(&file_path),
// std.os.O.WRONLY | std.os.O.CREAT | std.os.O.NONBLOCK,
// 0o664,
// ).unwrap();

// try pipe.init(uv.Loop.get(), false).unwrap();
// try pipe.open(bun.uvfdcast(path_fd)).unwrap();

// return uv.uv_stdio_container_s{
// .flags = @intCast(uv.UV_INHERIT_STREAM),
// .data = .{ .stream = @ptrCast(pipe) },
// };
var path_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
var resolver = bun.path.PosixToWinNormalizer{};
const flag: i32 = (if (isReadable) os.O.RDONLY else std.os.O.WRONLY);

var joined_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
var slice = pathlike.sliceZ(&path_buf);
if (!std.fs.path.isAbsoluteWindows(slice)) {
// we need the absolute path so we can open
var cwd_buf: [bun.MAX_PATH_BYTES + 1]u8 = undefined;

var parts = [_]string{
slice,
};

const application_cwd = try bun.getcwd(&cwd_buf);
cwd_buf[application_cwd.len] = std.fs.path.sep;
const file_path = bun.path.joinAbsStringBuf(
cwd_buf[0 .. application_cwd.len + 1],
&joined_buf,
&parts,
.auto,
);

joined_buf[file_path.len] = 0;
slice = joined_buf[0..file_path.len :0];
}

const path_fd = try bun.sys.open(
try resolver.resolveCWDZ(slice),
flag | std.os.O.CREAT | std.os.O.NONBLOCK,
0o664,
).unwrap();

return uv.uv_stdio_container_s{
.flags = uv.UV_INHERIT_FD,
.data = .{ .fd = bun.uvfdcast(path_fd) },
};
},
.inherit => uv.uv_stdio_container_s{
.flags = uv.UV_INHERIT_FD,
Expand Down

0 comments on commit f5f71e5

Please sign in to comment.