onenote-cli 0.2.1-win32-arm64 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +10 -0
- package/.gitattributes +7 -0
- package/.github/workflows/release.yml +170 -0
- package/.github/workflows/rust-npm-release.yml +109 -0
- package/.husky/pre-commit +1 -0
- package/.releaserc.json +15 -0
- package/.secretlintrc.json +3 -0
- package/CLAUDE.md +42 -0
- package/README.md +267 -0
- package/SKILL.md +77 -0
- package/docs/azure-app-registration.md +80 -0
- package/docs/development-notes.md +84 -0
- package/docs/graph-api-endpoints.md +131 -0
- package/docs/local-search-architecture.md +132 -0
- package/docs/onen0te-cli-analysis.md +130 -0
- package/docs/setup.md +141 -0
- package/docs/zhihu-intro.md +92 -0
- package/package.json +46 -12
- package/rustify/onenote +556 -0
- package/rustify/onenote-cli +556 -0
- package/rustify/package.json +3 -0
- package/src/auth.ts +359 -0
- package/src/cache.ts +1709 -0
- package/src/debug-tags.ts +207 -0
- package/src/export.ts +367 -0
- package/src/graph.ts +1480 -0
- package/src/index.ts +1176 -0
- package/src/markdown.ts +134 -0
- package/src/read-render.ts +621 -0
- package/src/sync-ui.tsx +220 -0
- package/tsconfig.json +16 -0
- package/bin/onenote-cli.exe +0 -0
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// rustify launcher v3 (generated by templates/npm/pack.mjs — do not edit).
|
|
3
|
+
// (v3: the binary starts with the environment the command was started with,
|
|
4
|
+
// not with bun's process.env, which also holds the working directory's .env
|
|
5
|
+
// files. Whatever is read is used, an empty environment and one holding a
|
|
6
|
+
// name that a preload deleted from process.env included; process.env takes
|
|
7
|
+
// its place only when it cannot be read. On Windows, what JavaScript set or
|
|
8
|
+
// deleted in process.env before this file ran is in it. On Linux and macOS
|
|
9
|
+
// the command is replaced only where it is a symbolic link to this file.)
|
|
10
|
+
//
|
|
11
|
+
// Runs the native binary that the package manager installed for this machine
|
|
12
|
+
// as an optional dependency. No install script is involved.
|
|
13
|
+
//
|
|
14
|
+
// It is meant to run once. The first run rewires the command so that later
|
|
15
|
+
// runs start the binary without any JavaScript runtime:
|
|
16
|
+
//
|
|
17
|
+
// Linux, macOS replaces this file with a hard link to the binary, where
|
|
18
|
+
// the command is a symbolic link to this file (not where
|
|
19
|
+
// it is a script that hands this file to bun, as pnpm's)
|
|
20
|
+
// Windows keeps a native copy beside this file and points the
|
|
21
|
+
// package manager's command shim (bun's .bunx, npm's .cmd)
|
|
22
|
+
// straight at it
|
|
23
|
+
//
|
|
24
|
+
// Every step is checked first and skipped when anything looks unfamiliar; the
|
|
25
|
+
// command then keeps working through this launcher, only slower.
|
|
26
|
+
//
|
|
27
|
+
// Set RUSTIFY_NO_LINK=1 to leave all files untouched.
|
|
28
|
+
"use strict";
|
|
29
|
+
|
|
30
|
+
const fs = require("fs");
|
|
31
|
+
const path = require("path");
|
|
32
|
+
|
|
33
|
+
// ──────────────────────── the binary's environment ─────────────────────────
|
|
34
|
+
|
|
35
|
+
// Before this file runs, bun has loaded the .env files of the working
|
|
36
|
+
// directory (.env, .env.local, .env.<NODE_ENV>, …, and an --env-file in
|
|
37
|
+
// BUN_OPTIONS) into process.env. It has not put them into the environment the
|
|
38
|
+
// operating system keeps for this process, so that one is read instead. The
|
|
39
|
+
// binary, and any JavaScript it hands a command back to, then start with what
|
|
40
|
+
// the user's shell gave the command, as they do once this launcher is out of
|
|
41
|
+
// the way: no .env value looks like a variable of the user's, and a NODE_ENV
|
|
42
|
+
// from .env does not make the next bun read .env.<NODE_ENV>.
|
|
43
|
+
//
|
|
44
|
+
// What a preload (of a bunfig.toml, or in BUN_OPTIONS) did to process.env
|
|
45
|
+
// before this file ran: on Linux and macOS bun writes none of process.env
|
|
46
|
+
// into what is read here (bun 1.3.14, 1.4.0 and 1.4.5 on Linux, 1.4.4 on
|
|
47
|
+
// macOS), so the binary gets the environment as the command was started,
|
|
48
|
+
// with what a preload deleted and without what it set. On Windows bun writes
|
|
49
|
+
// every assignment that changes process.env (a new name, in capitals, or a
|
|
50
|
+
// new value) and every delete into the process's environment block at once, and nothing keeps the block as it was
|
|
51
|
+
// (bun 1.4.2; the PEB's block is the same one), so there the binary gets what
|
|
52
|
+
// a preload set and not what it deleted.
|
|
53
|
+
//
|
|
54
|
+
// It is read under node too, and the runtime is not asked: a preload can
|
|
55
|
+
// delete process.versions.bun. node loads no .env file for this file (it
|
|
56
|
+
// refuses --env-file in NODE_OPTIONS), so where node cannot read it (macOS
|
|
57
|
+
// and Windows: no bun:ffi) its process.env is that environment, with what a
|
|
58
|
+
// --require preload did. When the environment cannot be read (no /proc, a
|
|
59
|
+
// process that may not read its own, no bun:ffi), process.env is used, as by
|
|
60
|
+
// launcher v2.
|
|
61
|
+
//
|
|
62
|
+
// What cannot be passed on as it is: a name that is not UTF-8 (on Windows,
|
|
63
|
+
// one with a lone surrogate) is left out, as v2 left it out on Linux; a value
|
|
64
|
+
// that is not UTF-8 arrives with U+FFFD in place of the bad bytes, as in v2;
|
|
65
|
+
// of two entries with the same name (on Windows in other letters, as Windows
|
|
66
|
+
// compares them: see windowsKey) the first is kept, which is the one
|
|
67
|
+
// getenv() and GetEnvironmentVariableW() find; an entry without "=" is left
|
|
68
|
+
// out; so is a name "__proto__", which a plain object does not hold as a
|
|
69
|
+
// key, and a NODE_CHANNEL_FD outside a fork (see TAKEN). Names that are
|
|
70
|
+
// array indices ("1", "42") come first, in numeric
|
|
71
|
+
// order, as they always did in process.env.
|
|
72
|
+
|
|
73
|
+
// The entries ("NAME=value") of the environment this process was started
|
|
74
|
+
// with, as the operating system holds it: Buffers on Linux and macOS, text on
|
|
75
|
+
// Windows; none for an empty environment. null when it cannot be read (under
|
|
76
|
+
// node on macOS and Windows, which have no bun:ffi).
|
|
77
|
+
function startEntries() {
|
|
78
|
+
try {
|
|
79
|
+
if (process.platform === "linux" || process.platform === "android") return procEntries();
|
|
80
|
+
if (process.platform === "darwin") return darwinEntries();
|
|
81
|
+
if (process.platform === "win32") return windowsEntries();
|
|
82
|
+
} catch {}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Linux: the environment the kernel handed to this process, NUL-separated.
|
|
87
|
+
function procEntries() {
|
|
88
|
+
const raw = fs.readFileSync("/proc/self/environ");
|
|
89
|
+
const entries = [];
|
|
90
|
+
for (let start = 0; start < raw.length; ) {
|
|
91
|
+
let end = raw.indexOf(0, start);
|
|
92
|
+
if (end < 0) end = raw.length;
|
|
93
|
+
entries.push(raw.subarray(start, end));
|
|
94
|
+
start = end + 1;
|
|
95
|
+
}
|
|
96
|
+
return entries;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// macOS: the C runtime's `environ`, through bun:ffi.
|
|
100
|
+
function darwinEntries() {
|
|
101
|
+
const { dlopen, read, toBuffer, FFIType } = require("bun:ffi");
|
|
102
|
+
const lib = dlopen("/usr/lib/libSystem.B.dylib", {
|
|
103
|
+
_NSGetEnviron: { args: [], returns: FFIType.ptr },
|
|
104
|
+
strlen: { args: [FFIType.ptr], returns: FFIType.u64_fast },
|
|
105
|
+
});
|
|
106
|
+
try {
|
|
107
|
+
const environ = read.ptr(lib.symbols._NSGetEnviron(), 0);
|
|
108
|
+
const entries = [];
|
|
109
|
+
// A NULL environ holds nothing: getenv() finds no variable.
|
|
110
|
+
if (!environ) return entries;
|
|
111
|
+
for (let at = 0; ; at += 8) {
|
|
112
|
+
const entry = read.ptr(environ, at);
|
|
113
|
+
if (!entry) break;
|
|
114
|
+
// toBuffer refuses a length of 0 (an empty entry, which execve allows).
|
|
115
|
+
const length = Number(lib.symbols.strlen(entry));
|
|
116
|
+
entries.push(length ? Buffer.from(toBuffer(entry, 0, length)) : Buffer.alloc(0));
|
|
117
|
+
}
|
|
118
|
+
return entries;
|
|
119
|
+
} finally {
|
|
120
|
+
lib.close();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Windows: the process's environment block, through bun:ffi. It also holds
|
|
125
|
+
// the hidden "=C:=C:\…" entries (the working directory of each drive), which
|
|
126
|
+
// process.env leaves out and which bun passes on when they are given.
|
|
127
|
+
function windowsEntries() {
|
|
128
|
+
const { dlopen, toBuffer, FFIType } = require("bun:ffi");
|
|
129
|
+
const lib = dlopen("kernel32.dll", {
|
|
130
|
+
GetEnvironmentStringsW: { args: [], returns: FFIType.ptr },
|
|
131
|
+
FreeEnvironmentStringsW: { args: [FFIType.ptr], returns: FFIType.i32 },
|
|
132
|
+
lstrlenW: { args: [FFIType.ptr], returns: FFIType.i32 },
|
|
133
|
+
});
|
|
134
|
+
try {
|
|
135
|
+
const block = lib.symbols.GetEnvironmentStringsW();
|
|
136
|
+
if (!block) return null;
|
|
137
|
+
try {
|
|
138
|
+
let units = 0;
|
|
139
|
+
for (let length; (length = lib.symbols.lstrlenW(block + units * 2)) > 0; ) units += length + 1;
|
|
140
|
+
// An empty block (CreateProcess can give one); toBuffer refuses a length of 0.
|
|
141
|
+
if (!units) return [];
|
|
142
|
+
const text = Buffer.from(toBuffer(block, 0, units * 2)).toString("utf16le");
|
|
143
|
+
return text.split("\0").filter(Boolean);
|
|
144
|
+
} finally {
|
|
145
|
+
lib.symbols.FreeEnvironmentStringsW(block);
|
|
146
|
+
}
|
|
147
|
+
} finally {
|
|
148
|
+
lib.close();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
let strictUtf8;
|
|
153
|
+
// `bytes` as UTF-8 text, or null when they are not UTF-8.
|
|
154
|
+
function utf8(bytes) {
|
|
155
|
+
strictUtf8 = strictUtf8 || new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
156
|
+
try {
|
|
157
|
+
return strictUtf8.decode(bytes);
|
|
158
|
+
} catch {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const NOT_ASCII = /[^\0-\x7f]/;
|
|
164
|
+
|
|
165
|
+
// A Windows variable's name as Windows compares it: one UTF-16 unit at a
|
|
166
|
+
// time, each in capitals where it has a capital of one unit whose small
|
|
167
|
+
// letter it is. So "été" and "ÉTÉ" are one name (and "dž" and "DŽ"), while
|
|
168
|
+
// "straße" and "STRASSE" are two, as are "ı" and "I", "ſ" and "S", "ς" and
|
|
169
|
+
// "Σ", "Dž" and "DŽ", which toUpperCase() makes one (Windows 11, measured).
|
|
170
|
+
// split("") gives UTF-16 units: a pair of surrogates is two, and stays as it
|
|
171
|
+
// is, as on Windows. Not matched: letters that got capitals after Windows'
|
|
172
|
+
// table was made (Georgian "ა" and "Ა", Unicode 11), which Windows keeps
|
|
173
|
+
// apart.
|
|
174
|
+
function windowsKey(name) {
|
|
175
|
+
// Most names are ASCII, where that is toUpperCase().
|
|
176
|
+
if (!NOT_ASCII.test(name)) return name.toUpperCase();
|
|
177
|
+
let key = "";
|
|
178
|
+
for (const unit of name.split("")) {
|
|
179
|
+
const upper = unit.toUpperCase();
|
|
180
|
+
key += upper.length === 1 && upper.toLowerCase() === unit ? upper : unit;
|
|
181
|
+
}
|
|
182
|
+
return key;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// The environment that `entries` (see startEntries) make, as an object for
|
|
186
|
+
// process.execve and spawn. See the top of this part for what is left out.
|
|
187
|
+
function environmentOf(entries, windows) {
|
|
188
|
+
const env = {};
|
|
189
|
+
const seen = new Set();
|
|
190
|
+
for (const entry of entries) {
|
|
191
|
+
let name;
|
|
192
|
+
let value;
|
|
193
|
+
if (typeof entry === "string") {
|
|
194
|
+
// On Windows a name may start with "=": "=C:=C:\Users".
|
|
195
|
+
const eq = entry.indexOf("=", windows ? 1 : 0);
|
|
196
|
+
if (eq < 0) continue;
|
|
197
|
+
name = entry.slice(0, eq);
|
|
198
|
+
if (typeof name.isWellFormed === "function" && !name.isWellFormed()) continue;
|
|
199
|
+
value = entry.slice(eq + 1);
|
|
200
|
+
} else {
|
|
201
|
+
const eq = entry.indexOf(0x3d, windows ? 1 : 0);
|
|
202
|
+
if (eq < 0) continue;
|
|
203
|
+
name = utf8(entry.subarray(0, eq));
|
|
204
|
+
if (name === null) continue;
|
|
205
|
+
value = entry.toString("utf8", eq + 1);
|
|
206
|
+
}
|
|
207
|
+
const key = windows ? windowsKey(name) : name;
|
|
208
|
+
if (seen.has(key)) continue;
|
|
209
|
+
seen.add(key);
|
|
210
|
+
env[name] = value;
|
|
211
|
+
}
|
|
212
|
+
return env;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// What bun takes out of process.env when it uses it: the IPC channel that
|
|
216
|
+
// child_process.fork gave it. Left out here too, as v2 left it out.
|
|
217
|
+
const TAKEN = ["NODE_CHANNEL_FD", "NODE_CHANNEL_SERIALIZATION_MODE", "NODE_UNIQUE_ID"];
|
|
218
|
+
|
|
219
|
+
// The environment the binary starts with: the one this process was started
|
|
220
|
+
// with (see startEntries), as it was read, else process.env; and `rootName`
|
|
221
|
+
// naming the package's directory. What was read is not compared with
|
|
222
|
+
// process.env: it may hold names process.env lacks (a preload deleted them;
|
|
223
|
+
// on Windows bun leaves some names outside ASCII out of process.env, "aß",
|
|
224
|
+
// "éa", bun 1.4.2), and it may be empty (`env -i`) where process.env holds
|
|
225
|
+
// the .env files.
|
|
226
|
+
function binaryEnvironment(entries, rootName, root, windows) {
|
|
227
|
+
let env = null;
|
|
228
|
+
try {
|
|
229
|
+
if (entries) env = environmentOf(entries, windows);
|
|
230
|
+
} catch {
|
|
231
|
+
env = null;
|
|
232
|
+
}
|
|
233
|
+
// Only a failed read falls back. A process.env that throws when asked (a
|
|
234
|
+
// preload's own object in its place) is no reason to: the IPC variables
|
|
235
|
+
// then go, as bun takes them out of a forked bun's process.env.
|
|
236
|
+
if (env) {
|
|
237
|
+
for (const name of TAKEN) {
|
|
238
|
+
let taken = true;
|
|
239
|
+
try {
|
|
240
|
+
taken = !(name in process.env);
|
|
241
|
+
} catch {}
|
|
242
|
+
if (taken) delete env[name];
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (!env) env = Object.assign({}, process.env);
|
|
246
|
+
// On Windows a variable is one in any letter case (see windowsKey).
|
|
247
|
+
if (windows) for (const name of Object.keys(env)) if (name !== rootName && windowsKey(name) === windowsKey(rootName)) delete env[name];
|
|
248
|
+
env[rootName] = root;
|
|
249
|
+
return env;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ─────────────────────── Windows command shims ───────────────────────────
|
|
253
|
+
|
|
254
|
+
// bun's `<command>.bunx` (UTF-16LE), as written by bun's BinLinkingShim:
|
|
255
|
+
//
|
|
256
|
+
// with a shebang: bin_path '"' NUL launcher ' ' u32 bin_path_bytes
|
|
257
|
+
// u32 launcher_bytes u16 flags
|
|
258
|
+
// direct: bin_path '"' NUL u16 flags
|
|
259
|
+
//
|
|
260
|
+
// flags: bit0 is_node_or_bun, bit1 is_node, bit2 has_shebang,
|
|
261
|
+
// bit3 is_absolute_target, bits 4.. format version.
|
|
262
|
+
const BUNX_VERSION = 2739; // "V5". Anything else is left alone.
|
|
263
|
+
|
|
264
|
+
// Returns the direct form of a shebang `.bunx` whose target gains `suffix`,
|
|
265
|
+
// or null when the file is not exactly the known format.
|
|
266
|
+
function directBunx(buf, suffix) {
|
|
267
|
+
if (!Buffer.isBuffer(buf) || buf.length < 16 || buf.length % 2) return null;
|
|
268
|
+
const flags = buf.readUInt16LE(buf.length - 2);
|
|
269
|
+
if (flags >> 4 !== BUNX_VERSION || !(flags & 0b100)) return null;
|
|
270
|
+
const pathBytes = buf.readUInt32LE(buf.length - 10);
|
|
271
|
+
const launcherBytes = buf.readUInt32LE(buf.length - 6);
|
|
272
|
+
if (pathBytes % 2 || launcherBytes % 2 || pathBytes === 0) return null;
|
|
273
|
+
if (buf.length !== pathBytes + 4 + launcherBytes + 8 + 2) return null;
|
|
274
|
+
if (buf.readUInt16LE(pathBytes) !== 0x22 || buf.readUInt16LE(pathBytes + 2) !== 0) return null;
|
|
275
|
+
const tail = Buffer.alloc(6);
|
|
276
|
+
tail.writeUInt16LE(0x22, 0);
|
|
277
|
+
tail.writeUInt16LE(0, 2);
|
|
278
|
+
tail.writeUInt16LE(flags & ~0b111, 4);
|
|
279
|
+
return Buffer.concat([buf.subarray(0, pathBytes), Buffer.from(suffix, "utf16le"), tail]);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// The target a `.bunx` names, as text, or null.
|
|
283
|
+
function bunxTarget(buf) {
|
|
284
|
+
if (!Buffer.isBuffer(buf) || buf.length < 8 || buf.length % 2) return null;
|
|
285
|
+
const flags = buf.readUInt16LE(buf.length - 2);
|
|
286
|
+
if (flags >> 4 !== BUNX_VERSION) return null;
|
|
287
|
+
const pathBytes = flags & 0b100 ? buf.readUInt32LE(buf.length - 10) : buf.length - 6;
|
|
288
|
+
if (pathBytes <= 0 || pathBytes > buf.length) return null;
|
|
289
|
+
return { path: buf.subarray(0, pathBytes).toString("utf16le"), absolute: Boolean(flags & 0b1000) };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function samePath(a, b) {
|
|
293
|
+
return path.resolve(a).toLowerCase() === path.resolve(b).toLowerCase();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function replaceFile(file, content) {
|
|
297
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
298
|
+
fs.writeFileSync(tmp, content);
|
|
299
|
+
try {
|
|
300
|
+
fs.renameSync(tmp, file);
|
|
301
|
+
} catch (err) {
|
|
302
|
+
try {
|
|
303
|
+
fs.unlinkSync(tmp);
|
|
304
|
+
} catch {}
|
|
305
|
+
throw err;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// bun: point `<shimDir>\<name>.bunx` at the native copy instead of this launcher.
|
|
310
|
+
function retargetBun(shimDir, name, launcher) {
|
|
311
|
+
const file = path.join(shimDir, `${name}.bunx`);
|
|
312
|
+
let buf;
|
|
313
|
+
try {
|
|
314
|
+
buf = fs.readFileSync(file);
|
|
315
|
+
} catch {
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
const target = bunxTarget(buf);
|
|
319
|
+
if (!target) return false;
|
|
320
|
+
// Relative targets start at the parent of the shim's directory.
|
|
321
|
+
const resolved = target.absolute ? target.path : path.resolve(path.dirname(shimDir), target.path);
|
|
322
|
+
if (!samePath(resolved, launcher)) return false;
|
|
323
|
+
const direct = directBunx(buf, ".exe");
|
|
324
|
+
if (!direct) return false;
|
|
325
|
+
replaceFile(file, direct);
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// npm: rewrite `<shimDir>\<name>.cmd`, `.ps1` and the sh shim to start the
|
|
330
|
+
// native copy. Each is rewritten only if it currently starts this launcher.
|
|
331
|
+
function retargetNpm(shimDir, name, launcher, native) {
|
|
332
|
+
const cmd = path.join(shimDir, `${name}.cmd`);
|
|
333
|
+
const relLauncher = path.relative(shimDir, launcher);
|
|
334
|
+
let current;
|
|
335
|
+
try {
|
|
336
|
+
current = fs.readFileSync(cmd, "utf8");
|
|
337
|
+
} catch {
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
if (!current.toLowerCase().includes(`${path.sep}${relLauncher.toLowerCase()}"`)) return false;
|
|
341
|
+
const win = path.relative(shimDir, native);
|
|
342
|
+
const posix = win.split(path.sep).join("/");
|
|
343
|
+
replaceFile(cmd, `@ECHO off\r\n"%~dp0${win}" %*\r\n`);
|
|
344
|
+
const shims = {
|
|
345
|
+
[`${name}.ps1`]:
|
|
346
|
+
`#!/usr/bin/env pwsh\n$exe = Join-Path $PSScriptRoot "${posix}"\n` +
|
|
347
|
+
`if ($MyInvocation.ExpectingInput) { $input | & $exe @args } else { & $exe @args }\n` +
|
|
348
|
+
`exit $LASTEXITCODE\n`,
|
|
349
|
+
[name]:
|
|
350
|
+
`#!/bin/sh\nbasedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")\n` +
|
|
351
|
+
`exec "$basedir/${posix}" "$@"\n`,
|
|
352
|
+
};
|
|
353
|
+
for (const [file, content] of Object.entries(shims)) {
|
|
354
|
+
const target = path.join(shimDir, file);
|
|
355
|
+
try {
|
|
356
|
+
if (fs.readFileSync(target, "utf8").includes(relLauncher.split(path.sep).join("/"))) {
|
|
357
|
+
replaceFile(target, content);
|
|
358
|
+
}
|
|
359
|
+
} catch {}
|
|
360
|
+
}
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Finds the shims that start `launcher` and points them at `native`.
|
|
365
|
+
function retargetShims(root, name, launcher, native) {
|
|
366
|
+
let modules = path.dirname(root);
|
|
367
|
+
if (path.basename(modules).startsWith("@")) modules = path.dirname(modules);
|
|
368
|
+
if (path.basename(modules) !== "node_modules") return;
|
|
369
|
+
const candidates = [
|
|
370
|
+
process.env.BUN_INSTALL_BIN,
|
|
371
|
+
path.resolve(modules, "..", "..", "..", "bin"), // bun global: <BUN_INSTALL>\bin
|
|
372
|
+
path.dirname(modules), // npm global: <prefix>
|
|
373
|
+
path.join(modules, ".bin"), // local installs
|
|
374
|
+
];
|
|
375
|
+
for (const dir of candidates) {
|
|
376
|
+
if (!dir) continue;
|
|
377
|
+
try {
|
|
378
|
+
if (retargetBun(dir, name, launcher) || retargetNpm(dir, name, launcher, native)) return;
|
|
379
|
+
} catch {}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// ─────────────────────────────── launcher ────────────────────────────────
|
|
384
|
+
|
|
385
|
+
function main() {
|
|
386
|
+
const self = fs.realpathSync(__filename);
|
|
387
|
+
const binDir = path.dirname(self);
|
|
388
|
+
const root = path.dirname(binDir);
|
|
389
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
|
|
390
|
+
const cfg = pkg.rustify || {};
|
|
391
|
+
const bins = cfg.bins || {};
|
|
392
|
+
const isWindows = process.platform === "win32";
|
|
393
|
+
const exe = isWindows ? ".exe" : "";
|
|
394
|
+
const name = path.basename(self);
|
|
395
|
+
|
|
396
|
+
function die(message) {
|
|
397
|
+
console.error(`${pkg.name}: ${message}`);
|
|
398
|
+
process.exit(1);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const os = process.platform === "android" ? "linux" : process.platform;
|
|
402
|
+
const key = `${os}-${process.arch}`;
|
|
403
|
+
const dep = `${pkg.name}-${key}`;
|
|
404
|
+
|
|
405
|
+
function platformDir() {
|
|
406
|
+
if (!(pkg.optionalDependencies || {})[dep]) {
|
|
407
|
+
const have = Object.keys(pkg.optionalDependencies || {})
|
|
408
|
+
.filter((d) => d.startsWith(`${pkg.name}-`))
|
|
409
|
+
.map((d) => d.slice(pkg.name.length + 1));
|
|
410
|
+
die(`no prebuilt binary for ${key}. Available: ${have.join(", ") || "none"}.`);
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
return path.dirname(require.resolve(`${dep}/package.json`, { paths: [root] }));
|
|
414
|
+
} catch {
|
|
415
|
+
die(
|
|
416
|
+
`the binary package for ${key} is not installed.\n` +
|
|
417
|
+
`It is an optional dependency, so this happens when optional dependencies ` +
|
|
418
|
+
`are skipped (--omit=optional, --no-optional) or the download failed.\n` +
|
|
419
|
+
`Reinstall ${pkg.name} with optional dependencies enabled.`,
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const nativePath = (dir, npmBin) => path.join(dir, "bin", bins[npmBin] + exe);
|
|
425
|
+
|
|
426
|
+
// Put `src` at `dest` in one step, as a hard link where the filesystem
|
|
427
|
+
// allows and as a copy otherwise.
|
|
428
|
+
function place(src, dest) {
|
|
429
|
+
const tmp = `${dest}.${process.pid}.tmp`;
|
|
430
|
+
try {
|
|
431
|
+
fs.linkSync(src, tmp);
|
|
432
|
+
} catch {
|
|
433
|
+
fs.copyFileSync(src, tmp);
|
|
434
|
+
fs.chmodSync(tmp, 0o755);
|
|
435
|
+
}
|
|
436
|
+
try {
|
|
437
|
+
fs.renameSync(tmp, dest);
|
|
438
|
+
} catch (err) {
|
|
439
|
+
try {
|
|
440
|
+
fs.unlinkSync(tmp);
|
|
441
|
+
} catch {}
|
|
442
|
+
throw err;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// True when `copy` is an up-to-date hard link or copy of `src`.
|
|
447
|
+
function current(src, copy) {
|
|
448
|
+
try {
|
|
449
|
+
const x = fs.statSync(src);
|
|
450
|
+
const y = fs.statSync(copy);
|
|
451
|
+
return x.size === y.size && y.mtimeMs >= x.mtimeMs;
|
|
452
|
+
} catch {
|
|
453
|
+
return false;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// Whether a bin directory of npm's or bun's (global, or node_modules/.bin)
|
|
458
|
+
// holds the command as a symbolic link to this file.
|
|
459
|
+
function linkedCommand() {
|
|
460
|
+
let modules = path.dirname(root);
|
|
461
|
+
if (path.basename(modules).startsWith("@")) modules = path.dirname(modules);
|
|
462
|
+
if (path.basename(modules) !== "node_modules") return false;
|
|
463
|
+
const dirs = [
|
|
464
|
+
process.env.BUN_INSTALL_BIN,
|
|
465
|
+
path.resolve(modules, "..", "..", "..", "bin"), // bun global: <BUN_INSTALL>/bin
|
|
466
|
+
path.resolve(modules, "..", "..", "bin"), // npm global: <prefix>/bin
|
|
467
|
+
path.join(modules, ".bin"), // local installs
|
|
468
|
+
];
|
|
469
|
+
for (const dir of dirs) {
|
|
470
|
+
if (!dir) continue;
|
|
471
|
+
const file = path.join(dir, name);
|
|
472
|
+
try {
|
|
473
|
+
if (fs.lstatSync(file).isSymbolicLink() && fs.realpathSync(file) === self) return true;
|
|
474
|
+
} catch {}
|
|
475
|
+
}
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// Returns the path to run: a native file inside this package's bin
|
|
480
|
+
// directory when linking works, otherwise the binary in the platform package.
|
|
481
|
+
function prepare(dir) {
|
|
482
|
+
const fallback = nativePath(dir, name);
|
|
483
|
+
if (!fs.existsSync(fallback)) {
|
|
484
|
+
die(`${fallback} is missing. Reinstall ${pkg.name}.`);
|
|
485
|
+
}
|
|
486
|
+
const linkable =
|
|
487
|
+
!process.env.RUSTIFY_NO_LINK && !process.versions.pnp && !self.includes(".zip");
|
|
488
|
+
if (!linkable) return fallback;
|
|
489
|
+
try {
|
|
490
|
+
if (isWindows) {
|
|
491
|
+
// The shim the package manager wrote is bound to this script, so the
|
|
492
|
+
// script stays. Keep a native copy beside it and repoint the shim.
|
|
493
|
+
const side = path.join(binDir, name + exe);
|
|
494
|
+
if (!current(fallback, side)) place(fallback, side);
|
|
495
|
+
try {
|
|
496
|
+
retargetShims(root, name, self, side);
|
|
497
|
+
} catch {}
|
|
498
|
+
return side;
|
|
499
|
+
}
|
|
500
|
+
// Only where the command is a symbolic link to this file, which the
|
|
501
|
+
// system starts by its #! line. A shim that hands this file to a
|
|
502
|
+
// runtime (pnpm's sh script: exec bun "<this file>") would hand that
|
|
503
|
+
// runtime the binary.
|
|
504
|
+
if (!linkedCommand()) return fallback;
|
|
505
|
+
// Replace every launcher of this package, not only the one that ran.
|
|
506
|
+
for (const npmBin of Object.keys(bins)) {
|
|
507
|
+
const target = path.join(binDir, npmBin);
|
|
508
|
+
if (npmBin === name || fs.existsSync(target)) {
|
|
509
|
+
place(nativePath(dir, npmBin), target);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
return self;
|
|
513
|
+
} catch {
|
|
514
|
+
return fallback;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (!bins[name]) die(`${name} is not a command of this package.`);
|
|
519
|
+
const binary = prepare(platformDir());
|
|
520
|
+
const args = process.argv.slice(2);
|
|
521
|
+
// The root variable tells the binary where the npm package is when it runs
|
|
522
|
+
// from the platform package instead of from this package's bin directory.
|
|
523
|
+
const env = binaryEnvironment(startEntries(), cfg.rootEnv || "RUSTIFY_PACKAGE_ROOT", root, isWindows);
|
|
524
|
+
|
|
525
|
+
if (!isWindows && typeof process.execve === "function") {
|
|
526
|
+
try {
|
|
527
|
+
process.execve(binary, [name, ...args], env);
|
|
528
|
+
} catch {}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const child = require("child_process").spawn(binary, args, {
|
|
532
|
+
stdio: "inherit",
|
|
533
|
+
env,
|
|
534
|
+
argv0: name,
|
|
535
|
+
});
|
|
536
|
+
child.on("error", (err) => die(`cannot start ${binary}: ${err.message}`));
|
|
537
|
+
// The terminal already delivers Ctrl+C to the child. Staying alive here
|
|
538
|
+
// keeps the shell waiting until the child has finished.
|
|
539
|
+
process.on("SIGINT", () => {});
|
|
540
|
+
for (const signal of ["SIGTERM", "SIGHUP", "SIGQUIT"]) {
|
|
541
|
+
try {
|
|
542
|
+
process.on(signal, () => child.kill(signal));
|
|
543
|
+
} catch {}
|
|
544
|
+
}
|
|
545
|
+
child.on("exit", (code, signal) => {
|
|
546
|
+
if (signal) {
|
|
547
|
+
process.removeAllListeners(signal);
|
|
548
|
+
process.kill(process.pid, signal);
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
process.exit(code === null ? 1 : code);
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if (require.main === module) main();
|
|
556
|
+
else module.exports = { BUNX_VERSION, directBunx, bunxTarget, retargetBun, retargetNpm, startEntries, environmentOf, binaryEnvironment, windowsKey };
|