|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -import { readFileSync } from "fs"; |
4 | | -import { spawnSync, execSync } from "child_process"; |
5 | | -import { createRequire } from "module"; |
6 | | - |
7 | | -const require = createRequire(import.meta.url); |
8 | | - |
9 | | -const isMusl = () => { |
10 | | - let musl = false; |
11 | | - if (process.platform === "linux") { |
12 | | - musl = isMuslFromFilesystem(); |
13 | | - if (musl === null) { |
14 | | - musl = isMuslFromReport(); |
15 | | - } |
16 | | - if (musl === null) { |
17 | | - musl = isMuslFromChildProcess(); |
18 | | - } |
19 | | - } |
20 | | - return musl; |
21 | | -}; |
22 | | - |
23 | | -const isFileMusl = (f) => f.includes("libc.musl-") || f.includes("ld-musl-"); |
24 | | - |
25 | | -const isMuslFromFilesystem = () => { |
26 | | - try { |
27 | | - return readFileSync("/usr/bin/ldd", "utf-8").includes("musl"); |
28 | | - } catch { |
29 | | - return null; |
30 | | - } |
31 | | -}; |
32 | | - |
33 | | -const isMuslFromReport = () => { |
34 | | - const report = |
35 | | - typeof process.report.getReport === "function" |
36 | | - ? process.report.getReport() |
37 | | - : null; |
38 | | - if (!report) { |
39 | | - return null; |
40 | | - } |
41 | | - if (report.header && report.header.glibcVersionRuntime) { |
42 | | - return false; |
43 | | - } |
44 | | - if (Array.isArray(report.sharedObjects)) { |
45 | | - if (report.sharedObjects.some(isFileMusl)) { |
46 | | - return true; |
47 | | - } |
48 | | - } |
49 | | - return false; |
50 | | -}; |
51 | | - |
52 | | -const isMuslFromChildProcess = () => { |
53 | | - try { |
54 | | - return execSync("ldd --version", { encoding: "utf8" }) |
55 | | - .includes("musl"); |
56 | | - } catch (e) { |
57 | | - // If we reach this case, we don't know if the system is musl or not, so is better to just fallback to false |
58 | | - return false; |
59 | | - } |
60 | | -}; |
61 | | - |
62 | | -const { platform, arch, env, version, release } = process; |
63 | | - |
64 | | -const PLATFORMS = { |
65 | | - win32: { |
66 | | - x64: { |
67 | | - musl: "@oxfmt/win32-x64/oxfmt.exe", |
68 | | - gnu: "@oxfmt/win32-x64/oxfmt.exe", |
69 | | - }, |
70 | | - arm64: { |
71 | | - musl: "@oxfmt/win32-arm64/oxfmt.exe", |
72 | | - gnu: "@oxfmt/win32-arm64/oxfmt.exe", |
73 | | - }, |
74 | | - }, |
75 | | - darwin: { |
76 | | - x64: { |
77 | | - musl: "@oxfmt/darwin-x64/oxfmt", |
78 | | - gnu: "@oxfmt/darwin-x64/oxfmt", |
79 | | - }, |
80 | | - arm64: { |
81 | | - musl: "@oxfmt/darwin-arm64/oxfmt", |
82 | | - gnu: "@oxfmt/darwin-arm64/oxfmt", |
83 | | - }, |
84 | | - }, |
85 | | - linux: { |
86 | | - x64: { |
87 | | - musl: "@oxfmt/linux-x64-musl/oxfmt", |
88 | | - gnu: "@oxfmt/linux-x64-gnu/oxfmt", |
89 | | - }, |
90 | | - arm64: { |
91 | | - musl: "@oxfmt/linux-arm64-musl/oxfmt", |
92 | | - gnu: "@oxfmt/linux-arm64-gnu/oxfmt", |
93 | | - }, |
94 | | - }, |
95 | | -}; |
96 | | - |
97 | | -let binPath = PLATFORMS[platform]?.[arch]?.[isMusl() ? "musl" : "gnu"]; |
98 | | - |
99 | | -if (binPath) { |
100 | | - const result = spawnSync( |
101 | | - require.resolve(binPath), |
102 | | - process.argv.slice(2), |
103 | | - { |
104 | | - shell: false, |
105 | | - stdio: "inherit", |
106 | | - env: { |
107 | | - ...env, |
108 | | - JS_RUNTIME_VERSION: version, |
109 | | - JS_RUNTIME_NAME: release.name, |
110 | | - NODE_PACKAGE_MANAGER: detectPackageManager(), |
111 | | - }, |
112 | | - } |
113 | | - ); |
114 | | - |
115 | | - if (result.error) { |
116 | | - throw result.error; |
117 | | - } |
118 | | - |
119 | | - process.exitCode = result.status; |
120 | | -} else { |
121 | | - let target = `${platform}-${arch}`; |
122 | | - if (isMusl()) { |
123 | | - target = `${target}-musl`; |
124 | | - } |
125 | | - console.error( |
126 | | - `The oxfmt CLI package doesn't ship with prebuilt binaries for your platform (${target}) yet. ` + |
127 | | - "You can create an issue at https://github.com/oxc-project/oxc/issues for support." |
128 | | - ); |
129 | | - process.exitCode = 1; |
130 | | -} |
131 | | - |
132 | | -/** |
133 | | - * NPM, Yarn, and other package manager set the `npm_config_user_agent`. It has the following format: |
134 | | - * |
135 | | - * ``` |
136 | | - * "npm/8.3.0 node/v16.13.2 win32 x64 workspaces/false |
137 | | - * ``` |
138 | | - * |
139 | | - * @returns The package manager string (`npm/8.3.0`) or null if the user agent string isn't set. |
140 | | - */ |
141 | | -function detectPackageManager() { |
142 | | - const userAgent = env.npm_config_user_agent; |
143 | | - |
144 | | - if (userAgent == null) { |
145 | | - return null; |
146 | | - } |
147 | | - |
148 | | - return userAgent.split(" ")[0]; |
149 | | -} |
| 3 | +import "../dist/cli.js"; |
0 commit comments