All files / src/cli/commands generate.ts

0% Statements 0/103
0% Branches 0/50
0% Functions 0/12
0% Lines 0/102

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
import inquirer from 'inquirer';
import chalk from 'chalk';
import ora from 'ora';
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
import { join, resolve as pathResolve } from 'path';
import { getTemplate } from '../templates';
import { ToolkitVersionDetails } from '@/core/types';
 
// Helper to get main package.json details
function getToolkitVersionDetails(): ToolkitVersionDetails {
  try {
    const packageJsonPath = pathResolve(__dirname, '../../../package.json');
    const packageJsonContent = readFileSync(packageJsonPath, 'utf-8');
    const packageJson = JSON.parse(packageJsonContent);
    return {
      version: packageJson.version,
      dependencies: packageJson.dependencies,
      devDependencies: packageJson.devDependencies,
    };
  } catch (error) {
    console.warn(
      chalk.yellow(
        'Could not read main package.json for dynamic versions. Using fallback versions.'
      )
    );
    // Fallback versions if reading fails (should ideally not happen in deployed package)
    return {
      version: '1.0.1', // Needs to be updated if this file is copied without context
      dependencies: {
        crawlee: '^3.7.0',
        playwright: '^1.40.0',
      },
      devDependencies: {
        '@types/node': '^20.10.0',
        typescript: '^5.3.0',
        'ts-node': '^10.9.0',
        jest: '^29.7.0',
        '@types/jest': '^29.5.0',
      },
    };
  }
}
 
const toolkitDetails = getToolkitVersionDetails();
 
/**
 * Template types available
 */
export type TemplateType = 'basic' | 'api' | 'form' | 'advanced' | 'infinite-scroll' | 'js-heavy';
 
/**
 * Generation options
 */
export interface GenerateOptions {
  template?: TemplateType;
  name?: string;
  output?: string;
  interactive?: boolean;
}
 
/**
 * Scraper configuration for generation
 */
export interface ScraperConfig {
  name: string;
  description: string;
  url: string;
  template: TemplateType;
  navigation: {
    type: 'direct' | 'form' | 'api';
    inputSelector?: string;
    submitSelector?: string;
    paramName?: string;
  };
  waitStrategy: {
    type: 'selector' | 'response' | 'timeout';
    selector?: string;
    urlPattern?: string;
    duration?: number;
  };
  outputFields: Array<{
    name: string;
    selector: string;
    type: 'text' | 'attribute' | 'html';
    attribute?: string;
  }>;
  options: {
    retries: number;
    timeout: number;
    loadImages: boolean;
  };
}
 
/**
 * Generate a new scraper
 */
export async function generateScraper(options: GenerateOptions): Promise<void> {
  console.log(chalk.blue('🚀 Crawlee Scraper Generator'));
  console.log();
 
  let config: ScraperConfig;
 
  if (options.interactive !== false) {
    config = await promptForConfig(options);
  } else {
    config = await createConfigFromOptions(options);
  }
 
  const spinner = ora('Generating scraper...').start();
 
  try {
    await generateScraperFiles(config, options.output || './scrapers');
    spinner.succeed(chalk.green('Scraper generated successfully!'));
 
    console.log();
    console.log(chalk.yellow('Next steps:'));
    console.log(`  1. cd ${options.output || './scrapers'}/${config.name}`);
    console.log('  2. npm install');
    console.log('  3. npm run dev');
    console.log();
  } catch (error) {
    spinner.fail(chalk.red('Failed to generate scraper'));
    console.error(chalk.red(error instanceof Error ? error.message : String(error)));
    process.exit(1);
  }
}
 
/**
 * Prompt user for scraper configuration
 */
async function promptForConfig(options: GenerateOptions): Promise<ScraperConfig> {
  const answers = await inquirer.prompt([
    {
      type: 'input',
      name: 'name',
      message: 'Scraper name:',
      default: options.name || 'my-scraper',
      validate: (input: string) => {
        Iif (!input.trim()) return 'Name is required';
        Iif (!/^[a-z0-9-_]+$/.test(input))
          return 'Name must contain only lowercase letters, numbers, hyphens, and underscores';
        return true;
      },
    },
    {
      type: 'input',
      name: 'description',
      message: 'Scraper description:',
      default: 'A web scraper built with Crawlee Scraper Toolkit',
    },
    {
      type: 'list',
      name: 'template',
      message: 'Choose a template:',
      choices: [
        { name: 'Basic - Simple page scraping', value: 'basic' },
        { name: 'API - Extract data from API responses', value: 'api' },
        { name: 'Form - Fill and submit forms', value: 'form' },
        { name: 'Advanced - Custom navigation and parsing', value: 'advanced' },
        { name: 'Infinite Scroll - Handles infinite scroll pagination', value: 'infinite-scroll' },
        { name: 'JS-Heavy Site - For sites with complex JavaScript interactions', value: 'js-heavy' },
      ],
      default: options.template || 'basic',
    },
    {
      type: 'input',
      name: 'url',
      message: 'Target URL:',
      validate: (input: string) => {
        try {
          new URL(input);
          return true;
        } catch {
          return 'Please enter a valid URL';
        }
      },
    },
  ]);
 
  // Template-specific questions
  const templateConfig = await promptForTemplateConfig(answers.template, answers.url);
 
  return {
    ...answers,
    ...templateConfig,
  };
}
 
