ScuttleBot

Source Blame History 296 lines
f7eb47b… lmata 1 "use strict";
f7eb47b… lmata 2 var __create = Object.create;
f7eb47b… lmata 3 var __defProp = Object.defineProperty;
f7eb47b… lmata 4 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
f7eb47b… lmata 5 var __getOwnPropNames = Object.getOwnPropertyNames;
f7eb47b… lmata 6 var __getProtoOf = Object.getPrototypeOf;
f7eb47b… lmata 7 var __hasOwnProp = Object.prototype.hasOwnProperty;
f7eb47b… lmata 8 var __copyProps = (to, from, except, desc) => {
f7eb47b… lmata 9 if (from && typeof from === "object" || typeof from === "function") {
f7eb47b… lmata 10 for (let key of __getOwnPropNames(from))
f7eb47b… lmata 11 if (!__hasOwnProp.call(to, key) && key !== except)
f7eb47b… lmata 12 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
f7eb47b… lmata 13 }
f7eb47b… lmata 14 return to;
f7eb47b… lmata 15 };
f7eb47b… lmata 16 var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
f7eb47b… lmata 17 // If the importer is in node compatibility mode or this is not an ESM
f7eb47b… lmata 18 // file that has been converted to a CommonJS file using a Babel-
f7eb47b… lmata 19 // compatible transform (i.e. "__esModule" has not been set), then set
f7eb47b… lmata 20 // "default" to the CommonJS "module.exports" for node compatibility.
f7eb47b… lmata 21 isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
f7eb47b… lmata 22 mod
f7eb47b… lmata 23 ));
f7eb47b… lmata 24 var import_child_process = require("child_process");
f7eb47b… lmata 25 var import_crypto = __toESM(require("crypto"));
f7eb47b… lmata 26 var import_fs = __toESM(require("fs"));
f7eb47b… lmata 27 var import_net = __toESM(require("net"));
f7eb47b… lmata 28 var import_os = __toESM(require("os"));
f7eb47b… lmata 29 var import_path = __toESM(require("path"));
f7eb47b… lmata 30 var import_utilsBundle = require("playwright-core/lib/utilsBundle");
f7eb47b… lmata 31 var import_socketConnection = require("./socketConnection");
f7eb47b… lmata 32 const debugCli = (0, import_utilsBundle.debug)("pw:cli");
f7eb47b… lmata 33 const packageJSON = require("../../../package.json");
f7eb47b… lmata 34 async function runCliCommand(sessionName, args) {
f7eb47b… lmata 35 const session = await connectToDaemon(sessionName);
f7eb47b… lmata 36 const result = await session.runCliCommand(args);
f7eb47b… lmata 37 console.log(result);
f7eb47b… lmata 38 session.dispose();
f7eb47b… lmata 39 }
f7eb47b… lmata 40 async function socketExists(socketPath) {
f7eb47b… lmata 41 try {
f7eb47b… lmata 42 const stat = await import_fs.default.promises.stat(socketPath);
f7eb47b… lmata 43 if (stat?.isSocket())
f7eb47b… lmata 44 return true;
f7eb47b… lmata 45 } catch (e) {
f7eb47b… lmata 46 }
f7eb47b… lmata 47 return false;
f7eb47b… lmata 48 }
f7eb47b… lmata 49 class SocketSession {
f7eb47b… lmata 50 constructor(connection) {
f7eb47b… lmata 51 this._nextMessageId = 1;
f7eb47b… lmata 52 this._callbacks = /* @__PURE__ */ new Map();
f7eb47b… lmata 53 this._connection = connection;
f7eb47b… lmata 54 this._connection.onmessage = (message) => this._onMessage(message);
f7eb47b… lmata 55 this._connection.onclose = () => this.dispose();
f7eb47b… lmata 56 }
f7eb47b… lmata 57 async callTool(name, args) {
f7eb47b… lmata 58 return this._send(name, args);
f7eb47b… lmata 59 }
f7eb47b… lmata 60 async runCliCommand(args) {
f7eb47b… lmata 61 return await this._send("runCliCommand", { args });
f7eb47b… lmata 62 }
f7eb47b… lmata 63 async _send(method, params = {}) {
f7eb47b… lmata 64 const messageId = this._nextMessageId++;
f7eb47b… lmata 65 const message = {
f7eb47b… lmata 66 id: messageId,
f7eb47b… lmata 67 method,
f7eb47b… lmata 68 params
f7eb47b… lmata 69 };
f7eb47b… lmata 70 await this._connection.send(message);
f7eb47b… lmata 71 return new Promise((resolve, reject) => {
f7eb47b… lmata 72 this._callbacks.set(messageId, { resolve, reject });
f7eb47b… lmata 73 });
f7eb47b… lmata 74 }
f7eb47b… lmata 75 dispose() {
f7eb47b… lmata 76 for (const callback of this._callbacks.values())
f7eb47b… lmata 77 callback.reject(new Error("Disposed"));
f7eb47b… lmata 78 this._callbacks.clear();
f7eb47b… lmata 79 this._connection.close();
f7eb47b… lmata 80 }
f7eb47b… lmata 81 _onMessage(object) {
f7eb47b… lmata 82 if (object.id && this._callbacks.has(object.id)) {
f7eb47b… lmata 83 const callback = this._callbacks.get(object.id);
f7eb47b… lmata 84 this._callbacks.delete(object.id);
f7eb47b… lmata 85 if (object.error)
f7eb47b… lmata 86 callback.reject(new Error(object.error));
f7eb47b… lmata 87 else
f7eb47b… lmata 88 callback.resolve(object.result);
f7eb47b… lmata 89 } else if (object.id) {
f7eb47b… lmata 90 throw new Error(`Unexpected message id: ${object.id}`);
f7eb47b… lmata 91 } else {
f7eb47b… lmata 92 throw new Error(`Unexpected message without id: ${JSON.stringify(object)}`);
f7eb47b… lmata 93 }
f7eb47b… lmata 94 }
f7eb47b… lmata 95 }
f7eb47b… lmata 96 function localCacheDir() {
f7eb47b… lmata 97 if (process.platform === "linux")
f7eb47b… lmata 98 return process.env.XDG_CACHE_HOME || import_path.default.join(import_os.default.homedir(), ".cache");
f7eb47b… lmata 99 if (process.platform === "darwin")
f7eb47b… lmata 100 return import_path.default.join(import_os.default.homedir(), "Library", "Caches");
f7eb47b… lmata 101 if (process.platform === "win32")
f7eb47b… lmata 102 return process.env.LOCALAPPDATA || import_path.default.join(import_os.default.homedir(), "AppData", "Local");
f7eb47b… lmata 103 throw new Error("Unsupported platform: " + process.platform);
f7eb47b… lmata 104 }
f7eb47b… lmata 105 function playwrightCacheDir() {
f7eb47b… lmata 106 return import_path.default.join(localCacheDir(), "ms-playwright");
f7eb47b… lmata 107 }
f7eb47b… lmata 108 function calculateSha1(buffer) {
f7eb47b… lmata 109 const hash = import_crypto.default.createHash("sha1");
f7eb47b… lmata 110 hash.update(buffer);
f7eb47b… lmata 111 return hash.digest("hex");
f7eb47b… lmata 112 }
f7eb47b… lmata 113 function socketDirHash() {
f7eb47b… lmata 114 return calculateSha1(__dirname);
f7eb47b… lmata 115 }
f7eb47b… lmata 116 function daemonSocketDir() {
f7eb47b… lmata 117 return import_path.default.resolve(playwrightCacheDir(), "daemon", socketDirHash());
f7eb47b… lmata 118 }
f7eb47b… lmata 119 function daemonSocketPath(sessionName) {
f7eb47b… lmata 120 const socketName = `${sessionName}.sock`;
f7eb47b… lmata 121 if (import_os.default.platform() === "win32")
f7eb47b… lmata 122 return `\\\\.\\pipe\\${socketDirHash()}-${socketName}`;
f7eb47b… lmata 123 return import_path.default.resolve(daemonSocketDir(), socketName);
f7eb47b… lmata 124 }
f7eb47b… lmata 125 async function connectToDaemon(sessionName) {
f7eb47b… lmata 126 const socketPath = daemonSocketPath(sessionName);
f7eb47b… lmata 127 debugCli(`Connecting to daemon at ${socketPath}`);
f7eb47b… lmata 128 if (await socketExists(socketPath)) {
f7eb47b… lmata 129 debugCli(`Socket file exists, attempting to connect...`);
f7eb47b… lmata 130 try {
f7eb47b… lmata 131 return await connectToSocket(socketPath);
f7eb47b… lmata 132 } catch (e) {
f7eb47b… lmata 133 if (import_os.default.platform() !== "win32")
f7eb47b… lmata 134 await import_fs.default.promises.unlink(socketPath).catch(() => {
f7eb47b… lmata 135 });
f7eb47b… lmata 136 }
f7eb47b… lmata 137 }
f7eb47b… lmata 138 const cliPath = import_path.default.join(__dirname, "../../../cli.js");
f7eb47b… lmata 139 debugCli(`Will launch daemon process: ${cliPath}`);
f7eb47b… lmata 140 const userDataDir = import_path.default.resolve(daemonSocketDir(), `${sessionName}-user-data`);
f7eb47b… lmata 141 const child = (0, import_child_process.spawn)(process.execPath, [cliPath, "run-mcp-server", `--daemon=${socketPath}`, `--user-data-dir=${userDataDir}`], {
f7eb47b… lmata 142 detached: true,
f7eb47b… lmata 143 stdio: "ignore",
f7eb47b… lmata 144 cwd: process.cwd()
f7eb47b… lmata 145 // Will be used as root.
f7eb47b… lmata 146 });
f7eb47b… lmata 147 child.unref();
f7eb47b… lmata 148 const maxRetries = 50;
f7eb47b… lmata 149 const retryDelay = 100;
f7eb47b… lmata 150 for (let i = 0; i < maxRetries; i++) {
f7eb47b… lmata 151 await new Promise((resolve) => setTimeout(resolve, 100));
f7eb47b… lmata 152 try {
f7eb47b… lmata 153 return await connectToSocket(socketPath);
f7eb47b… lmata 154 } catch (e) {
f7eb47b… lmata 155 if (e.code !== "ENOENT")
f7eb47b… lmata 156 throw e;
f7eb47b… lmata 157 debugCli(`Retrying to connect to daemon at ${socketPath} (${i + 1}/${maxRetries})`);
f7eb47b… lmata 158 }
f7eb47b… lmata 159 }
f7eb47b… lmata 160 throw new Error(`Failed to connect to daemon at ${socketPath} after ${maxRetries * retryDelay}ms`);
f7eb47b… lmata 161 }
f7eb47b… lmata 162 async function connectToSocket(socketPath) {
f7eb47b… lmata 163 const socket = await new Promise((resolve, reject) => {
f7eb47b… lmata 164 const socket2 = import_net.default.createConnection(socketPath, () => {
f7eb47b… lmata 165 debugCli(`Connected to daemon at ${socketPath}`);
f7eb47b… lmata 166 resolve(socket2);
f7eb47b… lmata 167 });
f7eb47b… lmata 168 socket2.on("error", reject);
f7eb47b… lmata 169 });
f7eb47b… lmata 170 return new SocketSession(new import_socketConnection.SocketConnection(socket));
f7eb47b… lmata 171 }
f7eb47b… lmata 172 function currentSessionPath() {
f7eb47b… lmata 173 return import_path.default.resolve(daemonSocketDir(), "current-session");
f7eb47b… lmata 174 }
f7eb47b… lmata 175 async function getCurrentSession() {
f7eb47b… lmata 176 try {
f7eb47b… lmata 177 const session = await import_fs.default.promises.readFile(currentSessionPath(), "utf-8");
f7eb47b… lmata 178 return session.trim() || "default";
f7eb47b… lmata 179 } catch {
f7eb47b… lmata 180 return "default";
f7eb47b… lmata 181 }
f7eb47b… lmata 182 }
f7eb47b… lmata 183 async function setCurrentSession(sessionName) {
f7eb47b… lmata 184 await import_fs.default.promises.mkdir(daemonSocketDir(), { recursive: true });
f7eb47b… lmata 185 await import_fs.default.promises.writeFile(currentSessionPath(), sessionName);
f7eb47b… lmata 186 }
f7eb47b… lmata 187 async function canConnectToSocket(socketPath) {
f7eb47b… lmata 188 return new Promise((resolve) => {
f7eb47b… lmata 189 const socket = import_net.default.createConnection(socketPath, () => {
f7eb47b… lmata 190 socket.destroy();
f7eb47b… lmata 191 resolve(true);
f7eb47b… lmata 192 });
f7eb47b… lmata 193 socket.on("error", () => {
f7eb47b… lmata 194 resolve(false);
f7eb47b… lmata 195 });
f7eb47b… lmata 196 });
f7eb47b… lmata 197 }
f7eb47b… lmata 198 async function listSessions() {
f7eb47b… lmata 199 const dir = daemonSocketDir();
f7eb47b… lmata 200 try {
f7eb47b… lmata 201 const files = await import_fs.default.promises.readdir(dir);
f7eb47b… lmata 202 const sessions = [];
f7eb47b… lmata 203 for (const file of files) {
f7eb47b… lmata 204 if (file.endsWith("-user-data")) {
f7eb47b… lmata 205 const sessionName = file.slice(0, -"-user-data".length);
f7eb47b… lmata 206 const socketPath = daemonSocketPath(sessionName);
f7eb47b… lmata 207 const live = await canConnectToSocket(socketPath);
f7eb47b… lmata 208 sessions.push({ name: sessionName, live });
f7eb47b… lmata 209 }
f7eb47b… lmata 210 }
f7eb47b… lmata 211 return sessions;
f7eb47b… lmata 212 } catch {
f7eb47b… lmata 213 return [];
f7eb47b… lmata 214 }
f7eb47b… lmata 215 }
f7eb47b… lmata 216 function resolveSessionName(args) {
f7eb47b… lmata 217 if (args.session)
f7eb47b… lmata 218 return args.session;
f7eb47b… lmata 219 if (process.env.PLAYWRIGHT_CLI_SESSION)
f7eb47b… lmata 220 return process.env.PLAYWRIGHT_CLI_SESSION;
f7eb47b… lmata 221 return "default";
f7eb47b… lmata 222 }
f7eb47b… lmata 223 async function handleSessionCommand(args) {
f7eb47b… lmata 224 const subcommand = args._[1];
f7eb47b… lmata 225 if (!subcommand) {
f7eb47b… lmata 226 const current = await getCurrentSession();
f7eb47b… lmata 227 console.log(current);
f7eb47b… lmata 228 return;
f7eb47b… lmata 229 }
f7eb47b… lmata 230 if (subcommand === "list") {
f7eb47b… lmata 231 const sessions = await listSessions();
f7eb47b… lmata 232 const current = await getCurrentSession();
f7eb47b… lmata 233 console.log("Sessions:");
f7eb47b… lmata 234 for (const session of sessions) {
f7eb47b… lmata 235 const marker = session.name === current ? "->" : " ";
f7eb47b… lmata 236 const liveMarker = session.live ? " (live)" : "";
f7eb47b… lmata 237 console.log(`${marker} ${session.name}${liveMarker}`);
f7eb47b… lmata 238 }
f7eb47b… lmata 239 if (sessions.length === 0)
f7eb47b… lmata 240 console.log(" (no sessions)");
f7eb47b… lmata 241 return;
f7eb47b… lmata 242 }
f7eb47b… lmata 243 if (subcommand === "set") {
f7eb47b… lmata 244 const sessionName = args._[2];
f7eb47b… lmata 245 if (!sessionName) {
f7eb47b… lmata 246 console.error("Usage: playwright-cli session set <session-name>");
f7eb47b… lmata 247 process.exit(1);
f7eb47b… lmata 248 }
f7eb47b… lmata 249 await setCurrentSession(sessionName);
f7eb47b… lmata 250 console.log(`Current session set to: ${sessionName}`);
f7eb47b… lmata 251 return;
f7eb47b… lmata 252 }
f7eb47b… lmata 253 console.error(`Unknown session subcommand: ${subcommand}`);
f7eb47b… lmata 254 process.exit(1);
f7eb47b… lmata 255 }
f7eb47b… lmata 256 async function main() {
f7eb47b… lmata 257 const argv = process.argv.slice(2);
f7eb47b… lmata 258 const args = require("minimist")(argv);
f7eb47b… lmata 259 const help = require("./help.json");
f7eb47b… lmata 260 const commandName = args._[0];
f7eb47b… lmata 261 if (args.version || args.v) {
f7eb47b… lmata 262 console.log(packageJSON.version);
f7eb47b… lmata 263 process.exit(0);
f7eb47b… lmata 264 }
f7eb47b… lmata 265 if (commandName === "session") {
f7eb47b… lmata 266 await handleSessionCommand(args);
f7eb47b… lmata 267 return;
f7eb47b… lmata 268 }
f7eb47b… lmata 269 const command = help.commands[commandName];
f7eb47b… lmata 270 if (args.help || args.h) {
f7eb47b… lmata 271 if (command) {
f7eb47b… lmata 272 console.log(command);
f7eb47b… lmata 273 } else {
f7eb47b… lmata 274 console.log("playwright-cli - run playwright mcp commands from terminal\n");
f7eb47b… lmata 275 console.log(help.global);
f7eb47b… lmata 276 }
f7eb47b… lmata 277 process.exit(0);
f7eb47b… lmata 278 }
f7eb47b… lmata 279 if (!command) {
f7eb47b… lmata 280 console.error(`Unknown command: ${commandName}
f7eb47b… lmata 281 `);
f7eb47b… lmata 282 console.log(help.global);
f7eb47b… lmata 283 process.exit(1);
f7eb47b… lmata 284 }
f7eb47b… lmata 285 let sessionName = resolveSessionName(args);
f7eb47b… lmata 286 if (sessionName === "default" && !args.session && !process.env.PLAYWRIGHT_CLI_SESSION)
f7eb47b… lmata 287 sessionName = await getCurrentSession();
f7eb47b… lmata 288 runCliCommand(sessionName, args).catch((e) => {
f7eb47b… lmata 289 console.error(e.message);
f7eb47b… lmata 290 process.exit(1);
f7eb47b… lmata 291 });
f7eb47b… lmata 292 }
f7eb47b… lmata 293 main().catch((e) => {
f7eb47b… lmata 294 console.error(e.message);
f7eb47b… lmata 295 process.exit(1);
f7eb47b… lmata 296 });

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button