@swell/cli
Version:
Swell's command line interface/utility
77 lines (76 loc) • 2.65 kB
JavaScript
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';
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 = await createTestsScaffold({
appPath: this.appPath,
appId,
overwrite: Boolean(flags.overwrite),
});
this.logScaffoldResult(result);
}
logScaffoldResult(result) {
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) {
this.log('');
this.log('Next steps:');
this.log(' 1. npm install');
this.log(' 2. npm test');
}
this.log('');
}
}