@souhengai/naxos 2.8.0 → 2.8.2
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/lib/postinstall.js +127 -0
- package/package.json +1 -1
package/lib/postinstall.js
CHANGED
|
@@ -10,6 +10,7 @@ const { existsSync, mkdirSync, chmodSync, renameSync, unlinkSync, createWriteStr
|
|
|
10
10
|
const { join, dirname } = require("path");
|
|
11
11
|
const https = require("https");
|
|
12
12
|
const http = require("http");
|
|
13
|
+
const { execSync } = require("child_process");
|
|
13
14
|
|
|
14
15
|
const BASE_URL = "https://mineify.de/naxos";
|
|
15
16
|
const VERSION = require("../package.json").version;
|
|
@@ -104,6 +105,128 @@ function download(url, dest) {
|
|
|
104
105
|
});
|
|
105
106
|
}
|
|
106
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Get npm's global bin directory (where `naxos.cmd` / `naxos` shim is installed).
|
|
110
|
+
*/
|
|
111
|
+
function getNpmBinDir() {
|
|
112
|
+
try {
|
|
113
|
+
const prefix = execSync("npm config get prefix", { encoding: "utf-8" }).trim();
|
|
114
|
+
return process.platform === "win32" ? prefix : join(prefix, "bin");
|
|
115
|
+
} catch {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Check if a directory is in the runtime PATH.
|
|
122
|
+
*/
|
|
123
|
+
function isDirInRuntimePath(dir) {
|
|
124
|
+
const pathSep = process.platform === "win32" ? ";" : ":";
|
|
125
|
+
const pathParts = (process.env.PATH || "")
|
|
126
|
+
.split(pathSep)
|
|
127
|
+
.map(p => p.toLowerCase().replace(/[\\\/]+$/, ""));
|
|
128
|
+
const needle = dir.toLowerCase().replace(/[\\\/]+$/, "");
|
|
129
|
+
return pathParts.includes(needle);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Add directory to the user's PATH on Windows via PowerShell.
|
|
134
|
+
* Uses User-level env var — no admin required.
|
|
135
|
+
* Returns true on success.
|
|
136
|
+
*/
|
|
137
|
+
function addToUserPathWindows(dir) {
|
|
138
|
+
const { spawnSync } = require("child_process");
|
|
139
|
+
const script = `
|
|
140
|
+
$dir = '${dir.replace(/'/g, "''")}'
|
|
141
|
+
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
|
|
142
|
+
if ($null -eq $userPath) { $userPath = '' }
|
|
143
|
+
$parts = $userPath -split ';' | Where-Object { $_ -ne '' }
|
|
144
|
+
$alreadyIn = $false
|
|
145
|
+
foreach ($p in $parts) {
|
|
146
|
+
if ($p.TrimEnd('\\') -ieq $dir.TrimEnd('\\')) { $alreadyIn = $true; break }
|
|
147
|
+
}
|
|
148
|
+
if (-not $alreadyIn) {
|
|
149
|
+
if ($userPath -eq '') { $newPath = $dir } else { $newPath = "$userPath;$dir" }
|
|
150
|
+
[Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
|
|
151
|
+
Write-Output 'ADDED'
|
|
152
|
+
} else {
|
|
153
|
+
Write-Output 'EXISTS'
|
|
154
|
+
}
|
|
155
|
+
`;
|
|
156
|
+
const result = spawnSync("powershell.exe",
|
|
157
|
+
["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-Command", script],
|
|
158
|
+
{ encoding: "utf-8", timeout: 10000 }
|
|
159
|
+
);
|
|
160
|
+
if (result.error || result.status !== 0) return false;
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Add the export line to shell rc files on Unix if not already present.
|
|
166
|
+
*/
|
|
167
|
+
function addToUserPathUnix(dir) {
|
|
168
|
+
const { readFileSync, appendFileSync } = require("fs");
|
|
169
|
+
const { homedir } = require("os");
|
|
170
|
+
|
|
171
|
+
const candidates = [
|
|
172
|
+
join(homedir(), ".bashrc"),
|
|
173
|
+
join(homedir(), ".zshrc"),
|
|
174
|
+
join(homedir(), ".profile"),
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
const exportLine = `\n# Added by @souhengai/naxos\nexport PATH="$PATH:${dir}"\n`;
|
|
178
|
+
const marker = `# Added by @souhengai/naxos`;
|
|
179
|
+
let modified = false;
|
|
180
|
+
|
|
181
|
+
for (const rc of candidates) {
|
|
182
|
+
if (!existsSync(rc)) continue;
|
|
183
|
+
try {
|
|
184
|
+
const content = readFileSync(rc, "utf-8");
|
|
185
|
+
if (content.includes(marker) || content.includes(dir)) continue;
|
|
186
|
+
appendFileSync(rc, exportLine);
|
|
187
|
+
modified = true;
|
|
188
|
+
} catch {
|
|
189
|
+
// Skip unreadable
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return modified;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Check PATH and auto-fix on supported platforms.
|
|
197
|
+
*/
|
|
198
|
+
function checkPath() {
|
|
199
|
+
const npmBin = getNpmBinDir();
|
|
200
|
+
if (!npmBin) return;
|
|
201
|
+
|
|
202
|
+
if (isDirInRuntimePath(npmBin)) return; // already good
|
|
203
|
+
|
|
204
|
+
console.log(" \x1b[33m⚠\x1b[0m npm's global bin is not in PATH — fixing automatically...");
|
|
205
|
+
console.log(" " + npmBin);
|
|
206
|
+
console.log("");
|
|
207
|
+
|
|
208
|
+
if (process.platform === "win32") {
|
|
209
|
+
if (addToUserPathWindows(npmBin)) {
|
|
210
|
+
console.log(" \x1b[32m✓\x1b[0m Added to User PATH.");
|
|
211
|
+
console.log(" \x1b[1mIMPORTANT: Open a NEW terminal window\x1b[0m (PATH changes don't affect running terminals).");
|
|
212
|
+
} else {
|
|
213
|
+
console.log(" \x1b[31m✗\x1b[0m Could not update PATH automatically. Manual fix:");
|
|
214
|
+
console.log(" 1. Open System Properties → Environment Variables → User PATH");
|
|
215
|
+
console.log(" 2. Add: \x1b[36m" + npmBin + "\x1b[0m");
|
|
216
|
+
console.log(" 3. Open a NEW terminal");
|
|
217
|
+
}
|
|
218
|
+
} else {
|
|
219
|
+
if (addToUserPathUnix(npmBin)) {
|
|
220
|
+
console.log(" \x1b[32m✓\x1b[0m Added to ~/.bashrc and ~/.zshrc (where they exist).");
|
|
221
|
+
console.log(" \x1b[1mRun:\x1b[0m \x1b[36msource ~/.bashrc\x1b[0m \x1b[2m(or open a new terminal)\x1b[0m");
|
|
222
|
+
} else {
|
|
223
|
+
console.log(" Add this to your shell rc file:");
|
|
224
|
+
console.log(" \x1b[36mexport PATH=\"$PATH:" + npmBin + "\"\x1b[0m");
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
console.log("");
|
|
228
|
+
}
|
|
229
|
+
|
|
107
230
|
async function main() {
|
|
108
231
|
console.log("");
|
|
109
232
|
console.log(" \x1b[35mNaxos\x1b[0m v" + VERSION + " — Installing...");
|
|
@@ -136,6 +259,10 @@ async function main() {
|
|
|
136
259
|
|
|
137
260
|
console.log(" \x1b[32m✓\x1b[0m Installed successfully!");
|
|
138
261
|
console.log("");
|
|
262
|
+
|
|
263
|
+
// Check if the npm global bin dir is in PATH
|
|
264
|
+
checkPath();
|
|
265
|
+
|
|
139
266
|
console.log(" Run \x1b[1mnaxos setup\x1b[0m to get started.");
|
|
140
267
|
console.log("");
|
|
141
268
|
} catch (err) {
|