diff --git a/package.json b/package.json index 391b7960d..5916af9ad 100644 --- a/package.json +++ b/package.json @@ -97,7 +97,7 @@ } }, "scripts": { - "esbuild": "esbuild ./src/babelBundle.ts ./src/extension.ts ./src/oopReporter.ts ./src/debugTransform.ts --bundle --outdir=out --external:vscode --external:./babelBundle --external:./debugTransform --external:./oopReporter --format=cjs --platform=node --target=ES2019", + "esbuild": "esbuild ./src/babelBundle.ts ./src/extension.ts ./src/oopReporter.ts ./src/debugTransform.ts ./src/playwrightFinder.ts --bundle --outdir=out --external:vscode --external:./babelBundle --external:./debugTransform --external:./oopReporter --external:./playwrightFinder --format=cjs --platform=node --target=ES2019", "build": "npm run esbuild -- --minify", "watch": "npm run esbuild -- --sourcemap --watch", "l10n": "npx @vscode/l10n-dev export -o ./l10n ./src", diff --git a/src/playwrightFinder.ts b/src/playwrightFinder.ts new file mode 100644 index 000000000..57a2d59c3 --- /dev/null +++ b/src/playwrightFinder.ts @@ -0,0 +1,50 @@ +/** + * 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 packages = [ + '@playwright/test', + 'playwright', + '@playwright/experimental-ct-react', + '@playwright/experimental-ct-react17', + '@playwright/experimental-ct-vue', + '@playwright/experimental-ct-vue2', + '@playwright/experimental-ct-solid', + '@playwright/experimental-ct-svelte', +]; + +for (const packageName of packages) { + let packageJSONPath; + try { + packageJSONPath = require.resolve(path.join(packageName, 'package.json'), { paths: [process.cwd()] }); + } catch (e) { + continue; + } + try { + const packageJSON = require(packageJSONPath); + const { version } = packageJSON; + const v = parseFloat(version.replace(/-(next|beta)$/, '')); + const cli = path.join(packageJSONPath, '../cli.js'); + console.log(JSON.stringify({ version: v, cli }, null, 2)); + process.exit(0); + } catch (e) { + console.log(JSON.stringify({ error: String(e) }, null, 2)); + process.exit(0); + } +} + +console.log({ error: 'Playwright installation no found for ' + process.cwd() }); diff --git a/src/playwrightTest.ts b/src/playwrightTest.ts index 1aa5ea158..0b0b01d7d 100644 --- a/src/playwrightTest.ts +++ b/src/playwrightTest.ts @@ -64,41 +64,16 @@ export class PlaywrightTest { } async getPlaywrightInfo(workspaceFolder: string, configFilePath: string): Promise<{ version: number, cli: string }> { - try { - return await this._getPlaywrightInfoImpl(workspaceFolder, configFilePath, '@playwright/test'); - } catch (error) { - // In order to support component testing - return await this._getPlaywrightInfoImpl(workspaceFolder, configFilePath, 'playwright'); - } - } - - private async _getPlaywrightInfoImpl(workspaceFolder: string, configFilePath: string, cliPackage: string): Promise<{ version: number, cli: string }> { const pwtInfo = await this._runNode([ - '-e', - `try { const pwtIndex = require.resolve("${cliPackage}"); const version = require("${cliPackage}/package.json").version; console.log(JSON.stringify({ pwtIndex, version})); } catch { console.log("null"); }`, + require.resolve('./playwrightFinder'), ], path.dirname(configFilePath)); - if (!pwtInfo) - throw new Error(`Cannot find Playwright Test package`); - const { version } = JSON.parse(pwtInfo); - const v = parseFloat(version.replace(/-(next|beta)$/, '')); - - // We only depend on playwright-core in 1.15+, bail out. - if (v < 1.19) - return { cli: '', version: v }; - - const cliInfo = await this._runNode([ - '-e', - `try { const path = require('path'); const cli = path.join(path.dirname(require.resolve("${cliPackage}")), 'cli.js'); console.log(JSON.stringify({ cli })); } catch { console.log("null"); }`, - ], path.dirname(configFilePath)); - if (!cliInfo) - throw new Error(`Cannot find Playwright Test CLI`); - let { cli } = JSON.parse(cliInfo); - - // Dogfood for 'ttest' + const { version, cli, error } = JSON.parse(pwtInfo) as { version: number, cli: string, error?: string }; + if (error) + throw new Error(error); + let cliOverride = cli; if (cli.includes('/playwright/packages/playwright-test/') && configFilePath.includes('playwright-test')) - cli = path.join(workspaceFolder, 'tests/playwright-test/stable-test-runner/node_modules/@playwright/test/cli.js'); - - return { cli, version: v }; + cliOverride = path.join(workspaceFolder, 'tests/playwright-test/stable-test-runner/node_modules/@playwright/test/cli.js'); + return { cli: cliOverride, version }; } async listFiles(config: TestConfig): Promise {