Skip to content

Commit 39454ac

Browse files
committed
Merge branch 'develop' into feature/review_dave
# Conflicts: # README.md # package.json # src/console/MakeListenerCommand.ts
2 parents c1b9099 + d498f6e commit 39454ac

30 files changed

+565
-329
lines changed

Procfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ All script are defined in the package.json file, but the most important ones are
7575
### Generating Commands
7676
All the templates for the commands are located in `src/console/templates`.
7777

78-
* `npm run console make:controller <file>` - Generates a controller with the CRUD routes.
79-
* `npm run console make:service <file>` - Generates a service with the CRUD logic.
80-
* `npm run console make:repo <file>` - Generates a repository with the CRUD operations.
78+
* `npm run console make:resource <file>` - Generates a controller, service, requests, repo, model and a migration with CRUD operations.
79+
* `npm run console make:controller <file>` - Generates a controller.
80+
* `npm run console make:service <file>` - Generates a service.
81+
* `npm run console make:repo <file>` - Generates a repository.
8182
* `npm run console make:model <file>` - Generates a model with the props and configurations.
8283
* `npm run console make:middleware <file>` - Generates a basic middleware.
8384
* `npm run console make:request <file>` - Generates a basic request.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
"db:migrate:rollback": "./node_modules/.bin/knex migrate:rollback",
2121
"db:seed": "./node_modules/.bin/knex seed:run",
2222
"db:reset": "npm run console db:reset",
23-
"console": "./node_modules/.bin/ts-node ./src/console/commander.ts",
24-
"console:help": "./node_modules/.bin/ts-node ./src/console/commander.ts --help",
23+
"console": "./node_modules/.bin/ts-node --fast ./src/console/commander.ts",
24+
"console:dev": "./node_modules/.bin/ts-node ./src/console/commander.ts",
25+
"console:help": "./node_modules/.bin/ts-node --fast ./src/console/commander.ts --help",
2526
"serve": "./node_modules/.bin/nodemon --watch 'src/**/*.ts' --watch 'src/**/*.json' --watch '.env'",
2627
"start": "node dist/index.js"
2728
},

src/console/AbstractCommand.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* AbstractCommand
3+
* -------------------------------------
4+
*
5+
*/
6+
import * as _ from 'lodash';
7+
8+
export interface Command {
9+
run(): Promise<void>;
10+
}
11+
12+
export class AbstractCommand {
13+
14+
static command = 'make:command';
15+
static description = 'description';
16+
17+
public context: any;
18+
19+
static async action(command: Command): Promise<void> {
20+
try {
21+
await command.run();
22+
process.exit(0);
23+
} catch (e) {
24+
process.exit(1);
25+
}
26+
}
27+
28+
constructor(context?: any) {
29+
this.context = _.cloneDeep(context);
30+
}
31+
32+
public async run(): Promise<void> {
33+
console.log('You have to implement a run method!');
34+
}
35+
36+
}

src/console/AbstractMakeCommand.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* AbstractMakeCommand
3+
* -------------------------------------
4+
*
5+
*/
6+
import * as _ from 'lodash';
7+
import { writeTemplate } from './lib/template';
8+
import { askFileName, buildFilePath, existsFile, parseName, updateTargets } from './lib/utils';
9+
10+
export interface MakeCommand {
11+
context: any;
12+
type: string;
13+
suffix: string;
14+
template: string;
15+
target: string;
16+
updateTargets: boolean;
17+
18+
run(): Promise<void>;
19+
write(): Promise<void>;
20+
}
21+
22+
export class AbstractMakeCommand {
23+
24+
static command = 'make:command';
25+
static description = 'description';
26+
27+
public context: any;
28+
public type = 'Type';
29+
public suffix = 'Suffix';
30+
public prefix = '';
31+
public template = 'template.hbs';
32+
public target = 'api/target/path';
33+
public updateTargets = true;
34+
35+
static async action(command: MakeCommand): Promise<void> {
36+
try {
37+
await command.run();
38+
await command.write();
39+
if (command.updateTargets) {
40+
await updateTargets();
41+
}
42+
process.exit(0);
43+
} catch (e) {
44+
process.exit(1);
45+
}
46+
}
47+
48+
constructor(context?: any) {
49+
this.context = _.cloneDeep(context);
50+
}
51+
52+
public async run(): Promise<void> {
53+
this.context = await askFileName(this.context, this.type, this.suffix, this.prefix);
54+
}
55+
56+
public async write(): Promise<void> {
57+
const filePath = buildFilePath(this.target, this.context.name);
58+
await existsFile(filePath, true);
59+
this.context.name = parseName(this.context.name, this.suffix);
60+
await writeTemplate(this.template, filePath, this.context);
61+
}
62+
63+
}

