Skip to content

Commit

Permalink
feat(test-utils): add webx-launch command (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmkx authored Jan 24, 2024
1 parent bf5d1bf commit ea9de66
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-snails-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@webx-kit/test-utils": patch
---

feat(test-utils): add webx-launch command
8 changes: 8 additions & 0 deletions packages/test-utils/bin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": ["../tsconfig.json"],
"compilerOptions": {
"target": "ES2020",
"module": "ES2020",
"noEmit": true
}
}
47 changes: 47 additions & 0 deletions packages/test-utils/bin/webx-launch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node

// @ts-check
/// <reference types="@webx-kit/chrome-types" />
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { chalk, fs, findMonorepoRoot } from '@modern-js/utils';
import commander from '@modern-js/utils/commander';
import { chromium } from '@playwright/test';

commander.program.option('--path <path>', 'extension path', 'dist');

const opts = commander.program.parse(process.argv).opts();

const extensionPath = path.resolve(opts.path);
if (!fs.existsSync(extensionPath)) throw new Error(`\`${extensionPath}\` does not exist`);
if (!fs.statSync(extensionPath).isDirectory()) throw new Error(`\`${extensionPath}\` is not a directory`);

const dirname = path.dirname(fileURLToPath(import.meta.url));
const monorepoRoot = findMonorepoRoot(dirname);
const userDataDir = path.resolve(
monorepoRoot || path.resolve(dirname, '..'),
'node_modules/.cache/webx-browser-user-dir'
);

// it must be "persisted", otherwise the browser will enable incognito mode
chromium
.launchPersistentContext(userDataDir, {
headless: false,
colorScheme: 'no-preference',
viewport: null,
ignoreDefaultArgs: ['--disable-extensions', '--enable-automation'],
args: ['--hide-crash-restore-bubble', `--load-extension=${extensionPath}`],
})
.then(async (context) => {
console.log(chalk.green`Start successfully`);

const outdatedPages = context.pages();
const page = await context.newPage();
await Promise.all(outdatedPages.map((page) => page.close()));
await page.goto(`chrome://extensions/`);

await page.evaluate(() => chrome.developerPrivate.updateProfileConfiguration({ inDeveloperMode: true }));
})
.catch((err) => {
console.log(chalk.red`Start failed`, err);
});
4 changes: 4 additions & 0 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": "@webx-kit/test-utils",
"version": "0.0.0",
"bin": {
"webx-launch": "bin/webx-launch.mjs"
},
"exports": {
"./*": {
"types": "./src/*.ts",
Expand Down Expand Up @@ -39,6 +42,7 @@
"lint:type": "tsc --noEmit"
},
"dependencies": {
"@modern-js/utils": "^2.46.1",
"@webx-kit/chrome-types": "workspace:^"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/test-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
}
"noEmit": false
},
"include": ["./src"]
}
3 changes: 2 additions & 1 deletion packages/test-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"experimentalDecorators": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"baseUrl": "./"
"baseUrl": "./",
"noEmit": true
},
"exclude": ["dist"]
}
5 changes: 3 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ea9de66

Please sign in to comment.