-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.ts
More file actions
223 lines (199 loc) · 8.16 KB
/
start.ts
File metadata and controls
223 lines (199 loc) · 8.16 KB
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
#!/usr/bin/env node
// Load .env file if present
import * as fs from 'fs';
import * as path from 'path';
const envFile = path.join(__dirname, '.env');
if (fs.existsSync(envFile)) {
for (const line of fs.readFileSync(envFile, 'utf-8').split('\n')) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const eqIndex = trimmed.indexOf('=');
if (eqIndex === -1) continue;
const key = trimmed.slice(0, eqIndex).trim();
const value = trimmed
.slice(eqIndex + 1)
.trim()
.replace(/^["']|["']$/g, '');
process.env[key] ??= value;
}
}
import { spawn } from 'child_process';
import * as zlib from 'zlib';
import type { ParsedArgs } from './lib/types';
import { parseArgs } from './lib/args-parser';
import { validateURL, sanitizeDomainName } from './validators';
import { printHelp, interactiveMode } from './lib/cli-interactive';
import { FileManager } from './lib/storage/file-manager';
import pkg from './package.json';
const MAX_RESTART_ATTEMPTS = 3;
// Global error handlers for unhandled rejections and uncaught exceptions
process.on('unhandledRejection', (reason, promise) => {
console.error('\n❌ Unhandled Promise Rejection:');
console.error('Reason:', reason);
console.error('Promise:', promise);
process.exit(1);
});
process.on('uncaughtException', (error) => {
console.error('\n❌ Uncaught Exception:');
console.error('Error:', error.message);
console.error('Stack:', error.stack);
process.exit(1);
});
async function audit(restoredArgs?: ParsedArgs, restartCount: number = 0) {
try {
// Parse arguments from command line or use restored values
let args = restoredArgs ?? parseArgs(process.argv.slice(2));
// Handle --version flag
if (args.version) {
console.log(`seo-audit v${pkg.version}`);
process.exit(0);
}
// Handle --help flag
if (args.help) {
printHelp();
process.exit(0);
}
// Interactive mode when no args provided
if (!args.origin && !args.sitemap && !restoredArgs && process.stdin.isTTY) {
args = (await interactiveMode()) as ParsedArgs;
}
const { origin, sitemap, compare, proceed, perf, saveHtml } = args;
// Validate that origin or sitemap is provided
if (!args.origin && !args.sitemap) {
printHelp();
process.exit(1);
}
// URL validation using centralized validator (skip when using sitemap file)
if (origin) {
const originValidation = validateURL(origin);
if (!originValidation.valid) {
console.error(`Error: Invalid URL "${origin}"`);
console.error(originValidation.error);
process.exit(1);
}
}
// Validate sitemap file exists
if (sitemap) {
if (!fs.existsSync(sitemap)) {
console.error(`Error: Sitemap file not found: "${sitemap}"`);
process.exit(1);
}
}
// Validate compare URL if provided
if (compare) {
const compareValidation = validateURL(compare);
if (!compareValidation.valid) {
console.error(`Error: Invalid compare URL "${compare}"`);
console.error(compareValidation.error);
process.exit(1);
}
}
// Build arguments for child process
// Use --max-old-space-size to increase heap limit for large sites
// Also use --expose-gc to allow manual garbage collection hints
const heapSize = process.env.NODE_HEAP_SIZE ?? args.heapSize ?? 8192;
const childArgs = [
`--max-old-space-size=${heapSize}`, // Heap limit (default 8GB, configurable via NODE_HEAP_SIZE env or --heap-size arg)
'--expose-gc', // Allow gc() calls
'--import=tsx', // Enable TypeScript support
'robots.ts',
];
if (origin) childArgs.push(`--origin=${origin}`);
if (sitemap) childArgs.push(`--sitemap=${sitemap}`);
if (args.compareSitemap) childArgs.push(`--compare-sitemap=${args.compareSitemap}`);
if (compare) childArgs.push(`--compare=${compare}`);
if (proceed) childArgs.push('--proceed');
if (perf) childArgs.push('--perf');
if (saveHtml) childArgs.push('--save-html');
if (args.screenshots) childArgs.push('--screenshots');
if (args.concurrency) childArgs.push(`--concurrency=${args.concurrency}`);
if (args.repo) childArgs.push(`--repo=${args.repo}`);
if (args.open) childArgs.push('--open');
if (args.verbose) childArgs.push('--verbose');
if (args.ci !== null && args.ci !== undefined) childArgs.push(`--ci=${args.ci}`);
if (args.json) childArgs.push('--json');
if (args.exclude && args.exclude.length > 0) {
args.exclude.forEach((pattern) => childArgs.push(`--exclude=${pattern}`));
}
if (args.limit) childArgs.push(`--limit=${args.limit}`);
if (args.onlyErrors) childArgs.push('--only-errors');
if (args.dryRun) childArgs.push('--dry-run');
if (args.quiet) childArgs.push('--quiet');
if (args.preset) childArgs.push(`--preset=${args.preset}`);
if (args.webhook) childArgs.push(`--webhook=${args.webhook}`);
if (args.generateCi) childArgs.push('--generate-ci');
if (args.validateConfig) childArgs.push('--validate-config');
if (args.sites) childArgs.push(`--sites=${args.sites}`);
if (args.slack) childArgs.push(`--slack=${args.slack}`);
if (args.brandLogo) childArgs.push(`--brand-logo=${args.brandLogo}`);
if (args.brandName) childArgs.push(`--brand-name=${args.brandName}`);
const child = spawn('node', childArgs, {
stdio: 'inherit',
});
child.on('error', (error) => {
console.error('\n❌ Failed to start child process:', error.message);
process.exit(1);
});
child.on('exit', async (code) => {
if (code !== 0) {
console.log('\n⚠️ Process crashed or interrupted (exit code: ' + code + ')');
// Try to show crash progress summary
try {
const domain = args.origin ? sanitizeDomainName(args.origin) : args.sitemap ? 'sitemap' : 'unknown';
const latestRun = FileManager.findLatestRun(domain);
if (latestRun) {
const manifestPath = path.join(latestRun, 'manifest.json.gz');
if (fs.existsSync(manifestPath)) {
const data = JSON.parse(zlib.gunzipSync(fs.readFileSync(manifestPath)).toString());
const processed = data.pageData?.processedPages?.length ?? Object.keys(data.pageData?.pages ?? {}).length;
const total = data.sitemaps?.total ?? 0;
if (total > 0) {
const pct = ((processed / total) * 100).toFixed(1);
console.log(` Progress before crash: ${processed}/${total} pages (${pct}%)`);
}
}
}
} catch {
/* ignore - best effort */
}
if (restartCount >= MAX_RESTART_ATTEMPTS) {
console.error(`\n❌ Maximum restart attempts (${MAX_RESTART_ATTEMPTS}) reached. Giving up.`);
console.error('Please check the logs above for errors and try again manually.');
throw new Error(`Maximum restart attempts (${MAX_RESTART_ATTEMPTS}) reached`);
}
console.log(
`🔄 Restarting with --proceed to resume from saved progress (attempt ${restartCount + 1}/${MAX_RESTART_ATTEMPTS})...\n`,
);
// Restart with proceed flag enabled
await audit({ ...args, proceed: true }, restartCount + 1);
} else {
console.log('\n✅ Audit completed successfully (exit code: 0)');
}
});
} catch (error: unknown) {
console.error('Error:', (error as Error).message);
process.exit(1);
}
}
// Check for watch mode
const watchArgs = parseArgs(process.argv.slice(2));
if (watchArgs.watch) {
const intervalMin = watchArgs.watch;
console.log(`\nWatch mode: re-auditing every ${intervalMin} minute(s). Press Ctrl+C to stop.\n`);
void (async function watchLoop() {
while (true) {
try {
await audit();
} catch (err: unknown) {
console.error(`\n⚠️ Audit failed: ${(err as Error).message}. Will retry next cycle.`);
}
console.log(`\nNext audit in ${intervalMin} minute(s)...`);
await new Promise((r) => setTimeout(r, intervalMin * 60 * 1000));
}
})();
} else {
void audit().catch((err: unknown) => {
console.error('Audit failed:', (err as Error).message ?? err);
process.exit(1);
});
}