|
1 | 1 | import os from "node:os"; |
2 | 2 | import syncFs from "node:fs"; |
3 | 3 | import fs from "node:fs/promises"; |
4 | | -import { Readable } from "node:stream"; |
5 | | -import { finished } from "node:stream/promises"; |
6 | | -import { spawnSync, spawn } from "node:child_process"; |
| 4 | +import { spawn, spawnSync } from "node:child_process"; |
7 | 5 |
|
8 | | -const constructSourceTarballName = (version) => `node-${version}.tar.gz`; |
9 | | -const constructSourceTarballDirName = (version) => `node-${version}`; |
10 | | - |
11 | | -const constructSourceDownloadLink = (version) => |
12 | | - `https://github.com/nodejs/node/archive/refs/tags/v${version}.tar.gz`; |
| 6 | +const coreCount = os.cpus().length; |
| 7 | +const threadCount = coreCount * 2; |
13 | 8 |
|
| 9 | +const nodejsGithubRepo = "https://github.com/nodejs/node"; |
14 | 10 | const removeTheVCharacter = (str) => str.replace("v", ""); |
15 | 11 |
|
16 | 12 | const nodeIndexUrl = "https://nodejs.org/dist/index.json"; |
@@ -41,43 +37,35 @@ const isANewerVersion = (oldVer, newVer) => { |
41 | 37 |
|
42 | 38 | const spawnAsync = (program, args, cwd) => |
43 | 39 | new Promise((resolve, reject) => { |
44 | | - const child = spawn(program, args, { cwd }); |
| 40 | + console.log([program, ...args].join(" ")); |
45 | 41 |
|
46 | | - child.stdout.on("data", (chunk) => console.log(chunk.toString())); |
47 | | - child.stderr.on("data", (chunk) => { |
48 | | - console.error(chunk.toString()); |
49 | | - reject(chunk); |
50 | | - }); |
| 42 | + const child = spawn(program, args, cwd ? { cwd } : {}); |
51 | 43 |
|
52 | | - child.on("close", (code) => resolve(code)); |
| 44 | + child.stdout.on("data", (chunk) => console.log(chunk.toString())); |
| 45 | + child.stderr.on("data", (chunk) => console.warn(chunk.toString())); |
| 46 | + child.on("close", (code) => resolve(code.toString())); |
53 | 47 | }); |
54 | 48 |
|
55 | 49 | const latestNodeVersion = await getLatestNodeVersion(); |
56 | | - |
57 | 50 | if (!isANewerVersion(await getLatestPublishedVersion(), latestNodeVersion)) { |
58 | 51 | console.log("Nothing to do!"); |
59 | 52 | process.exit(0); |
60 | 53 | } |
61 | 54 |
|
62 | | -const tarballName = constructSourceTarballName(latestNodeVersion); |
63 | | -if (!syncFs.existsSync(tarballName)) { |
64 | | - const stream = syncFs.createWriteStream(tarballName); |
65 | | - const { body } = await fetch(constructSourceDownloadLink(latestNodeVersion)); |
66 | | - await finished(Readable.fromWeb(body).pipe(stream)); |
| 55 | +if (!syncFs.existsSync("node")) { |
| 56 | + await spawnAsync( |
| 57 | + "git", |
| 58 | + [ |
| 59 | + "clone", |
| 60 | + nodejsGithubRepo, |
| 61 | + "--branch", |
| 62 | + `v${latestNodeVersion}`, |
| 63 | + "--depth=1", |
| 64 | + ], |
| 65 | + undefined |
| 66 | + ); |
67 | 67 | } |
68 | 68 |
|
69 | | -const tarballDir = constructSourceTarballDirName(latestNodeVersion); |
70 | | -if (!syncFs.existsSync(tarballDir)) { |
71 | | - const { stderr } = spawnSync("tar", ["-xvf", `${tarballName}`]); |
72 | | - if (stderr.length > 0) { |
73 | | - console.error(stderr); |
74 | | - process.exit(0); |
75 | | - } |
76 | | -} |
77 | | - |
78 | | -await spawnAsync("./configure", ["--ninja", "--shared", "--debug"], tarballDir); |
79 | | - |
80 | | -const coreCount = os.cpus().length; |
81 | | -const threadCount = coreCount * 2; |
| 69 | +await spawnAsync("./configure", ["--ninja", "--shared", "--debug"], "node"); |
82 | 70 |
|
83 | | -await spawnAsync("make", [`-j${threadCount}`], tarballDir); |
| 71 | +await spawnAsync("make", [`-j${threadCount}`], "node"); |
0 commit comments