@guanzhu.me/pw-cli 0.0.6 → 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 +43 -14
- 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');
|
|
@@ -725,25 +753,27 @@ async function main() {
|
|
|
725
753
|
const fullUrl = /^https?:\/\//.test(rawUrl) ? rawUrl : `https://${rawUrl}`;
|
|
726
754
|
const navCode = `async (page, context) => {
|
|
727
755
|
const pages = context.pages();
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
756
|
+
const info = await Promise.all(pages.map(async p => {
|
|
757
|
+
try {
|
|
758
|
+
const [hidden, focused] = await Promise.all([
|
|
759
|
+
p.evaluate(() => document.hidden),
|
|
760
|
+
p.evaluate(() => document.hasFocus()),
|
|
761
|
+
]);
|
|
762
|
+
return { p, hidden, focused };
|
|
763
|
+
} catch { return { p, hidden: true, focused: false }; }
|
|
764
|
+
}));
|
|
765
|
+
const focused = info.find(v => v.focused);
|
|
766
|
+
const visible = info.filter(v => !v.hidden);
|
|
767
|
+
const target = (focused || visible[visible.length - 1] || info[info.length - 1]).p;
|
|
732
768
|
await target.goto(${JSON.stringify(fullUrl)}, { waitUntil: 'domcontentloaded', timeout: 0 });
|
|
733
769
|
return target.url();
|
|
734
770
|
}`;
|
|
771
|
+
await ensureBrowserRunning();
|
|
735
772
|
await handleRunCode(['run-code', navCode]);
|
|
736
773
|
return;
|
|
737
774
|
}
|
|
738
775
|
}
|
|
739
776
|
|
|
740
|
-
// ── From here on: delegate to playwright-cli (with enhancements) ─────────
|
|
741
|
-
const cliPath = findPlaywrightCli();
|
|
742
|
-
if (!cliPath) {
|
|
743
|
-
process.stderr.write('pw-cli: @playwright/cli not found.\nInstall: npm install -g @playwright/cli\n');
|
|
744
|
-
process.exit(1);
|
|
745
|
-
}
|
|
746
|
-
|
|
747
777
|
let argv = [...rawArgv];
|
|
748
778
|
|
|
749
779
|
// ── run-code: stdin support + auto-wrap plain code as function ───────────
|
|
@@ -781,13 +811,12 @@ async function main() {
|
|
|
781
811
|
const openIdx = argv.indexOf('open');
|
|
782
812
|
const afterOpen = argv.slice(openIdx + 1);
|
|
783
813
|
|
|
784
|
-
// If a URL is provided with open,
|
|
785
|
-
// getConnection() handles all cases: reuse playwright-cli session, reuse own daemon,
|
|
786
|
-
// 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.
|
|
787
815
|
const rawUrlArg = afterOpen.find(a => !a.startsWith('-') && /^(https?:\/\/|[a-zA-Z0-9]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,})/.test(a));
|
|
788
816
|
const urlArg = rawUrlArg && !/^https?:\/\//.test(rawUrlArg) ? `https://${rawUrlArg}` : rawUrlArg;
|
|
789
817
|
if (urlArg) {
|
|
790
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();
|
|
791
820
|
await handleRunCode(['run-code', navCode]);
|
|
792
821
|
return;
|
|
793
822
|
} else {
|
package/package.json
CHANGED
package/src/browser-manager.js
CHANGED