From 5856f799ab81fe5356dbdc313dafaae6ab8199c2 Mon Sep 17 00:00:00 2001 From: "kristof.muhi" Date: Sun, 29 Mar 2026 18:52:07 +0200 Subject: [PATCH] feat: add init command to scaffold config and example test Adds `mobilewright init` which copies pre-bundled template files (mobilewright.config.ts and example.test.ts) into the user's project directory so they can run their first test immediately after install. Skips files that already exist to avoid clobbering existing work. --- packages/mobilewright/package.json | 3 ++- packages/mobilewright/src/cli.ts | 26 ++++++++++++++++++- .../mobilewright/templates/example.test.ts | 5 ++++ .../templates/mobilewright.config.ts | 8 ++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 packages/mobilewright/templates/example.test.ts create mode 100644 packages/mobilewright/templates/mobilewright.config.ts diff --git a/packages/mobilewright/package.json b/packages/mobilewright/package.json index f2683a1..0aaa1f9 100644 --- a/packages/mobilewright/package.json +++ b/packages/mobilewright/package.json @@ -29,7 +29,8 @@ "directory": "packages/mobilewright" }, "files": [ - "dist" + "dist", + "templates" ], "dependencies": { "@mobilewright/core": "^0.0.1", diff --git a/packages/mobilewright/src/cli.ts b/packages/mobilewright/src/cli.ts index e3d0887..6099669 100644 --- a/packages/mobilewright/src/cli.ts +++ b/packages/mobilewright/src/cli.ts @@ -2,7 +2,9 @@ import { Command } from 'commander'; import { existsSync } from 'node:fs'; -import { resolve } from 'node:path'; +import { readFile, writeFile } from 'node:fs/promises'; +import { resolve, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; import { createRequire } from 'node:module'; import { MobilecliDriver, DEFAULT_URL } from '@mobilewright/driver-mobilecli'; @@ -13,6 +15,7 @@ const _require = createRequire(import.meta.url); const _pkg = _require('../package.json') as { version: string }; const HTML_REPORT_DIR = 'mobilewright-report'; +const TEMPLATES_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'templates'); const program = new Command(); program.name('mobilewright'); @@ -156,6 +159,27 @@ program if (checks.some(c => c.status === 'error')) process.exitCode = 1; }); +// ── init ─────────────────────────────────────────────────────────────── +program + .command('init') + .description('scaffold a mobilewright.config.ts and example test in the current directory') + .action(async () => { + const files = [ + { src: 'mobilewright.config.ts', dest: resolve(process.cwd(), 'mobilewright.config.ts') }, + { src: 'example.test.ts', dest: resolve(process.cwd(), 'example.test.ts') }, + ]; + + for (const { src, dest } of files) { + if (existsSync(dest)) { + console.log(`skipped ${src} (already exists)`); + continue; + } + const content = await readFile(resolve(TEMPLATES_DIR, src), 'utf8'); + await writeFile(dest, content, 'utf8'); + console.log(`created ${src}`); + } + }); + function padRight(str: string, len: number): string { return str.length >= len ? str + ' ' : str + ' '.repeat(len - str.length); } diff --git a/packages/mobilewright/templates/example.test.ts b/packages/mobilewright/templates/example.test.ts new file mode 100644 index 0000000..c4d83e0 --- /dev/null +++ b/packages/mobilewright/templates/example.test.ts @@ -0,0 +1,5 @@ +import { test, expect } from '@mobilewright/test'; + +test('app launches and shows home screen', async ({ screen }) => { + await expect(screen.getByText('Welcome')).toBeVisible(); +}); diff --git a/packages/mobilewright/templates/mobilewright.config.ts b/packages/mobilewright/templates/mobilewright.config.ts new file mode 100644 index 0000000..dda7acb --- /dev/null +++ b/packages/mobilewright/templates/mobilewright.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from 'mobilewright'; + +export default defineConfig({ + platform: 'ios', + bundleId: 'com.example.myapp', + deviceName: /iPhone 16/, + timeout: 10_000, +});