@bitefoundry/notion 0.3.0 → 0.4.0
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/LICENSE +21 -21
- package/README.md +152 -141
- package/docs/API.md +37 -35
- package/docs/MCP.md +120 -113
- package/docs/NPM.md +21 -21
- package/docs/openapi.yaml +185 -185
- package/package.json +50 -50
- package/scripts/postinstall.mjs +266 -266
package/scripts/postinstall.mjs
CHANGED
|
@@ -1,266 +1,266 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Zero-Node-Process npm native binary linker.
|
|
3
|
-
//
|
|
4
|
-
// On Unix (linux/darwin): replaces installed `bin/notion.js` with
|
|
5
|
-
// hardlinks/symlinks directly to `@bitefoundry/notion-<platform>-<arch>/bin/notion` so npm's
|
|
6
|
-
// bin symlink execve's the native Rust binary directly (0ms Node startup, 0MB Node RSS).
|
|
7
|
-
//
|
|
8
|
-
// On Windows (win32): creates NTFS hardlinks (fallback copy) `bin/notion.exe`
|
|
9
|
-
// and rewrites npm's `.bin/notion[.cmd|.ps1]` shims when present to invoke
|
|
10
|
-
// the native executable directly without spawning `node.exe`.
|
|
11
|
-
|
|
12
|
-
import fs from 'node:fs';
|
|
13
|
-
import path from 'node:path';
|
|
14
|
-
import { createRequire } from 'node:module';
|
|
15
|
-
import { fileURLToPath } from 'node:url';
|
|
16
|
-
|
|
17
|
-
const BIN_NAMES = ['notion'];
|
|
18
|
-
const PKG_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
19
|
-
|
|
20
|
-
function linkOrCopyFile(source, destination) {
|
|
21
|
-
const tmp = `${destination}.tmp-${process.pid}`;
|
|
22
|
-
try {
|
|
23
|
-
fs.rmSync(tmp, { force: true });
|
|
24
|
-
} catch {
|
|
25
|
-
// Ignore cleanup errors on nonexistent temp paths.
|
|
26
|
-
}
|
|
27
|
-
try {
|
|
28
|
-
fs.linkSync(source, tmp);
|
|
29
|
-
} catch {
|
|
30
|
-
fs.copyFileSync(source, tmp);
|
|
31
|
-
}
|
|
32
|
-
try {
|
|
33
|
-
fs.renameSync(tmp, destination);
|
|
34
|
-
} catch {
|
|
35
|
-
try {
|
|
36
|
-
fs.rmSync(destination, { force: true });
|
|
37
|
-
} catch {
|
|
38
|
-
// Ignore; renameSync below will surface or fail safely.
|
|
39
|
-
}
|
|
40
|
-
fs.renameSync(tmp, destination);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function replaceJsWithNativeLinkUnix(binaryPath, destinationJs) {
|
|
45
|
-
const tmp = `${destinationJs}.tmp-${process.pid}`;
|
|
46
|
-
try {
|
|
47
|
-
fs.rmSync(tmp, { force: true });
|
|
48
|
-
} catch {
|
|
49
|
-
// Ignore cleanup errors.
|
|
50
|
-
}
|
|
51
|
-
try {
|
|
52
|
-
fs.linkSync(binaryPath, tmp);
|
|
53
|
-
} catch {
|
|
54
|
-
try {
|
|
55
|
-
fs.rmSync(tmp, { force: true });
|
|
56
|
-
} catch {
|
|
57
|
-
// Ignore cleanup errors.
|
|
58
|
-
}
|
|
59
|
-
fs.symlinkSync(binaryPath, tmp);
|
|
60
|
-
}
|
|
61
|
-
try {
|
|
62
|
-
fs.renameSync(tmp, destinationJs);
|
|
63
|
-
} catch (err) {
|
|
64
|
-
try {
|
|
65
|
-
fs.rmSync(tmp, { force: true });
|
|
66
|
-
} catch {
|
|
67
|
-
// Ignore cleanup errors.
|
|
68
|
-
}
|
|
69
|
-
throw err;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function collectWindowsShimDirs(pkgRoot, env = process.env) {
|
|
74
|
-
const dirs = [];
|
|
75
|
-
const parentScope = path.dirname(pkgRoot);
|
|
76
|
-
const nodeModulesDir = path.dirname(parentScope);
|
|
77
|
-
if (
|
|
78
|
-
path.basename(parentScope) === '@bitefoundry' &&
|
|
79
|
-
path.basename(nodeModulesDir) === 'node_modules'
|
|
80
|
-
) {
|
|
81
|
-
dirs.push(path.join(nodeModulesDir, '.bin'));
|
|
82
|
-
// On global Windows npm installs (`%APPDATA%\npm\node_modules\@bitefoundry\notion`),
|
|
83
|
-
// shims sit directly in the parent of `node_modules` even when `npm_config_prefix` is unset in npm v9+.
|
|
84
|
-
dirs.push(path.dirname(nodeModulesDir));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (env.npm_config_global === 'true' && env.npm_config_prefix) {
|
|
88
|
-
const prefix = path.resolve(env.npm_config_prefix);
|
|
89
|
-
dirs.push(prefix);
|
|
90
|
-
dirs.push(path.join(prefix, 'bin'));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return [...new Set(dirs)];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function shimReferencesLauncher(content, name) {
|
|
97
|
-
const normalized = content.replace(/\\/g, '/').toLowerCase();
|
|
98
|
-
return (
|
|
99
|
-
normalized.includes('@bitefoundry/notion') &&
|
|
100
|
-
normalized.includes(`bin/${name}.js`)
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function rewriteWindowsShims(shimDir, pkgRoot) {
|
|
105
|
-
if (!fs.existsSync(shimDir)) return 0;
|
|
106
|
-
let rewritten = 0;
|
|
107
|
-
|
|
108
|
-
for (const name of BIN_NAMES) {
|
|
109
|
-
const exePath = path.join(pkgRoot, 'bin', `${name}.exe`);
|
|
110
|
-
if (!fs.existsSync(exePath)) continue;
|
|
111
|
-
|
|
112
|
-
const relWin = path.relative(shimDir, exePath).replace(/\//g, '\\');
|
|
113
|
-
const relPosix = path.relative(shimDir, exePath).replace(/\\/g, '/');
|
|
114
|
-
|
|
115
|
-
// 1. `<name>.cmd`
|
|
116
|
-
const cmdPath = path.join(shimDir, `${name}.cmd`);
|
|
117
|
-
try {
|
|
118
|
-
if (fs.existsSync(cmdPath)) {
|
|
119
|
-
const content = fs.readFileSync(cmdPath, 'utf8');
|
|
120
|
-
if (shimReferencesLauncher(content, name)) {
|
|
121
|
-
fs.writeFileSync(cmdPath, `@ECHO off\r\n"%~dp0${relWin}" %*\r\n`, 'utf8');
|
|
122
|
-
rewritten += 1;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
} catch {
|
|
126
|
-
// Fall back cleanly to bin/<name>.js if shim is read-only or locked.
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// 2. `<name>.ps1`
|
|
130
|
-
const ps1Path = path.join(shimDir, `${name}.ps1`);
|
|
131
|
-
try {
|
|
132
|
-
if (fs.existsSync(ps1Path)) {
|
|
133
|
-
const content = fs.readFileSync(ps1Path, 'utf8');
|
|
134
|
-
if (shimReferencesLauncher(content, name)) {
|
|
135
|
-
const ps1Body = [
|
|
136
|
-
'#!/usr/bin/env pwsh',
|
|
137
|
-
'$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent',
|
|
138
|
-
`$exePath = Join-Path $basedir '${relWin.replace(/'/g, "''")}'`,
|
|
139
|
-
'if ($MyInvocation.ExpectingInput) {',
|
|
140
|
-
' $input | & $exePath @args',
|
|
141
|
-
'} else {',
|
|
142
|
-
' & $exePath @args',
|
|
143
|
-
'}',
|
|
144
|
-
'exit $LASTEXITCODE',
|
|
145
|
-
'',
|
|
146
|
-
].join('\n');
|
|
147
|
-
fs.writeFileSync(ps1Path, ps1Body, 'utf8');
|
|
148
|
-
rewritten += 1;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
} catch {
|
|
152
|
-
// Fall back cleanly.
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// 3. `<name>` (Git Bash / MSYS2 POSIX shell shim)
|
|
156
|
-
const shPath = path.join(shimDir, name);
|
|
157
|
-
try {
|
|
158
|
-
if (fs.existsSync(shPath)) {
|
|
159
|
-
const content = fs.readFileSync(shPath, 'utf8');
|
|
160
|
-
if (shimReferencesLauncher(content, name)) {
|
|
161
|
-
const shBody = [
|
|
162
|
-
'#!/bin/sh',
|
|
163
|
-
'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
|
|
164
|
-
`exec "$basedir/${relPosix}" "$@"`,
|
|
165
|
-
'',
|
|
166
|
-
].join('\n');
|
|
167
|
-
fs.writeFileSync(shPath, shBody, 'utf8');
|
|
168
|
-
rewritten += 1;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
} catch {
|
|
172
|
-
// Fall back cleanly.
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return rewritten;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
export function runPostinstall(options = {}) {
|
|
180
|
-
const pkgRoot = path.resolve(options.pkgRoot || PKG_ROOT);
|
|
181
|
-
const platform = options.platform || process.platform;
|
|
182
|
-
const arch = options.arch || process.arch;
|
|
183
|
-
const env = options.env || process.env;
|
|
184
|
-
|
|
185
|
-
// CRITICAL SAFETY GUARD: Never mutate files inside the git development repository.
|
|
186
|
-
if (
|
|
187
|
-
fs.existsSync(path.join(pkgRoot, 'Cargo.toml')) ||
|
|
188
|
-
fs.existsSync(path.join(pkgRoot, '.git'))
|
|
189
|
-
) {
|
|
190
|
-
return { skipped: true, reason: 'dev_repo' };
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const platformKey = `${platform}-${arch}`;
|
|
194
|
-
const packageName = `@bitefoundry/notion-${platformKey}`;
|
|
195
|
-
const binaryName = platform === 'win32' ? 'notion.exe' : 'notion';
|
|
196
|
-
|
|
197
|
-
let packageDir = null;
|
|
198
|
-
try {
|
|
199
|
-
const requireFromPkg = createRequire(path.join(pkgRoot, 'package.json'));
|
|
200
|
-
packageDir = path.dirname(requireFromPkg.resolve(`${packageName}/package.json`));
|
|
201
|
-
} catch {
|
|
202
|
-
packageDir = null;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const binaryPath = packageDir ? path.join(packageDir, 'bin', binaryName) : null;
|
|
206
|
-
if (!binaryPath || !fs.existsSync(binaryPath)) {
|
|
207
|
-
return { skipped: true, reason: 'missing_platform_binary' };
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const binDir = path.join(pkgRoot, 'bin');
|
|
211
|
-
if (!fs.existsSync(binDir)) {
|
|
212
|
-
return { skipped: true, reason: 'missing_bin_dir' };
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (platform === 'win32') {
|
|
216
|
-
let linked = 0;
|
|
217
|
-
for (const name of BIN_NAMES) {
|
|
218
|
-
const destExe = path.join(binDir, `${name}.exe`);
|
|
219
|
-
try {
|
|
220
|
-
linkOrCopyFile(binaryPath, destExe);
|
|
221
|
-
linked += 1;
|
|
222
|
-
} catch {
|
|
223
|
-
// Leave fallback JS launcher untouched.
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
let shimsRewritten = 0;
|
|
228
|
-
for (const shimDir of collectWindowsShimDirs(pkgRoot, env)) {
|
|
229
|
-
try {
|
|
230
|
-
shimsRewritten += rewriteWindowsShims(shimDir, pkgRoot);
|
|
231
|
-
} catch {
|
|
232
|
-
// Never fail npm install on read-only or custom shims.
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return { skipped: false, platform, linked, shimsRewritten };
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// Unix (linux, darwin): chmod once and replace bin/*.js with hardlink/symlink to native binary.
|
|
239
|
-
try {
|
|
240
|
-
fs.chmodSync(binaryPath, 0o755);
|
|
241
|
-
} catch {
|
|
242
|
-
// Ignore on read-only stores.
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
let linked = 0;
|
|
246
|
-
for (const name of BIN_NAMES) {
|
|
247
|
-
const destJs = path.join(binDir, `${name}.js`);
|
|
248
|
-
try {
|
|
249
|
-
replaceJsWithNativeLinkUnix(binaryPath, destJs);
|
|
250
|
-
linked += 1;
|
|
251
|
-
} catch {
|
|
252
|
-
// Leave fallback JS launcher intact.
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return { skipped: false, platform, linked };
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
260
|
-
try {
|
|
261
|
-
runPostinstall();
|
|
262
|
-
} catch {
|
|
263
|
-
// Postinstall optimization must never fail npm install.
|
|
264
|
-
}
|
|
265
|
-
process.exit(0);
|
|
266
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Zero-Node-Process npm native binary linker.
|
|
3
|
+
//
|
|
4
|
+
// On Unix (linux/darwin): replaces installed `bin/notion.js` with
|
|
5
|
+
// hardlinks/symlinks directly to `@bitefoundry/notion-<platform>-<arch>/bin/notion` so npm's
|
|
6
|
+
// bin symlink execve's the native Rust binary directly (0ms Node startup, 0MB Node RSS).
|
|
7
|
+
//
|
|
8
|
+
// On Windows (win32): creates NTFS hardlinks (fallback copy) `bin/notion.exe`
|
|
9
|
+
// and rewrites npm's `.bin/notion[.cmd|.ps1]` shims when present to invoke
|
|
10
|
+
// the native executable directly without spawning `node.exe`.
|
|
11
|
+
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { createRequire } from 'node:module';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
|
|
17
|
+
const BIN_NAMES = ['notion'];
|
|
18
|
+
const PKG_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
19
|
+
|
|
20
|
+
function linkOrCopyFile(source, destination) {
|
|
21
|
+
const tmp = `${destination}.tmp-${process.pid}`;
|
|
22
|
+
try {
|
|
23
|
+
fs.rmSync(tmp, { force: true });
|
|
24
|
+
} catch {
|
|
25
|
+
// Ignore cleanup errors on nonexistent temp paths.
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
fs.linkSync(source, tmp);
|
|
29
|
+
} catch {
|
|
30
|
+
fs.copyFileSync(source, tmp);
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
fs.renameSync(tmp, destination);
|
|
34
|
+
} catch {
|
|
35
|
+
try {
|
|
36
|
+
fs.rmSync(destination, { force: true });
|
|
37
|
+
} catch {
|
|
38
|
+
// Ignore; renameSync below will surface or fail safely.
|
|
39
|
+
}
|
|
40
|
+
fs.renameSync(tmp, destination);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function replaceJsWithNativeLinkUnix(binaryPath, destinationJs) {
|
|
45
|
+
const tmp = `${destinationJs}.tmp-${process.pid}`;
|
|
46
|
+
try {
|
|
47
|
+
fs.rmSync(tmp, { force: true });
|
|
48
|
+
} catch {
|
|
49
|
+
// Ignore cleanup errors.
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
fs.linkSync(binaryPath, tmp);
|
|
53
|
+
} catch {
|
|
54
|
+
try {
|
|
55
|
+
fs.rmSync(tmp, { force: true });
|
|
56
|
+
} catch {
|
|
57
|
+
// Ignore cleanup errors.
|
|
58
|
+
}
|
|
59
|
+
fs.symlinkSync(binaryPath, tmp);
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
fs.renameSync(tmp, destinationJs);
|
|
63
|
+
} catch (err) {
|
|
64
|
+
try {
|
|
65
|
+
fs.rmSync(tmp, { force: true });
|
|
66
|
+
} catch {
|
|
67
|
+
// Ignore cleanup errors.
|
|
68
|
+
}
|
|
69
|
+
throw err;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function collectWindowsShimDirs(pkgRoot, env = process.env) {
|
|
74
|
+
const dirs = [];
|
|
75
|
+
const parentScope = path.dirname(pkgRoot);
|
|
76
|
+
const nodeModulesDir = path.dirname(parentScope);
|
|
77
|
+
if (
|
|
78
|
+
path.basename(parentScope) === '@bitefoundry' &&
|
|
79
|
+
path.basename(nodeModulesDir) === 'node_modules'
|
|
80
|
+
) {
|
|
81
|
+
dirs.push(path.join(nodeModulesDir, '.bin'));
|
|
82
|
+
// On global Windows npm installs (`%APPDATA%\npm\node_modules\@bitefoundry\notion`),
|
|
83
|
+
// shims sit directly in the parent of `node_modules` even when `npm_config_prefix` is unset in npm v9+.
|
|
84
|
+
dirs.push(path.dirname(nodeModulesDir));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (env.npm_config_global === 'true' && env.npm_config_prefix) {
|
|
88
|
+
const prefix = path.resolve(env.npm_config_prefix);
|
|
89
|
+
dirs.push(prefix);
|
|
90
|
+
dirs.push(path.join(prefix, 'bin'));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return [...new Set(dirs)];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function shimReferencesLauncher(content, name) {
|
|
97
|
+
const normalized = content.replace(/\\/g, '/').toLowerCase();
|
|
98
|
+
return (
|
|
99
|
+
normalized.includes('@bitefoundry/notion') &&
|
|
100
|
+
normalized.includes(`bin/${name}.js`)
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function rewriteWindowsShims(shimDir, pkgRoot) {
|
|
105
|
+
if (!fs.existsSync(shimDir)) return 0;
|
|
106
|
+
let rewritten = 0;
|
|
107
|
+
|
|
108
|
+
for (const name of BIN_NAMES) {
|
|
109
|
+
const exePath = path.join(pkgRoot, 'bin', `${name}.exe`);
|
|
110
|
+
if (!fs.existsSync(exePath)) continue;
|
|
111
|
+
|
|
112
|
+
const relWin = path.relative(shimDir, exePath).replace(/\//g, '\\');
|
|
113
|
+
const relPosix = path.relative(shimDir, exePath).replace(/\\/g, '/');
|
|
114
|
+
|
|
115
|
+
// 1. `<name>.cmd`
|
|
116
|
+
const cmdPath = path.join(shimDir, `${name}.cmd`);
|
|
117
|
+
try {
|
|
118
|
+
if (fs.existsSync(cmdPath)) {
|
|
119
|
+
const content = fs.readFileSync(cmdPath, 'utf8');
|
|
120
|
+
if (shimReferencesLauncher(content, name)) {
|
|
121
|
+
fs.writeFileSync(cmdPath, `@ECHO off\r\n"%~dp0${relWin}" %*\r\n`, 'utf8');
|
|
122
|
+
rewritten += 1;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
// Fall back cleanly to bin/<name>.js if shim is read-only or locked.
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 2. `<name>.ps1`
|
|
130
|
+
const ps1Path = path.join(shimDir, `${name}.ps1`);
|
|
131
|
+
try {
|
|
132
|
+
if (fs.existsSync(ps1Path)) {
|
|
133
|
+
const content = fs.readFileSync(ps1Path, 'utf8');
|
|
134
|
+
if (shimReferencesLauncher(content, name)) {
|
|
135
|
+
const ps1Body = [
|
|
136
|
+
'#!/usr/bin/env pwsh',
|
|
137
|
+
'$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent',
|
|
138
|
+
`$exePath = Join-Path $basedir '${relWin.replace(/'/g, "''")}'`,
|
|
139
|
+
'if ($MyInvocation.ExpectingInput) {',
|
|
140
|
+
' $input | & $exePath @args',
|
|
141
|
+
'} else {',
|
|
142
|
+
' & $exePath @args',
|
|
143
|
+
'}',
|
|
144
|
+
'exit $LASTEXITCODE',
|
|
145
|
+
'',
|
|
146
|
+
].join('\n');
|
|
147
|
+
fs.writeFileSync(ps1Path, ps1Body, 'utf8');
|
|
148
|
+
rewritten += 1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
} catch {
|
|
152
|
+
// Fall back cleanly.
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 3. `<name>` (Git Bash / MSYS2 POSIX shell shim)
|
|
156
|
+
const shPath = path.join(shimDir, name);
|
|
157
|
+
try {
|
|
158
|
+
if (fs.existsSync(shPath)) {
|
|
159
|
+
const content = fs.readFileSync(shPath, 'utf8');
|
|
160
|
+
if (shimReferencesLauncher(content, name)) {
|
|
161
|
+
const shBody = [
|
|
162
|
+
'#!/bin/sh',
|
|
163
|
+
'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
|
|
164
|
+
`exec "$basedir/${relPosix}" "$@"`,
|
|
165
|
+
'',
|
|
166
|
+
].join('\n');
|
|
167
|
+
fs.writeFileSync(shPath, shBody, 'utf8');
|
|
168
|
+
rewritten += 1;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} catch {
|
|
172
|
+
// Fall back cleanly.
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return rewritten;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function runPostinstall(options = {}) {
|
|
180
|
+
const pkgRoot = path.resolve(options.pkgRoot || PKG_ROOT);
|
|
181
|
+
const platform = options.platform || process.platform;
|
|
182
|
+
const arch = options.arch || process.arch;
|
|
183
|
+
const env = options.env || process.env;
|
|
184
|
+
|
|
185
|
+
// CRITICAL SAFETY GUARD: Never mutate files inside the git development repository.
|
|
186
|
+
if (
|
|
187
|
+
fs.existsSync(path.join(pkgRoot, 'Cargo.toml')) ||
|
|
188
|
+
fs.existsSync(path.join(pkgRoot, '.git'))
|
|
189
|
+
) {
|
|
190
|
+
return { skipped: true, reason: 'dev_repo' };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const platformKey = `${platform}-${arch}`;
|
|
194
|
+
const packageName = `@bitefoundry/notion-${platformKey}`;
|
|
195
|
+
const binaryName = platform === 'win32' ? 'notion.exe' : 'notion';
|
|
196
|
+
|
|
197
|
+
let packageDir = null;
|
|
198
|
+
try {
|
|
199
|
+
const requireFromPkg = createRequire(path.join(pkgRoot, 'package.json'));
|
|
200
|
+
packageDir = path.dirname(requireFromPkg.resolve(`${packageName}/package.json`));
|
|
201
|
+
} catch {
|
|
202
|
+
packageDir = null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const binaryPath = packageDir ? path.join(packageDir, 'bin', binaryName) : null;
|
|
206
|
+
if (!binaryPath || !fs.existsSync(binaryPath)) {
|
|
207
|
+
return { skipped: true, reason: 'missing_platform_binary' };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const binDir = path.join(pkgRoot, 'bin');
|
|
211
|
+
if (!fs.existsSync(binDir)) {
|
|
212
|
+
return { skipped: true, reason: 'missing_bin_dir' };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (platform === 'win32') {
|
|
216
|
+
let linked = 0;
|
|
217
|
+
for (const name of BIN_NAMES) {
|
|
218
|
+
const destExe = path.join(binDir, `${name}.exe`);
|
|
219
|
+
try {
|
|
220
|
+
linkOrCopyFile(binaryPath, destExe);
|
|
221
|
+
linked += 1;
|
|
222
|
+
} catch {
|
|
223
|
+
// Leave fallback JS launcher untouched.
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let shimsRewritten = 0;
|
|
228
|
+
for (const shimDir of collectWindowsShimDirs(pkgRoot, env)) {
|
|
229
|
+
try {
|
|
230
|
+
shimsRewritten += rewriteWindowsShims(shimDir, pkgRoot);
|
|
231
|
+
} catch {
|
|
232
|
+
// Never fail npm install on read-only or custom shims.
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return { skipped: false, platform, linked, shimsRewritten };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Unix (linux, darwin): chmod once and replace bin/*.js with hardlink/symlink to native binary.
|
|
239
|
+
try {
|
|
240
|
+
fs.chmodSync(binaryPath, 0o755);
|
|
241
|
+
} catch {
|
|
242
|
+
// Ignore on read-only stores.
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
let linked = 0;
|
|
246
|
+
for (const name of BIN_NAMES) {
|
|
247
|
+
const destJs = path.join(binDir, `${name}.js`);
|
|
248
|
+
try {
|
|
249
|
+
replaceJsWithNativeLinkUnix(binaryPath, destJs);
|
|
250
|
+
linked += 1;
|
|
251
|
+
} catch {
|
|
252
|
+
// Leave fallback JS launcher intact.
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return { skipped: false, platform, linked };
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
260
|
+
try {
|
|
261
|
+
runPostinstall();
|
|
262
|
+
} catch {
|
|
263
|
+
// Postinstall optimization must never fail npm install.
|
|
264
|
+
}
|
|
265
|
+
process.exit(0);
|
|
266
|
+
}
|