From de8d59f179614aba5571abd5fce34f9e45dc3fc7 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 23 Jan 2024 14:35:19 -0800 Subject: [PATCH] chore: split cli processing into cli endpoint and program --- packages/playwright-chromium/cli.js | 4 +++- packages/playwright-core/cli.js | 3 ++- packages/playwright-core/package.json | 1 - packages/playwright-core/src/cli/program.ts | 3 +-- .../src/cli/{cli.ts => programWithTestStub.ts} | 7 ++----- packages/playwright-core/src/outofprocess.ts | 2 +- packages/playwright-ct-core/cli.js | 4 +++- packages/playwright-ct-core/package.json | 2 +- packages/playwright-ct-core/src/program.ts | 17 +++++++++++++++++ packages/playwright-ct-react/cli.js | 4 +++- packages/playwright-ct-react17/cli.js | 4 +++- packages/playwright-ct-solid/cli.js | 4 +++- packages/playwright-ct-svelte/cli.js | 4 +++- packages/playwright-ct-vue/cli.js | 4 +++- packages/playwright-ct-vue2/cli.js | 4 +++- packages/playwright-firefox/cli.js | 4 +++- packages/playwright-test/cli.js | 4 +++- packages/playwright-webkit/cli.js | 4 +++- packages/playwright/cli.js | 4 +++- packages/playwright/package.json | 3 +-- packages/playwright/src/DEPS.list | 2 +- packages/playwright/src/{cli.ts => program.ts} | 7 +++---- tests/config/remoteServer.ts | 2 +- tests/library/inspector/inspectorTest.ts | 2 +- utils/build/run-driver-posix.sh | 2 +- 25 files changed, 68 insertions(+), 33 deletions(-) rename packages/playwright-core/src/cli/{cli.ts => programWithTestStub.ts} (96%) mode change 100755 => 100644 create mode 100644 packages/playwright-ct-core/src/program.ts rename packages/playwright/src/{cli.ts => program.ts} (98%) diff --git a/packages/playwright-chromium/cli.js b/packages/playwright-chromium/cli.js index bd995de11422c..86adb86a8489e 100755 --- a/packages/playwright-chromium/cli.js +++ b/packages/playwright-chromium/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('playwright-core/lib/cli/cli'); + +const { program } = require('playwright-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-core/cli.js b/packages/playwright-core/cli.js index bd995de11422c..fb309ead0ae54 100755 --- a/packages/playwright-core/cli.js +++ b/packages/playwright-core/cli.js @@ -14,4 +14,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('playwright-core/lib/cli/cli'); +const { program } = require('./lib/cli/programWithTestStub'); +program.parse(process.argv); diff --git a/packages/playwright-core/package.json b/packages/playwright-core/package.json index eb8667a8b4607..c846a750e7214 100644 --- a/packages/playwright-core/package.json +++ b/packages/playwright-core/package.json @@ -27,7 +27,6 @@ "./lib/image_tools/compare": "./lib/image_tools/compare.js", "./lib/image_tools/imageChannel": "./lib/image_tools/imageChannel.js", "./lib/image_tools/colorUtils": "./lib/image_tools/colorUtils.js", - "./lib/cli/cli": "./lib/cli/cli.js", "./lib/cli/program": "./lib/cli/program.js", "./lib/server/registry/index": "./lib/server/registry/index.js", "./lib/remote/playwrightServer": "./lib/remote/playwrightServer.js", diff --git a/packages/playwright-core/src/cli/program.ts b/packages/playwright-core/src/cli/program.ts index 6e67df3f38b8d..8a00626d19666 100644 --- a/packages/playwright-core/src/cli/program.ts +++ b/packages/playwright-core/src/cli/program.ts @@ -21,6 +21,7 @@ import os from 'os'; import path from 'path'; import type { Command } from '../utilsBundle'; import { program } from '../utilsBundle'; +export { program } from '../utilsBundle'; import { runDriver, runServer, printApiJson, launchBrowserServer } from './driver'; import type { OpenTraceViewerOptions } from '../server/trace/viewer/traceViewer'; import { openTraceInBrowser, openTraceViewerApp } from '../server/trace/viewer/traceViewer'; @@ -690,5 +691,3 @@ function buildBasePlaywrightCLICommand(cliTargetLang: string | undefined): strin } } } - -export default program; diff --git a/packages/playwright-core/src/cli/cli.ts b/packages/playwright-core/src/cli/programWithTestStub.ts old mode 100755 new mode 100644 similarity index 96% rename from packages/playwright-core/src/cli/cli.ts rename to packages/playwright-core/src/cli/programWithTestStub.ts index 8681efe73a97f..1c11c14ec2773 --- a/packages/playwright-core/src/cli/cli.ts +++ b/packages/playwright-core/src/cli/programWithTestStub.ts @@ -1,5 +1,3 @@ -#!/usr/bin/env node - /** * Copyright (c) Microsoft Corporation. * @@ -19,7 +17,8 @@ /* eslint-disable no-console */ import { getPackageManager, gracefullyProcessExitDoNotHang } from '../utils'; -import program from './program'; +import { program } from './program'; +export { program } from './program'; function printPlaywrightTestError(command: string) { const packages: string[] = []; @@ -66,5 +65,3 @@ function addExternalPlaywrightTestCommands() { if (!process.env.PW_LANG_NAME) addExternalPlaywrightTestCommands(); - -program.parse(process.argv); diff --git a/packages/playwright-core/src/outofprocess.ts b/packages/playwright-core/src/outofprocess.ts index 537f37c28d06b..81deaead67e1a 100644 --- a/packages/playwright-core/src/outofprocess.ts +++ b/packages/playwright-core/src/outofprocess.ts @@ -34,7 +34,7 @@ class PlaywrightClient { private _closePromise = new ManualPromise(); constructor(env: any) { - this._driverProcess = childProcess.fork(path.join(__dirname, 'cli', 'cli.js'), ['run-driver'], { + this._driverProcess = childProcess.fork(path.join(__dirname, '..', 'cli.js'), ['run-driver'], { stdio: 'pipe', detached: true, env: { diff --git a/packages/playwright-ct-core/cli.js b/packages/playwright-ct-core/cli.js index 2d0ff422819d8..5db59b62cdc8c 100755 --- a/packages/playwright-ct-core/cli.js +++ b/packages/playwright-ct-core/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('playwright/lib/cli'); + +const { program } = require('./lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-ct-core/package.json b/packages/playwright-ct-core/package.json index 5113fde082ec0..08076282d1e05 100644 --- a/packages/playwright-ct-core/package.json +++ b/packages/playwright-ct-core/package.json @@ -19,8 +19,8 @@ "types": "./index.d.ts", "default": "./index.js" }, - "./cli": "./cli.js", "./lib/mount": "./lib/mount.js", + "./lib/program": "./lib/program.js", "./plugin": "./plugin.js", "./types/component": { "types": "./types/component.d.ts" diff --git a/packages/playwright-ct-core/src/program.ts b/packages/playwright-ct-core/src/program.ts new file mode 100644 index 0000000000000..3014578d6964a --- /dev/null +++ b/packages/playwright-ct-core/src/program.ts @@ -0,0 +1,17 @@ +/** + * 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. + */ + +export { program } from 'playwright/lib/program'; diff --git a/packages/playwright-ct-react/cli.js b/packages/playwright-ct-react/cli.js index 14f0c7df88aec..db414c590442c 100755 --- a/packages/playwright-ct-react/cli.js +++ b/packages/playwright-ct-react/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('@playwright/experimental-ct-core/cli'); + +const { program } = require('@playwright/experimental-ct-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-ct-react17/cli.js b/packages/playwright-ct-react17/cli.js index 14f0c7df88aec..db414c590442c 100755 --- a/packages/playwright-ct-react17/cli.js +++ b/packages/playwright-ct-react17/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('@playwright/experimental-ct-core/cli'); + +const { program } = require('@playwright/experimental-ct-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-ct-solid/cli.js b/packages/playwright-ct-solid/cli.js index 14f0c7df88aec..db414c590442c 100755 --- a/packages/playwright-ct-solid/cli.js +++ b/packages/playwright-ct-solid/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('@playwright/experimental-ct-core/cli'); + +const { program } = require('@playwright/experimental-ct-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-ct-svelte/cli.js b/packages/playwright-ct-svelte/cli.js index 14f0c7df88aec..db414c590442c 100755 --- a/packages/playwright-ct-svelte/cli.js +++ b/packages/playwright-ct-svelte/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('@playwright/experimental-ct-core/cli'); + +const { program } = require('@playwright/experimental-ct-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-ct-vue/cli.js b/packages/playwright-ct-vue/cli.js index 14f0c7df88aec..db414c590442c 100755 --- a/packages/playwright-ct-vue/cli.js +++ b/packages/playwright-ct-vue/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('@playwright/experimental-ct-core/cli'); + +const { program } = require('@playwright/experimental-ct-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-ct-vue2/cli.js b/packages/playwright-ct-vue2/cli.js index 14f0c7df88aec..db414c590442c 100755 --- a/packages/playwright-ct-vue2/cli.js +++ b/packages/playwright-ct-vue2/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('@playwright/experimental-ct-core/cli'); + +const { program } = require('@playwright/experimental-ct-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-firefox/cli.js b/packages/playwright-firefox/cli.js index bd995de11422c..86adb86a8489e 100755 --- a/packages/playwright-firefox/cli.js +++ b/packages/playwright-firefox/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('playwright-core/lib/cli/cli'); + +const { program } = require('playwright-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-test/cli.js b/packages/playwright-test/cli.js index 9d6dee9693417..e42facb076f4b 100755 --- a/packages/playwright-test/cli.js +++ b/packages/playwright-test/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('playwright/cli'); + +const { program } = require('playwright/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright-webkit/cli.js b/packages/playwright-webkit/cli.js index bd995de11422c..86adb86a8489e 100755 --- a/packages/playwright-webkit/cli.js +++ b/packages/playwright-webkit/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('playwright-core/lib/cli/cli'); + +const { program } = require('playwright-core/lib/program'); +program.parse(process.argv); diff --git a/packages/playwright/cli.js b/packages/playwright/cli.js index d4eb087af8879..5db59b62cdc8c 100755 --- a/packages/playwright/cli.js +++ b/packages/playwright/cli.js @@ -14,4 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -module.exports = require('./lib/cli'); + +const { program } = require('./lib/program'); +program.parse(process.argv); diff --git a/packages/playwright/package.json b/packages/playwright/package.json index 7c3e9cae7ddd0..d48364ec90e61 100644 --- a/packages/playwright/package.json +++ b/packages/playwright/package.json @@ -18,9 +18,8 @@ "require": "./index.js", "default": "./index.js" }, - "./cli": "./cli.js", "./package.json": "./package.json", - "./lib/cli": "./lib/cli.js", + "./lib/program": "./lib/program.js", "./lib/transform/babelBundle": "./lib/transform/babelBundle.js", "./lib/transform/compilationCache": "./lib/transform/compilationCache.js", "./lib/transform/esmLoader": "./lib/transform/esmLoader.js", diff --git a/packages/playwright/src/DEPS.list b/packages/playwright/src/DEPS.list index c7a905a82b342..200d75de1ad1f 100644 --- a/packages/playwright/src/DEPS.list +++ b/packages/playwright/src/DEPS.list @@ -3,7 +3,7 @@ common/ ./utils.ts ./utilsBundle.ts -[cli.ts] +[program.ts] ** [index.ts] diff --git a/packages/playwright/src/cli.ts b/packages/playwright/src/program.ts similarity index 98% rename from packages/playwright/src/cli.ts rename to packages/playwright/src/program.ts index 34a0b2372cba7..98bf8f1d122d7 100644 --- a/packages/playwright/src/cli.ts +++ b/packages/playwright/src/program.ts @@ -30,7 +30,8 @@ import type { FullResult, TestError } from '../types/testReporter'; import type { TraceMode } from '../types/test'; import { builtInReporters, defaultReporter, defaultTimeout } from './common/config'; import type { FullConfigInternal } from './common/config'; -import program from 'playwright-core/lib/cli/program'; +import { program } from 'playwright-core/lib/cli/program'; +export { program } from 'playwright-core/lib/cli/program'; import type { ReporterDescription } from '../types/test'; import { prepareErrorStack } from './reporters/base'; import { registerESMLoader } from './common/esmLoaderHost'; @@ -289,7 +290,7 @@ function restartWithExperimentalTsEsm(configFile: string | null): boolean { return false; // Node.js < 20 if (!require('node:module').register) { - const innerProcess = (require('child_process') as typeof import('child_process')).fork(require.resolve('./cli'), process.argv.slice(2), { + const innerProcess = (require('child_process') as typeof import('child_process')).fork(require.resolve('../cli'), process.argv.slice(2), { env: { ...process.env, PW_TS_ESM_LEGACY_LOADER_ON: '1', @@ -346,5 +347,3 @@ addTestCommand(program); addShowReportCommand(program); addListFilesCommand(program); addMergeReportsCommand(program); - -program.parse(process.argv); diff --git a/tests/config/remoteServer.ts b/tests/config/remoteServer.ts index 05ddae2179bf1..04305f31a4fc6 100644 --- a/tests/config/remoteServer.ts +++ b/tests/config/remoteServer.ts @@ -28,7 +28,7 @@ export class RunServer implements PlaywrightServer { _wsEndpoint!: string; async start(childProcess: CommonFixtures['childProcess'], mode?: 'extension' | 'default', env?: NodeJS.ProcessEnv) { - const command = ['node', path.join(__dirname, '..', '..', 'packages', 'playwright-core', 'lib', 'cli', 'cli.js'), 'run-server']; + const command = ['node', path.join(__dirname, '..', '..', 'packages', 'playwright-core', 'cli.js'), 'run-server']; if (mode === 'extension') command.push('--mode=extension'); this._process = childProcess({ diff --git a/tests/library/inspector/inspectorTest.ts b/tests/library/inspector/inspectorTest.ts index 2730a5df03336..8485c9f2196ca 100644 --- a/tests/library/inspector/inspectorTest.ts +++ b/tests/library/inspector/inspectorTest.ts @@ -202,7 +202,7 @@ class CLIMock { constructor(childProcess: CommonFixtures['childProcess'], browserName: string, channel: string | undefined, headless: boolean | undefined, args: string[], executablePath: string | undefined, autoExitWhen: string | undefined) { const nodeArgs = [ 'node', - path.join(__dirname, '..', '..', '..', 'packages', 'playwright-core', 'lib', 'cli', 'cli.js'), + path.join(__dirname, '..', '..', '..', 'packages', 'playwright-core', 'cli.js'), 'codegen', ...args, `--browser=${browserName}`, diff --git a/utils/build/run-driver-posix.sh b/utils/build/run-driver-posix.sh index d00a15ba42080..af4ed059c5f36 100755 --- a/utils/build/run-driver-posix.sh +++ b/utils/build/run-driver-posix.sh @@ -3,4 +3,4 @@ SCRIPT_PATH="$(cd "$(dirname "$0")" ; pwd -P)" if [ -z "$PLAYWRIGHT_NODEJS_PATH" ]; then PLAYWRIGHT_NODEJS_PATH="$SCRIPT_PATH/node" fi -"$PLAYWRIGHT_NODEJS_PATH" "$SCRIPT_PATH/package/lib/cli/cli.js" "$@" +"$PLAYWRIGHT_NODEJS_PATH" "$SCRIPT_PATH/package/cli.js" "$@"