aboutsummaryrefslogtreecommitdiff
path: root/scripts/seed-data.ts
blob: 82a0564c3672662e5e44c33ae9c1057ed5075850 (plain) (blame)
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
#!/usr/bin/env node
/* eslint-disable no-console */

/**
 * Umami Sample Data Generator
 *
 * Generates realistic analytics data for local development and testing.
 * Creates two demo websites:
 *   - Demo Blog: Low traffic (~100 sessions/month)
 *   - Demo SaaS: Average traffic (~500 sessions/day)
 *
 * Usage:
 *   npm run seed-data              # Generate 30 days of data
 *   npm run seed-data -- --days 90 # Generate 90 days of data
 *   npm run seed-data -- --clear   # Clear existing demo data first
 *   npm run seed-data -- --verbose # Show detailed progress
 */

import { seed, type SeedConfig } from './seed/index.js';

function parseArgs(): SeedConfig {
  const args = process.argv.slice(2);

  const config: SeedConfig = {
    days: 30,
    clear: false,
    verbose: false,
  };

  for (let i = 0; i < args.length; i++) {
    const arg = args[i];

    if (arg === '--days' && args[i + 1]) {
      config.days = parseInt(args[i + 1], 10);
      if (isNaN(config.days) || config.days < 1) {
        console.error('Error: --days must be a positive integer');
        process.exit(1);
      }
      i++;
    } else if (arg === '--clear') {
      config.clear = true;
    } else if (arg === '--verbose' || arg === '-v') {
      config.verbose = true;
    } else if (arg === '--help' || arg === '-h') {
      printHelp();
      process.exit(0);
    } else if (arg.startsWith('--days=')) {
      config.days = parseInt(arg.split('=')[1], 10);
      if (isNaN(config.days) || config.days < 1) {
        console.error('Error: --days must be a positive integer');
        process.exit(1);
      }
    }
  }

  return config;
}

function printHelp(): void {
  console.log(`
Umami Sample Data Generator

Generates realistic analytics data for local development and testing.

Usage:
  npm run seed-data [options]

Options:
  --days <number>    Number of days of data to generate (default: 30)
  --clear            Clear existing demo data before generating
  --verbose, -v      Show detailed progress
  --help, -h         Show this help message

Examples:
  npm run seed-data                   # Generate 30 days of data
  npm run seed-data -- --days 90      # Generate 90 days of data
  npm run seed-data -- --clear        # Clear existing demo data first
  npm run seed-data -- --days 7 -v    # Generate 7 days with verbose output

Generated Sites:
  - Demo Blog:  Low traffic (~90 sessions/month)
  - Demo SaaS:  Average traffic (~500 sessions/day) with revenue tracking

Note:
  This script is blocked from running in production environments
  (NODE_ENV=production or cloud platforms like Vercel/Netlify/Railway).
`);
}

function checkEnvironment(): void {
  const nodeEnv = process.env.NODE_ENV;

  if (nodeEnv === 'production') {
    console.error('\nError: seed-data cannot run in production environment.');
    console.error('This script is only for local development and testing.\n');
    process.exit(1);
  }

  if (process.env.VERCEL || process.env.NETLIFY || process.env.RAILWAY_ENVIRONMENT) {
    console.error('\nError: seed-data cannot run in cloud environments.');
    console.error('This script is only for local development and testing.\n');
    process.exit(1);
  }
}

async function main(): Promise<void> {
  console.log('\nUmami Sample Data Generator\n');

  checkEnvironment();

  const config = parseArgs();

  try {
    await seed(config);
  } catch (error) {
    console.error('\nError generating seed data:', error);
    process.exit(1);
  }
}

main();