forked from BeOnAuto/auto-engineer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
164 lines (137 loc) · 5.31 KB
/
index.ts
File metadata and controls
164 lines (137 loc) · 5.31 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
#!/usr/bin/env node
import { Command } from 'commander';
import chalk from 'chalk';
import gradient from 'gradient-string';
import figlet from 'figlet';
import { loadConfig, validateConfig } from './utils/config.js';
import { handleError } from './utils/errors.js';
import { createOutput, supportsColor } from './utils/terminal.js';
import { Analytics } from './utils/analytics.js';
import { createInitCommand } from './commands/init.js';
import { createDemoCommand } from './commands/demo.js';
import { createStartCommand } from './commands/start.js';
const VERSION = process.env.npm_package_version ?? '0.1.2';
const checkNodeVersion = () => {
const nodeVersion = process.version;
const majorVersion = parseInt(nodeVersion.slice(1).split('.')[0]);
if (majorVersion < 18) {
console.error(chalk.red(`Error: Node.js version ${nodeVersion} is not supported.`));
console.error(chalk.yellow('Auto-engineer requires Node.js 18.0.0 or higher.'));
console.error(chalk.blue('Please upgrade Node.js and try again.'));
process.exit(1);
}
};
const clearConsole = () => {
if (process.stdout.isTTY) {
// Clear console for TTY environments
process.stdout.write('\x1Bc');
}
};
const setupSignalHandlers = () => {
process.on('SIGINT', () => {
console.log('\n' + chalk.yellow('Operation cancelled by user'));
process.exit(0);
});
process.on('SIGTERM', () => {
console.log('\n' + chalk.yellow('Process terminated'));
process.exit(0);
});
process.on('uncaughtException', (error) => {
console.error(chalk.red('Uncaught Exception:'), error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error(chalk.red('Unhandled Rejection at:'), promise, chalk.red('reason:'), reason);
process.exit(1);
});
};
const createCLI = () => {
const program = new Command();
program
.name('auto-engineer')
.description('Auto Engineer - Build production ready full-stack apps with AI')
.version(VERSION, '-v, --version')
.option('-d, --debug', 'Enable debug mode')
.option('--no-color', 'Disable colored output')
.option('--json', 'Output in JSON format')
.option('--api-token <token>', 'API token for external services')
.option('--project-path <path>', 'Project path to work with');
return program;
};
const displayBanner = (config: ReturnType<typeof loadConfig>) => {
if (config.output === 'text' && supportsColor(config) && process.stdout.isTTY) {
const asciiText = figlet.textSync('AutoEngineer', { font: 'Slant' });
console.log(chalk.bgBlack(gradient(['#F44B4B', '#FF9C1A', '#F9F871', '#4CD964', '#4BC6F4'])(asciiText)));
console.log();
}
};
const setupProgram = (config: ReturnType<typeof loadConfig>) => {
const program = createCLI();
const analytics = new Analytics(config);
program.addCommand(createInitCommand(config, analytics));
program.addCommand(createDemoCommand(config, analytics));
program.addCommand(createStartCommand(config, analytics));
program.addHelpText(
'after',
`
Examples:
$ auto-engineer start Create flows interactively using AI
$ ag start Create flows interactively using AI (short alias)
$ auto-engineer demo Start demo mode
$ auto-engineer init Initialize configuration
$ auto-engineer generate --type code Generate code templates
$ auto-engineer analyze --format json Analyze code in JSON format
$ echo "code" | auto-engineer analyze Analyze content from STDIN
Environment Variables:
DEBUG=auto-engineer Enable debug mode
NO_COLOR=1 Disable colored output
OUTPUT_FORMAT=json Set output format
AUTO_ENGINEER_API_TOKEN=<token> Set API token
AUTO_ENGINEER_ANALYTICS=false Disable analytics (enabled by default)
For more information, visit: https://github.com/SamHatoum/auto-engineer
`,
);
return program;
};
const main = async () => {
try {
checkNodeVersion();
clearConsole();
setupSignalHandlers();
const program = createCLI();
program.parse(process.argv, { from: 'user' });
const globalOptions = program.opts();
const config = loadConfig({
debug: Boolean(globalOptions.debug),
noColor: Boolean(globalOptions.noColor),
output: globalOptions.json === true ? 'json' : 'text',
apiToken: typeof globalOptions.apiToken === 'string' ? globalOptions.apiToken : undefined,
projectPath: typeof globalOptions.projectPath === 'string' ? globalOptions.projectPath : undefined,
});
validateConfig(config);
createOutput(config);
displayBanner(config);
const fullProgram = setupProgram(config);
await fullProgram.parseAsync(process.argv);
} catch (error: unknown) {
if (
error instanceof Error &&
(error.message.includes('commander') ||
error.message.includes('helpDisplayed') ||
error.message.includes('version'))
) {
process.exit(0);
}
if (error instanceof Error) {
handleError(error);
} else {
console.error(chalk.red('Unknown error:'), error);
process.exit(1);
}
}
};
main().catch((error: unknown) => {
const errorMessage = error instanceof Error ? error.message : String(error);
console.error(chalk.red('Fatal error:'), errorMessage);
process.exit(1);
});