node-version-use 2.1.3 → 2.1.5

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.
@@ -0,0 +1,425 @@
1
+ "use strict";
2
+ /**
3
+ * Postinstall script for node-version-use
4
+ *
5
+ * Downloads the platform-specific binary and installs it to ~/.nvu/bin/
6
+ * This enables transparent Node version switching.
7
+ *
8
+ * Uses safe atomic download pattern:
9
+ * 1. Download to temp file
10
+ * 2. Extract to temp directory
11
+ * 3. Atomic rename to final location
12
+ */ var spawn = require('child_process').spawn;
13
+ var exit = require('exit-compat');
14
+ var fs = require('fs');
15
+ var mkdirp = require('mkdirp-classic');
16
+ var os = require('os');
17
+ var path = require('path');
18
+ var hasHomedir = typeof os.homedir === 'function';
19
+ function homedir() {
20
+ if (hasHomedir) {
21
+ return os.homedir();
22
+ }
23
+ var home = require('homedir-polyfill');
24
+ return home();
25
+ }
26
+ module.exports = {
27
+ homedir: homedir
28
+ };
29
+ // Configuration
30
+ var GITHUB_REPO = 'kmalakoff/node-version-use';
31
+ // Path is relative to dist/cjs/scripts/ at runtime
32
+ var BINARY_VERSION = require(path.join(__dirname, '..', 'package.json')).binaryVersion;
33
+ /**
34
+ * Get the platform-specific archive base name (without extension)
35
+ */ function getArchiveBaseName() {
36
+ var platform = os.platform();
37
+ var arch = os.arch();
38
+ var platformMap = {
39
+ darwin: 'darwin',
40
+ linux: 'linux',
41
+ win32: 'win32'
42
+ };
43
+ var archMap = {
44
+ x64: 'x64',
45
+ arm64: 'arm64',
46
+ amd64: 'x64'
47
+ };
48
+ var platformName = platformMap[platform];
49
+ var archName = archMap[arch];
50
+ if (!platformName || !archName) {
51
+ return null;
52
+ }
53
+ return "nvu-binary-".concat(platformName, "-").concat(archName);
54
+ }
55
+ /**
56
+ * Get the extracted binary name (includes .exe on Windows)
57
+ */ function getExtractedBinaryName(archiveBaseName) {
58
+ var ext = os.platform() === 'win32' ? '.exe' : '';
59
+ return archiveBaseName + ext;
60
+ }
61
+ /**
62
+ * Get the download URL for the binary archive
63
+ */ function getDownloadUrl(archiveBaseName) {
64
+ var ext = os.platform() === 'win32' ? '.zip' : '.tar.gz';
65
+ return "https://github.com/".concat(GITHUB_REPO, "/releases/download/binary-v").concat(BINARY_VERSION, "/").concat(archiveBaseName).concat(ext);
66
+ }
67
+ /**
68
+ * Copy file
69
+ */ function copyFileSync(src, dest) {
70
+ var content = fs.readFileSync(src);
71
+ fs.writeFileSync(dest, content);
72
+ }
73
+ /**
74
+ * Atomic rename with fallback to copy+delete for cross-device moves
75
+ */ function atomicRename(src, dest, callback) {
76
+ fs.rename(src, dest, function(err) {
77
+ if (!err) {
78
+ callback(null);
79
+ return;
80
+ }
81
+ // Cross-device link error - fall back to copy + delete
82
+ if (err.code === 'EXDEV') {
83
+ try {
84
+ copyFileSync(src, dest);
85
+ fs.unlinkSync(src);
86
+ callback(null);
87
+ } catch (copyErr) {
88
+ callback(copyErr);
89
+ }
90
+ return;
91
+ }
92
+ callback(err);
93
+ });
94
+ }
95
+ /**
96
+ * Remove directory recursively
97
+ */ function rmRecursive(dir) {
98
+ if (!fs.existsSync(dir)) return;
99
+ var files = fs.readdirSync(dir);
100
+ for(var i = 0; i < files.length; i++){
101
+ var filePath = path.join(dir, files[i]);
102
+ var stat = fs.statSync(filePath);
103
+ if (stat.isDirectory()) {
104
+ rmRecursive(filePath);
105
+ } else {
106
+ fs.unlinkSync(filePath);
107
+ }
108
+ }
109
+ fs.rmdirSync(dir);
110
+ }
111
+ /**
112
+ * Get temp directory
113
+ */ function getTmpDir() {
114
+ return typeof os.tmpdir === 'function' ? os.tmpdir() : process.env.TMPDIR || process.env.TMP || process.env.TEMP || '/tmp';
115
+ }
116
+ /**
117
+ * Download using curl (macOS, Linux, Windows 10+)
118
+ */ function downloadWithCurl(downloadUrl, destPath, callback) {
119
+ var curl = spawn('curl', [
120
+ '-L',
121
+ '-f',
122
+ '-s',
123
+ '-o',
124
+ destPath,
125
+ downloadUrl
126
+ ]);
127
+ curl.on('close', function(code) {
128
+ if (code !== 0) {
129
+ // curl exit codes: 22 = HTTP error (4xx/5xx), 56 = receive error (often 404 with -f)
130
+ if (code === 22 || code === 56) {
131
+ callback(new Error('HTTP 404'));
132
+ } else {
133
+ callback(new Error("curl failed with exit code ".concat(code)));
134
+ }
135
+ return;
136
+ }
137
+ callback(null);
138
+ });
139
+ curl.on('error', function(err) {
140
+ callback(err);
141
+ });
142
+ }
143
+ /**
144
+ * Download using PowerShell (Windows 7+ fallback)
145
+ */ function downloadWithPowerShell(downloadUrl, destPath, callback) {
146
+ var psCommand = 'Invoke-WebRequest -Uri "'.concat(downloadUrl, '" -OutFile "').concat(destPath, '" -UseBasicParsing');
147
+ var ps = spawn('powershell', [
148
+ '-NoProfile',
149
+ '-Command',
150
+ psCommand
151
+ ]);
152
+ ps.on('close', function(code) {
153
+ if (code !== 0) {
154
+ callback(new Error("PowerShell download failed with exit code ".concat(code)));
155
+ return;
156
+ }
157
+ callback(null);
158
+ });
159
+ ps.on('error', function(err) {
160
+ callback(err);
161
+ });
162
+ }
163
+ /**
164
+ * Download a file - tries curl first, falls back to PowerShell on Windows
165
+ * Node 0.8's OpenSSL doesn't support TLS 1.2+ required by GitHub
166
+ */ function downloadFile(downloadUrl, destPath, callback) {
167
+ downloadWithCurl(downloadUrl, destPath, function(err) {
168
+ var _err_message;
169
+ if (!err) {
170
+ callback(null);
171
+ return;
172
+ }
173
+ // If curl failed and we're on Windows, try PowerShell
174
+ if (os.platform() === 'win32' && (err === null || err === void 0 ? void 0 : (_err_message = err.message) === null || _err_message === void 0 ? void 0 : _err_message.indexOf('ENOENT')) >= 0) {
175
+ downloadWithPowerShell(downloadUrl, destPath, callback);
176
+ return;
177
+ }
178
+ callback(err);
179
+ });
180
+ }
181
+ /**
182
+ * Extract archive to a directory (callback-based)
183
+ */ function extractArchive(archivePath, destDir, callback) {
184
+ var platform = os.platform();
185
+ if (platform === 'win32') {
186
+ // Windows: extract zip using PowerShell
187
+ var ps = spawn('powershell', [
188
+ '-Command',
189
+ "Expand-Archive -Path '".concat(archivePath, "' -DestinationPath '").concat(destDir, "' -Force")
190
+ ]);
191
+ ps.on('close', function(code) {
192
+ if (code !== 0) {
193
+ callback(new Error('Failed to extract archive'));
194
+ return;
195
+ }
196
+ callback(null);
197
+ });
198
+ } else {
199
+ // Unix: extract tar.gz
200
+ var tar = spawn('tar', [
201
+ '-xzf',
202
+ archivePath,
203
+ '-C',
204
+ destDir
205
+ ]);
206
+ tar.on('close', function(code) {
207
+ if (code !== 0) {
208
+ callback(new Error('Failed to extract archive'));
209
+ return;
210
+ }
211
+ callback(null);
212
+ });
213
+ }
214
+ }
215
+ /**
216
+ * Install binaries using atomic rename pattern
217
+ * 1. Extract to temp directory
218
+ * 2. Copy binary to temp files in destination directory
219
+ * 3. Atomic rename temp files to final names
220
+ */ function extractAndInstall(archivePath, destDir, binaryName, callback) {
221
+ var platform = os.platform();
222
+ var isWindows = platform === 'win32';
223
+ var ext = isWindows ? '.exe' : '';
224
+ // Create temp extraction directory
225
+ var tempExtractDir = path.join(getTmpDir(), "nvu-extract-".concat(Date.now()));
226
+ mkdirp.sync(tempExtractDir);
227
+ extractArchive(archivePath, tempExtractDir, function(extractErr) {
228
+ if (extractErr) {
229
+ rmRecursive(tempExtractDir);
230
+ callback(extractErr);
231
+ return;
232
+ }
233
+ var extractedPath = path.join(tempExtractDir, binaryName);
234
+ if (!fs.existsSync(extractedPath)) {
235
+ rmRecursive(tempExtractDir);
236
+ callback(new Error("Extracted binary not found: ".concat(binaryName)));
237
+ return;
238
+ }
239
+ // Binary names to install
240
+ var binaries = [
241
+ 'node',
242
+ 'npm',
243
+ 'npx',
244
+ 'corepack'
245
+ ];
246
+ var timestamp = Date.now();
247
+ var installError = null;
248
+ // Step 1: Copy extracted binary to temp files in destination directory
249
+ // This ensures the temp files are on the same filesystem for atomic rename
250
+ for(var i = 0; i < binaries.length; i++){
251
+ var name = binaries[i];
252
+ var tempDest = path.join(destDir, "".concat(name, ".tmp-").concat(timestamp).concat(ext));
253
+ try {
254
+ // Copy to temp file in destination directory
255
+ copyFileSync(extractedPath, tempDest);
256
+ // Set permissions on Unix
257
+ if (!isWindows) {
258
+ fs.chmodSync(tempDest, 493);
259
+ }
260
+ } catch (err) {
261
+ installError = err;
262
+ break;
263
+ }
264
+ }
265
+ if (installError) {
266
+ // Clean up any temp files we created
267
+ for(var j = 0; j < binaries.length; j++){
268
+ var tempPath = path.join(destDir, "".concat(binaries[j], ".tmp-").concat(timestamp).concat(ext));
269
+ if (fs.existsSync(tempPath)) {
270
+ try {
271
+ fs.unlinkSync(tempPath);
272
+ } catch (_e) {
273
+ // ignore cleanup errors
274
+ }
275
+ }
276
+ }
277
+ rmRecursive(tempExtractDir);
278
+ callback(installError);
279
+ return;
280
+ }
281
+ // Step 2: Atomic rename temp files to final names
282
+ var renameError = null;
283
+ function doRename(index) {
284
+ if (index >= binaries.length) {
285
+ // All renames complete
286
+ rmRecursive(tempExtractDir);
287
+ callback(renameError);
288
+ return;
289
+ }
290
+ var name = binaries[index];
291
+ var tempDest = path.join(destDir, "".concat(name, ".tmp-").concat(timestamp).concat(ext));
292
+ var finalDest = path.join(destDir, "".concat(name).concat(ext));
293
+ // Remove existing file if present (for atomic replacement)
294
+ if (fs.existsSync(finalDest)) {
295
+ try {
296
+ fs.unlinkSync(finalDest);
297
+ } catch (_e) {
298
+ // ignore cleanup errors
299
+ }
300
+ }
301
+ atomicRename(tempDest, finalDest, function(err) {
302
+ if (err && !renameError) {
303
+ renameError = err;
304
+ }
305
+ doRename(index + 1);
306
+ });
307
+ }
308
+ doRename(0);
309
+ });
310
+ }
311
+ /**
312
+ * Print setup instructions
313
+ */ function printInstructions(installed) {
314
+ var homedirPath = homedir();
315
+ var nvuBinPath = path.join(homedirPath, '.nvu', 'bin');
316
+ var platform = os.platform();
317
+ console.log('');
318
+ console.log('============================================================');
319
+ if (installed) {
320
+ console.log(' nvu binaries installed to ~/.nvu/bin/');
321
+ } else {
322
+ console.log(' nvu installed (binaries not yet available)');
323
+ }
324
+ console.log('============================================================');
325
+ console.log('');
326
+ console.log('To enable transparent Node version switching, add to your shell profile:');
327
+ console.log('');
328
+ if (platform === 'win32') {
329
+ console.log(' PowerShell (add to $PROFILE):');
330
+ console.log(' $env:PATH = "'.concat(nvuBinPath, ';$env:PATH"'));
331
+ console.log('');
332
+ console.log(' CMD (run as administrator):');
333
+ console.log(' setx PATH "'.concat(nvuBinPath, ';%PATH%"'));
334
+ } else {
335
+ console.log(' # For bash (~/.bashrc):');
336
+ console.log(' export PATH="$HOME/.nvu/bin:$PATH"');
337
+ console.log('');
338
+ console.log(' # For zsh (~/.zshrc):');
339
+ console.log(' export PATH="$HOME/.nvu/bin:$PATH"');
340
+ console.log('');
341
+ console.log(' # For fish (~/.config/fish/config.fish):');
342
+ console.log(' set -gx PATH $HOME/.nvu/bin $PATH');
343
+ }
344
+ console.log('');
345
+ console.log('Then restart your terminal or source your shell profile.');
346
+ console.log('');
347
+ console.log("Without this, 'nvu 18 npm test' still works - you just won't have");
348
+ console.log("transparent 'node' command override.");
349
+ console.log('============================================================');
350
+ }
351
+ /**
352
+ * Main installation function
353
+ */ function main() {
354
+ var archiveBaseName = getArchiveBaseName();
355
+ if (!archiveBaseName) {
356
+ console.log('postinstall: Unsupported platform/architecture for binary.');
357
+ console.log("Platform: ".concat(os.platform(), ", Arch: ").concat(os.arch()));
358
+ console.log('Binary not installed. You can still use nvu with explicit versions: nvu 18 npm test');
359
+ exit(0);
360
+ return;
361
+ }
362
+ var extractedBinaryName = getExtractedBinaryName(archiveBaseName);
363
+ var homedirPath = homedir();
364
+ var nvuDir = path.join(homedirPath, '.nvu');
365
+ var binDir = path.join(nvuDir, 'bin');
366
+ // Create directories
367
+ mkdirp.sync(nvuDir);
368
+ mkdirp.sync(binDir);
369
+ var downloadUrl = getDownloadUrl(archiveBaseName);
370
+ var ext = os.platform() === 'win32' ? '.zip' : '.tar.gz';
371
+ var tempPath = path.join(getTmpDir(), "nvu-binary-".concat(Date.now()).concat(ext));
372
+ console.log("postinstall: Downloading binary for ".concat(os.platform(), "-").concat(os.arch(), "..."));
373
+ downloadFile(downloadUrl, tempPath, function(downloadErr) {
374
+ if (downloadErr) {
375
+ var _downloadErr_message;
376
+ // Clean up temp file if it exists
377
+ if (fs.existsSync(tempPath)) {
378
+ try {
379
+ fs.unlinkSync(tempPath);
380
+ } catch (_e) {
381
+ // ignore cleanup errors
382
+ }
383
+ }
384
+ if (((_downloadErr_message = downloadErr.message) === null || _downloadErr_message === void 0 ? void 0 : _downloadErr_message.indexOf('404')) >= 0) {
385
+ console.log('postinstall: Binaries not yet published to GitHub releases.');
386
+ console.log('');
387
+ console.log('To build and install binaries locally:');
388
+ console.log(' cd node_modules/node-version-use/binary');
389
+ console.log(' make install');
390
+ console.log('');
391
+ console.log('Or wait for the next release which will include pre-built binaries.');
392
+ } else {
393
+ console.log("postinstall warning: Failed to install binary: ".concat(downloadErr.message || downloadErr));
394
+ console.log('You can still use nvu with explicit versions: nvu 18 npm test');
395
+ console.log('To install binaries manually: cd node_modules/node-version-use/binary && make install');
396
+ }
397
+ printInstructions(false);
398
+ exit(0);
399
+ return;
400
+ }
401
+ console.log('postinstall: Extracting binary...');
402
+ extractAndInstall(tempPath, binDir, extractedBinaryName, function(extractErr) {
403
+ // Clean up temp file
404
+ if (fs.existsSync(tempPath)) {
405
+ try {
406
+ fs.unlinkSync(tempPath);
407
+ } catch (_e) {
408
+ // ignore cleanup errors
409
+ }
410
+ }
411
+ if (extractErr) {
412
+ console.log("postinstall warning: Failed to extract binary: ".concat(extractErr.message || extractErr));
413
+ console.log('You can still use nvu with explicit versions: nvu 18 npm test');
414
+ printInstructions(false);
415
+ exit(0);
416
+ return;
417
+ }
418
+ console.log('postinstall: Binary installed successfully!');
419
+ printInstructions(true);
420
+ exit(0);
421
+ });
422
+ });
423
+ }
424
+ main();
425
+ /* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }