playwright-repl 0.1.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.
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Workspace detection and daemon lifecycle.
3
+ *
4
+ * Socket hash: sha1(workspaceDir || packageLocation).substring(0, 16)
5
+ * where packageLocation = our package.json (same as daemon-launcher.cjs uses).
6
+ */
7
+
8
+ import path from 'node:path';
9
+ import fs from 'node:fs';
10
+ import os from 'node:os';
11
+ import net from 'node:net';
12
+ import crypto from 'node:crypto';
13
+ import { execSync } from 'node:child_process';
14
+ import { fileURLToPath } from 'node:url';
15
+ import { packageLocation } from './resolve.mjs';
16
+
17
+ // ─── Workspace detection ─────────────────────────────────────────────────────
18
+
19
+ export function findWorkspaceDir(startDir) {
20
+ let dir = startDir;
21
+ for (let i = 0; i < 10; i++) {
22
+ if (fs.existsSync(path.join(dir, '.playwright'))) return dir;
23
+ const parent = path.dirname(dir);
24
+ if (parent === dir) break;
25
+ dir = parent;
26
+ }
27
+ return undefined;
28
+ }
29
+
30
+ // ─── Hash (must match daemon-launcher.cjs → program.js logic) ────────────────
31
+
32
+ const workspaceDir = findWorkspaceDir(process.cwd());
33
+ const hashInput = workspaceDir || packageLocation;
34
+ const workspaceDirHash = crypto.createHash('sha1').update(hashInput).digest('hex').substring(0, 16);
35
+
36
+ // ─── Socket path ─────────────────────────────────────────────────────────────
37
+
38
+ function socketsBaseDir() {
39
+ if (process.platform === 'win32') return null;
40
+ return process.env.PLAYWRIGHT_DAEMON_SOCKETS_DIR || path.join(os.tmpdir(), 'playwright-cli');
41
+ }
42
+
43
+ export function socketPath(sessionName) {
44
+ if (process.platform === 'win32')
45
+ return `\\\\.\\pipe\\${workspaceDirHash}-${sessionName}.sock`;
46
+ return path.join(socketsBaseDir(), workspaceDirHash, `${sessionName}.sock`);
47
+ }
48
+
49
+ // ─── Daemon profiles dir ─────────────────────────────────────────────────────
50
+
51
+ function baseDaemonDir() {
52
+ if (process.platform === 'darwin')
53
+ return path.join(os.homedir(), 'Library', 'Caches', 'ms-playwright', 'daemon');
54
+ if (process.platform === 'win32')
55
+ return path.join(process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local'), 'ms-playwright', 'daemon');
56
+ return path.join(os.homedir(), '.cache', 'ms-playwright', 'daemon');
57
+ }
58
+
59
+ export const daemonProfilesDir = path.join(baseDaemonDir(), workspaceDirHash);
60
+
61
+ // ─── Daemon lifecycle ────────────────────────────────────────────────────────
62
+
63
+ export async function isDaemonRunning(sessionName) {
64
+ const sockPath = socketPath(sessionName);
65
+ return new Promise((resolve) => {
66
+ const sock = net.createConnection(sockPath, () => {
67
+ sock.destroy();
68
+ resolve(true);
69
+ });
70
+ sock.on('error', () => resolve(false));
71
+ });
72
+ }
73
+
74
+ /**
75
+ * Start daemon using our own launcher (no @playwright/cli needed).
76
+ */
77
+ export async function startDaemon(sessionName, opts = {}) {
78
+ const launcherPath = fileURLToPath(new URL('../bin/daemon-launcher.cjs', import.meta.url));
79
+
80
+ const args = [launcherPath];
81
+ if (sessionName !== 'default') args.push(`-s=${sessionName}`);
82
+ args.push('open');
83
+ if (opts.headed) args.push('--headed');
84
+ if (opts.browser) args.push('--browser', opts.browser);
85
+ if (opts.persistent) args.push('--persistent');
86
+ if (opts.profile) args.push('--profile', opts.profile);
87
+ if (opts.config) args.push('--config', opts.config);
88
+
89
+ if (!opts.silent) console.log(`🚀 Starting daemon...`);
90
+
91
+ try {
92
+ const output = execSync(`node ${args.map(a => `"${a}"`).join(' ')}`, {
93
+ encoding: 'utf-8',
94
+ timeout: 30000,
95
+ stdio: ['pipe', 'pipe', 'pipe'],
96
+ });
97
+ if (output.trim()) console.log(output.trim());
98
+ } catch (err) {
99
+ if (err.stdout?.trim()) console.log(err.stdout.trim());
100
+ if (err.stderr?.trim()) console.error(err.stderr.trim());
101
+ }
102
+ }
103
+
104
+ export { workspaceDirHash };