[bun run] Set more environment variables

This commit is contained in:
Jarred Sumner
2022-03-02 03:06:59 -08:00
parent c3b96c90d3
commit 4b36efd50a
2 changed files with 55 additions and 0 deletions

View File

@@ -775,6 +775,35 @@ pub const RunCommand = struct {
PATH = new_path.items;
}
this_bundler.env.loadNodeJSConfig(this_bundler.fs) catch {};
this_bundler.env.map.putDefault("npm_config_local_prefix", this_bundler.fs.top_level_dir) catch unreachable;
if (this_bundler.env.get("BUN_INSTALL")) |bun_install| {
this_bundler.env.map.putDefault("npm_config_prefix", bun_install) catch unreachable;
}
// we have no way of knowing what version they're expecting without running the node executable
// running the node executable is too slow
// so we will just hardcode it to LTS
this_bundler.env.map.putDefault(
"npm_config_user_agent",
// the use of npm/? is copying yarn
// e.g.
// > "yarn/1.22.4 npm/? node/v12.16.3 darwin x64",
"bun/" ++ Global.package_json_version ++ "npm/? node/v16.14.0 " ++ Global.os_name ++ " " ++ Global.arch_name,
) catch unreachable;
if (this_bundler.env.get("npm_execpath") == null) {
// we don't care if this fails
if (std.fs.selfExePathAlloc(ctx.allocator)) |self_exe_path| {
this_bundler.env.map.putDefault("npm_execpath", self_exe_path) catch unreachable;
if (strings.lastIndexOf(self_exe_path, std.fs.path.sep_str ++ "bin" ++ std.fs.path.sep_str)) |bin_dir_i| {
this_bundler.env.map.putDefault("npm_config_prefix", std.fs.path.dirname(self_exe_path[0..bin_dir_i]) orelse "/") catch unreachable;
} else {
this_bundler.env.map.putDefault("npm_config_prefix", std.fs.path.dirname(self_exe_path) orelse "/") catch unreachable;
}
} else |_| {}
}
var did_print = false;
if (root_dir_info.enclosing_package_json) |package_json| {
if (package_json.name.len > 0) {
@@ -783,6 +812,8 @@ pub const RunCommand = struct {
}
}
this_bundler.env.map.putDefault("npm_package_json", package_json.source.path.text) catch unreachable;
if (package_json.version.len > 0) {
if (this_bundler.env.map.get(NpmArgs.package_version) == null) {
this_bundler.env.map.put(NpmArgs.package_version, package_json.version) catch unreachable;

View File

@@ -15,6 +15,7 @@ const CodepointIterator = @import("./string_immutable.zig").CodepointIterator;
const Analytics = @import("./analytics/analytics_thread.zig");
const Fs = @import("./fs.zig");
const Api = @import("./api/schema.zig").Api;
const which = @import("./which.zig").which;
const Variable = struct {
key: string,
value: string,
@@ -396,6 +397,29 @@ pub const Loader = struct {
const empty_string_value: string = "\"\"";
pub fn getNodePath(this: *Loader, fs: *Fs.FileSystem, buf: *Fs.PathBuffer) ?[:0]const u8 {
if (this.get("NODE") orelse this.get("npm_node_execpath")) |node| {
@memcpy(buf, node.ptr, node.len);
buf[node.len] = 0;
return buf[0..node.len :0];
}
if (which(buf, this.map.get("PATH") orelse return null, fs.top_level_dir, "node")) |node| {
return node;
}
return null;
}
pub fn loadNodeJSConfig(this: *Loader, fs: *Fs.FileSystem) !void {
var buf: Fs.PathBuffer = undefined;
var node = this.getNodePath(fs, &buf) orelse return;
var cloned = try fs.dirname_store.append([]const u8, std.mem.span(node));
try this.map.put("NODE", cloned);
try this.map.put("npm_node_execpath", cloned);
}
pub fn get(this: *const Loader, key: string) ?string {
var _key = key;
if (_key.len > 0 and _key[0] == '$') {