/**
 * Prompt for template-specific configuration
 */
async function promptForTemplateConfig(
  template: TemplateType,
  _url: string
): Promise<Partial<ScraperConfig>> {
  const config: Partial<ScraperConfig> = {
    navigation: { type: 'direct' },
    waitStrategy: { type: 'timeout', duration: 2000 },
    outputFields: [],
    options: {
      retries: 3,
      timeout: 30000,
      loadImages: false,
    },
  };
 
  switch (template) {
    case 'form':
      const formAnswers = await inquirer.prompt([
        {
          type: 'input',
          name: 'inputSelector',
          message: 'Input field selector (CSS selector):',
          default: 'input[type="text"]',
        },
        {
          type: 'input',
          name: 'submitSelector',
          message: 'Submit button selector (CSS selector):',
          default: 'button[type="submit"]',
        },
      ]);
 
      config.navigation = {
        type: 'form',
        inputSelector: formAnswers.inputSelector,
        submitSelector: formAnswers.submitSelector,
      };
 
      config.waitStrategy = { type: 'selector', selector: '.results' };
      break;
 
    case 'api':
      const apiAnswers = await inquirer.prompt([
        {
          type: 'input',
          name: 'paramName',
          message: 'URL parameter name for input:',
          default: 'q',
        },
        {
          type: 'input',
          name: 'urlPattern',
          message: 'API response URL pattern to wait for:',
          default: '/api/',
        },
      ]);
 
      config.navigation = {
        type: 'api',
        paramName: apiAnswers.paramName,
      };
 
      config.waitStrategy = {
        type: 'response',
        urlPattern: apiAnswers.urlPattern,
      };
      break;
 
    case 'basic':
    case 'advanced':
    case 'infinite-scroll':
    case 'js-heavy':
      const waitAnswers = await inquirer.prompt([
        {
          type: 'list',
          name: 'waitType',
          message: 'How to wait for page to load:',
          choices: [
            { name: 'Wait for specific element', value: 'selector' },
            { name: 'Wait for network response', value: 'response' },
            { name: 'Wait for fixed time', value: 'timeout' },
          ],
          default: 'timeout',
        },
      ]);
 
      if (waitAnswers.waitType === 'selector') {
        const selectorAnswer = await inquirer.prompt([
          {
            type: 'input',
            name: 'selector',
            message: 'CSS selector to wait for:',
            default: '.content',
          },
        ]);
        config.waitStrategy = { type: 'selector', selector: selectorAnswer.selector };
      } else if (waitAnswers.waitType === 'response') {
        const responseAnswer = await inquirer.prompt([
          {
            type: 'input',
            name: 'urlPattern',
            message: 'URL pattern to wait for:',
            default: '/api/',
          },
        ]);
        config.waitStrategy = { type: 'response', urlPattern: responseAnswer.urlPattern };
      } else {
        const timeoutAnswer = await inquirer.prompt([
          {
            type: 'number',
            name: 'duration',
            message: 'Wait duration (milliseconds):',
            default: 2000,
          },
        ]);
        config.waitStrategy = { type: 'timeout', duration: timeoutAnswer.duration };
      }
      break;
  }
 
  // Ask for output fields
  const addFields = await inquirer.prompt([
    {
      type: 'confirm',
      name: 'addOutputFields',
      message: 'Do you want to configure output fields now?',
      default: true,
    },
  ]);
 
  Iif (addFields.addOutputFields) {
    config.outputFields = await promptForOutputFields();
  }
 
  return config;
}
 
/**
 * Prompt for output fields configuration
 */
async function promptForOutputFields(): Promise<ScraperConfig['outputFields']> {
  const fields: ScraperConfig['outputFields'] = [];
  let addMore = true;
 
  while (addMore) {
    const fieldConfig = await inquirer.prompt([
      {
        type: 'input',
        name: 'name',
        message: 'Field name:',
        validate: (input: string) => (input.trim() ? true : 'Field name is required'),
      },
      {
        type: 'input',
        name: 'selector',
        message: 'CSS selector:',
        validate: (input: string) => (input.trim() ? true : 'Selector is required'),
      },
      {
        type: 'list',
        name: 'type',
        message: 'Data type:',
        choices: [
          { name: 'Text content', value: 'text' },
          { name: 'HTML content', value: 'html' },
          { name: 'Attribute value', value: 'attribute' },
        ],
        default: 'text',
      },
    ]);
 
    Iif (fieldConfig.type === 'attribute') {
      const attrAnswer = await inquirer.prompt([
        {
          type: 'input',
          name: 'attribute',
          message: 'Attribute name:',
          default: 'href',
        },
      ]);
      fieldConfig.attribute = attrAnswer.attribute;
    }
 
    fields.push(fieldConfig);
 
    const continueAnswer = await inquirer.prompt([
      {
        type: 'confirm',
        name: 'addMore',
        message: 'Add another field?',
        default: false,
      },
    ]);
 
    addMore = continueAnswer.addMore;
  }
 
  return fields;
}
 
