fragment-ts 1.0.2 → 1.0.3
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/README.md +401 -0
- package/USAGE.md +1439 -0
- package/bin/fragment.js +2 -0
- package/dist/ai/ai.module.d.ts +27 -0
- package/dist/ai/ai.module.d.ts.map +1 -0
- package/dist/ai/ai.module.js +77 -0
- package/dist/ai/ai.module.js.map +1 -0
- package/dist/auth/auth.module.d.ts +18 -0
- package/dist/auth/auth.module.d.ts.map +1 -0
- package/dist/auth/auth.module.js +89 -0
- package/dist/auth/auth.module.js.map +1 -0
- package/dist/cli/commands/build.command.d.ts +6 -1
- package/dist/cli/commands/build.command.d.ts.map +1 -0
- package/dist/cli/commands/build.command.js +130 -10
- package/dist/cli/commands/build.command.js.map +1 -1
- package/dist/cli/commands/diagnostics.command.d.ts +16 -0
- package/dist/cli/commands/diagnostics.command.d.ts.map +1 -0
- package/dist/cli/commands/diagnostics.command.js +419 -0
- package/dist/cli/commands/diagnostics.command.js.map +1 -0
- package/dist/cli/commands/generate.command.d.ts +13 -6
- package/dist/cli/commands/generate.command.d.ts.map +1 -0
- package/dist/cli/commands/generate.command.js +200 -128
- package/dist/cli/commands/generate.command.js.map +1 -1
- package/dist/cli/commands/init.command.d.ts +11 -5
- package/dist/cli/commands/init.command.d.ts.map +1 -0
- package/dist/cli/commands/init.command.js +267 -305
- package/dist/cli/commands/init.command.js.map +1 -1
- package/dist/cli/commands/migrate.command.d.ts +12 -5
- package/dist/cli/commands/migrate.command.d.ts.map +1 -0
- package/dist/cli/commands/migrate.command.js +211 -83
- package/dist/cli/commands/migrate.command.js.map +1 -1
- package/dist/cli/commands/serve.command.d.ts +5 -4
- package/dist/cli/commands/serve.command.d.ts.map +1 -0
- package/dist/cli/commands/serve.command.js +178 -21
- package/dist/cli/commands/serve.command.js.map +1 -1
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +22 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/core/container/di-container.d.ts +13 -7
- package/dist/core/container/di-container.d.ts.map +1 -0
- package/dist/core/container/di-container.js +75 -25
- package/dist/core/container/di-container.js.map +1 -1
- package/dist/core/decorators/application.decorator.d.ts +8 -0
- package/dist/core/decorators/application.decorator.d.ts.map +1 -0
- package/dist/core/decorators/application.decorator.js +16 -0
- package/dist/core/decorators/application.decorator.js.map +1 -0
- package/dist/core/decorators/auto-configuration.decorator.d.ts +2 -0
- package/dist/core/decorators/auto-configuration.decorator.d.ts.map +1 -0
- package/dist/core/decorators/auto-configuration.decorator.js +19 -0
- package/dist/core/decorators/auto-configuration.decorator.js.map +1 -0
- package/dist/core/decorators/conditional.decorators.d.ts +4 -0
- package/dist/core/decorators/conditional.decorators.d.ts.map +1 -0
- package/dist/core/decorators/conditional.decorators.js +22 -0
- package/dist/core/decorators/conditional.decorators.js.map +1 -0
- package/dist/core/decorators/controller.decorator.d.ts +1 -3
- package/dist/core/decorators/controller.decorator.d.ts.map +1 -0
- package/dist/core/decorators/controller.decorator.js +12 -9
- package/dist/core/decorators/controller.decorator.js.map +1 -1
- package/dist/core/decorators/http.decorators.d.ts +12 -0
- package/dist/core/decorators/http.decorators.d.ts.map +1 -0
- package/dist/core/decorators/http.decorators.js +52 -0
- package/dist/core/decorators/http.decorators.js.map +1 -0
- package/dist/core/decorators/injectable.decorator.d.ts +3 -3
- package/dist/core/decorators/injectable.decorator.d.ts.map +1 -0
- package/dist/core/decorators/injectable.decorator.js +11 -8
- package/dist/core/decorators/injectable.decorator.js.map +1 -1
- package/dist/core/decorators/injection.decorators.d.ts +5 -0
- package/dist/core/decorators/injection.decorators.d.ts.map +1 -0
- package/dist/core/decorators/injection.decorators.js +29 -0
- package/dist/core/decorators/injection.decorators.js.map +1 -0
- package/dist/core/decorators/repository.decorator.d.ts +1 -0
- package/dist/core/decorators/repository.decorator.d.ts.map +1 -0
- package/dist/core/decorators/repository.decorator.js +12 -1
- package/dist/core/decorators/repository.decorator.js.map +1 -1
- package/dist/core/decorators/service.decorator.d.ts +1 -0
- package/dist/core/decorators/service.decorator.d.ts.map +1 -0
- package/dist/core/decorators/service.decorator.js +12 -1
- package/dist/core/decorators/service.decorator.js.map +1 -1
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +29 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/loader/file-loader.d.ts +5 -0
- package/dist/core/loader/file-loader.d.ts.map +1 -0
- package/dist/core/loader/file-loader.js +85 -0
- package/dist/core/loader/file-loader.js.map +1 -0
- package/dist/core/metadata/metadata-keys.d.ts +20 -0
- package/dist/core/metadata/metadata-keys.d.ts.map +1 -0
- package/dist/core/metadata/metadata-keys.js +23 -0
- package/dist/core/metadata/metadata-keys.js.map +1 -0
- package/dist/core/metadata/metadata-storage.d.ts +36 -0
- package/dist/core/metadata/metadata-storage.d.ts.map +1 -0
- package/dist/core/metadata/metadata-storage.js +53 -0
- package/dist/core/metadata/metadata-storage.js.map +1 -0
- package/dist/core/scanner/component-scanner.d.ts +16 -0
- package/dist/core/scanner/component-scanner.d.ts.map +1 -0
- package/dist/core/scanner/component-scanner.js +147 -0
- package/dist/core/scanner/component-scanner.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/plugin-manager.d.ts +14 -0
- package/dist/plugins/plugin-manager.d.ts.map +1 -0
- package/dist/{cli/utils/file-generator.js → plugins/plugin-manager.js} +32 -24
- package/dist/plugins/plugin-manager.js.map +1 -0
- package/dist/shared/errors.d.ts +18 -0
- package/dist/shared/errors.d.ts.map +1 -0
- package/dist/shared/errors.js +41 -0
- package/dist/shared/errors.js.map +1 -0
- package/dist/testing/runner.d.ts +26 -0
- package/dist/testing/runner.d.ts.map +1 -0
- package/dist/testing/runner.js +143 -0
- package/dist/testing/runner.js.map +1 -0
- package/dist/typeorm/typeorm-module.d.ts +36 -0
- package/dist/typeorm/typeorm-module.d.ts.map +1 -0
- package/dist/typeorm/typeorm-module.js +150 -0
- package/dist/typeorm/typeorm-module.js.map +1 -0
- package/dist/web/application.d.ts +29 -0
- package/dist/web/application.d.ts.map +1 -0
- package/dist/web/application.js +301 -0
- package/dist/web/application.js.map +1 -0
- package/dist/web/interfaces.d.ts +14 -0
- package/dist/web/interfaces.d.ts.map +1 -0
- package/dist/{auth/dto/login.dto.js → web/interfaces.js} +1 -1
- package/dist/web/interfaces.js.map +1 -0
- package/examples/blog-api/fragment.json +14 -0
- package/examples/blog-api/package-lock.json +3405 -0
- package/examples/blog-api/package.json +19 -0
- package/examples/blog-api/src/controllers/app.controller.ts +9 -0
- package/examples/blog-api/src/controllers/auth.controller.ts +17 -0
- package/examples/blog-api/src/controllers/category.controller.ts +29 -0
- package/examples/blog-api/src/controllers/comment.controller.ts +31 -0
- package/examples/blog-api/src/controllers/post.controller.ts +46 -0
- package/examples/blog-api/src/dto/create-category.dto.ts +6 -0
- package/examples/blog-api/src/dto/create-comment.dto.ts +6 -0
- package/examples/blog-api/src/dto/create-post.dto.ts +6 -0
- package/examples/blog-api/src/entities/category.entity.ts +16 -0
- package/examples/blog-api/src/entities/comment.entity.ts +29 -0
- package/examples/blog-api/src/entities/post.entity.ts +42 -0
- package/examples/blog-api/src/entities/user.entity.ts +25 -0
- package/examples/blog-api/src/main.ts +16 -0
- package/examples/blog-api/src/migrations/1767737463842-InitialSchema.ts +60 -0
- package/examples/blog-api/src/repositories/category.repository.ts +25 -0
- package/examples/blog-api/src/repositories/comment.repository.ts +25 -0
- package/examples/blog-api/src/repositories/post.repository.ts +29 -0
- package/examples/blog-api/src/seeds/SampleData.seed.ts +41 -0
- package/examples/blog-api/src/services/app.service.ts +8 -0
- package/examples/blog-api/src/services/auth.service.ts +15 -0
- package/examples/blog-api/src/services/category.service.ts +27 -0
- package/examples/blog-api/src/services/comment.service.ts +31 -0
- package/examples/blog-api/src/services/post.service.ts +35 -0
- package/examples/blog-api/tsconfig.json +23 -0
- package/package.json +55 -33
- package/src/ai/ai.module.ts +110 -0
- package/src/auth/auth.module.ts +77 -0
- package/src/cli/commands/build.command.ts +123 -13
- package/src/cli/commands/diagnostics.command.ts +438 -0
- package/src/cli/commands/generate.command.ts +206 -137
- package/src/cli/commands/init.command.ts +337 -349
- package/src/cli/commands/migrate.command.ts +203 -88
- package/src/cli/commands/serve.command.ts +176 -24
- package/src/cli/index.ts +23 -0
- package/src/core/container/di-container.ts +83 -26
- package/src/core/decorators/application.decorator.ts +26 -0
- package/src/core/decorators/auto-configuration.decorator.ts +17 -0
- package/src/core/decorators/conditional.decorators.ts +19 -0
- package/src/core/decorators/controller.decorator.ts +14 -11
- package/src/core/decorators/http.decorators.ts +71 -0
- package/src/core/decorators/injectable.decorator.ts +14 -9
- package/src/core/decorators/injection.decorators.ts +26 -0
- package/src/core/decorators/repository.decorator.ts +13 -2
- package/src/core/decorators/service.decorator.ts +13 -2
- package/src/core/index.ts +13 -0
- package/src/core/loader/file-loader.ts +55 -0
- package/src/core/metadata/metadata-keys.ts +19 -0
- package/src/core/metadata/metadata-storage.ts +91 -0
- package/src/core/scanner/component-scanner.ts +129 -0
- package/src/index.ts +45 -0
- package/src/plugins/plugin-manager.ts +52 -0
- package/src/shared/errors.ts +34 -0
- package/src/testing/runner.ts +143 -0
- package/src/typeorm/typeorm-module.ts +216 -0
- package/src/web/application.ts +348 -0
- package/src/web/interfaces.ts +17 -0
- package/tsconfig.json +8 -6
- package/.env.example +0 -0
- package/base.ts +0 -1810
- package/base2.ts +0 -968
- package/bin/frg.ts +0 -5
- package/config/fragment.lock.yaml +0 -0
- package/config/fragment.yaml +0 -0
- package/dist/app.d.ts +0 -15
- package/dist/app.js +0 -91
- package/dist/app.js.map +0 -1
- package/dist/auth/auth.controller.d.ts +0 -10
- package/dist/auth/auth.controller.js +0 -88
- package/dist/auth/auth.controller.js.map +0 -1
- package/dist/auth/auth.middleware.d.ts +0 -2
- package/dist/auth/auth.middleware.js +0 -25
- package/dist/auth/auth.middleware.js.map +0 -1
- package/dist/auth/auth.service.d.ts +0 -20
- package/dist/auth/auth.service.js +0 -144
- package/dist/auth/auth.service.js.map +0 -1
- package/dist/auth/dto/login.dto.d.ts +0 -9
- package/dist/auth/dto/login.dto.js.map +0 -1
- package/dist/cli/cli.d.ts +0 -12
- package/dist/cli/cli.js +0 -187
- package/dist/cli/cli.js.map +0 -1
- package/dist/cli/commands/config.command.d.ts +0 -6
- package/dist/cli/commands/config.command.js +0 -285
- package/dist/cli/commands/config.command.js.map +0 -1
- package/dist/cli/templates/controller.template.d.ts +0 -1
- package/dist/cli/templates/controller.template.js +0 -53
- package/dist/cli/templates/controller.template.js.map +0 -1
- package/dist/cli/templates/entity.template.d.ts +0 -1
- package/dist/cli/templates/entity.template.js +0 -24
- package/dist/cli/templates/entity.template.js.map +0 -1
- package/dist/cli/templates/repository.template.d.ts +0 -1
- package/dist/cli/templates/repository.template.js +0 -44
- package/dist/cli/templates/repository.template.js.map +0 -1
- package/dist/cli/templates/service.template.d.ts +0 -1
- package/dist/cli/templates/service.template.js +0 -44
- package/dist/cli/templates/service.template.js.map +0 -1
- package/dist/cli/utils/file-generator.d.ts +0 -9
- package/dist/cli/utils/file-generator.js.map +0 -1
- package/dist/cli/utils/logger.d.ts +0 -14
- package/dist/cli/utils/logger.js +0 -50
- package/dist/cli/utils/logger.js.map +0 -1
- package/dist/controllers/health.controller.d.ts +0 -13
- package/dist/controllers/health.controller.js +0 -51
- package/dist/controllers/health.controller.js.map +0 -1
- package/dist/core/config/config-loader.d.ts +0 -31
- package/dist/core/config/config-loader.js +0 -99
- package/dist/core/config/config-loader.js.map +0 -1
- package/dist/core/decorators/auth-guard.decorator.d.ts +0 -3
- package/dist/core/decorators/auth-guard.decorator.js +0 -19
- package/dist/core/decorators/auth-guard.decorator.js.map +0 -1
- package/dist/core/decorators/autowire.decorator.d.ts +0 -3
- package/dist/core/decorators/autowire.decorator.js +0 -18
- package/dist/core/decorators/autowire.decorator.js.map +0 -1
- package/dist/core/decorators/middleware.decorator.d.ts +0 -3
- package/dist/core/decorators/middleware.decorator.js +0 -21
- package/dist/core/decorators/middleware.decorator.js.map +0 -1
- package/dist/core/decorators/route.decorator.d.ts +0 -14
- package/dist/core/decorators/route.decorator.js +0 -33
- package/dist/core/decorators/route.decorator.js.map +0 -1
- package/dist/core/openai/openai-client.d.ts +0 -12
- package/dist/core/openai/openai-client.js +0 -94
- package/dist/core/openai/openai-client.js.map +0 -1
- package/dist/database/data-source.d.ts +0 -4
- package/dist/database/data-source.js +0 -27
- package/dist/database/data-source.js.map +0 -1
- package/dist/entities/session.entity.d.ts +0 -9
- package/dist/entities/session.entity.js +0 -46
- package/dist/entities/session.entity.js.map +0 -1
- package/dist/entities/user.entity.d.ts +0 -10
- package/dist/entities/user.entity.js +0 -49
- package/dist/entities/user.entity.js.map +0 -1
- package/dist/middlewares/logging.middleware.d.ts +0 -2
- package/dist/middlewares/logging.middleware.js +0 -29
- package/dist/middlewares/logging.middleware.js.map +0 -1
- package/dist/repositories/session.repository.d.ts +0 -9
- package/dist/repositories/session.repository.js +0 -51
- package/dist/repositories/session.repository.js.map +0 -1
- package/dist/repositories/user.repository.d.ts +0 -10
- package/dist/repositories/user.repository.js +0 -44
- package/dist/repositories/user.repository.js.map +0 -1
- package/dist/server.d.ts +0 -1
- package/dist/server.js +0 -31
- package/dist/server.js.map +0 -1
- package/dist/services/health.service.d.ts +0 -13
- package/dist/services/health.service.js +0 -45
- package/dist/services/health.service.js.map +0 -1
- package/readme.md +0 -120
- package/src/app.ts +0 -121
- package/src/auth/auth.controller.ts +0 -52
- package/src/auth/auth.middleware.ts +0 -27
- package/src/auth/auth.service.ts +0 -110
- package/src/auth/dto/login.dto.ts +0 -11
- package/src/cli/cli.ts +0 -212
- package/src/cli/commands/config.command.ts +0 -280
- package/src/cli/templates/controller.template.ts +0 -51
- package/src/cli/templates/entity.template.ts +0 -22
- package/src/cli/templates/repository.template.ts +0 -42
- package/src/cli/templates/service.template.ts +0 -42
- package/src/cli/utils/file-generator.ts +0 -37
- package/src/cli/utils/logger.ts +0 -52
- package/src/controllers/health.controller.ts +0 -24
- package/src/core/config/config-loader.ts +0 -98
- package/src/core/decorators/auth-guard.decorator.ts +0 -15
- package/src/core/decorators/autowire.decorator.ts +0 -18
- package/src/core/decorators/middleware.decorator.ts +0 -18
- package/src/core/decorators/route.decorator.ts +0 -33
- package/src/core/openai/openai-client.ts +0 -99
- package/src/database/data-source.ts +0 -29
- package/src/entities/session.entity.ts +0 -25
- package/src/entities/user.entity.ts +0 -27
- package/src/middlewares/logging.middleware.ts +0 -28
- package/src/repositories/session.repository.ts +0 -42
- package/src/repositories/user.repository.ts +0 -37
- package/src/server.ts +0 -32
- package/src/services/health.service.ts +0 -29
|
@@ -1,170 +1,239 @@
|
|
|
1
|
-
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import * as fs from 'fs-extra';
|
|
2
3
|
import * as path from 'path';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import inquirer from 'inquirer';
|
|
6
|
-
import { controllerTemplate } from '../templates/controller.template';
|
|
7
|
-
import { entityTemplate } from '../templates/entity.template';
|
|
8
|
-
import { repositoryTemplate } from '../templates/repository.template';
|
|
9
|
-
import { serviceTemplate } from '../templates/service.template';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import ora from 'ora';
|
|
10
6
|
|
|
11
7
|
export class GenerateCommand {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
]);
|
|
25
|
-
name = answer.name;
|
|
26
|
-
}
|
|
8
|
+
static register(program: Command): void {
|
|
9
|
+
program
|
|
10
|
+
.command('generate <type> <name>')
|
|
11
|
+
.alias('g')
|
|
12
|
+
.description('Generate a new resource')
|
|
13
|
+
.option('--with-repository', 'Generate repository for service')
|
|
14
|
+
.action(async (type, name, options) => {
|
|
15
|
+
await GenerateCommand.execute(type, name, options);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
27
18
|
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
19
|
+
static async execute(type: string, name: string, options: any): Promise<void> {
|
|
20
|
+
const spinner = ora();
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
spinner.start(`Generating ${type}...`);
|
|
24
|
+
|
|
25
|
+
switch (type) {
|
|
26
|
+
case 'controller':
|
|
27
|
+
await this.generateController(name);
|
|
28
|
+
break;
|
|
29
|
+
case 'service':
|
|
30
|
+
await this.generateService(name, options.withRepository);
|
|
31
|
+
break;
|
|
32
|
+
case 'resource':
|
|
33
|
+
await this.generateResource(name);
|
|
34
|
+
break;
|
|
35
|
+
case 'entity':
|
|
36
|
+
await this.generateEntity(name);
|
|
37
|
+
break;
|
|
38
|
+
case 'dto':
|
|
39
|
+
await this.generateDto(name);
|
|
40
|
+
break;
|
|
41
|
+
case 'repository':
|
|
42
|
+
await this.generateRepository(name);
|
|
43
|
+
break;
|
|
44
|
+
default:
|
|
45
|
+
spinner.fail(`Unknown type: ${type}`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
spinner.succeed(`${type} generated successfully!`);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
spinner.fail(`Failed to generate ${type}`);
|
|
52
|
+
console.error(chalk.red(error));
|
|
50
53
|
}
|
|
51
54
|
}
|
|
52
55
|
|
|
53
|
-
private async generateController(name: string
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
name: 'route',
|
|
58
|
-
message: 'Route path:',
|
|
59
|
-
default: `/api/${fileName}s`
|
|
60
|
-
}
|
|
61
|
-
]);
|
|
56
|
+
private static async generateController(name: string): Promise<void> {
|
|
57
|
+
const className = this.capitalize(name) + 'Controller';
|
|
58
|
+
const fileName = this.kebabCase(name) + '.controller.ts';
|
|
59
|
+
const filePath = path.join(process.cwd(), 'src', 'controllers', fileName);
|
|
62
60
|
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
if (FileGenerator.fileExists(filePath)) {
|
|
66
|
-
CLILogger.error(`Controller ${name} already exists`);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
61
|
+
const content = `import { Controller, Get, Post, Put, Delete, Body, Param } from 'fragment-ts';
|
|
69
62
|
|
|
70
|
-
|
|
71
|
-
|
|
63
|
+
@Controller('/${this.kebabCase(name)}')
|
|
64
|
+
export class ${className} {
|
|
65
|
+
@Get()
|
|
66
|
+
findAll() {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
72
69
|
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
@Get('/:id')
|
|
71
|
+
findOne(@Param('id') id: string) {
|
|
72
|
+
return { id };
|
|
75
73
|
}
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
CLILogger.error(`Service ${name} already exists`);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
75
|
+
@Post()
|
|
76
|
+
create(@Body() body: any) {
|
|
77
|
+
return body;
|
|
78
|
+
}
|
|
84
79
|
|
|
85
|
-
|
|
86
|
-
|
|
80
|
+
@Put('/:id')
|
|
81
|
+
update(@Param('id') id: string, @Body() body: any) {
|
|
82
|
+
return { id, ...body };
|
|
83
|
+
}
|
|
87
84
|
|
|
88
|
-
|
|
85
|
+
@Delete('/:id')
|
|
86
|
+
delete(@Param('id') id: string) {
|
|
87
|
+
return { deleted: true, id };
|
|
89
88
|
}
|
|
89
|
+
}
|
|
90
|
+
`;
|
|
90
91
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
type: 'input',
|
|
95
|
-
name: 'entity',
|
|
96
|
-
message: 'Entity name:',
|
|
97
|
-
default: name.replace('Repository', '')
|
|
98
|
-
}
|
|
99
|
-
]);
|
|
92
|
+
await fs.ensureDir(path.dirname(filePath));
|
|
93
|
+
await fs.writeFile(filePath, content);
|
|
94
|
+
}
|
|
100
95
|
|
|
101
|
-
|
|
96
|
+
private static async generateService(name: string, withRepository: boolean): Promise<void> {
|
|
97
|
+
const className = this.capitalize(name) + 'Service';
|
|
98
|
+
const fileName = this.kebabCase(name) + '.service.ts';
|
|
99
|
+
const filePath = path.join(process.cwd(), 'src', 'services', fileName);
|
|
100
|
+
|
|
101
|
+
let imports = "import { Service } from 'fragment-ts';\n";
|
|
102
|
+
let repositoryProp = '';
|
|
102
103
|
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
if (withRepository) {
|
|
105
|
+
const repoName = this.capitalize(name) + 'Repository';
|
|
106
|
+
imports += `import { ${repoName} } from '../repositories/${this.kebabCase(name)}.repository';\n`;
|
|
107
|
+
repositoryProp = `\n constructor(private ${this.camelCase(name)}Repository: ${repoName}) {}\n`;
|
|
106
108
|
}
|
|
107
109
|
|
|
108
|
-
const content =
|
|
109
|
-
|
|
110
|
+
const content = `${imports}
|
|
111
|
+
@Service()
|
|
112
|
+
export class ${className} {${repositoryProp}
|
|
113
|
+
async findAll() {
|
|
114
|
+
return [];
|
|
115
|
+
}
|
|
110
116
|
|
|
111
|
-
|
|
117
|
+
async findOne(id: string) {
|
|
118
|
+
return { id };
|
|
112
119
|
}
|
|
113
120
|
|
|
114
|
-
|
|
115
|
-
|
|
121
|
+
async create(data: any) {
|
|
122
|
+
return data;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async update(id: string, data: any) {
|
|
126
|
+
return { id, ...data };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async delete(id: string) {
|
|
130
|
+
return { deleted: true, id };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
`;
|
|
134
|
+
|
|
135
|
+
await fs.ensureDir(path.dirname(filePath));
|
|
136
|
+
await fs.writeFile(filePath, content);
|
|
116
137
|
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
return;
|
|
138
|
+
if (withRepository) {
|
|
139
|
+
await this.generateRepository(name);
|
|
120
140
|
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private static async generateResource(name: string): Promise<void> {
|
|
144
|
+
await this.generateController(name);
|
|
145
|
+
await this.generateService(name, true);
|
|
146
|
+
await this.generateEntity(name);
|
|
147
|
+
await this.generateDto(name);
|
|
148
|
+
}
|
|
121
149
|
|
|
122
|
-
|
|
123
|
-
|
|
150
|
+
private static async generateEntity(name: string): Promise<void> {
|
|
151
|
+
const className = this.capitalize(name);
|
|
152
|
+
const fileName = this.kebabCase(name) + '.entity.ts';
|
|
153
|
+
const filePath = path.join(process.cwd(), 'src', 'entities', fileName);
|
|
124
154
|
|
|
125
|
-
|
|
155
|
+
const content = `import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
|
156
|
+
|
|
157
|
+
@Entity()
|
|
158
|
+
export class ${className} {
|
|
159
|
+
@PrimaryGeneratedColumn()
|
|
160
|
+
id: number;
|
|
161
|
+
|
|
162
|
+
@Column()
|
|
163
|
+
name: string;
|
|
164
|
+
|
|
165
|
+
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
|
|
166
|
+
createdAt: Date;
|
|
167
|
+
|
|
168
|
+
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP', onUpdate: 'CURRENT_TIMESTAMP' })
|
|
169
|
+
updatedAt: Date;
|
|
170
|
+
}
|
|
171
|
+
`;
|
|
172
|
+
|
|
173
|
+
await fs.ensureDir(path.dirname(filePath));
|
|
174
|
+
await fs.writeFile(filePath, content);
|
|
126
175
|
}
|
|
127
176
|
|
|
128
|
-
private async
|
|
129
|
-
|
|
177
|
+
private static async generateDto(name: string): Promise<void> {
|
|
178
|
+
const className = 'Create' + this.capitalize(name) + 'Dto';
|
|
179
|
+
const fileName = 'create-' + this.kebabCase(name) + '.dto.ts';
|
|
180
|
+
const filePath = path.join(process.cwd(), 'src', 'dto', fileName);
|
|
130
181
|
|
|
131
|
-
const
|
|
182
|
+
const content = `export class ${className} {
|
|
183
|
+
name: string;
|
|
184
|
+
}
|
|
185
|
+
`;
|
|
132
186
|
|
|
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
|
-
|
|
187
|
+
await fs.ensureDir(path.dirname(filePath));
|
|
188
|
+
await fs.writeFile(filePath, content);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private static async generateRepository(name: string): Promise<void> {
|
|
192
|
+
const className = this.capitalize(name) + 'Repository';
|
|
193
|
+
const entityName = this.capitalize(name);
|
|
194
|
+
const fileName = this.kebabCase(name) + '.repository.ts';
|
|
195
|
+
const filePath = path.join(process.cwd(), 'src', 'repositories', fileName);
|
|
196
|
+
|
|
197
|
+
const content = `import { Repository } from 'fragment-ts';
|
|
198
|
+
import { ${entityName} } from '../entities/${this.kebabCase(name)}.entity';
|
|
199
|
+
|
|
200
|
+
@Repository()
|
|
201
|
+
export class ${className} {
|
|
202
|
+
async findAll(): Promise<${entityName}[]> {
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async findById(id: number): Promise<${entityName} | null> {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async create(data: Partial<${entityName}>): Promise<${entityName}> {
|
|
211
|
+
return data as ${entityName};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async update(id: number, data: Partial<${entityName}>): Promise<${entityName} | null> {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async delete(id: number): Promise<boolean> {
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
`;
|
|
223
|
+
|
|
224
|
+
await fs.ensureDir(path.dirname(filePath));
|
|
225
|
+
await fs.writeFile(filePath, content);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private static capitalize(str: string): string {
|
|
229
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private static kebabCase(str: string): string {
|
|
233
|
+
return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
private static camelCase(str: string): string {
|
|
237
|
+
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
169
238
|
}
|
|
170
239
|
}
|