#!/usr/bin/env node /** * Copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const path = require('path'); const fs = require('fs'); const { program } = require('patchright-core/lib/tools/cli-client/program'); const packageJson = require('./package.json'); const argv = process.argv.slice(2); // `reap` is ours, not patchright-core's: sweep up daemons and browsers that // outlived their session. See lib/reap.js. if (argv[0] === 'reap') { require('./lib/reap').runReapCommand(argv.slice(1)); return; } // The daemon is spawned as a detached child of this process and inherits its // environment, so preloading the idle guard here is enough to get it into a // daemon we never call into directly. Without it a daemon lives until someone // runs `close`, which agents routinely forget to do. // // The preload also arms the orphan guard, so turning the idle timer off is not // a reason to skip it: those two protect against different failures. if (process.env.PATCHWRIGHT_IDLE_TIMEOUT !== 'off' || process.env.PATCHWRIGHT_ORPHAN_GUARD !== 'off') { const guardPath = path.join(__dirname, 'lib', 'idle-guard.js'); const nodeOptions = process.env.NODE_OPTIONS || ''; if (!nodeOptions.includes(guardPath)) { const quoted = /\s/.test(guardPath) ? `"${guardPath}"` : guardPath; process.env.NODE_OPTIONS = `${nodeOptions} --require ${quoted}`.trim(); } } // Intercept `install --skills` to use skills bundled with this package // instead of the ones from patchright-core. const commandIndex = argv.findIndex(a => a === 'install'); if (commandIndex !== -1) { const skillsArgIndex = argv.findIndex(a => a === '--skills' || a.startsWith('--skills=')); if (skillsArgIndex !== -1) { const skillsArg = argv[skillsArgIndex]; let skillsValue = 'claude'; if (skillsArg.startsWith('--skills=')) { skillsValue = skillsArg.split('=')[1]; } else if (skillsArgIndex + 1 < argv.length && !argv[skillsArgIndex + 1].startsWith('-')) { skillsValue = argv[skillsArgIndex + 1]; } const target = skillsValue === 'agents' ? 'agents' : 'claude'; const skillSourceDir = path.join(__dirname, 'skills', 'patchwright-cli'); const skillDestDir = path.join(process.cwd(), `.${target}`, 'skills', 'patchwright-cli'); if (!fs.existsSync(skillSourceDir)) { console.error('\u274C Skills source directory not found:', skillSourceDir); process.exit(1); } fs.cpSync(skillSourceDir, skillDestDir, { recursive: true }); console.log(`\u2705 Skills installed to \`${path.relative(process.cwd(), skillDestDir)}\`.`); // Strip --skills from argv so patchright-core doesn't also try to install skills process.argv = process.argv.filter(a => a !== '--skills' && !a.startsWith('--skills=')); // Also remove the value arg if --skills was separate from its value if (!skillsArg.startsWith('--skills=') && skillsArgIndex + 1 < argv.length && !argv[skillsArgIndex + 1].startsWith('-')) { const valueToRemove = argv[skillsArgIndex + 1]; const idx = process.argv.indexOf(valueToRemove); if (idx !== -1) process.argv.splice(idx, 1); } } } program({ embedderVersion: packageJson.version });