@vizzly-testing/cli
Version:
Visual review platform for UI developers and designers
102 lines (93 loc) ⢠2.81 kB
JavaScript
import fs from 'fs/promises';
import path from 'path';
import { VizzlyError } from '../errors/vizzly-error.js';
import { createComponentLogger } from '../utils/logger-factory.js';
/**
* Simple configuration setup for Vizzly CLI
*/
export class InitCommand {
constructor(logger) {
this.logger = logger || createComponentLogger('INIT', {
level: 'info'
});
}
async run(options = {}) {
this.logger.info('šÆ Initializing Vizzly configuration...\n');
try {
// Check for existing config
const configPath = path.join(process.cwd(), 'vizzly.config.js');
const hasConfig = await this.fileExists(configPath);
if (hasConfig && !options.force) {
this.logger.info('ā A vizzly.config.js file already exists. Use --force to overwrite.');
return;
}
// Generate config file with defaults
await this.generateConfigFile(configPath);
// Show next steps
this.showNextSteps();
this.logger.info('\nā
Vizzly CLI setup complete!');
} catch (error) {
throw new VizzlyError('Failed to initialize Vizzly configuration', 'INIT_FAILED', {
error: error.message
});
}
}
async generateConfigFile(configPath) {
const configContent = `export default {
// API configuration
// Set VIZZLY_TOKEN environment variable or uncomment and set here:
// apiToken: 'your-token-here',
// Screenshot configuration
screenshots: {
directory: './screenshots',
formats: ['png']
},
// Server configuration
server: {
port: 47392,
screenshotPath: '/screenshot'
},
// Comparison configuration
comparison: {
threshold: 0.1,
ignoreAntialiasing: true
},
// Upload configuration
upload: {
concurrency: 5,
timeout: 30000
}
};
`;
await fs.writeFile(configPath, configContent, 'utf8');
this.logger.info(`š Created vizzly.config.js`);
}
showNextSteps() {
this.logger.info('\nš Next steps:');
this.logger.info(' 1. Set your API token:');
this.logger.info(' export VIZZLY_TOKEN="your-api-key"');
this.logger.info(' 2. Run your tests with Vizzly:');
this.logger.info(' npx vizzly run "npm test"');
this.logger.info(' 3. Upload screenshots:');
this.logger.info(' npx vizzly upload ./screenshots');
}
async fileExists(filePath) {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
}
}
// Export factory function for CLI
export function createInitCommand(options) {
const command = new InitCommand(options.logger);
return () => command.run(options);
}
// Simple export for direct CLI usage
export async function init(options = {}) {
const command = new InitCommand();
return await command.run(options);
}