humanbehavior-js 0.4.13 → 0.4.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,8 @@
10
10
  */
11
11
 
12
12
  import { AutoInstallationWizard } from '../core/install-wizard';
13
- import * as readline from 'readline';
13
+ import { ManualFrameworkInstallationWizard } from '../ai/manual-framework-wizard';
14
+ import * as clack from '@clack/prompts';
14
15
  import * as fs from 'fs';
15
16
  import * as path from 'path';
16
17
 
@@ -22,54 +23,57 @@ interface CLIOptions {
22
23
  }
23
24
 
24
25
  class AutoInstallCLI {
25
- private rl: readline.Interface;
26
26
  private options: CLIOptions;
27
27
 
28
28
  constructor(options: CLIOptions) {
29
29
  this.options = options;
30
- this.rl = readline.createInterface({
31
- input: process.stdin,
32
- output: process.stdout
33
- });
34
30
  }
35
31
 
36
32
  async run() {
37
- console.log('šŸš€ HumanBehavior SDK Auto-Installation');
38
- console.log('=====================================\n');
33
+ clack.intro('šŸš€ HumanBehavior SDK Auto-Installation');
39
34
 
40
35
  try {
41
36
  // Get API key
42
37
  const apiKey = await this.getApiKey();
43
38
  if (!apiKey) {
44
- console.error('āŒ API key is required');
39
+ clack.cancel('API key is required');
45
40
  process.exit(1);
46
41
  }
47
42
 
48
43
  // Get project path
49
44
  const projectPath = this.options.projectPath || process.cwd();
50
45
 
46
+ // Choose framework
47
+ const framework = await this.chooseFramework();
48
+ if (!framework) {
49
+ clack.cancel('Installation cancelled.');
50
+ process.exit(0);
51
+ }
52
+
51
53
  // Confirm installation
52
54
  if (!this.options.yes) {
53
- const confirmed = await this.confirmInstallation(projectPath);
55
+ const confirmed = await this.confirmInstallation(projectPath, framework);
54
56
  if (!confirmed) {
55
- console.log('Installation cancelled.');
57
+ clack.cancel('Installation cancelled.');
56
58
  process.exit(0);
57
59
  }
58
60
  }
59
61
 
60
- // Run auto-installation
61
- console.log('šŸ” Detecting your project setup...');
62
- const wizard = new AutoInstallationWizard(apiKey, projectPath);
62
+ // Run installation
63
+ const spinner = clack.spinner();
64
+ spinner.start('šŸ” Analyzing your project...');
65
+
66
+ const wizard = new ManualFrameworkInstallationWizard(apiKey, projectPath, framework);
63
67
  const result = await wizard.install();
64
68
 
69
+ spinner.stop('Detection complete!');
70
+
65
71
  // Display results
66
72
  this.displayResults(result);
67
73
 
68
74
  } catch (error) {
69
- console.error('āŒ Error:', error instanceof Error ? error.message : error);
75
+ clack.cancel(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`);
70
76
  process.exit(1);
71
- } finally {
72
- this.rl.close();
73
77
  }
74
78
  }
75
79
 
@@ -78,68 +82,79 @@ class AutoInstallCLI {
78
82
  return this.options.apiKey;
79
83
  }
80
84
 
81
- return new Promise((resolve) => {
82
- this.rl.question('Enter your HumanBehavior API key: ', (answer) => {
83
- resolve(answer.trim());
84
- });
85
+ const apiKey = await clack.text({
86
+ message: 'Enter your HumanBehavior API key:',
87
+ placeholder: 'hb_...',
88
+ validate: (value) => {
89
+ if (!value) return 'API key is required';
90
+ if (!value.startsWith('hb_')) return 'API key should start with "hb_"';
91
+ return undefined;
92
+ }
93
+ });
94
+
95
+ return apiKey;
96
+ }
97
+
98
+
99
+
100
+ private async chooseFramework(): Promise<string> {
101
+ const framework = await clack.select({
102
+ message: 'Select your framework:',
103
+ options: [
104
+ { label: 'React', value: 'react' },
105
+ { label: 'Next.js', value: 'nextjs' },
106
+ { label: 'Vue', value: 'vue' },
107
+ { label: 'Angular', value: 'angular' },
108
+ { label: 'Svelte', value: 'svelte' },
109
+ { label: 'Nuxt.js', value: 'nuxt' },
110
+ { label: 'Remix', value: 'remix' },
111
+ { label: 'Astro', value: 'astro' },
112
+ { label: 'Gatsby', value: 'gatsby' },
113
+ { label: 'Vanilla JS/TS', value: 'vanilla' }
114
+ ]
85
115
  });
116
+
117
+ return framework;
86
118
  }
87
119
 
88
- private async confirmInstallation(projectPath: string): Promise<boolean> {
89
- console.log(`šŸ“ Project path: ${projectPath}`);
90
- console.log('āš ļø This will modify your codebase to integrate HumanBehavior SDK.');
91
- console.log(' The following changes will be made:');
92
- console.log(' - Install humanbehavior-js package');
93
- console.log(' - Modify your main app file');
94
- console.log(' - Create environment files');
95
- console.log(' - Add SDK initialization code\n');
96
-
97
- return new Promise((resolve) => {
98
- this.rl.question('Continue with auto-installation? (y/n): ', (answer) => {
99
- resolve(answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes');
100
- });
120
+ private async confirmInstallation(projectPath: string, framework: string): Promise<boolean> {
121
+ const confirmed = await clack.confirm({
122
+ message: `Ready to install HumanBehavior SDK in ${projectPath} for ${framework}?`
101
123
  });
124
+
125
+ return confirmed;
102
126
  }
103
127
 
104
128
  private displayResults(result: any) {
105
129
  if (result.success) {
106
- console.log('\nāœ… Installation completed successfully!');
107
- console.log(`šŸ“¦ Framework detected: ${result.framework.name}`);
108
-
109
- if (result.framework.bundler) {
110
- console.log(`šŸ”§ Bundler: ${result.framework.bundler}`);
111
- }
112
-
113
- if (result.framework.packageManager) {
114
- console.log(`šŸ“‹ Package Manager: ${result.framework.packageManager}`);
115
- }
130
+ clack.outro('šŸŽ‰ Installation completed successfully!');
116
131
 
117
- console.log('\nšŸ“ Changes made:');
118
- result.modifications.forEach((mod: any) => {
119
- console.log(` ${mod.action === 'create' ? 'āž•' : 'āœļø'} ${mod.description}`);
120
- console.log(` ${mod.filePath}`);
121
- });
132
+ // Display framework info
133
+ clack.note(`Framework detected: ${result.framework.name} (${result.framework.type})`, 'Framework Info');
122
134
 
123
- console.log('\nšŸŽÆ Next steps:');
124
- result.nextSteps.forEach((step: string) => {
125
- console.log(` ${step}`);
126
- });
135
+ // Display modifications
136
+ if (result.modifications && result.modifications.length > 0) {
137
+ const modifications = result.modifications.map((mod: any) =>
138
+ `${mod.action}: ${mod.filePath} - ${mod.description}`
139
+ );
140
+ clack.note(modifications.join('\n'), 'Files Modified');
141
+ }
142
+
143
+ // Display next steps
144
+ if (result.nextSteps && result.nextSteps.length > 0) {
145
+ clack.note(result.nextSteps.join('\n'), 'Next Steps');
146
+ }
127
147
 
128
- console.log('\nšŸš€ Your app is now ready to track user behavior!');
129
- console.log('šŸ“Š View sessions in your HumanBehavior dashboard');
130
-
131
148
  } else {
132
- console.log('\nāŒ Installation failed:');
133
- result.errors.forEach((error: string) => {
134
- console.log(` ${error}`);
135
- });
149
+ clack.cancel('Installation failed');
136
150
 
137
- console.log('\nšŸ’” Try running with --help for more options');
151
+ if (result.errors && result.errors.length > 0) {
152
+ clack.note(result.errors.join('\n'), 'Errors');
153
+ }
138
154
  }
139
155
  }
140
156
  }
141
157
 
142
- // CLI argument parsing
143
158
  function parseArgs(): CLIOptions {
144
159
  const args = process.argv.slice(2);
145
160
  const options: CLIOptions = {};
@@ -147,18 +162,28 @@ function parseArgs(): CLIOptions {
147
162
  for (let i = 0; i < args.length; i++) {
148
163
  const arg = args[i];
149
164
 
150
- if (arg === '--yes' || arg === '-y') {
151
- options.yes = true;
152
- } else if (arg === '--dry-run' || arg === '-d') {
153
- options.dryRun = true;
154
- } else if (arg === '--project' || arg === '-p') {
155
- options.projectPath = args[i + 1];
156
- i++;
157
- } else if (arg === '--help' || arg === '-h') {
158
- showHelp();
159
- process.exit(0);
160
- } else if (!options.apiKey) {
161
- options.apiKey = arg;
165
+ switch (arg) {
166
+ case '--help':
167
+ case '-h':
168
+ showHelp();
169
+ process.exit(0);
170
+ break;
171
+ case '--yes':
172
+ case '-y':
173
+ options.yes = true;
174
+ break;
175
+ case '--dry-run':
176
+ options.dryRun = true;
177
+ break;
178
+ case '--project':
179
+ case '-p':
180
+ options.projectPath = args[++i];
181
+ break;
182
+ default:
183
+ if (!options.apiKey && !arg.startsWith('-')) {
184
+ options.apiKey = arg;
185
+ }
186
+ break;
162
187
  }
163
188
  }
164
189
 
@@ -167,59 +192,33 @@ function parseArgs(): CLIOptions {
167
192
 
168
193
  function showHelp() {
169
194
  console.log(`
170
- HumanBehavior SDK Auto-Installation CLI
195
+ šŸš€ HumanBehavior SDK Auto-Installation
171
196
 
172
- Usage: npx humanbehavior-js [api-key] [options]
197
+ Usage: npx humanbehavior-js auto-install [api-key] [options]
173
198
 
174
- This tool automatically detects your project's framework and modifies your codebase
175
- to integrate the HumanBehavior SDK with minimal user intervention.
176
-
177
- Arguments:
178
- api-key Your HumanBehavior API key
199
+ This tool automatically detects your framework and integrates the HumanBehavior SDK.
179
200
 
180
201
  Options:
181
- -y, --yes Skip all prompts and use defaults
182
- -d, --dry-run Show what would be changed without making changes
183
- -p, --project <path> Project directory (default: current directory)
184
- -h, --help Show this help message
202
+ -h, --help Show this help message
203
+ -y, --yes Skip all prompts and use defaults
204
+ --dry-run Show what would be changed without making changes
205
+ -p, --project <path> Specify project directory
185
206
 
186
207
  Examples:
187
- npx humanbehavior-js your-api-key
188
- npx humanbehavior-js your-api-key --yes
189
- npx humanbehavior-js your-api-key -p /path/to/project
190
-
191
- Supported Frameworks:
192
- āœ… React (CRA, Vite, Webpack)
193
- āœ… Next.js (App Router, Pages Router)
194
- āœ… Vue (Vue CLI, Vite)
195
- āœ… Angular
196
- āœ… Svelte (SvelteKit, Vite)
197
- āœ… Remix
198
- āœ… Vanilla JS/TS
199
- āœ… Node.js (CommonJS & ESM)
200
-
201
- The tool will:
202
- 1. šŸ” Auto-detect your project's framework and setup
203
- 2. šŸ“¦ Install the humanbehavior-js package
204
- 3. āœļø Modify your codebase to integrate the SDK
205
- 4. šŸ”§ Create environment files with your API key
206
- 5. šŸš€ Make your app ready to track user behavior
208
+ npx humanbehavior-js auto-install
209
+ npx humanbehavior-js auto-install hb_your_api_key_here
210
+ npx humanbehavior-js auto-install --project ./my-app --yes
207
211
  `);
208
212
  }
209
213
 
210
214
  // Main execution
211
- const options = parseArgs();
212
-
213
- // Check if we have enough arguments (api-key is required)
214
- if (process.argv.length < 3 || process.argv.includes('--help') || process.argv.includes('-h')) {
215
- showHelp();
216
- process.exit(0);
215
+ if (import.meta.url === `file://${process.argv[1]}`) {
216
+ const options = parseArgs();
217
+ const cli = new AutoInstallCLI(options);
218
+ cli.run().catch((error) => {
219
+ clack.cancel(`Unexpected error: ${error.message}`);
220
+ process.exit(1);
221
+ });
217
222
  }
218
223
 
219
- const cli = new AutoInstallCLI(options);
220
- cli.run().catch((error) => {
221
- console.error('āŒ Fatal error:', error);
222
- process.exit(1);
223
- });
224
-
225
224
  export { AutoInstallCLI };