diff --git a/src/cli/cli.ts b/src/cli/cli.ts index 76a643cb43..b84e9a7433 100755 --- a/src/cli/cli.ts +++ b/src/cli/cli.ts @@ -22,7 +22,6 @@ import * as path from 'path'; import * as program from 'commander'; import * as os from 'os'; import * as fs from 'fs'; -import { installBrowsersWithProgressBar } from '../install/installer'; import * as consoleApiSource from '../generated/consoleApiSource'; import { OutputMultiplexer, TerminalOutput, FileOutput } from './codegen/outputs'; import { CodeGenerator, CodeGeneratorOutput } from './codegen/codeGenerator'; @@ -30,6 +29,7 @@ import { JavaScriptLanguageGenerator, LanguageGenerator } from './codegen/langua import { PythonLanguageGenerator } from './codegen/languages/python'; import { CSharpLanguageGenerator } from './codegen/languages/csharp'; import { RecorderController } from './codegen/recorderController'; +import { runServer, printApiJson, installBrowsers } from './driver'; import type { Browser, BrowserContext, Page, BrowserType, BrowserContextOptions, LaunchOptions } from '../..'; import * as playwright from '../..'; @@ -130,19 +130,18 @@ program .command('install') .description('Ensure browsers necessary for this version of Playwright are installed') .action(function() { - let browsersJsonDir = path.dirname(process.execPath); - if (!fs.existsSync(path.join(browsersJsonDir, 'browsers.json'))) { - browsersJsonDir = path.join(__dirname, '..', '..'); - if (!fs.existsSync(path.join(browsersJsonDir, 'browsers.json'))) - throw new Error('Failed to find browsers.json in ' + browsersJsonDir); - } - installBrowsersWithProgressBar(browsersJsonDir).catch((e: any) => { + installBrowsers().catch((e: any) => { console.log(`Failed to install browsers\n${e}`); process.exit(1); }); }); -program.parse(process.argv); +if (process.argv[2] === 'run-driver') + runServer(); +else if (process.argv[2] === 'print-api-json') + printApiJson(); +else + program.parse(process.argv); type Options = { diff --git a/src/driver.ts b/src/cli/driver.ts similarity index 51% rename from src/driver.ts rename to src/cli/driver.ts index ad190a133e..2d58b43c9f 100644 --- a/src/driver.ts +++ b/src/cli/driver.ts @@ -14,33 +14,26 @@ * limitations under the License. */ +/* eslint-disable no-console */ + import * as fs from 'fs'; -import * as util from 'util'; -import { installDebugController } from './debug/debugController'; -import { DispatcherConnection } from './dispatchers/dispatcher'; -import { PlaywrightDispatcher } from './dispatchers/playwrightDispatcher'; -import { installBrowsersWithProgressBar } from './install/installer'; -import { Transport } from './protocol/transport'; -import { Playwright } from './server/playwright'; -import { gracefullyCloseAll } from './server/processLauncher'; -import { installHarTracer } from './trace/harTracer'; -import { installTracer } from './trace/tracer'; +import * as path from 'path'; +import { installDebugController } from '../debug/debugController'; +import { DispatcherConnection } from '../dispatchers/dispatcher'; +import { PlaywrightDispatcher } from '../dispatchers/playwrightDispatcher'; +import { installBrowsersWithProgressBar } from '../install/installer'; +import { Transport } from '../protocol/transport'; +import { Playwright } from '../server/playwright'; +import { gracefullyCloseAll } from '../server/processLauncher'; +import { installHarTracer } from '../trace/harTracer'; +import { installTracer } from '../trace/tracer'; - -const readFileAsync = util.promisify(fs.readFile); -const writeFileAsync = util.promisify(fs.writeFile); - -export async function copyPrintDeps(destination: string) { - const content = await readFileAsync(require.resolve('../bin/PrintDeps.exe')); - await writeFileAsync(destination, content); +export function printApiJson() { + console.log(JSON.stringify(require('../../api.json'))); } -export async function installWithProgressBar(location: string) { - await installBrowsersWithProgressBar(location); -} - -export async function apiJson(): Promise { - return (await readFileAsync(require.resolve('../docs/api.json'))).toString(); +export function printProtocol() { + console.log(fs.readFileSync(path.join(__dirname, '..', '..', 'protocol.yml'), 'utf8')); } export function runServer() { @@ -62,9 +55,16 @@ export function runServer() { process.exit(0); }; - const playwright = new Playwright(__dirname, require('../browsers.json')['browsers']); + const playwright = new Playwright(__dirname, require('../../browsers.json')['browsers']); new PlaywrightDispatcher(dispatcherConnection.rootDispatcher(), playwright); } -if (process.argv[2] === 'serve') - runServer(); +export async function installBrowsers() { + let browsersJsonDir = path.dirname(process.execPath); + if (!fs.existsSync(path.join(browsersJsonDir, 'browsers.json'))) { + browsersJsonDir = path.join(__dirname, '..', '..'); + if (!fs.existsSync(path.join(browsersJsonDir, 'browsers.json'))) + throw new Error('Failed to find browsers.json in ' + browsersJsonDir); + } + await installBrowsersWithProgressBar(browsersJsonDir); +} diff --git a/test/fixtures.ts b/test/fixtures.ts index 1c784a0854..7c520072c3 100644 --- a/test/fixtures.ts +++ b/test/fixtures.ts @@ -108,7 +108,7 @@ fixtures.playwright.override(async ({ browserName, testWorkerIndex, platform, mo if (mode === 'driver') { require('../lib/utils/utils').setUnderTest(); const connection = new Connection(); - const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'driver.js'), ['serve'], { + const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'cli', 'cli.js'), ['run-driver'], { stdio: 'pipe', detached: true, }); diff --git a/utils/check_deps.js b/utils/check_deps.js index f0c1c27f25..fdfece79f4 100644 --- a/utils/check_deps.js +++ b/utils/check_deps.js @@ -123,7 +123,7 @@ DEPS['src/server/android/'] = [...DEPS['src/server/'], 'src/server/chromium/', ' DEPS['src/server/electron/'] = [...DEPS['src/server/'], 'src/server/chromium/']; DEPS['src/server/playwright.ts'] = [...DEPS['src/server/'], 'src/server/chromium/', 'src/server/webkit/', 'src/server/firefox/', 'src/server/android/', 'src/server/electron/']; -DEPS['src/driver.ts'] = DEPS['src/inprocess.ts'] = DEPS['src/browserServerImpl.ts'] = ['src/**']; +DEPS['src/cli/driver.ts'] = DEPS['src/inprocess.ts'] = DEPS['src/browserServerImpl.ts'] = ['src/**']; // Tracing is a client/server plugin, nothing should depend on it. DEPS['src/trace/'] = ['src/utils/', 'src/client/**', 'src/server/**'];