aboutsummaryrefslogtreecommitdiff
path: root/packages/bun-npm/scripts/npm-build.ts
blob: 34598b92c893f0e174a23d4ee8eccf26843dd316 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import type { Endpoints } from "@octokit/types";
import { fetch, spawn } from "../src/util";
import type { JSZipObject } from "jszip";
import { loadAsync } from "jszip";
import { join } from "node:path";
import { chmod, read, write } from "../src/util";
import type { BuildOptions } from "esbuild";
import { buildSync, formatMessagesSync } from "esbuild";
import type { Platform } from "../src/platform";
import { platforms } from "../src/platform";

type Release =
  Endpoints["GET /repos/{owner}/{repo}/releases/latest"]["response"]["data"];

const npmPackage = "bun";
const npmOwner = "@oven";
let npmVersion: string;

const [tag, action] = process.argv.slice(2);

await build(tag);
if (action === "publish") {
  await publish();
} else if (action === "dry-run") {
  await publish(true);
} else if (action) {
  throw new Error(`Unknown action: ${action}`);
}

async function build(version: string): Promise<void> {
  const release = await getRelease(version);
  if (release.tag_name === "canary") {
    const { tag_name } = await getRelease();
    const sha = await getSha(tag_name);
    // Note: this needs to be run using canary
    npmVersion = `${Bun.version}-canary+${sha}`;
  } else {
    npmVersion = release.tag_name.replace("bun-v", "");
  }
  await buildBasePackage();
  for (const platform of platforms) {
    await buildPackage(release, platform);
  }
}

async function publish(dryRun?: boolean): Promise<void> {
  const npmPackages = platforms.map(({ bin }) => `${npmOwner}/${bin}`);
  npmPackages.push(npmPackage);
  for (const npmPackage of npmPackages) {
    publishPackage(npmPackage, dryRun);
  }
}

async function buildBasePackage() {
  const done = log("Building:", `${npmPackage}@${npmVersion}`);
  const cwd = join("npm", npmPackage);
  const define = {
    npmVersion: `"${npmVersion}"`,
    npmPackage: `"${npmPackage}"`,
    npmOwner: `"${npmOwner}"`,
  };
  buildJs(join("scripts", "npm-postinstall.ts"), join(cwd, "install.js"), {
    define,
  });
  buildJs(join("scripts", "npm-exec.ts"), join(cwd, "bin", "bun"), {
    define,
    banner: {
      js: "#!/usr/bin/env node",
    },
  });
  const os = [...new Set(platforms.map(({ os }) => os))];
  const cpu = [...new Set(platforms.map(({ arch }) => arch))];
  patchJson(join(cwd, "package.json"), {
    name: npmPackage,
    version: npmVersion,
    scripts: {
      postinstall: "node install.js",
    },
    optionalDependencies: Object.fromEntries(
      platforms.map(({ bin }) => [`${npmOwner}/${bin}`, npmVersion]),
    ),
    bin: {
      bun: "bin/bun",
    },
    os,
    cpu,
  });
  done();
}

async function buildPackage(
  release: Release,
  { bin, exe, os, arch }: Platform,
): Promise<void> {
  const npmPackage = `${npmOwner}/${bin}`;
  const done = log("Building:", `${npmPackage}@${npmVersion}`);
  const asset = release.assets.find(({ name }) => name === `${bin}.zip`);
  if (!asset) {
    throw new Error(`No asset found: ${bin}`);
  }
  const bun = await extractFromZip(asset.browser_download_url, `${bin}/bun`);
  const cwd = join("npm", npmPackage);
  write(join(cwd, exe), await bun.async("arraybuffer"));
  chmod(join(cwd, exe), 0o755);
  patchJson(join(cwd, "package.json"), {
    name: npmPackage,
    version: npmVersion,
    preferUnplugged: true,
    os: [os],
    cpu: [arch],
  });
  done();
}

function publishPackage(name: string, dryRun?: boolean): void {
  const done = log(dryRun ? "Dry-run Publishing:" : "Publishing:", name);
  const { exitCode, stdout, stderr } = spawn(
    "npm",
    [
      "publish",
      "--access",
      "public",
      "--tag",
      npmVersion.startsWith("canary") ? "canary" : "latest",
      ...(dryRun ? ["--dry-run"] : []),
    ],
    {
      cwd: join("npm", name),
    },
  );
  if (exitCode === 0) {
    done();
    return;
  }
  throw new Error(stdout || stderr);
}

async function extractFromZip(
  url: string,
  filename: string,
): Promise<JSZipObject> {
  const response = await fetch(url);
  const buffer = await response.arrayBuffer();
  const zip = await loadAsync(buffer);
  for (const [name, file] of Object.entries(zip.files)) {
    if (!file.dir && name.startsWith(filename)) {
      return file;
    }
  }
  console.warn("Found files:", Object.keys(zip.files));
  throw new Error(`File not found: ${filename}`);
}

async function getRelease(version?: string | null): Promise<Release> {
  const response = await fetchGithub(
    version ? `releases/tags/${formatTag(version)}` : `releases/latest`,
  );
  return response.json();
}

async function getSha(version: string): Promise<string> {
  const response = await fetchGithub(`git/ref/tags/${formatTag(version)}`);
  const {
    object,
  }: Endpoints["GET /repos/{owner}/{repo}/git/ref/{ref}"]["response"]["data"] =
    await response.json();
  return object.sha.substring(0, 7);
}

async function fetchGithub(path: string) {
  const headers = new Headers();
  const token = process.env.GITHUB_TOKEN;
  if (token) {
    headers.set("Authorization", `Bearer ${token}`);
  }
  const url = new URL(path, "https://api.github.com/repos/oven-sh/bun/");
  return fetch(url.toString());
}

function formatTag(version: string): string {
  if (version.startsWith("canary") || version.startsWith("bun-v")) {
    return version;
  }
  return `bun-v${version}`;
}

function patchJson(path: string, patch: object): void {
  let value;
  try {
    const existing = JSON.parse(read(path));
    value = {
      ...existing,
      ...patch,
    };
  } catch {
    value = patch;
  }
  write(path, `${JSON.stringify(value, undefined, 2)}\n`);
}

function buildJs(src: string, dst: string, options: BuildOptions = {}): void {
  const { errors } = buildSync({
    bundle: true,
    treeShaking: true,
    keepNames: true,
    minifySyntax: true,
    pure: ["console.debug"],
    platform: "node",
    target: "es6",
    format: "cjs",
    entryPoints: [src],
    outfile: dst,
    ...options,
  });
  if (errors?.length) {
    const messages = formatMessagesSync(errors, { kind: "error" });
    throw new Error(messages.join("\n"));
  }
}

function log(...args: any[]): () => void {
  console.write(Bun.inspect(...args));
  const start = Date.now();
  return () => {
    console.write(` [${(Date.now() - start).toFixed()} ms]\n`);
  };
}