UNPKG

@swell/cli

Version:

Swell's command line interface/utility

82 lines (81 loc) 2.95 kB
import { Flags } from '@oclif/core'; import { AppCommand } from '../../app-command.js'; import { toAppId } from '../../lib/create/index.js'; import { createTestsScaffold, } from '../../lib/create/tests.js'; import { detectPackageManager, getPackageManagerCommands, } from '../../lib/package-manager.js'; export default class CreateTests extends AppCommand { static description = 'Initialize a vitest + Cloudflare Workers test setup that reuses swell-cli authentication.'; static examples = [ '$ swell create tests', '$ swell create tests --overwrite', ]; static helpMeta = { usageDirect: '[-y]', }; static flags = { overwrite: Flags.boolean({ default: false, description: 'Overwrite existing test files', }), yes: Flags.boolean({ char: 'y', default: false, description: 'Skip prompts, require all arguments', }), }; static summary = 'Create tests scaffolding for your Swell app.'; async run() { const { flags } = await this.parse(CreateTests); const appIdFromConfig = (this.swellConfig.get('id') || this.swellConfig.get('private_id') || this.swellConfig.get('public_id')); const appId = appIdFromConfig || toAppId(this.swellConfig.get('name')) || 'app'; const [result, pm] = await Promise.all([ createTestsScaffold({ appPath: this.appPath, appId, overwrite: Boolean(flags.overwrite), }), detectPackageManager(this.appPath), ]); this.logScaffoldResult(result, pm); } logScaffoldResult(result, pm) { const hasChanges = result.createdFiles.length > 0 || result.packageJsonUpdated; if (hasChanges) { this.log(''); this.log('✓ Created test scaffolding'); } if (result.createdFiles.length > 0) { this.log(''); this.log('Created:'); for (const file of result.createdFiles) { this.log(` ${file}`); } } if (result.skippedFiles.length > 0) { this.log(''); this.log('Skipped (already exist):'); for (const file of result.skippedFiles) { this.log(` ${file}`); } } if (result.packageJsonUpdated) { this.log(''); this.log('Updated package.json with test scripts and devDependencies.'); } for (const warning of result.warnings) { this.warn(warning); } if (hasChanges) { const commands = getPackageManagerCommands(pm); this.log(''); this.log('Next steps:'); this.log(` 1. ${commands.install}`); this.log(` 2. ${commands.run('test')}`); } this.log(''); } }