aboutsummaryrefslogtreecommitdiff
path: root/src/bunfig.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2022-11-06 21:42:05 -0800
committerGravatar GitHub <noreply@github.com> 2022-11-06 21:42:05 -0800
commite45f72e8e422191adeb4fd1bad896dc6a47c76b3 (patch)
tree3a76da8b343c081dba84e0ac95f3c2cc2423106a /src/bunfig.zig
parent645cf903350a7fe5f5076100b7c4a6bc8cd1b431 (diff)
downloadbun-e45f72e8e422191adeb4fd1bad896dc6a47c76b3.tar.gz
bun-e45f72e8e422191adeb4fd1bad896dc6a47c76b3.tar.zst
bun-e45f72e8e422191adeb4fd1bad896dc6a47c76b3.zip
Automatically install npm packages when running a script in Bun's runtime (#1459)
* Update bundler.zig * WIP * Update README.md * Update README.md * wip * Support running scripts without package.json * Add `--no-auto-install` and `--prefer-offline` flags * WIP * wip * Update headers-handwritten.h * WIP * Build fixes * Fix UAF * Update install.zig * Must call .allocate() * Micro-optimization: only call .timestamp() once per tick when installing packages * Support progress bar * Extend the timestamp for package staleness checks to 1 day * Add `--prefer-latest`, `-i` CLI Flags * Fix crash * Support line text manually being set on an Error instance * Add a few more fields for error messages * Fix bug when counting 8 character strings in string builder * Implement error handling for automatic package installs! * Fix crash * Make it say module when there's a slash * Update module_loader.zig * Ban dependency versions in import specifiers when a package.json is present * Remove unused field * Update README.md * Update README.md * Update README.md * Update README.md Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bunfig.zig')
-rw-r--r--src/bunfig.zig40
1 files changed, 39 insertions, 1 deletions
diff --git a/src/bunfig.zig b/src/bunfig.zig
index b789bd89c..d2ca86380 100644
--- a/src/bunfig.zig
+++ b/src/bunfig.zig
@@ -31,6 +31,17 @@ const TOML = @import("./toml/toml_parser.zig").TOML;
// TODO: replace Api.TransformOptions with Bunfig
pub const Bunfig = struct {
+ pub const OfflineMode = enum {
+ online,
+ latest,
+ offline,
+ };
+ pub const Prefer = bun.ComptimeStringMap(OfflineMode, .{
+ &.{ "offline", OfflineMode.offline },
+ &.{ "latest", OfflineMode.latest },
+ &.{ "online", OfflineMode.online },
+ });
+
const Parser = struct {
json: js_ast.Expr,
source: *const logger.Source,
@@ -180,7 +191,7 @@ pub const Bunfig = struct {
}
}
- if (comptime cmd.isNPMRelated()) {
+ if (comptime cmd.isNPMRelated() or cmd == .RunCommand or cmd == .AutoCommand) {
if (json.get("install")) |_bun| {
var install: *Api.BunInstall = this.ctx.install orelse brk: {
var install_ = try this.allocator.create(Api.BunInstall);
@@ -189,6 +200,33 @@ pub const Bunfig = struct {
break :brk install_;
};
+ if (json.get("auto")) |auto_install_expr| {
+ if (auto_install_expr.data == .e_string) {
+ this.ctx.debug.global_cache = options.GlobalCache.Map.get(auto_install_expr.asString(this.allocator) orelse "") orelse {
+ try this.addError(auto_install_expr.loc, "Invalid auto install setting, must be one of true, false, or \"force\" \"fallback\" \"disable\"");
+ return;
+ };
+ } else if (auto_install_expr.data == .e_boolean) {
+ this.ctx.debug.global_cache = if (auto_install_expr.asBool().?)
+ options.GlobalCache.allow_install
+ else
+ options.GlobalCache.disable;
+ } else {
+ try this.addError(auto_install_expr.loc, "Invalid auto install setting, must be one of true, false, or \"force\" \"fallback\" \"disable\"");
+ return;
+ }
+ }
+
+ if (json.get("prefer")) |prefer_expr| {
+ try this.expect(prefer_expr, .e_string);
+
+ if (Prefer.get(prefer_expr.asString(bun.default_allocator) orelse "")) |setting| {
+ this.ctx.debug.offline_mode_setting = setting;
+ } else {
+ try this.addError(prefer_expr.loc, "Invalid prefer setting, must be one of online or offline");
+ }
+ }
+
if (_bun.get("registry")) |registry| {
install.default_registry = try this.parseRegistry(registry);
}