diff options
author | 2021-10-14 14:49:48 -0700 | |
---|---|---|
committer | 2021-10-14 14:49:48 -0700 | |
commit | 901d7d8759dd4bb1cd4bea6cd18d57137afd3505 (patch) | |
tree | 38e951ddbeca7e514614bfe13eaecf65e095f4b9 | |
parent | 4b618f9ad1a45f6819dcb65e9b6b5b859e0df9fd (diff) | |
download | bun-901d7d8759dd4bb1cd4bea6cd18d57137afd3505.tar.gz bun-901d7d8759dd4bb1cd4bea6cd18d57137afd3505.tar.zst bun-901d7d8759dd4bb1cd4bea6cd18d57137afd3505.zip |
Add doc explaining bun-create
-rw-r--r-- | examples/bun-create.md | 59 | ||||
-rw-r--r-- | src/cli.zig | 2 | ||||
-rw-r--r-- | src/which.zig | 2 |
3 files changed, 60 insertions, 3 deletions
diff --git a/examples/bun-create.md b/examples/bun-create.md index 28ed72f7b..68119ed0d 100644 --- a/examples/bun-create.md +++ b/examples/bun-create.md @@ -1,4 +1,61 @@ # `bun create` -This folder +## Config +The `bun-create` section of package.json is automatically removed from the final output. This lets you add create-only steps without installing an extra package. + +There are currently two options: + +- `postinstall` +- `preinstall` + +They can be an array of strings or one string. An array of strings will be executed one after another. + +Here are examples: + +``` +{ + "name": "@bun-examples/next", + "version": "0.0.31", + "main": "index.js", + "dependencies": { + "next": "11.1.2", + "react": "^17.0.2", + "react-dom": "^17.0.2", + "react-is": "^17.0.2" + }, + "devDependencies": { + "@types/react": "^17.0.19", + "bun-framework-next": "^0.0.0-21", + "typescript": "^4.3.5" + }, + "bun-create": { + "postinstall": [ + "bun bun --use next" + ] + } +} +``` + +By default, all commands run inside the environment exposed by the auto-detected npm client. This incurs a significant performance penalty, something like 150ms wasted on waiting for the npm client to start on each invocation. + +Any command that starts with `"bun "` will be run without npm. + +## How it works + +When you run `bun create ${template} ${destination}`, here's what happens: + +1. GET `registry.npmjs.org/@bun-examples/${template}/latest` and parse it +2. GET `registry.npmjs.org/@bun-examples/${template}/-/${template}-${latestVersion}.tgz` +3. Decompress & extract `${template}-${latestVersion}.tgz` into `${destination}` + 3a. If there are files that would overwrite, warn and exit unless `--force` is passed +4. Parse the `package.json`(again!), update `name` to be `${basename(destination)}`, remove the `bun-create` section from the `package.json` and save updated `package.json` to disk +5. Auto-detect the npm client, preferring `pnpm`, `yarn` (v1), and lastly `npm` +6. Run any tasks defined in `"bun-create": { "preinstall" }` with the npm client +7. Run `${npmClient} install` +8. Run any tasks defined in `"bun-create": { "preinstall" }` with the npm client +9. Run `git init; git add -A .; git commit -am "Initial Commit";`. + 8a. Rename `gitignore` to `.gitignore`. NPM automatically removes `.gitignore` files from appearing in packages. +10. Done + +`../misctools/publish-examples.js` publishes all examples to npm. diff --git a/src/cli.zig b/src/cli.zig index c0ee57a5d..7a20cc3b0 100644 --- a/src/cli.zig +++ b/src/cli.zig @@ -519,7 +519,7 @@ pub const Command = struct { pub fn create(allocator: *std.mem.Allocator, log: *logger.Log, comptime command: Command.Tag) anyerror!Context { return Command.Context{ - .args = if (comptime command != Command.Tag.CreateCommand) + .args = if (comptime command != Command.Tag.CreateCommand) try Arguments.parse(allocator, command) else std.mem.zeroes(Api.TransformOptions), diff --git a/src/which.zig b/src/which.zig index 7cb65cbe2..0b8e5f1f8 100644 --- a/src/which.zig +++ b/src/which.zig @@ -5,7 +5,7 @@ fn isValid(buf: *[std.fs.MAX_PATH_BYTES]u8, segment: []const u8, bin: []const u8 buf[segment.len] = std.fs.path.sep; std.mem.copy(u8, buf[segment.len + 1 ..], bin); buf[segment.len + 1 + bin.len ..][0] = 0; - var filepath = buf[0 .. segment.len + 1 + bin.len :0]; + const filepath = buf[0 .. segment.len + 1 + bin.len :0]; std.os.accessZ(filepath, std.os.X_OK) catch return null; return @intCast(u16, filepath.len); |