launch-ide
Version:
Automatically recognize the editor by running processes and open the specified file in it.
589 lines (588 loc) • 18 kB
JavaScript
var W = Object.defineProperty, $ = Object.defineProperties;
var F = Object.getOwnPropertyDescriptors;
var N = Object.getOwnPropertySymbols;
var H = Object.prototype.hasOwnProperty, V = Object.prototype.propertyIsEnumerable;
var _ = (t, e, n) => e in t ? W(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n, T = (t, e) => {
for (var n in e || (e = {}))
H.call(e, n) && _(t, n, e[n]);
if (N)
for (var n of N(e))
V.call(e, n) && _(t, n, e[n]);
return t;
}, B = (t, e) => $(t, F(e));
import I from "fs";
import y from "path";
import g from "child_process";
import X from "os";
import l from "chalk";
import Q from "dotenv";
const j = {
"/Kiro.app/Contents/MacOS/Electron": "/Kiro.app/Contents/MacOS/Electron",
"/Antigravity.app/Contents/MacOS/Electron": "/Antigravity.app/Contents/MacOS/Electron",
"/Cursor.app/Contents/MacOS/Cursor": "/Cursor.app/Contents/MacOS/Cursor",
"/Comate.app/Contents/MacOS/Electron": "/Comate.app/Contents/MacOS/Electron",
"/Qoder.app/Contents/MacOS/Electron": "/Qoder.app/Contents/MacOS/Electron",
"/Qoder CN.app/Contents/MacOS/Electron": "/Qoder CN.app/Contents/MacOS/Electron",
"/Windsurf.app/Contents/MacOS/Electron": "/Windsurf.app/Contents/MacOS/Electron",
"/Trae.app/Contents/MacOS/Electron": "/Trae.app/Contents/MacOS/Electron",
"/Trae CN.app/Contents/MacOS/Electron": "/Trae CN.app/Contents/MacOS/Electron",
"/Applications/CodeBuddy.app/Contents/MacOS/Electron": "/Applications/CodeBuddy.app/Contents/MacOS/Electron",
"/Applications/CodeBuddy CN.app/Contents/MacOS/Electron": "/Applications/CodeBuddy CN.app/Contents/MacOS/Electron",
"/Visual Studio Code.app/Contents/MacOS/Electron": "/Visual Studio Code.app/Contents/MacOS/Electron",
"/Visual Studio Code - Insiders.app/Contents/MacOS/Electron": "/Visual Studio Code - Insiders.app/Contents/MacOS/Electron",
"/VSCodium.app/Contents/MacOS/Electron": "/VSCodium.app/Contents/MacOS/Electron",
"/WebStorm.app/Contents/MacOS/webstorm": "/WebStorm.app/Contents/MacOS/webstorm",
"/HBuilderX.app/Contents/MacOS/HBuilderX": "/HBuilderX.app/Contents/MacOS/HBuilderX",
"/Atom.app/Contents/MacOS/Atom": "atom",
"/Atom Beta.app/Contents/MacOS/Atom Beta": "/Atom Beta.app/Contents/MacOS/Atom Beta",
"/Brackets.app/Contents/MacOS/Brackets": "brackets",
"/Sublime Text.app/Contents/MacOS/Sublime Text": "/Sublime Text.app/Contents/SharedSupport/bin/subl",
"/Sublime Text.app/Contents/MacOS/sublime_text": "/Sublime Text.app/Contents/SharedSupport/bin/subl",
"/Sublime Text 2.app/Contents/MacOS/Sublime Text 2": "/Sublime Text 2.app/Contents/SharedSupport/bin/subl",
"/Sublime Text Dev.app/Contents/MacOS/Sublime Text": "/Sublime Text Dev.app/Contents/SharedSupport/bin/subl",
"/PhpStorm.app/Contents/MacOS/phpstorm": "/PhpStorm.app/Contents/MacOS/phpstorm",
"/PyCharm.app/Contents/MacOS/pycharm": "/PyCharm.app/Contents/MacOS/pycharm",
"/PyCharm CE.app/Contents/MacOS/pycharm": "/PyCharm CE.app/Contents/MacOS/pycharm",
"/IntelliJ IDEA.app/Contents/MacOS/idea": "/IntelliJ IDEA.app/Contents/MacOS/idea",
"/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea": "/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea",
"/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea": "/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea",
"/Zed.app/Contents/MacOS/zed": "zed",
"/GoLand.app/Contents/MacOS/goland": "/GoLand.app/Contents/MacOS/goland",
"/AppCode.app/Contents/MacOS/appcode": "/AppCode.app/Contents/MacOS/appcode",
"/CLion.app/Contents/MacOS/clion": "/CLion.app/Contents/MacOS/clion",
"/RubyMine.app/Contents/MacOS/rubymine": "/RubyMine.app/Contents/MacOS/rubymine",
"/MacVim.app/Contents/MacOS/MacVim": "mvim",
"/Rider.app/Contents/MacOS/rider": "/Rider.app/Contents/MacOS/rider"
}, J = {
kiro: ["/Kiro.app/Contents/MacOS/Electron"],
cursor: ["/Cursor.app/Contents/MacOS/Cursor"],
comate: ["/Comate.app/Contents/MacOS/Electron"],
qoder: [
"/Qoder.app/Contents/MacOS/Electron",
"/Qoder CN.app/Contents/MacOS/Electron"
],
windsurf: ["/Windsurf.app/Contents/MacOS/Electron"],
trae: [
"/Trae.app/Contents/MacOS/Electron",
"/Trae CN.app/Contents/MacOS/Electron"
],
codebuddy: [
"/Applications/CodeBuddy.app/Contents/MacOS/Electron",
"/Applications/CodeBuddy CN.app/Contents/MacOS/Electron"
],
antigravity: ["Antigravity.app/Contents/MacOS/Electron"],
code: ["/Visual Studio Code.app/Contents/MacOS/Electron"],
"code-insiders": [
"/Visual Studio Code - Insiders.app/Contents/MacOS/Electron"
],
webstorm: ["/WebStorm.app/Contents/MacOS/webstorm"],
atom: ["/Atom.app/Contents/MacOS/Atom"],
hbuilder: ["/HBuilderX.app/Contents/MacOS/HBuilderX"],
phpstorm: ["/PhpStorm.app/Contents/MacOS/phpstorm"],
pycharm: ["/PyCharm.app/Contents/MacOS/pycharm"],
idea: ["/IntelliJ IDEA.app/Contents/MacOS/idea"],
codium: ["/VSCodium.app/Contents/MacOS/Electron"],
goland: ["/GoLand.app/Contents/MacOS/goland"],
colin: ["/CLion.app/Contents/MacOS/clion"],
appcode: ["/AppCode.app/Contents/MacOS/appcode"],
"atom-beta": ["/Atom Beta.app/Contents/MacOS/Atom Beta"],
brackets: ["/Brackets.app/Contents/MacOS/Brackets"],
rider: ["/Rider.app/Contents/MacOS/rider"],
rubymine: ["/RubyMine.app/Contents/MacOS/rubymine"],
sublime: ["/Sublime Text.app/Contents/MacOS/sublime_text"],
zed: ["/Zed.app/Contents/MacOS/zed"]
}, U = {
kiro: "kiro",
antigravity: "antigravity",
cursor: "cursor",
windsurf: "windsurf",
code: "code",
vscodium: "vscodium",
codium: "codium",
webstorm: "webstorm",
"webstorm.sh": "webstorm",
hbuilderx: "hbuilderx",
"hbuilderx.sh": "hbuilderx",
atom: "atom",
Brackets: "brackets",
"code-insiders": "code-insiders",
emacs: "emacs",
gvim: "gvim",
idea: "idea",
"idea.sh": "idea",
phpstorm: "phpstorm",
"phpstorm.sh": "phpstorm",
pycharm: "pycharm",
"pycharm.sh": "pycharm",
rubymine: "rubymine",
"rubymine.sh": "rubymine",
sublime_text: "subl",
vim: "vim",
goland: "goland",
"goland.sh": "goland",
rider: "rider",
"rider.sh": "rider"
}, z = {
kiro: ["kiro"],
antigravity: ["antigravity"],
code: ["code"],
"code-insiders": ["code-insiders"],
webstorm: ["webstorm", "webstorm.sh"],
cursor: ["cursor"],
windsurf: ["windsurf"],
trae: ["trae"],
comate: ["comate"],
qoder: ["qoder"],
codebuddy: ["codebuddy"],
atom: ["atom"],
hbuilder: ["hbuilderx", "hbuilderx.sh"],
phpstorm: ["phpstorm", "phpstorm.sh"],
pycharm: ["pycharm", "pycharm.sh"],
idea: ["idea", "idea.sh"],
codium: ["vscodium"],
goland: ["goland"],
brackets: ["Brackets"],
rider: ["rider"],
rubymine: ["rubymine", "rubymine.sh"],
sublime: ["sublime_text"],
vim: ["vim"],
emacs: ["emacs"]
}, K = {
"Kiro.exe": "",
"Antigravity.exe": "",
"Cursor.exe": "",
"Windsurf.exe": "",
"Trae.exe": "",
"Trae CN.exe": "",
"comate.exe": "",
"Qoder.exe": "",
"Qoder CN.exe": "",
"CodeBuddy.exe": "",
"CodeBuddy CN.exe": "",
"Code.exe": "",
"Code - Insiders.exe": "",
"VSCodium.exe": "",
"webstorm.exe": "",
"webstorm64.exe": "",
"HBuilderX.exe": "",
"HBuilderX64.exe": "",
"HBuilder.exe": "",
"HBuilder64.exe": "",
"Brackets.exe": "",
"atom.exe": "",
"sublime_text.exe": "",
"notepad++.exe": "",
"clion.exe": "",
"clion64.exe": "",
"idea.exe": "",
"idea64.exe": "",
"phpstorm.exe": "",
"phpstorm64.exe": "",
"pycharm.exe": "",
"pycharm64.exe": "",
"rubymine.exe": "",
"rubymine64.exe": "",
"goland.exe": "",
"goland64.exe": "",
"rider.exe": "",
"rider64.exe": ""
}, G = {
kiro: ["Kiro.exe"],
antigravity: ["Antigravity.exe"],
code: ["Code.exe"],
"code-insiders": ["Code - Insiders.exe"],
webstorm: ["webstorm.exe", "webstorm64.exe"],
cursor: ["Cursor.exe"],
windsurf: ["Windsurf.exe"],
trae: ["Trae.exe", "Trae CN.exe"],
comate: ["comate.exe"],
qoder: ["Qoder.exe", "Qoder CN.exe"],
codebuddy: ["CodeBuddy.exe", "CodeBuddy CN.exe"],
atom: ["atom.exe"],
hbuilder: [
"HBuilderX.exe",
"HBuilder.exe",
"HBuilderX64.exe",
"HBuilder64.exe"
],
phpstorm: ["phpstorm.exe", "phpstorm64.exe"],
pycharm: ["pycharm.exe", "pycharm64.exe"],
idea: ["idea.exe", "idea64.exe"],
codium: ["VSCodium.exe"],
goland: ["goland.exe", "goland64.exe"],
colin: ["clion.exe", "clion64.exe"],
brackets: ["Brackets.exe"],
rider: ["rider.exe", "rider64.exe"],
rubymine: ["rubymine.exe", "rubymine64.exe"],
sublime: ["sublime_text.exe"],
notepad: ["notepad++.exe"]
}, k = {
darwin: j,
linux: U,
win32: K
}, P = {
darwin: J,
linux: z,
win32: G
}, m = "{file}", u = "{line}", M = "{column}";
function q(t, e, n, o) {
let a = "".concat(t, ":").concat(e, ":").concat(n);
if (typeof o == "string")
a = o.replace(m, t).replace(u, e.toString()).replace(M, n.toString());
else if (o instanceof Array)
return o.map((r) => r.replace(m, t).replace(u, e.toString()).replace(M, n.toString()));
return [a];
}
const Z = ["-g", "-r", "{file}:{line}:{column}"];
function Y(t) {
const {
processName: e,
fileName: n,
lineNumber: o,
colNumber: a,
workspace: r,
openWindowParams: i,
pathFormat: c
} = t, h = { editorBasename: ee(e), openWindowParams: i, workspace: r }, d = te(h) || Z;
return q(n, o, a, c || d);
}
function ee(t) {
let e = y.basename(t).replace(/\.(exe|cmd|bat|sh)$/i, "");
const n = process.platform, o = Object.keys(P[n]);
for (let a = 0; a < o.length; a++)
if ((P[n][o[a]] || []).some((i) => t.endsWith(i))) {
e = o[a];
break;
}
return e.toLowerCase();
}
function te(t) {
const { editorBasename: e, openWindowParams: n, workspace: o } = t;
switch (e) {
case "atom":
case "atom beta":
case "subl":
case "sublime":
case "sublime_text":
case "wstorm":
case "charm":
case "zed":
return "".concat(m, ":").concat(u, ":").concat(M);
case "notepad++":
return ["-n" + u, "-c" + M, m];
case "vim":
case "mvim":
return ["+call cursor(".concat(u, ", ").concat(M, ")"), m];
case "joe":
case "gvim":
return ["+" + u, m];
case "emacs":
case "emacsclient":
return ["+" + u + ":" + M, m];
case "rmate":
case "mate":
case "mine":
return ["--line", u, m];
case "code":
case "code-insiders":
case "code - insiders":
case "codium":
case "cursor":
case "windsurf":
case "trae":
case "codebuddy":
case "antigravity":
case "comate":
case "qoder":
case "vscodium":
case "hbuilderx":
case "hbuilder":
case "kiro":
return [
...o ? [o] : [],
"-g",
...n ? [n] : [],
"".concat(m, ":").concat(u, ":").concat(M)
];
case "appcode":
case "clion":
case "clion64":
case "idea":
case "idea64":
case "phpstorm":
case "phpstorm64":
case "pycharm":
case "pycharm64":
case "rubymine":
case "rubymine64":
case "webstorm":
case "webstorm64":
case "goland":
case "goland64":
case "rider":
case "rider64":
return [
...o ? [o] : [],
"--line",
u,
m
];
}
return "";
}
function R(t, e) {
if (process.env[t])
return process.env[t];
let n = "";
if (e) {
const o = y.resolve(e, ".env.local");
I.existsSync(o) && (n = o);
}
if (!n) {
const o = y.resolve(process.cwd(), ".env.local");
I.existsSync(o) && (n = o);
}
if (n) {
const o = I.readFileSync(n, "utf-8");
return Q.parse(o || "")[t];
}
return null;
}
const ne = {
darwin: "ps ax -o comm=",
linux: "ps -eo comm --sort=comm",
win32: 'powershell -NoProfile -Command "Get-CimInstance -Query \\"select executablepath from win32_process where executablepath is not null\\" | % { $_.ExecutablePath }"'
};
function oe(t, e, n) {
let o = null;
const a = R("CODE_EDITOR", e || "");
if (a) {
const r = A(a);
if (r)
o = r;
else
return [a];
}
if (!o && t) {
const r = A(t);
r && (o = r);
}
if (!o && n) {
const r = re();
if (r)
return [r];
}
try {
let r;
const i = process.platform, c = process.platform === "win32", f = ne[i], h = k[i];
L(c);
const d = g.execSync(f, { encoding: "utf-8" }), S = Object.keys(h), w = d.split(c ? "\r\n" : "\n").map((O) => O.trim());
for (let O = 0; O < S.length; O++) {
const s = S[O];
let C = "", x = "";
if (c) {
const p = w.find(
(E) => y.basename(E).toLowerCase() === s.toLowerCase()
);
p && (x = y.basename(p), C = p);
} else if (i === "darwin") {
const p = w.find(
(E) => E.toLowerCase().endsWith(s.toLowerCase())
);
if (p) {
const E = p.replace(s, ""), v = h[s];
x = s, v.includes("/") ? C = "".concat(E).concat(v) : C = v;
}
} else
d.indexOf(s) !== -1 && (x = s, C = h[s]);
if (x && C) {
if (o != null && o.includes(x))
return [C];
r || (r = [C]);
}
}
if (r)
return r;
} catch (r) {
}
return process.env.VISUAL ? [process.env.VISUAL] : process.env.EDITOR ? [process.env.EDITOR] : [null];
}
const A = (t) => {
const e = process.platform;
return P[e] && P[e][t] || null;
}, L = (t) => {
if (t)
try {
g.execSync("chcp 65001");
} catch (e) {
}
};
function re() {
const t = process.platform, e = Object.keys(k[t]);
try {
return ae(process.pid, t, e);
} catch (n) {
return console.log(l.red("Error while getting editor by PID:"), n), null;
}
}
function ae(t, e, n) {
let o = 0;
const a = 50, r = 0;
for (; t && t !== r && o < a; ) {
const i = se(t, e);
if (!i)
break;
const { command: c, parentPid: f } = i;
if (de(c, n))
return c;
t = f, o++;
}
return null;
}
function se(t, e) {
switch (e) {
case "darwin":
case "linux":
return ce(t);
case "win32":
return ie(t);
default:
return null;
}
}
function ce(t) {
try {
const n = g.execSync("ps -p ".concat(t, " -o ppid=,comm="), {
encoding: "utf8"
}).trim().split("\n");
return n.length ? pe(n[0].trim()) : null;
} catch (e) {
return null;
}
}
function ie(t) {
try {
L(!0);
const e = 'powershell -NoProfile -Command "Get-CimInstance -ClassName Win32_Process -Filter "ProcessId='.concat(t, '" | Select-Object ParentProcessId, ExecutablePath | ConvertTo-Csv -NoTypeInformation'), o = g.execSync(e, {
encoding: "utf8"
}).trim().split("\r\n").filter((i) => i.trim()).slice(1);
if (o.length === 0)
return null;
const [a, r] = o[0].split(",");
return !r || !a ? null : {
command: r.slice(1, -1),
parentPid: Number(a.slice(1, -1))
};
} catch (e) {
return null;
}
}
function pe(t) {
const e = t.match(/^(\d+)\s+(.+)$/);
return e ? {
command: e[2],
parentPid: parseInt(e[1])
} : null;
}
function de(t, e) {
return e.some(
(n) => t.toLowerCase().endsWith(n.toLowerCase())
);
}
function le(t) {
switch (t) {
case "vim":
case "emacs":
case "nano":
return !0;
}
return !1;
}
function me(t) {
const e = R(
"CODE_INSPECTOR_FORMAT_PATH",
t
);
if (e)
try {
return JSON.parse(e);
} catch (n) {
return null;
}
return null;
}
function D(t, e) {
console.log(
l.red("Could not open " + y.basename(t) + " in the editor.")
), e && (e[e.length - 1] !== "." && (e += "."), console.log(
l.red("The editor process exited with an error: " + e)
)), console.log(
"To set up the editor integration, add something like " + l.cyan("CODE_EDITOR=code") + " to the " + l.green(".env.local") + " file in your project folder, or add " + l.green('editor: "code"') + " to CodeInspectorPlugin config, and then restart the development server. Learn more: " + l.green("https://goo.gl/MMTaZt")
);
}
let b = null;
function ue(t) {
return t === "reuse" ? "-r" : t === "new" ? "-n" : "";
}
function Me(t) {
let {
file: e,
line: n = 1,
column: o = 1,
editor: a,
method: r,
format: i,
onError: c,
rootDir: f,
usePid: h
} = t;
if (!I.existsSync(e))
return;
let [d, ...S] = oe(a, f, h);
const w = me(f || "") || i;
if (!d || d.toLowerCase() === "none") {
typeof c == "function" ? c(e, "Failed to recognize IDE automatically") : console.log(
"Failed to recognize IDE automatically, add something like " + l.cyan("CODE_EDITOR=code") + " to the " + l.green(".env.local") + " file in your project folder, or add " + l.green('editor: "code"') + " to CodeInspectorPlugin config, and then restart the development server. Learn more: " + l.green("https://goo.gl/MMTaZt")
);
return;
}
process.platform === "linux" && e.startsWith("/mnt/") && /Microsoft/i.test(X.release()) && (e = y.relative("", e));
let O = null;
if (n ? S = S.concat(
Y({
processName: d,
fileName: e,
lineNumber: n,
colNumber: o,
workspace: O,
openWindowParams: ue(r),
pathFormat: w
})
) : S.push(e), b && le(d) && b.kill("SIGKILL"), process.platform === "win32") {
const s = (p) => p.replace(/([&|<>,;=^])/g, "^$1"), C = (p) => p.includes("^") ? '^"'.concat(p, '^"') : p.includes(" ") ? '"'.concat(p, '"') : p, x = [d, ...S.map(s)].map(C).join(" ");
b = g.exec(x, {
stdio: "ignore",
// @ts-ignore
shell: !0,
env: B(T({}, process.env), {
NODE_OPTIONS: ""
})
});
} else
b = g.spawn(d, S, {
stdio: "ignore",
env: B(T({}, process.env), {
NODE_OPTIONS: ""
})
});
b.on("exit", function(s) {
b = null, s && (typeof c == "function" ? c(e, "(code " + s + ")") : D(e, "(code " + s + ")"));
}), b.on("error", function(s) {
typeof c == "function" ? c(e, s.message) : D(e, s.message);
});
}
export {
q as formatOpenPath,
R as getEnvVariable,
Me as launchIDE
};