node-version-use 2.1.5 → 2.1.7
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/assets/installBinaries.cjs +284 -0
- package/assets/postinstall.cjs +11 -404
- package/dist/cjs/assets/installBinaries.cjs +284 -0
- package/dist/cjs/assets/installBinaries.cjs.map +1 -0
- package/dist/cjs/assets/installBinaries.d.cts +1 -0
- package/dist/cjs/assets/postinstall.cjs +11 -404
- package/dist/cjs/assets/postinstall.cjs.map +1 -1
- package/dist/cjs/commands/default.js.map +1 -1
- package/dist/cjs/commands/index.js +2 -3
- package/dist/cjs/commands/index.js.map +1 -1
- package/dist/cjs/commands/list.js.map +1 -1
- package/dist/cjs/commands/setup.js +23 -41
- package/dist/cjs/commands/setup.js.map +1 -1
- package/dist/cjs/commands/teardown.js +2 -1
- package/dist/cjs/commands/teardown.js.map +1 -1
- package/dist/cjs/commands/uninstall.js.map +1 -1
- package/dist/cjs/commands/which.js.map +1 -1
- package/dist/cjs/compat.d.cts +1 -0
- package/dist/cjs/compat.d.ts +1 -0
- package/dist/cjs/compat.js +11 -4
- package/dist/cjs/compat.js.map +1 -1
- package/dist/cjs/lib/findInstalledVersions.js.map +1 -1
- package/dist/esm/assets/installBinaries.cjs +282 -0
- package/dist/esm/assets/installBinaries.cjs.map +1 -0
- package/dist/esm/assets/installBinaries.d.cts +1 -0
- package/dist/esm/assets/postinstall.cjs +11 -404
- package/dist/esm/assets/postinstall.cjs.map +1 -1
- package/dist/esm/commands/default.js +8 -8
- package/dist/esm/commands/default.js.map +1 -1
- package/dist/esm/commands/index.js +2 -3
- package/dist/esm/commands/index.js.map +1 -1
- package/dist/esm/commands/list.js +3 -3
- package/dist/esm/commands/list.js.map +1 -1
- package/dist/esm/commands/setup.js +39 -57
- package/dist/esm/commands/setup.js.map +1 -1
- package/dist/esm/commands/teardown.js +2 -1
- package/dist/esm/commands/teardown.js.map +1 -1
- package/dist/esm/commands/uninstall.js +12 -12
- package/dist/esm/commands/uninstall.js.map +1 -1
- package/dist/esm/commands/which.js +5 -5
- package/dist/esm/commands/which.js.map +1 -1
- package/dist/esm/compat.d.ts +1 -0
- package/dist/esm/compat.js +17 -13
- package/dist/esm/compat.js.map +1 -1
- package/dist/esm/lib/findInstalledVersions.js +19 -19
- package/dist/esm/lib/findInstalledVersions.js.map +1 -1
- package/package.json +24 -19
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var envPathKey = require('env-path-key');
|
|
3
|
+
var fs = require('fs');
|
|
4
|
+
var safeRmSync = require('fs-remove-compat').safeRmSync;
|
|
5
|
+
var getFile = require('get-file-compat');
|
|
6
|
+
var mkdirp = require('mkdirp-classic');
|
|
7
|
+
var os = require('os');
|
|
8
|
+
var path = require('path');
|
|
9
|
+
var Queue = require('queue-cb');
|
|
10
|
+
var moduleRoot = require('module-root-sync');
|
|
11
|
+
var root = moduleRoot(__dirname);
|
|
12
|
+
// Configuration
|
|
13
|
+
var GITHUB_REPO = 'kmalakoff/node-version-use';
|
|
14
|
+
var BINARY_VERSION = require(path.join(root, 'package.json')).binaryVersion;
|
|
15
|
+
var isWindows = process.platform === 'win32' || /^(msys|cygwin)$/.test(process.env.OSTYPE);
|
|
16
|
+
var hasHomedir = typeof os.homedir === 'function';
|
|
17
|
+
function homedir() {
|
|
18
|
+
if (hasHomedir) return os.homedir();
|
|
19
|
+
var home = require('homedir-polyfill');
|
|
20
|
+
return home();
|
|
21
|
+
}
|
|
22
|
+
// Allow NVU_HOME override for testing
|
|
23
|
+
var storagePath = process.env.NVU_HOME || path.join(homedir(), '.nvu');
|
|
24
|
+
var hasTmpdir = typeof os.tmpdir === 'function';
|
|
25
|
+
function tmpdir() {
|
|
26
|
+
if (hasTmpdir) return os.tmpdir();
|
|
27
|
+
var osShim = require('os-shim');
|
|
28
|
+
return osShim.tmpdir();
|
|
29
|
+
}
|
|
30
|
+
function removeIfExistsSync(filePath) {
|
|
31
|
+
if (fs.existsSync(filePath)) {
|
|
32
|
+
try {
|
|
33
|
+
fs.unlinkSync(filePath);
|
|
34
|
+
} catch (_e) {
|
|
35
|
+
// ignore cleanup errors
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Get the platform-specific archive base name (without extension)
|
|
41
|
+
*/ function getArchiveBaseName() {
|
|
42
|
+
var platform = process.platform, arch = process.arch;
|
|
43
|
+
var platformMap = {
|
|
44
|
+
darwin: 'darwin',
|
|
45
|
+
linux: 'linux',
|
|
46
|
+
win32: 'win32'
|
|
47
|
+
};
|
|
48
|
+
var archMap = {
|
|
49
|
+
x64: 'x64',
|
|
50
|
+
arm64: 'arm64',
|
|
51
|
+
amd64: 'x64'
|
|
52
|
+
};
|
|
53
|
+
var platformName = platformMap[platform];
|
|
54
|
+
var archName = archMap[arch];
|
|
55
|
+
if (!platformName || !archName) return null;
|
|
56
|
+
return "nvu-binary-".concat(platformName, "-").concat(archName);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Copy file
|
|
60
|
+
*/ function copyFileSync(src, dest) {
|
|
61
|
+
var content = fs.readFileSync(src);
|
|
62
|
+
fs.writeFileSync(dest, content);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Atomic rename with fallback to copy+delete for cross-device moves
|
|
66
|
+
*/ function atomicRename(src, dest, callback) {
|
|
67
|
+
fs.rename(src, dest, function(err) {
|
|
68
|
+
if (!err) {
|
|
69
|
+
callback(null);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
// Cross-device link error - fall back to copy + delete
|
|
73
|
+
if (err.code === 'EXDEV') {
|
|
74
|
+
try {
|
|
75
|
+
copyFileSync(src, dest);
|
|
76
|
+
fs.unlinkSync(src);
|
|
77
|
+
callback(null);
|
|
78
|
+
} catch (copyErr) {
|
|
79
|
+
callback(copyErr);
|
|
80
|
+
}
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
callback(err);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Extract archive to a directory (callback-based)
|
|
88
|
+
*/ function extractArchive(archivePath, dest, callback) {
|
|
89
|
+
var Iterator = isWindows ? require('zip-iterator') : require('tar-iterator');
|
|
90
|
+
var stream = isWindows ? fs.createReadStream(archivePath) : fs.createReadStream(archivePath).pipe(require('zlib').createGunzip());
|
|
91
|
+
var iterator = new Iterator(stream);
|
|
92
|
+
// one by one
|
|
93
|
+
var links = [];
|
|
94
|
+
iterator.forEach(function(entry, callback) {
|
|
95
|
+
if (entry.type === 'link') {
|
|
96
|
+
links.unshift(entry);
|
|
97
|
+
callback();
|
|
98
|
+
} else if (entry.type === 'symlink') {
|
|
99
|
+
links.push(entry);
|
|
100
|
+
callback();
|
|
101
|
+
} else entry.create(dest, callback);
|
|
102
|
+
}, {
|
|
103
|
+
callbacks: true,
|
|
104
|
+
concurrency: 1
|
|
105
|
+
}, function(_err) {
|
|
106
|
+
// create links after directories and files
|
|
107
|
+
var queue = new Queue();
|
|
108
|
+
for(var index = 0; index < links.length; index++){
|
|
109
|
+
var entry = links[index];
|
|
110
|
+
queue.defer(entry.create.bind(entry, dest));
|
|
111
|
+
}
|
|
112
|
+
queue.await(function(err) {
|
|
113
|
+
iterator.destroy();
|
|
114
|
+
iterator = null;
|
|
115
|
+
callback(err);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Install binaries using atomic rename pattern
|
|
121
|
+
* 1. Extract to temp directory
|
|
122
|
+
* 2. Copy binary to temp files in destination directory
|
|
123
|
+
* 3. Atomic rename temp files to final names
|
|
124
|
+
*/ function extractAndInstall(archivePath, destDir, binaryName, callback) {
|
|
125
|
+
var ext = isWindows ? '.exe' : '';
|
|
126
|
+
// Create temp extraction directory
|
|
127
|
+
var tempExtractDir = path.join(tmpdir(), "nvu-extract-".concat(Date.now()));
|
|
128
|
+
mkdirp.sync(tempExtractDir);
|
|
129
|
+
extractArchive(archivePath, tempExtractDir, function(err) {
|
|
130
|
+
if (err) {
|
|
131
|
+
safeRmSync(tempExtractDir);
|
|
132
|
+
callback(err);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
var extractedPath = path.join(tempExtractDir, binaryName);
|
|
136
|
+
if (!fs.existsSync(extractedPath)) {
|
|
137
|
+
safeRmSync(tempExtractDir);
|
|
138
|
+
callback(new Error("Extracted binary not found: ".concat(binaryName, ". ").concat(archivePath, " ").concat(tempExtractDir)));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Binary names to install
|
|
142
|
+
var binaries = [
|
|
143
|
+
'node',
|
|
144
|
+
'npm',
|
|
145
|
+
'npx',
|
|
146
|
+
'corepack'
|
|
147
|
+
];
|
|
148
|
+
var timestamp = Date.now();
|
|
149
|
+
var installError = null;
|
|
150
|
+
// Step 1: Copy extracted binary to temp files in destination directory
|
|
151
|
+
// This ensures the temp files are on the same filesystem for atomic rename
|
|
152
|
+
for(var i = 0; i < binaries.length; i++){
|
|
153
|
+
var name = binaries[i];
|
|
154
|
+
var tempDest = path.join(destDir, "".concat(name, ".tmp-").concat(timestamp).concat(ext));
|
|
155
|
+
try {
|
|
156
|
+
// Copy to temp file in destination directory
|
|
157
|
+
copyFileSync(extractedPath, tempDest);
|
|
158
|
+
// Set permissions on Unix
|
|
159
|
+
if (!isWindows) fs.chmodSync(tempDest, 493);
|
|
160
|
+
} catch (err) {
|
|
161
|
+
installError = err;
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (installError) {
|
|
166
|
+
// Clean up any temp files we created
|
|
167
|
+
for(var j = 0; j < binaries.length; j++){
|
|
168
|
+
var tempPath = path.join(destDir, "".concat(binaries[j], ".tmp-").concat(timestamp).concat(ext));
|
|
169
|
+
removeIfExistsSync(tempPath);
|
|
170
|
+
}
|
|
171
|
+
safeRmSync(tempExtractDir);
|
|
172
|
+
callback(installError);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
// Step 2: Atomic rename temp files to final names
|
|
176
|
+
var renameError = null;
|
|
177
|
+
function doRename(index) {
|
|
178
|
+
if (index >= binaries.length) {
|
|
179
|
+
// All renames complete
|
|
180
|
+
safeRmSync(tempExtractDir);
|
|
181
|
+
callback(renameError);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
var name = binaries[index];
|
|
185
|
+
var tempDest = path.join(destDir, "".concat(name, ".tmp-").concat(timestamp).concat(ext));
|
|
186
|
+
var finalDest = path.join(destDir, "".concat(name).concat(ext));
|
|
187
|
+
// Remove existing file if present (for atomic replacement)
|
|
188
|
+
removeIfExistsSync(finalDest);
|
|
189
|
+
atomicRename(tempDest, finalDest, function(err) {
|
|
190
|
+
if (err && !renameError) {
|
|
191
|
+
renameError = err;
|
|
192
|
+
}
|
|
193
|
+
doRename(index + 1);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
doRename(0);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Print setup instructions
|
|
201
|
+
*/ module.exports.printInstructions = function printInstructions() {
|
|
202
|
+
var nvuBinPath = path.join(storagePath, 'bin');
|
|
203
|
+
console.log('nvu binaries installed in ~/.nvu/bin/');
|
|
204
|
+
var pathKey = envPathKey();
|
|
205
|
+
var envPath = process.env[pathKey] || '';
|
|
206
|
+
if (envPath.indexOf('.nvu/bin') >= 0) return; // path exists
|
|
207
|
+
// provide instructions for path setup
|
|
208
|
+
console.log('');
|
|
209
|
+
console.log('============================================================');
|
|
210
|
+
console.log(' Global node setup');
|
|
211
|
+
console.log('============================================================');
|
|
212
|
+
console.log('');
|
|
213
|
+
if (isWindows) {
|
|
214
|
+
console.log(' PowerShell (add to $PROFILE):');
|
|
215
|
+
console.log(' $env:PATH = "'.concat(nvuBinPath, ';$env:PATH"'));
|
|
216
|
+
console.log('');
|
|
217
|
+
console.log(' CMD (run as administrator):');
|
|
218
|
+
console.log(' setx PATH "'.concat(nvuBinPath, ';%PATH%"'));
|
|
219
|
+
} else {
|
|
220
|
+
console.log(' # For bash (~/.bashrc):');
|
|
221
|
+
console.log(' echo \'export PATH="$HOME/.nvu/bin:$PATH"\' >> ~/.bashrc');
|
|
222
|
+
console.log('');
|
|
223
|
+
console.log(' # For zsh (~/.zshrc):');
|
|
224
|
+
console.log(' echo \'export PATH="$HOME/.nvu/bin:$PATH"\' >> ~/.zshrc');
|
|
225
|
+
console.log('');
|
|
226
|
+
console.log(' # For fish (~/.config/fish/config.fish):');
|
|
227
|
+
console.log(" echo 'set -gx PATH $HOME/.nvu/bin $PATH' >> ~/.config/fish/config.fish");
|
|
228
|
+
}
|
|
229
|
+
console.log('');
|
|
230
|
+
console.log('Then restart your terminal or source your shell profile.');
|
|
231
|
+
console.log('');
|
|
232
|
+
console.log("Without this, 'nvu 18 npm test' still works - you just won't have");
|
|
233
|
+
console.log("transparent 'node' command override.");
|
|
234
|
+
console.log('============================================================');
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* Main installation function
|
|
238
|
+
*/ module.exports.installBinaries = function installBinaries(options, callback) {
|
|
239
|
+
var archiveBaseName = getArchiveBaseName();
|
|
240
|
+
if (!archiveBaseName) {
|
|
241
|
+
callback(new Error('Unsupported platform/architecture for binary.'));
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
var extractedBinaryName = "".concat(archiveBaseName).concat(isWindows ? '.exe' : '');
|
|
245
|
+
var binDir = path.join(storagePath, 'bin');
|
|
246
|
+
// check if we need to upgrade
|
|
247
|
+
if (!options.force) {
|
|
248
|
+
try {
|
|
249
|
+
// already installed
|
|
250
|
+
if (fs.statSync(binDir)) {
|
|
251
|
+
if (fs.readFileSync(path.join(binDir, 'version.txt'), 'utf8') === BINARY_VERSION) {
|
|
252
|
+
callback(null, false);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
} catch (_err) {}
|
|
257
|
+
}
|
|
258
|
+
// Create directories
|
|
259
|
+
mkdirp.sync(storagePath);
|
|
260
|
+
mkdirp.sync(binDir);
|
|
261
|
+
var downloadUrl = "https://github.com/".concat(GITHUB_REPO, "/releases/download/binary-v").concat(BINARY_VERSION, "/").concat(archiveBaseName).concat(isWindows ? '.zip' : '.tar.gz');
|
|
262
|
+
var tempPath = path.join(tmpdir(), "nvu-binary-".concat(Date.now()).concat(isWindows ? '.zip' : '.tar.gz'));
|
|
263
|
+
console.log("Downloading binary for ".concat(process.platform, "-").concat(process.arch, "..."));
|
|
264
|
+
getFile(downloadUrl, tempPath, function(err) {
|
|
265
|
+
if (err) {
|
|
266
|
+
removeIfExistsSync(tempPath);
|
|
267
|
+
callback(new Error("No prebuilt binary available for ".concat(process.platform, "-").concat(process.arch, ". Download: ").concat(downloadUrl, ". Error: ").concat(err.message)));
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
console.log('Extracting binary...');
|
|
271
|
+
extractAndInstall(tempPath, binDir, extractedBinaryName, function(err) {
|
|
272
|
+
removeIfExistsSync(tempPath);
|
|
273
|
+
if (err) {
|
|
274
|
+
callback(err);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
// save binary version for upgrade checks
|
|
278
|
+
fs.writeFileSync(path.join(binDir, 'version.txt'), BINARY_VERSION, 'utf8');
|
|
279
|
+
console.log('Binary installed successfully!');
|
|
280
|
+
callback(null, true);
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
/* 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; }
|