src/console/DatabaseResetCommand.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Log } from '../core/log';
22

33
import * as Knex from 'knex';
4+
import { AbstractCommand } from './AbstractCommand';
45
const options = require('../../knexfile.ts');
56
const log = new Log('app:console:DatabaseResetCommand');
67

@@ -12,21 +13,11 @@ const log = new Log('app:console:DatabaseResetCommand');
1213
* @export
1314
* @class DatabaseResetCommand
1415
*/
15-
export class DatabaseResetCommand {
16+
export class DatabaseResetCommand extends AbstractCommand {
1617

1718
static command = 'db:reset';
1819
static description = 'Reverse all current migrations and migrate to latest.';
1920

20-
static async action(): Promise<void> {
21-
try {
22-
const command = new DatabaseResetCommand();
23-
await command.run();
24-
process.exit(0);
25-
} catch (e) {
26-
process.exit(1);
27-
}
28-
}
29-
3021
public async run(): Promise<void> {
3122
const knex = Knex(options);
3223

src/console/MakeControllerCommand.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,17 @@
33
* -------------------------------------
44
*
55
*/
6-
import { writeTemplate } from './lib/template';
7-
import { askFileName, buildFilePath, existsFile, parseName } from './lib/utils';
6+
import { AbstractMakeCommand } from './AbstractMakeCommand';
87

98

10-
export class MakeControllerCommand {
9+
export class MakeControllerCommand extends AbstractMakeCommand {
1110

1211
static command = 'make:controller';
1312
static description = 'Generate new controller';
14-
static type = 'Controller';
15-
static suffix = 'Controller';
16-
static template = 'controller.hbs';
17-
static target = 'api/controllers';
1813

19-
static async action(): Promise<void> {
20-
try {
21-
const command = new MakeControllerCommand();
22-
await command.run();
23-
} catch (e) {
24-
process.exit(1);
25-
}
26-
}
27-
28-
public async run(): Promise<void> {
29-
const context = await askFileName(MakeControllerCommand.type, MakeControllerCommand.suffix);
30-
const filePath = buildFilePath(MakeControllerCommand.target, context.name);
31-
await existsFile(filePath, true);
32-
await writeTemplate(MakeControllerCommand.template, filePath, {
33-
name: parseName(context.name, MakeControllerCommand.suffix),
34-
deepness: context.deepness
35-
});
36-
process.exit(0);
37-
}
14+
public type = 'Controller';
15+
public suffix = 'Controller';
16+
public template = 'controller.hbs';
17+
public target = 'api/controllers';
3818

3919
}

src/console/MakeExceptionCommand.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,17 @@
33
* -------------------------------------
44
*
55
*/
6-
import { writeTemplate } from './lib/template';
7-
import { askFileName, buildFilePath, existsFile, parseName } from './lib/utils';
6+
import { AbstractMakeCommand } from './AbstractMakeCommand';
87

98

10-
export class MakeExceptionCommand {
9+
export class MakeExceptionCommand extends AbstractMakeCommand {
1110

1211
static command = 'make:exception';
1312
static description = 'Generate new exception';
14-
static type = 'Exception';
15-
static suffix = 'Exception';
16-
static template = 'exception.hbs';
17-
static target = 'api/exceptions';
1813

19-
static async action(): Promise<void> {
20-
try {
21-
const command = new MakeExceptionCommand();
22-
await command.run();
23-
process.exit(0);
24-
} catch (e) {
25-
process.exit(1);
26-
}
27-
}
28-
29-
public async run(): Promise<void> {
30-
const context = await askFileName(MakeExceptionCommand.type, MakeExceptionCommand.suffix);
31-
const filePath = buildFilePath(MakeExceptionCommand.target, context.name);
32-
await existsFile(filePath, true);
33-
await writeTemplate(MakeExceptionCommand.template, filePath, {
34-
name: parseName(context.name, MakeExceptionCommand.suffix),
35-
deepness: context.deepness
36-
});
37-
process.exit(0);
38-
}
14+
public type = 'Exception';
15+
public suffix = 'Exception';
16+
public template = 'exception.hbs';
17+
public target = 'api/exceptions';
3918

4019
}

src/console/MakeListenerCommand.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,17 @@
33
* -------------------------------------
44
*
55
*/
6-
import { writeTemplate } from './lib/template';
7-
import { askFileName, buildFilePath, existsFile, parseName } from './lib/utils';
6+
import { AbstractMakeCommand } from './AbstractMakeCommand';
87

98

10-
export class MakeListenerCommand {
9+
export class MakeListenerCommand extends AbstractMakeCommand {
1110

1211
static command = 'make:listener';
1312
static description = 'Generate new listener';
14-
static type = 'Listener';
15-
static suffix = 'Listener';
16-
static template = 'listener.hbs';
17-
static target = 'api/listeners';
1813

19-
static async action(): Promise<void> {
20-
try {
21-
const command = new MakeListenerCommand();
22-
await command.run();
23-
process.exit(0);
24-
} catch (e) {
25-
process.exit(1);
26-
}
27-
}
28-
29-
public async run(): Promise<void> {
30-
const context = await askFileName(MakeListenerCommand.type, MakeListenerCommand.suffix);
31-
const filePath = buildFilePath(MakeListenerCommand.target, context.name);
32-
await existsFile(filePath, true);
33-
await writeTemplate(MakeListenerCommand.template, filePath, {
34-
name: parseName(context.name, MakeListenerCommand.suffix),
35-
deepness: context.deepness
36-
});
37-
process.exit(0);
38-
}
14+
public type = 'Listener';
15+
public suffix = 'Listener';
16+
public template = 'listener.hbs';
17+
public target = 'api/listeners';
3918

4019
}

src/console/MakeMiddlewareCommand.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,17 @@
33
* -------------------------------------
44
*
55
*/
6-
import { writeTemplate } from './lib/template';
7-
import { askFileName, buildFilePath, existsFile, parseName } from './lib/utils';
6+
import { AbstractMakeCommand } from './AbstractMakeCommand';
87

98

10-
export class MakeMiddlewareCommand {
9+
export class MakeMiddlewareCommand extends AbstractMakeCommand {
1110

1211
static command = 'make:middleware';
1312
static description = 'Generate new middleware';
14-
static type = 'Middleware';
15-
static suffix = 'Middleware';
16-
static template = 'middleware.hbs';
17-
static target = 'api/middlewares';
1813

19-
static async action(): Promise<void> {
20-
try {
21-
const command = new MakeMiddlewareCommand();
22-
await command.run();
23-
process.exit(0);
24-
} catch (e) {
25-
process.exit(1);
26-
}
27-
}
28-
29-
public async run(): Promise<void> {
30-
const context = await askFileName(MakeMiddlewareCommand.type, MakeMiddlewareCommand.suffix);
31-
const filePath = buildFilePath(MakeMiddlewareCommand.target, context.name);
32-
await existsFile(filePath, true);
33-
await writeTemplate(MakeMiddlewareCommand.template, filePath, {
34-
name: parseName(context.name, MakeMiddlewareCommand.suffix),
35-
deepness: context.deepness
36-
});
37-
process.exit(0);
38-
}
14+
public type = 'Middleware';
15+
public suffix = 'Middleware';
16+
public template = 'middleware.hbs';
17+
public target = 'api/middlewares';
3918

4019
}

0 commit comments

Comments
 (0)