@guanzhu.me/pw-cli 0.0.7 → 0.0.8
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/bin/pw-cli.js +31 -10
- package/package.json +1 -1
- package/src/browser-manager.js +1 -1
package/bin/pw-cli.js
CHANGED
|
@@ -716,6 +716,34 @@ async function main() {
|
|
|
716
716
|
return;
|
|
717
717
|
}
|
|
718
718
|
|
|
719
|
+
// ── From here on: delegate to playwright-cli (with enhancements) ─────────
|
|
720
|
+
const cliPath = findPlaywrightCli();
|
|
721
|
+
if (!cliPath) {
|
|
722
|
+
process.stderr.write('pw-cli: @playwright/cli not found.\nInstall: npm install -g @playwright/cli\n');
|
|
723
|
+
process.exit(1);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// Ensures a browser is reachable via CDP; if not, spawns playwright-cli open first.
|
|
727
|
+
async function ensureBrowserRunning() {
|
|
728
|
+
const { getPlaywrightCliCdpPort } = require('../src/browser-manager');
|
|
729
|
+
const { probeCDP } = require('../src/utils');
|
|
730
|
+
const { readState } = require('../src/state');
|
|
731
|
+
const cliPort = getPlaywrightCliCdpPort();
|
|
732
|
+
if (cliPort && await probeCDP(cliPort, 2000)) return;
|
|
733
|
+
const state = readState();
|
|
734
|
+
if (state && await probeCDP(state.port, 2000)) return;
|
|
735
|
+
// No browser reachable — start one via playwright-cli
|
|
736
|
+
const { spawnSync } = require('child_process');
|
|
737
|
+
const res = spawnSync(process.execPath, [cliPath, 'open', '--headed', '--persistent', '--profile', DEFAULT_PROFILE], {
|
|
738
|
+
stdio: 'inherit',
|
|
739
|
+
cwd: PW_CLI_DIR,
|
|
740
|
+
});
|
|
741
|
+
if (res.status !== 0) {
|
|
742
|
+
process.stderr.write('pw-cli: failed to open browser\n');
|
|
743
|
+
process.exit(res.status || 1);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
719
747
|
// ── goto: navigate the currently active tab (not always the first one) ───
|
|
720
748
|
if (command === 'goto') {
|
|
721
749
|
const gotoIdx = rawArgv.indexOf('goto');
|
|
@@ -740,18 +768,12 @@ async function main() {
|
|
|
740
768
|
await target.goto(${JSON.stringify(fullUrl)}, { waitUntil: 'domcontentloaded', timeout: 0 });
|
|
741
769
|
return target.url();
|
|
742
770
|
}`;
|
|
771
|
+
await ensureBrowserRunning();
|
|
743
772
|
await handleRunCode(['run-code', navCode]);
|
|
744
773
|
return;
|
|
745
774
|
}
|
|
746
775
|
}
|
|
747
776
|
|
|
748
|
-
// ── From here on: delegate to playwright-cli (with enhancements) ─────────
|
|
749
|
-
const cliPath = findPlaywrightCli();
|
|
750
|
-
if (!cliPath) {
|
|
751
|
-
process.stderr.write('pw-cli: @playwright/cli not found.\nInstall: npm install -g @playwright/cli\n');
|
|
752
|
-
process.exit(1);
|
|
753
|
-
}
|
|
754
|
-
|
|
755
777
|
let argv = [...rawArgv];
|
|
756
778
|
|
|
757
779
|
// ── run-code: stdin support + auto-wrap plain code as function ───────────
|
|
@@ -789,13 +811,12 @@ async function main() {
|
|
|
789
811
|
const openIdx = argv.indexOf('open');
|
|
790
812
|
const afterOpen = argv.slice(openIdx + 1);
|
|
791
813
|
|
|
792
|
-
// If a URL is provided with open,
|
|
793
|
-
// getConnection() handles all cases: reuse playwright-cli session, reuse own daemon,
|
|
794
|
-
// or start a new daemon — so we never need to spawn playwright-cli open separately.
|
|
814
|
+
// If a URL is provided with open, ensure a browser is running then open a new tab.
|
|
795
815
|
const rawUrlArg = afterOpen.find(a => !a.startsWith('-') && /^(https?:\/\/|[a-zA-Z0-9]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,})/.test(a));
|
|
796
816
|
const urlArg = rawUrlArg && !/^https?:\/\//.test(rawUrlArg) ? `https://${rawUrlArg}` : rawUrlArg;
|
|
797
817
|
if (urlArg) {
|
|
798
818
|
const navCode = `async page => { const newPage = await page.context().newPage(); await newPage.goto(${JSON.stringify(urlArg)}, { waitUntil: 'domcontentloaded', timeout: 0 }); return newPage.url(); }`;
|
|
819
|
+
await ensureBrowserRunning();
|
|
799
820
|
await handleRunCode(['run-code', navCode]);
|
|
800
821
|
return;
|
|
801
822
|
} else {
|
package/package.json
CHANGED
package/src/browser-manager.js
CHANGED