humanbehavior-js
Version:
SDK for HumanBehavior session and event recording
224 lines (183 loc) ⢠6.06 kB
text/typescript
/**
* HumanBehavior SDK Auto-Installation CLI
*
* Usage: npx humanbehavior-js auto-install [api-key]
*
* This tool automatically detects the user's framework and modifies their codebase
* to integrate the SDK with minimal user intervention.
*/
import { AutoInstallationWizard } from '../core/install-wizard';
import { ManualFrameworkInstallationWizard } from '../ai/manual-framework-wizard';
import * as clack from '@clack/prompts';
import * as fs from 'fs';
import * as path from 'path';
interface CLIOptions {
apiKey?: string;
projectPath?: string;
yes?: boolean;
dryRun?: boolean;
}
class AutoInstallCLI {
private options: CLIOptions;
constructor(options: CLIOptions) {
this.options = options;
}
async run() {
clack.intro('š HumanBehavior SDK Auto-Installation');
try {
// Get API key
const apiKey = await this.getApiKey();
if (!apiKey) {
clack.cancel('API key is required');
process.exit(1);
}
// Get project path
const projectPath = this.options.projectPath || process.cwd();
// Choose framework
const framework = await this.chooseFramework();
if (!framework) {
clack.cancel('Installation cancelled.');
process.exit(0);
}
// Confirm installation
if (!this.options.yes) {
const confirmed = await this.confirmInstallation(projectPath, framework);
if (!confirmed) {
clack.cancel('Installation cancelled.');
process.exit(0);
}
}
// Run installation
const spinner = clack.spinner();
spinner.start('š Analyzing your project...');
const wizard = new ManualFrameworkInstallationWizard(apiKey, projectPath, framework);
const result = await wizard.install();
spinner.stop('Detection complete!');
// Display results
this.displayResults(result);
} catch (error) {
clack.cancel(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`);
process.exit(1);
}
}
private async getApiKey(): Promise<string> {
if (this.options.apiKey) {
return this.options.apiKey;
}
const apiKey = await clack.text({
message: 'Enter your HumanBehavior API key:',
placeholder: 'hb_...',
validate: (value) => {
if (!value) return 'API key is required';
if (!value.startsWith('hb_')) return 'API key should start with "hb_"';
return undefined;
}
});
return apiKey;
}
private async chooseFramework(): Promise<string> {
const framework = await clack.select({
message: 'Select your framework:',
options: [
{ label: 'React', value: 'react' },
{ label: 'Next.js', value: 'nextjs' },
{ label: 'Vue', value: 'vue' },
{ label: 'Angular', value: 'angular' },
{ label: 'Svelte', value: 'svelte' },
{ label: 'Nuxt.js', value: 'nuxt' },
{ label: 'Remix', value: 'remix' },
{ label: 'Astro', value: 'astro' },
{ label: 'Gatsby', value: 'gatsby' },
{ label: 'Vanilla JS/TS', value: 'vanilla' }
]
});
return framework;
}
private async confirmInstallation(projectPath: string, framework: string): Promise<boolean> {
const confirmed = await clack.confirm({
message: `Ready to install HumanBehavior SDK in ${projectPath} for ${framework}?`
});
return confirmed;
}
private displayResults(result: any) {
if (result.success) {
clack.outro('š Installation completed successfully!');
// Display framework info
clack.note(`Framework detected: ${result.framework.name} (${result.framework.type})`, 'Framework Info');
// Display modifications
if (result.modifications && result.modifications.length > 0) {
const modifications = result.modifications.map((mod: any) =>
`${mod.action}: ${mod.filePath} - ${mod.description}`
);
clack.note(modifications.join('\n'), 'Files Modified');
}
// Display next steps
if (result.nextSteps && result.nextSteps.length > 0) {
clack.note(result.nextSteps.join('\n'), 'Next Steps');
}
} else {
clack.cancel('Installation failed');
if (result.errors && result.errors.length > 0) {
clack.note(result.errors.join('\n'), 'Errors');
}
}
}
}
function parseArgs(): CLIOptions {
const args = process.argv.slice(2);
const options: CLIOptions = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case '--help':
case '-h':
showHelp();
process.exit(0);
break;
case '--yes':
case '-y':
options.yes = true;
break;
case '--dry-run':
options.dryRun = true;
break;
case '--project':
case '-p':
options.projectPath = args[++i];
break;
default:
if (!options.apiKey && !arg.startsWith('-')) {
options.apiKey = arg;
}
break;
}
}
return options;
}
function showHelp() {
console.log(`
š HumanBehavior SDK Auto-Installation
Usage: npx humanbehavior-js auto-install [api-key] [options]
This tool automatically detects your framework and integrates the HumanBehavior SDK.
Options:
-h, --help Show this help message
-y, --yes Skip all prompts and use defaults
--dry-run Show what would be changed without making changes
-p, --project <path> Specify project directory
Examples:
npx humanbehavior-js auto-install
npx humanbehavior-js auto-install hb_your_api_key_here
npx humanbehavior-js auto-install --project ./my-app --yes
`);
}
// Main execution
if (import.meta.url === `file://${process.argv[1]}`) {
const options = parseArgs();
const cli = new AutoInstallCLI(options);
cli.run().catch((error) => {
clack.cancel(`Unexpected error: ${error.message}`);
process.exit(1);
});
}
export { AutoInstallCLI };