UNPKG

@appetize/create-playwright

Version:

CLI tool to set up Playwright with Appetize

107 lines (90 loc) 3.9 kB
#!/usr/bin/env node import axios from 'axios'; import fs from 'fs'; import inquirer from 'inquirer'; import chalk from 'chalk'; import path from 'path'; import {execSync} from 'child_process'; import {dirname} from 'path'; import {fileURLToPath} from 'url'; async function fetchDevices() { try { const response = await axios.get('https://appetize.io/api/v2/service/devices'); return response.data; } catch (error) { console.error('Error fetching devices:', error); process.exit(1); } } async function main() { const args = process.argv.slice(2); const folder = args[0] || "" const rootDir = path.resolve(process.cwd(), folder); const templatesPath = path.join(dirname(fileURLToPath(import.meta.url)), 'templates'); const devices = await fetchDevices(); const deviceChoices = devices.map(device => ({ name: `${device.name} (${device.platform})`, value: device.id })); const questions = [ { type: 'input', name: 'buildId', message: 'Enter your Appetize App buildId (previously known as publicKey):', default: 'demo' }, { type: 'list', name: 'device', message: 'Select the default device you want to use:', choices: deviceChoices } ]; inquirer.prompt(questions).then(answers => { console.log(chalk.green('Initializing Playwright project...')); execSync(`npm init -y playwright@latest -- --quiet ${folder}`, {stdio: ['inherit', 'ignore', 'inherit']}); console.log(chalk.green('Installing @appetize/playwright...')); execSync(`npm install --prefix ${rootDir} @appetize/playwright`, {stdio: 'inherit'}); const configTemplatePath = path.join(templatesPath, 'configTemplate.txt'); const configTemplate = fs.readFileSync(configTemplatePath, 'utf-8'); const configContent = configTemplate .replace('${answers.device}', answers.device) .replace('${answers.buildId}', answers.buildId); // Create playwright.config.ts const configPath = path.join(rootDir, 'playwright.config.ts'); fs.writeFileSync(configPath, configContent.trim()); console.log(chalk.green('playwright.config.ts has been generated successfully.')); // remove examples directory const testExamplesDir = path.join(rootDir, 'tests-examples'); if (fs.existsSync(testExamplesDir)) { fs.rmSync(testExamplesDir, {recursive: true, force: true}); } // clear tests directory const testDir = path.join(rootDir, 'tests'); if (fs.existsSync(testDir)) { fs.readdirSync(testDir).forEach(file => { fs.unlinkSync(path.join(testDir, file)); }); } // Create app.spec.ts const specTemplatePath = path.join(templatesPath, 'specFileTemplate.txt'); const specTemplate = fs.readFileSync(specTemplatePath, 'utf-8'); const specFilePath = path.join(testDir, 'app.spec.ts'); fs.writeFileSync(specFilePath, specTemplate.trim()); console.log(chalk.green('app.spec.ts has been generated successfully.')); console.log(); // Give user instructions console.info(chalk.blue.bold('You are now ready to run your first test! ✨')); console.log(); console.info(chalk.blue('Start running your tests by typing:')); console.info(chalk.cyan.bold(' npx playwright test --headed')); console.info(chalk.blue('or, headlessly:')); console.info(chalk.cyan.bold(' npx playwright test')); console.log(); console.info(chalk.white('Visit ') + chalk.white.bold('https://docs.appetize.io/testing') + chalk.white(' for more information.') ); }); } main();