openmagic 0.4.0 → 0.5.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/dist/cli.js +70 -8
- package/dist/cli.js.map +1 -1
- package/dist/toolbar/index.global.js +1 -1
- package/dist/toolbar/index.global.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -71,6 +71,7 @@ function createProxyServer(targetHost, targetPort, serverPort) {
|
|
|
71
71
|
});
|
|
72
72
|
proxy.on("error", (err, _req, res) => {
|
|
73
73
|
if (res instanceof http.ServerResponse && !res.headersSent) {
|
|
74
|
+
const toolbarScript = buildInjectionScript(serverPort, token);
|
|
74
75
|
res.writeHead(502, { "Content-Type": "text/html" });
|
|
75
76
|
res.end(
|
|
76
77
|
`<html><body style="font-family:system-ui;padding:40px;background:#1a1a2e;color:#e0e0e0;">
|
|
@@ -78,6 +79,7 @@ function createProxyServer(targetHost, targetPort, serverPort) {
|
|
|
78
79
|
<p>Could not reach <code>${targetHost}:${targetPort}</code></p>
|
|
79
80
|
<p style="color:#888;">Make sure your dev server is running, then refresh this page.</p>
|
|
80
81
|
<p style="color:#666;font-size:13px;">${err.message}</p>
|
|
82
|
+
${toolbarScript}
|
|
81
83
|
</body></html>`
|
|
82
84
|
);
|
|
83
85
|
}
|
|
@@ -1419,7 +1421,7 @@ function createOpenMagicServer(proxyPort, roots) {
|
|
|
1419
1421
|
"Content-Type": "application/json",
|
|
1420
1422
|
"Access-Control-Allow-Origin": "*"
|
|
1421
1423
|
});
|
|
1422
|
-
res.end(JSON.stringify({ status: "ok", version: "0.
|
|
1424
|
+
res.end(JSON.stringify({ status: "ok", version: "0.5.0" }));
|
|
1423
1425
|
return;
|
|
1424
1426
|
}
|
|
1425
1427
|
res.writeHead(404);
|
|
@@ -1477,7 +1479,7 @@ async function handleMessage(ws, msg, state, roots, _proxyPort) {
|
|
|
1477
1479
|
id: msg.id,
|
|
1478
1480
|
type: "handshake.ok",
|
|
1479
1481
|
payload: {
|
|
1480
|
-
version: "0.
|
|
1482
|
+
version: "0.5.0",
|
|
1481
1483
|
roots,
|
|
1482
1484
|
config: {
|
|
1483
1485
|
provider: config.provider,
|
|
@@ -1803,6 +1805,11 @@ function checkDependenciesInstalled(cwd = process.cwd()) {
|
|
|
1803
1805
|
}
|
|
1804
1806
|
|
|
1805
1807
|
// src/cli.ts
|
|
1808
|
+
var origEmitWarning = process.emitWarning;
|
|
1809
|
+
process.emitWarning = function(warning, ...args) {
|
|
1810
|
+
if (typeof warning === "string" && warning.includes("util._extend")) return;
|
|
1811
|
+
return origEmitWarning.call(process, warning, ...args);
|
|
1812
|
+
};
|
|
1806
1813
|
process.on("unhandledRejection", (err) => {
|
|
1807
1814
|
console.error(chalk.red("\n [OpenMagic] Unhandled error:"), err?.message || err);
|
|
1808
1815
|
console.error(chalk.dim(" Please report this at https://github.com/Kalmuraee/OpenMagic/issues"));
|
|
@@ -1813,7 +1820,7 @@ process.on("uncaughtException", (err) => {
|
|
|
1813
1820
|
process.exit(1);
|
|
1814
1821
|
});
|
|
1815
1822
|
var childProcesses = [];
|
|
1816
|
-
var VERSION = "0.
|
|
1823
|
+
var VERSION = "0.5.0";
|
|
1817
1824
|
function ask(question) {
|
|
1818
1825
|
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
1819
1826
|
return new Promise((resolve3) => {
|
|
@@ -1873,6 +1880,57 @@ function runCommand(cmd, args, cwd = process.cwd()) {
|
|
|
1873
1880
|
}
|
|
1874
1881
|
});
|
|
1875
1882
|
}
|
|
1883
|
+
async function healthCheck(proxyPort, targetPort) {
|
|
1884
|
+
try {
|
|
1885
|
+
const controller = new AbortController();
|
|
1886
|
+
const timeout = setTimeout(() => controller.abort(), 5e3);
|
|
1887
|
+
const res = await fetch(`http://127.0.0.1:${proxyPort}/`, {
|
|
1888
|
+
signal: controller.signal,
|
|
1889
|
+
headers: { Accept: "text/html" }
|
|
1890
|
+
});
|
|
1891
|
+
clearTimeout(timeout);
|
|
1892
|
+
if (res.ok) {
|
|
1893
|
+
const text = await res.text();
|
|
1894
|
+
if (text.includes("__OPENMAGIC_LOADED__")) {
|
|
1895
|
+
console.log(chalk.green(" \u2713 Toolbar injection verified."));
|
|
1896
|
+
} else {
|
|
1897
|
+
console.log(chalk.yellow(" \u26A0 Page loaded but toolbar may not have injected (non-HTML response or CSP)."));
|
|
1898
|
+
}
|
|
1899
|
+
} else {
|
|
1900
|
+
console.log(
|
|
1901
|
+
chalk.yellow(` \u26A0 Dev server returned ${res.status}. Pages may have errors.`)
|
|
1902
|
+
);
|
|
1903
|
+
console.log(
|
|
1904
|
+
chalk.dim(" The toolbar will still appear on pages that load successfully.")
|
|
1905
|
+
);
|
|
1906
|
+
}
|
|
1907
|
+
} catch {
|
|
1908
|
+
console.log(
|
|
1909
|
+
chalk.yellow(" \u26A0 Could not verify proxy. The dev server may still be starting.")
|
|
1910
|
+
);
|
|
1911
|
+
console.log(
|
|
1912
|
+
chalk.dim(" Try refreshing the page in a few seconds.")
|
|
1913
|
+
);
|
|
1914
|
+
}
|
|
1915
|
+
console.log("");
|
|
1916
|
+
}
|
|
1917
|
+
function formatDevServerLine(line) {
|
|
1918
|
+
const trimmed = line.trim();
|
|
1919
|
+
if (!trimmed) return "";
|
|
1920
|
+
if (trimmed.startsWith("Error:") || trimmed.includes("ModuleNotFoundError") || trimmed.includes("Can't resolve")) {
|
|
1921
|
+
return chalk.red(` \u2502 ${trimmed}`);
|
|
1922
|
+
}
|
|
1923
|
+
if (trimmed.includes("EADDRINUSE") || trimmed.includes("address already in use")) {
|
|
1924
|
+
return chalk.red(` \u2502 ${trimmed}`) + "\n" + chalk.yellow(" \u2502 \u2192 Port is already in use. Stop the other process or use --port <different-port>");
|
|
1925
|
+
}
|
|
1926
|
+
if (trimmed.includes("EACCES") || trimmed.includes("permission denied")) {
|
|
1927
|
+
return chalk.red(` \u2502 ${trimmed}`) + "\n" + chalk.yellow(" \u2502 \u2192 Permission denied. Try a different port or check file permissions.");
|
|
1928
|
+
}
|
|
1929
|
+
if (trimmed.includes("Cannot find module") || trimmed.includes("MODULE_NOT_FOUND")) {
|
|
1930
|
+
return chalk.red(` \u2502 ${trimmed}`) + "\n" + chalk.yellow(" \u2502 \u2192 Missing dependency. Try running npm install.");
|
|
1931
|
+
}
|
|
1932
|
+
return chalk.dim(` \u2502 ${trimmed}`);
|
|
1933
|
+
}
|
|
1876
1934
|
var program = new Command();
|
|
1877
1935
|
program.name("openmagic").description("AI-powered coding toolbar for any web application").version(VERSION).option("-p, --port <port>", "Dev server port to proxy", "").option(
|
|
1878
1936
|
"-l, --listen <port>",
|
|
@@ -1941,16 +1999,20 @@ program.name("openmagic").description("AI-powered coding toolbar for any web app
|
|
|
1941
1999
|
targetPort,
|
|
1942
2000
|
proxyPort + 1
|
|
1943
2001
|
);
|
|
1944
|
-
proxyServer.listen(proxyPort, "127.0.0.1", () => {
|
|
2002
|
+
proxyServer.listen(proxyPort, "127.0.0.1", async () => {
|
|
1945
2003
|
console.log("");
|
|
1946
2004
|
console.log(
|
|
1947
2005
|
chalk.bold.green(` \u{1F680} Proxy running at \u2192 `) + chalk.bold.underline.cyan(`http://localhost:${proxyPort}`)
|
|
1948
2006
|
);
|
|
1949
2007
|
console.log("");
|
|
2008
|
+
await healthCheck(proxyPort, targetPort);
|
|
1950
2009
|
console.log(
|
|
1951
2010
|
chalk.dim(" Open the URL above in your browser to start.")
|
|
1952
2011
|
);
|
|
1953
2012
|
console.log(chalk.dim(" Press Ctrl+C to stop."));
|
|
2013
|
+
console.log(
|
|
2014
|
+
chalk.dim(" Errors below are from your dev server, not OpenMagic.")
|
|
2015
|
+
);
|
|
1954
2016
|
console.log("");
|
|
1955
2017
|
if (opts.open !== false) {
|
|
1956
2018
|
open(`http://localhost:${proxyPort}`).catch(() => {
|
|
@@ -2095,14 +2157,14 @@ async function offerToStartDevServer(expectedPort) {
|
|
|
2095
2157
|
let childExited = false;
|
|
2096
2158
|
child.stdout?.on("data", (data) => {
|
|
2097
2159
|
for (const line of data.toString().trim().split("\n")) {
|
|
2098
|
-
|
|
2099
|
-
|
|
2160
|
+
const formatted = formatDevServerLine(line);
|
|
2161
|
+
if (formatted) process.stdout.write(formatted + "\n");
|
|
2100
2162
|
}
|
|
2101
2163
|
});
|
|
2102
2164
|
child.stderr?.on("data", (data) => {
|
|
2103
2165
|
for (const line of data.toString().trim().split("\n")) {
|
|
2104
|
-
|
|
2105
|
-
|
|
2166
|
+
const formatted = formatDevServerLine(line);
|
|
2167
|
+
if (formatted) process.stdout.write(formatted + "\n");
|
|
2106
2168
|
}
|
|
2107
2169
|
});
|
|
2108
2170
|
child.on("error", (err) => {
|