Skip to content

Commit

Permalink
Implement --registry CLI flag in bun install
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Sep 22, 2024
1 parent 27e7aa7 commit c26f6b2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 20 deletions.
21 changes: 12 additions & 9 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7165,9 +7165,7 @@ pub const PackageManager = struct {
}

if (cli_) |cli| {
if (cli.registry.len > 0 and strings.startsWith(cli.registry, "https://") or
strings.startsWith(cli.registry, "http://"))
{
if (cli.registry.len > 0) {
this.scope.url = URL.parse(cli.registry);
}

Expand Down Expand Up @@ -9100,7 +9098,7 @@ pub const PackageManager = struct {
clap.parseParam("-g, --global Install globally") catch unreachable,
clap.parseParam("--cwd <STR> Set a specific cwd") catch unreachable,
clap.parseParam("--backend <STR> Platform-specific optimizations for installing dependencies. " ++ platform_specific_backend_label) catch unreachable,
clap.parseParam("--link-native-bins <STR>... Link \"bin\" from a matching platform-specific \"optionalDependencies\" instead. Default: esbuild, turbo") catch unreachable,
clap.parseParam("--registry <STR> Use a specific registry by default, overriding .npmrc, bunfig.toml and environment variables") catch unreachable,
clap.parseParam("--concurrent-scripts <NUM> Maximum number of concurrent jobs for lifecycle scripts (default 5)") catch unreachable,
clap.parseParam("-h, --help Print this help menu") catch unreachable,
};
Expand Down Expand Up @@ -9171,7 +9169,6 @@ pub const PackageManager = struct {
});

pub const CommandLineArguments = struct {
registry: string = "",
cache_dir: string = "",
lockfile: string = "",
token: string = "",
Expand Down Expand Up @@ -9203,8 +9200,6 @@ pub const PackageManager = struct {
pack_destination: string = "",
pack_gzip_level: ?string = null,

link_native_bins: []const string = &[_]string{},

development: bool = false,
optional: bool = false,

Expand All @@ -9217,6 +9212,8 @@ pub const PackageManager = struct {

patch: PatchOpts = .{ .nothing = .{} },

registry: string = "",

const PatchOpts = union(enum) {
nothing: struct {},
patch: struct {},
Expand Down Expand Up @@ -9586,8 +9583,6 @@ pub const PackageManager = struct {
cli.config = opt;
}

cli.link_native_bins = args.options("--link-native-bins");

if (comptime subcommand == .add or subcommand == .install) {
cli.development = args.flag("--development") or args.flag("--dev");
cli.optional = args.flag("--optional");
Expand Down Expand Up @@ -9636,6 +9631,14 @@ pub const PackageManager = struct {
}
}

if (args.option("--registry")) |registry| {
if (!strings.hasPrefixComptime(registry, "https://") and !strings.hasPrefixComptime(registry, "http://")) {
Output.errGeneric("Registry URL must start with 'https://' or 'http://': {}\n", .{bun.fmt.quote(registry)});
Global.crash();
}
cli.registry = registry;
}

cli.positionals = args.positionals();

if (subcommand == .patch and cli.positionals.len < 2) {
Expand Down
93 changes: 82 additions & 11 deletions test/cli/install/bun-install.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { file, listen, Socket, spawn } from "bun";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, setDefaultTimeout, test } from "bun:test";
import {
jest,
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
expect,
it,
setDefaultTimeout,
test,
} from "bun:test";
import { access, mkdir, readlink, rm, writeFile } from "fs/promises";
import { bunEnv, bunExe, bunEnv as env, tempDirWithFiles, toBeValidBin, toBeWorkspaceLink, toHaveBins } from "harness";
import { join, sep } from "path";
Expand Down Expand Up @@ -81,7 +92,7 @@ it("should not error when package.json has comments and trailing commas", async
expect(requested).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down Expand Up @@ -226,10 +237,70 @@ registry = "http://${server.hostname}:${server.port}/"
expect(await exited).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
});

it("should support --registry CLI flag", async () => {
const connected = jest.fn();
function end(socket: Socket) {
connected();
socket.end();
}
const server = listen({
socket: {
data: function data(socket) {
end(socket);
},
drain: function drain(socket) {
end(socket);
},
open: function open(socket) {
end(socket);
},
},
hostname: "localhost",
port: 0,
});
await writeFile(
join(package_dir, "bunfig.toml"),
`
[install]
cache = false
registry = "https://badssl.com:bad"
`,
);
await writeFile(
join(package_dir, "package.json"),
JSON.stringify({
name: "foo",
version: "0.0.1",
dependencies: {
bar: "0.0.2",
},
}),
);
const { stdout, stderr, exited } = spawn({
cmd: [bunExe(), "install", "--registry", `http://${server.hostname}:${server.port}/`],
cwd: package_dir,
stdout: "pipe",
stdin: "pipe",
stderr: "pipe",
env,
});
const err = await new Response(stderr).text();
expect(err).toMatch(/error: (ConnectionRefused|ConnectionClosed) downloading package manifest bar/gm);
expect(await new Response(stdout).text()).toBeEmpty();
expect(await exited).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
expect(connected).toHaveBeenCalled();
});

it("should work when moving workspace packages", async () => {
Expand Down Expand Up @@ -410,7 +481,7 @@ it("should handle missing package", async () => {
expect(requested).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down Expand Up @@ -461,7 +532,7 @@ foo = { token = "bar" }
expect(requested).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down Expand Up @@ -1551,7 +1622,7 @@ it("should handle ^1 in dependencies", async () => {
expect(requested).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down Expand Up @@ -1628,7 +1699,7 @@ it("should handle ^0.1 in dependencies", async () => {
expect(requested).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down Expand Up @@ -1663,7 +1734,7 @@ it("should handle ^0.0.0 in dependencies", async () => {
expect(requested).toBe(1);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down Expand Up @@ -4512,7 +4583,7 @@ it("should fail on invalid Git URL", async () => {
expect(requested).toBe(0);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down Expand Up @@ -4548,7 +4619,7 @@ it("should fail on ssh Git URL if invalid credentials", async () => {
expect(requested).toBe(0);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down Expand Up @@ -4586,7 +4657,7 @@ it("should fail on Git URL with invalid committish", async () => {
expect(requested).toBe(0);
try {
await access(join(package_dir, "bun.lockb"));
expect(() => {}).toThrow();
expect.unreachable();
} catch (err: any) {
expect(err.code).toBe("ENOENT");
}
Expand Down

0 comments on commit c26f6b2

Please sign in to comment.