@hazeljs/cli 0.2.0-beta.1
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.
- package/@template/README.md +61 -0
- package/@template/package.json +48 -0
- package/@template/src/app.module.ts +7 -0
- package/@template/src/hello.controller.ts +9 -0
- package/@template/src/index.ts +10 -0
- package/README.md +675 -0
- package/dist/commands/add.d.ts +2 -0
- package/dist/commands/add.js +90 -0
- package/dist/commands/build.d.ts +2 -0
- package/dist/commands/build.js +52 -0
- package/dist/commands/generate-agent.d.ts +2 -0
- package/dist/commands/generate-agent.js +56 -0
- package/dist/commands/generate-ai-service.d.ts +2 -0
- package/dist/commands/generate-ai-service.js +49 -0
- package/dist/commands/generate-app.d.ts +2 -0
- package/dist/commands/generate-app.js +246 -0
- package/dist/commands/generate-controller.d.ts +2 -0
- package/dist/commands/generate-controller.js +59 -0
- package/dist/commands/generate-crud.d.ts +2 -0
- package/dist/commands/generate-crud.js +203 -0
- package/dist/commands/generate-dto.d.ts +2 -0
- package/dist/commands/generate-dto.js +56 -0
- package/dist/commands/generate-exception-filter.d.ts +2 -0
- package/dist/commands/generate-exception-filter.js +50 -0
- package/dist/commands/generate-guard.d.ts +2 -0
- package/dist/commands/generate-guard.js +35 -0
- package/dist/commands/generate-interceptor.d.ts +2 -0
- package/dist/commands/generate-interceptor.js +37 -0
- package/dist/commands/generate-middleware.d.ts +2 -0
- package/dist/commands/generate-middleware.js +79 -0
- package/dist/commands/generate-module.d.ts +2 -0
- package/dist/commands/generate-module.js +96 -0
- package/dist/commands/generate-pipe.d.ts +2 -0
- package/dist/commands/generate-pipe.js +33 -0
- package/dist/commands/generate-repository.d.ts +2 -0
- package/dist/commands/generate-repository.js +38 -0
- package/dist/commands/generate-serverless-handler.d.ts +2 -0
- package/dist/commands/generate-serverless-handler.js +46 -0
- package/dist/commands/generate-service.d.ts +2 -0
- package/dist/commands/generate-service.js +51 -0
- package/dist/commands/generate-websocket-gateway.d.ts +2 -0
- package/dist/commands/generate-websocket-gateway.js +47 -0
- package/dist/commands/info.d.ts +2 -0
- package/dist/commands/info.js +91 -0
- package/dist/commands/start.d.ts +2 -0
- package/dist/commands/start.js +61 -0
- package/dist/commands/test.d.ts +2 -0
- package/dist/commands/test.js +63 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +61 -0
- package/dist/utils/generator.d.ts +14 -0
- package/dist/utils/generator.js +79 -0
- package/package.json +67 -0
- package/src/commands/add.ts +101 -0
- package/src/commands/build.ts +56 -0
- package/src/commands/generate-agent.ts +58 -0
- package/src/commands/generate-ai-service.ts +52 -0
- package/src/commands/generate-app.ts +270 -0
- package/src/commands/generate-controller.ts +61 -0
- package/src/commands/generate-crud.ts +214 -0
- package/src/commands/generate-dto.ts +61 -0
- package/src/commands/generate-exception-filter.ts +53 -0
- package/src/commands/generate-guard.ts +37 -0
- package/src/commands/generate-interceptor.ts +39 -0
- package/src/commands/generate-middleware.ts +86 -0
- package/src/commands/generate-module.ts +102 -0
- package/src/commands/generate-pipe.ts +36 -0
- package/src/commands/generate-repository.ts +41 -0
- package/src/commands/generate-serverless-handler.ts +53 -0
- package/src/commands/generate-service.ts +53 -0
- package/src/commands/generate-websocket-gateway.ts +50 -0
- package/src/commands/info.ts +106 -0
- package/src/commands/start.ts +61 -0
- package/src/commands/test.ts +70 -0
- package/src/index.ts +68 -0
- package/src/utils/generator.ts +93 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import { Generator, GeneratorOptions } from '../utils/generator';
|
|
3
|
+
|
|
4
|
+
const SERVERLESS_HANDLER_TEMPLATE = `import { createLambdaHandler } from '@hazeljs/serverless';
|
|
5
|
+
import { AppModule } from './app.module';
|
|
6
|
+
|
|
7
|
+
export const handler = createLambdaHandler(AppModule);
|
|
8
|
+
`;
|
|
9
|
+
|
|
10
|
+
const SERVERLESS_CLOUD_FUNCTION_TEMPLATE = `import { createCloudFunctionHandler } from '@hazeljs/serverless';
|
|
11
|
+
import { AppModule } from './app.module';
|
|
12
|
+
|
|
13
|
+
export const handler = createCloudFunctionHandler(AppModule);
|
|
14
|
+
`;
|
|
15
|
+
|
|
16
|
+
class ServerlessHandlerGenerator extends Generator {
|
|
17
|
+
protected getDefaultTemplate(): string {
|
|
18
|
+
return SERVERLESS_HANDLER_TEMPLATE;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async generate(options: GeneratorOptions & { platform?: string }): Promise<void> {
|
|
22
|
+
const { platform = 'lambda', ...restOptions } = options;
|
|
23
|
+
|
|
24
|
+
const template = platform === 'lambda'
|
|
25
|
+
? SERVERLESS_HANDLER_TEMPLATE
|
|
26
|
+
: SERVERLESS_CLOUD_FUNCTION_TEMPLATE;
|
|
27
|
+
|
|
28
|
+
await super.generate({
|
|
29
|
+
...restOptions,
|
|
30
|
+
template,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function generateServerlessHandler(program: Command): void {
|
|
36
|
+
program
|
|
37
|
+
.command('serverless <name>')
|
|
38
|
+
.description('Generate a serverless handler (Lambda or Cloud Function)')
|
|
39
|
+
.alias('sls')
|
|
40
|
+
.option('-p, --path <path>', 'Path where the handler should be generated', 'src')
|
|
41
|
+
.option('--platform <platform>', 'Platform: lambda or cloud-function', 'lambda')
|
|
42
|
+
.action(async (name: string, options: { path?: string; platform?: string }) => {
|
|
43
|
+
const generator = new ServerlessHandlerGenerator();
|
|
44
|
+
const generatorOptions: Partial<GeneratorOptions> = {
|
|
45
|
+
name,
|
|
46
|
+
path: options.path,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const finalOptions = await generator.promptForOptions(generatorOptions);
|
|
50
|
+
await generator.generate({ ...finalOptions, platform: options.platform });
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import { Generator, GeneratorOptions } from '../utils/generator';
|
|
3
|
+
|
|
4
|
+
const SERVICE_TEMPLATE = `import { Injectable } from '@hazeljs/core';
|
|
5
|
+
|
|
6
|
+
@Injectable()
|
|
7
|
+
export class {{className}}Service {
|
|
8
|
+
constructor() {}
|
|
9
|
+
|
|
10
|
+
async findAll() {
|
|
11
|
+
return [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async findOne(id: string) {
|
|
15
|
+
return { id };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async create(create{{className}}Dto: any) {
|
|
19
|
+
return create{{className}}Dto;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async update(id: string, update{{className}}Dto: any) {
|
|
23
|
+
return { id, ...update{{className}}Dto };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async remove(id: string) {
|
|
27
|
+
return { id };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
`;
|
|
31
|
+
|
|
32
|
+
class ServiceGenerator extends Generator {
|
|
33
|
+
protected getDefaultTemplate(): string {
|
|
34
|
+
return SERVICE_TEMPLATE;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function generateService(program: Command): void {
|
|
39
|
+
program
|
|
40
|
+
.command('service <name>')
|
|
41
|
+
.description('Generate a new service')
|
|
42
|
+
.option('-p, --path <path>', 'Path where the service should be generated')
|
|
43
|
+
.action(async (name: string, options: { path?: string }) => {
|
|
44
|
+
const generator = new ServiceGenerator();
|
|
45
|
+
const generatorOptions: Partial<GeneratorOptions> = {
|
|
46
|
+
name,
|
|
47
|
+
path: options.path,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const finalOptions = await generator.promptForOptions(generatorOptions);
|
|
51
|
+
await generator.generate(finalOptions);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import { Generator, GeneratorOptions } from '../utils/generator';
|
|
3
|
+
|
|
4
|
+
const WEBSOCKET_GATEWAY_TEMPLATE = `import { Realtime, OnConnect, OnDisconnect, OnMessage, Subscribe, Client, Data, WebSocketClient } from '@hazeljs/websocket';
|
|
5
|
+
|
|
6
|
+
@Realtime('/{{fileName}}')
|
|
7
|
+
export class {{className}}Gateway {
|
|
8
|
+
@OnConnect()
|
|
9
|
+
handleConnection(@Client() client: WebSocketClient) {
|
|
10
|
+
console.log('Client connected:', client.id);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
@OnDisconnect()
|
|
14
|
+
handleDisconnect(@Client() client: WebSocketClient) {
|
|
15
|
+
console.log('Client disconnected:', client.id);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@Subscribe('message')
|
|
19
|
+
@OnMessage('message')
|
|
20
|
+
handleMessage(@Client() client: WebSocketClient, @Data() data: unknown) {
|
|
21
|
+
console.log('Message received from', client.id, ':', data);
|
|
22
|
+
// Handle message logic here
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
class WebSocketGatewayGenerator extends Generator {
|
|
28
|
+
protected getDefaultTemplate(): string {
|
|
29
|
+
return WEBSOCKET_GATEWAY_TEMPLATE;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function generateWebSocketGateway(program: Command): void {
|
|
34
|
+
program
|
|
35
|
+
.command('gateway <name>')
|
|
36
|
+
.description('Generate a new WebSocket gateway')
|
|
37
|
+
.alias('ws')
|
|
38
|
+
.option('-p, --path <path>', 'Path where the gateway should be generated')
|
|
39
|
+
.action(async (name: string, options: { path?: string }) => {
|
|
40
|
+
const generator = new WebSocketGatewayGenerator();
|
|
41
|
+
const generatorOptions: Partial<GeneratorOptions> = {
|
|
42
|
+
name,
|
|
43
|
+
path: options.path,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const finalOptions = await generator.promptForOptions(generatorOptions);
|
|
47
|
+
await generator.generate(finalOptions);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
|
|
6
|
+
interface PackageJson {
|
|
7
|
+
name?: string;
|
|
8
|
+
version?: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
dependencies?: Record<string, string>;
|
|
11
|
+
devDependencies?: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function infoCommand(program: Command) {
|
|
15
|
+
program
|
|
16
|
+
.command('info')
|
|
17
|
+
.description('Display information about the current HazelJS project')
|
|
18
|
+
.action(() => {
|
|
19
|
+
try {
|
|
20
|
+
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
21
|
+
|
|
22
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
23
|
+
console.log(chalk.yellow('⚠ No package.json found in current directory'));
|
|
24
|
+
console.log(chalk.gray('This does not appear to be a Node.js project'));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const packageJson: PackageJson = JSON.parse(
|
|
29
|
+
fs.readFileSync(packageJsonPath, 'utf-8')
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
console.log(chalk.bold.blue('\n📦 Project Information\n'));
|
|
33
|
+
|
|
34
|
+
// Basic info
|
|
35
|
+
console.log(chalk.bold('Name:'), packageJson.name || 'N/A');
|
|
36
|
+
console.log(chalk.bold('Version:'), packageJson.version || 'N/A');
|
|
37
|
+
console.log(chalk.bold('Description:'), packageJson.description || 'N/A');
|
|
38
|
+
|
|
39
|
+
// HazelJS packages
|
|
40
|
+
const hazelPackages: string[] = [];
|
|
41
|
+
const allDeps = {
|
|
42
|
+
...packageJson.dependencies,
|
|
43
|
+
...packageJson.devDependencies,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
Object.keys(allDeps).forEach((dep) => {
|
|
47
|
+
if (dep.startsWith('@hazeljs/')) {
|
|
48
|
+
hazelPackages.push(`${dep}@${allDeps[dep]}`);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
if (hazelPackages.length > 0) {
|
|
53
|
+
console.log(chalk.bold('\n🔧 HazelJS Packages:\n'));
|
|
54
|
+
hazelPackages.forEach((pkg) => {
|
|
55
|
+
console.log(chalk.green(' ✓'), pkg);
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
console.log(chalk.yellow('\n⚠ No HazelJS packages found'));
|
|
59
|
+
console.log(chalk.gray('Install HazelJS packages with: npm install @hazeljs/core'));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Project structure
|
|
63
|
+
console.log(chalk.bold('\n📁 Project Structure:\n'));
|
|
64
|
+
const srcPath = path.join(process.cwd(), 'src');
|
|
65
|
+
if (fs.existsSync(srcPath)) {
|
|
66
|
+
const items = fs.readdirSync(srcPath);
|
|
67
|
+
items.forEach((item) => {
|
|
68
|
+
const itemPath = path.join(srcPath, item);
|
|
69
|
+
const stat = fs.statSync(itemPath);
|
|
70
|
+
const icon = stat.isDirectory() ? '📁' : '📄';
|
|
71
|
+
console.log(` ${icon} ${item}`);
|
|
72
|
+
});
|
|
73
|
+
} else {
|
|
74
|
+
console.log(chalk.gray(' No src directory found'));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Environment
|
|
78
|
+
console.log(chalk.bold('\n🌍 Environment:\n'));
|
|
79
|
+
console.log(chalk.bold('Node:'), process.version);
|
|
80
|
+
console.log(chalk.bold('Platform:'), process.platform);
|
|
81
|
+
console.log(chalk.bold('Architecture:'), process.arch);
|
|
82
|
+
|
|
83
|
+
// Config files
|
|
84
|
+
console.log(chalk.bold('\n⚙️ Configuration Files:\n'));
|
|
85
|
+
const configFiles = [
|
|
86
|
+
'tsconfig.json',
|
|
87
|
+
'.env',
|
|
88
|
+
'.env.example',
|
|
89
|
+
'.eslintrc.js',
|
|
90
|
+
'.prettierrc',
|
|
91
|
+
'jest.config.js',
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
configFiles.forEach((file) => {
|
|
95
|
+
const exists = fs.existsSync(path.join(process.cwd(), file));
|
|
96
|
+
const icon = exists ? chalk.green('✓') : chalk.gray('✗');
|
|
97
|
+
console.log(` ${icon} ${file}`);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
console.log('');
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error(chalk.red('Error reading project information:'), error);
|
|
103
|
+
process.exit(1);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import { execSync } from 'child_process';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
|
|
7
|
+
export function startCommand(program: Command) {
|
|
8
|
+
program
|
|
9
|
+
.command('start')
|
|
10
|
+
.description('Start the HazelJS application')
|
|
11
|
+
.option('-d, --dev', 'Start in development mode')
|
|
12
|
+
.option('-p, --port <port>', 'Specify port')
|
|
13
|
+
.action((options: { dev?: boolean; port?: string }) => {
|
|
14
|
+
try {
|
|
15
|
+
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
16
|
+
|
|
17
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
18
|
+
console.log(chalk.red('✗ No package.json found'));
|
|
19
|
+
console.log(chalk.gray('Run this command from your project root'));
|
|
20
|
+
process.exit(1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
24
|
+
|
|
25
|
+
let command: string;
|
|
26
|
+
|
|
27
|
+
if (options.dev) {
|
|
28
|
+
if (!packageJson.scripts?.dev) {
|
|
29
|
+
console.log(chalk.yellow('⚠ No dev script found in package.json'));
|
|
30
|
+
console.log(chalk.gray('\nAdd a dev script to your package.json:'));
|
|
31
|
+
console.log(chalk.gray(' "scripts": {'));
|
|
32
|
+
console.log(chalk.gray(' "dev": "ts-node-dev --respawn src/index.ts"'));
|
|
33
|
+
console.log(chalk.gray(' }'));
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
command = 'npm run dev';
|
|
37
|
+
console.log(chalk.blue('🚀 Starting in development mode...\n'));
|
|
38
|
+
} else {
|
|
39
|
+
if (!packageJson.scripts?.start) {
|
|
40
|
+
console.log(chalk.yellow('⚠ No start script found in package.json'));
|
|
41
|
+
console.log(chalk.gray('\nAdd a start script to your package.json:'));
|
|
42
|
+
console.log(chalk.gray(' "scripts": {'));
|
|
43
|
+
console.log(chalk.gray(' "start": "node dist/index.js"'));
|
|
44
|
+
console.log(chalk.gray(' }'));
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
command = 'npm start';
|
|
48
|
+
console.log(chalk.blue('🚀 Starting application...\n'));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (options.port) {
|
|
52
|
+
process.env.PORT = options.port;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
execSync(command, { stdio: 'inherit' });
|
|
56
|
+
} catch {
|
|
57
|
+
console.error(chalk.red('\n✗ Failed to start application'));
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import { execSync } from 'child_process';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
|
|
7
|
+
export function testCommand(program: Command) {
|
|
8
|
+
program
|
|
9
|
+
.command('test [pattern]')
|
|
10
|
+
.description('Run tests')
|
|
11
|
+
.option('-w, --watch', 'Watch mode')
|
|
12
|
+
.option('-c, --coverage', 'Generate coverage report')
|
|
13
|
+
.option('--ci', 'Run in CI mode')
|
|
14
|
+
.action((pattern?: string, options?: { watch?: boolean; coverage?: boolean; ci?: boolean }) => {
|
|
15
|
+
try {
|
|
16
|
+
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
17
|
+
|
|
18
|
+
if (!fs.existsSync(packageJsonPath)) {
|
|
19
|
+
console.log(chalk.red('✗ No package.json found'));
|
|
20
|
+
console.log(chalk.gray('Run this command from your project root'));
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
|
25
|
+
|
|
26
|
+
if (!packageJson.scripts?.test) {
|
|
27
|
+
console.log(chalk.yellow('⚠ No test script found in package.json'));
|
|
28
|
+
console.log(chalk.gray('\nAdd a test script to your package.json:'));
|
|
29
|
+
console.log(chalk.gray(' "scripts": {'));
|
|
30
|
+
console.log(chalk.gray(' "test": "jest"'));
|
|
31
|
+
console.log(chalk.gray(' }'));
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
console.log(chalk.blue('🧪 Running tests...\n'));
|
|
36
|
+
|
|
37
|
+
let command = 'npm test';
|
|
38
|
+
const args: string[] = [];
|
|
39
|
+
|
|
40
|
+
if (pattern) {
|
|
41
|
+
args.push(pattern);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (options?.watch) {
|
|
45
|
+
args.push('--watch');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (options?.coverage) {
|
|
49
|
+
args.push('--coverage');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (options?.ci) {
|
|
53
|
+
command = packageJson.scripts['test:ci'] ? 'npm run test:ci' : 'npm test -- --ci';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (args.length > 0 && !options?.ci) {
|
|
57
|
+
command += ' -- ' + args.join(' ');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
execSync(command, { stdio: 'inherit' });
|
|
61
|
+
|
|
62
|
+
if (!options?.watch) {
|
|
63
|
+
console.log(chalk.green('\n✓ Tests completed'));
|
|
64
|
+
}
|
|
65
|
+
} catch {
|
|
66
|
+
console.error(chalk.red('\n✗ Tests failed'));
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Command } from 'commander';
|
|
4
|
+
import { generateModule } from './commands/generate-module';
|
|
5
|
+
import { generateApp } from './commands/generate-app';
|
|
6
|
+
import { generateController } from './commands/generate-controller';
|
|
7
|
+
import { generateService } from './commands/generate-service';
|
|
8
|
+
import { generateDto } from './commands/generate-dto';
|
|
9
|
+
import { generateGuard } from './commands/generate-guard';
|
|
10
|
+
import { generateInterceptor } from './commands/generate-interceptor';
|
|
11
|
+
import { generateWebSocketGateway } from './commands/generate-websocket-gateway';
|
|
12
|
+
import { generateExceptionFilter } from './commands/generate-exception-filter';
|
|
13
|
+
import { generatePipe } from './commands/generate-pipe';
|
|
14
|
+
import { generateRepository } from './commands/generate-repository';
|
|
15
|
+
import { generateAIService } from './commands/generate-ai-service';
|
|
16
|
+
import { generateAgent } from './commands/generate-agent';
|
|
17
|
+
import { generateServerlessHandler } from './commands/generate-serverless-handler';
|
|
18
|
+
import { generateCrud } from './commands/generate-crud';
|
|
19
|
+
import { generateMiddleware } from './commands/generate-middleware';
|
|
20
|
+
import { infoCommand } from './commands/info';
|
|
21
|
+
import { addCommand } from './commands/add';
|
|
22
|
+
import { buildCommand } from './commands/build';
|
|
23
|
+
import { startCommand } from './commands/start';
|
|
24
|
+
import { testCommand } from './commands/test';
|
|
25
|
+
|
|
26
|
+
const program = new Command();
|
|
27
|
+
|
|
28
|
+
program
|
|
29
|
+
.name('hazel')
|
|
30
|
+
.description('CLI for generating HazelJS components and applications')
|
|
31
|
+
.version('0.2.0');
|
|
32
|
+
|
|
33
|
+
// New app command
|
|
34
|
+
generateApp(program);
|
|
35
|
+
|
|
36
|
+
// Utility commands
|
|
37
|
+
infoCommand(program);
|
|
38
|
+
addCommand(program);
|
|
39
|
+
buildCommand(program);
|
|
40
|
+
startCommand(program);
|
|
41
|
+
testCommand(program);
|
|
42
|
+
|
|
43
|
+
// Generate command group
|
|
44
|
+
const generateCommand = program
|
|
45
|
+
.command('generate')
|
|
46
|
+
.description('Generate HazelJS components')
|
|
47
|
+
.alias('g');
|
|
48
|
+
|
|
49
|
+
// Core components
|
|
50
|
+
generateController(generateCommand);
|
|
51
|
+
generateService(generateCommand);
|
|
52
|
+
generateModule(generateCommand);
|
|
53
|
+
generateDto(generateCommand);
|
|
54
|
+
generateGuard(generateCommand);
|
|
55
|
+
generateInterceptor(generateCommand);
|
|
56
|
+
generateMiddleware(generateCommand);
|
|
57
|
+
|
|
58
|
+
// Advanced generators
|
|
59
|
+
generateCrud(generateCommand);
|
|
60
|
+
generateWebSocketGateway(generateCommand);
|
|
61
|
+
generateExceptionFilter(generateCommand);
|
|
62
|
+
generatePipe(generateCommand);
|
|
63
|
+
generateRepository(generateCommand);
|
|
64
|
+
generateAIService(generateCommand);
|
|
65
|
+
generateAgent(generateCommand);
|
|
66
|
+
generateServerlessHandler(generateCommand);
|
|
67
|
+
|
|
68
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import inquirer from 'inquirer';
|
|
5
|
+
import mustache from 'mustache';
|
|
6
|
+
|
|
7
|
+
export interface GeneratorOptions {
|
|
8
|
+
name: string;
|
|
9
|
+
path?: string;
|
|
10
|
+
template?: string;
|
|
11
|
+
data?: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class Generator {
|
|
15
|
+
public async generate(options: GeneratorOptions): Promise<void> {
|
|
16
|
+
const { name, path: customPath, template, data = {} } = options;
|
|
17
|
+
|
|
18
|
+
// Get the template
|
|
19
|
+
const templateContent = template || this.getDefaultTemplate();
|
|
20
|
+
|
|
21
|
+
// Prepare the data
|
|
22
|
+
const templateData = {
|
|
23
|
+
name,
|
|
24
|
+
className: this.toPascalCase(name),
|
|
25
|
+
fileName: this.toKebabCase(name),
|
|
26
|
+
...data,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Render the template
|
|
30
|
+
const content = mustache.render(templateContent, templateData);
|
|
31
|
+
|
|
32
|
+
// Determine the file path
|
|
33
|
+
const filePath = this.getFilePath(name, customPath);
|
|
34
|
+
|
|
35
|
+
// Create directory if it doesn't exist
|
|
36
|
+
const dir = path.dirname(filePath);
|
|
37
|
+
if (!fs.existsSync(dir)) {
|
|
38
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Write the file
|
|
42
|
+
fs.writeFileSync(filePath, content);
|
|
43
|
+
|
|
44
|
+
console.log(chalk.green(`✓ Generated ${filePath}`));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected toPascalCase(str: string): string {
|
|
48
|
+
return str
|
|
49
|
+
.split(/[-_]/)
|
|
50
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
51
|
+
.join('');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected toKebabCase(str: string): string {
|
|
55
|
+
return str
|
|
56
|
+
.replace(/([a-z])([A-Z])/g, '$1-$2')
|
|
57
|
+
.replace(/[\s_]+/g, '-')
|
|
58
|
+
.toLowerCase();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
protected getFilePath(name: string, customPath?: string): string {
|
|
62
|
+
const fileName = this.toKebabCase(name);
|
|
63
|
+
const basePath = customPath || 'src';
|
|
64
|
+
return path.join(process.cwd(), basePath, `${fileName}.ts`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected getDefaultTemplate(): string {
|
|
68
|
+
return '';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public async promptForOptions(options: Partial<GeneratorOptions>): Promise<GeneratorOptions> {
|
|
72
|
+
const answers = await inquirer.prompt([
|
|
73
|
+
{
|
|
74
|
+
type: 'input',
|
|
75
|
+
name: 'name',
|
|
76
|
+
message: 'What is the name of the component?',
|
|
77
|
+
when: !options.name,
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
type: 'input',
|
|
81
|
+
name: 'path',
|
|
82
|
+
message: 'Where should the component be generated?',
|
|
83
|
+
default: 'src',
|
|
84
|
+
when: !options.path,
|
|
85
|
+
},
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
...options,
|
|
90
|
+
...answers,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|