/**
 * Create configuration from command line options
 */
async function createConfigFromOptions(options: GenerateOptions): Promise<ScraperConfig> {
  Iif (!options.name) {
    throw new Error('Scraper name is required in non-interactive mode');
  }
 
  return {
    name: options.name,
    description: 'Generated scraper',
    url: 'https://example.com',
    template: options.template || 'basic',
    navigation: { type: 'direct' },
    waitStrategy: { type: 'timeout', duration: 2000 },
    outputFields: [],
    options: {
      retries: 3,
      timeout: 30000,
      loadImages: false,
    },
  };
}
 
/**
 * Generate scraper files
 */
async function generateScraperFiles(config: ScraperConfig, outputDir: string): Promise<void> {
  const scraperDir = join(outputDir, config.name);
 
  // Create directory
  Iif (!existsSync(outputDir)) {
    mkdirSync(outputDir, { recursive: true });
  }
 
  Iif (!existsSync(scraperDir)) {
    mkdirSync(scraperDir, { recursive: true });
  }
 
  // Get template files
  const template = getTemplate(config.template);
 
  // Generate files
  for (const [filename, content] of Object.entries(template.files)) {
    const filePath = join(scraperDir, filename);
    const processedContent = processTemplate(content, config);
 
    // Create subdirectories if needed
    const dir = join(filePath, '..');
    Iif (!existsSync(dir)) {
      mkdirSync(dir, { recursive: true });
    }
 
    writeFileSync(filePath, processedContent);
  }
 
  // Generate package.json
  const packageJson = {
    name: config.name,
    version: '1.0.0', // Individual scraper version
    description: config.description,
    main: 'dist/index.js', // Assuming TS output
    scripts: {
      build: 'tsc',
      dev: 'ts-node src/index.ts', // For direct execution
      start: 'node dist/index.js', // For direct execution after build
      test: 'jest', // Basic test setup
    },
    dependencies: {
      'crawlee-scraper-toolkit': `^${toolkitDetails.version}`, // Use current toolkit version
      playwright: toolkitDetails.dependencies?.playwright || '^1.40.0', // From toolkit's deps
      // Crawlee is a peer/direct dep of the toolkit, but generated scrapers might need it directly too
      crawlee: toolkitDetails.dependencies?.crawlee || '^3.7.0',
    },
    devDependencies: {
      '@types/node': toolkitDetails.devDependencies?.['@types/node'] || '^20.10.0',
      typescript: toolkitDetails.devDependencies?.typescript || '^5.3.0',
      'ts-node': toolkitDetails.devDependencies?.['ts-node'] || '^10.9.0',
      jest: toolkitDetails.devDependencies?.jest || '^29.7.0',
      '@types/jest': toolkitDetails.devDependencies?.['@types/jest'] || '^29.5.0',
    },
  };
 
  writeFileSync(join(scraperDir, 'package.json'), JSON.stringify(packageJson, null, 2));
 
  // Generate TypeScript config
  const tsConfig = {
    compilerOptions: {
      target: 'ES2020',
      module: 'commonjs',
      outDir: './dist',
      rootDir: './src',
      strict: true,
      esModuleInterop: true,
      skipLibCheck: true,
      forceConsistentCasingInFileNames: true,
    },
    include: ['src/**/*'],
    exclude: ['node_modules', 'dist'],
  };
 
  writeFileSync(join(scraperDir, 'tsconfig.json'), JSON.stringify(tsConfig, null, 2));
}
 
/**
 * Process template with configuration
 */
function processTemplate(template: string, config: ScraperConfig): string {
  return template
    .replace(/\{\{name\}\}/g, config.name)
    .replace(/\{\{description\}\}/g, config.description)
    .replace(/\{\{url\}\}/g, config.url)
    .replace(/\{\{navigation\}\}/g, JSON.stringify(config.navigation, null, 2))
    .replace(/\{\{waitStrategy\}\}/g, JSON.stringify(config.waitStrategy, null, 2))
    .replace(/\{\{outputFields\}\}/g, JSON.stringify(config.outputFields, null, 2))
    .replace(/\{\{options\}\}/g, JSON.stringify(config.options, null, 2));
}