Node 使用 WebStorm 打开文件
Node 脚本中, 打开文件.
如果有 WebStorm 就用 WebStorm 打开, 如果有 VSCode 就用 VSCode 打开, 否则 打开 目录
import { exec } from "child_process";
import fs from "fs-extra";
import open from "open";
import path from "path";
function openFileOrDirectory(filePath) {
const webstormCommand = "webstorm";
const vscodeCommand = "code";
if (!fs.existsSync(filePath)) {
log.fail(`文件或目录不存在: ${filePath}`);
return;
}
// 检查是否安装了 WebStorm
exec(`${webstormCommand}`, (error, stdout) => {
if (!error) {
exec(`${webstormCommand} ${filePath}`);
} else {
// 检查是否安装了 VSCode
exec(`${vscodeCommand} -v`, async (error, stdout) => {
if (!error) {
exec(`${vscodeCommand} ${filePath}`);
} else {
await open(path.dirname(filePath));
}
});
}
});
}