Skip to content

Commit 185b476

Browse files
author
hirsch88
committed
add resource interface generator
1 parent a37672f commit 185b476

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

src/console/MakeModelCommand.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import * as _ from 'lodash';
77
import * as inquirer from 'inquirer';
88
import { AbstractMakeCommand } from './AbstractMakeCommand';
99
import { MakeMigrationCommand } from './MakeMigrationCommand';
10-
import { askProperties } from './lib/utils';
10+
import { askProperties, buildFilePath, existsFile } from './lib/utils';
11+
import { writeTemplate } from './lib/template';
1112

1213

1314
export class MakeModelCommand extends AbstractMakeCommand {
@@ -37,10 +38,18 @@ export class MakeModelCommand extends AbstractMakeCommand {
3738
}
3839

3940
public async write(): Promise<void> {
41+
// Create migration file
4042
if (this.context.hasMigration) {
4143
await this.makeMigrationCommand.write();
4244
}
45+
46+
// Create model
4347
await super.write();
48+
49+
// Create interface for this resource object
50+
const filePath = buildFilePath('types/resources', this.context.name.camelCase, '.d.ts');
51+
await existsFile(filePath, true);
52+
await writeTemplate('resource.hbs', filePath, this.context);
4453
}
4554

4655
private async askMetaData(context: any): Promise<any> {

src/console/lib/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export const filterInput = (suffix: string, prefix = '') => (value: string) => {
2727
return (vs.join('/')) + prefix + suffix;
2828
};
2929

30-
export const buildFilePath = (targetPath: string, fileName: string) => path.join(__dirname, `/../../${targetPath}`, `${fileName}.ts`);
30+
export const buildFilePath = (targetPath: string, fileName: string, extension = '.ts') =>
31+
path.join(__dirname, `/../../${targetPath}`, `${fileName}${extension}`);
3132

3233
export const inputIsRequired = (value: any) => !!value;
3334

src/console/templates/resource.hbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare module 'resources' {
2+
3+
interface {{name.capitalize}} {
4+
{{#each properties}}
5+
{{name.camelCase}}: {{type.script}};
6+
{{/each}}
7+
}
8+
9+
}

src/types/my-express.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ declare module 'my-express' {
1010

1111
import * as expressLib from 'express';
1212
import * as auth0 from 'auth0';
13-
import * as dto from 'dto';
13+
import * as resources from 'resources';
1414

1515
namespace myExpress {
1616

@@ -22,7 +22,7 @@ declare module 'my-express' {
2222

2323
interface Request extends expressLib.Request {
2424
tokeninfo: auth0.User;
25-
user: dto.User;
25+
user: resources.User;
2626
}
2727

2828
interface Response extends expressLib.Response {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare module 'dto' {
1+
declare module 'resources' {
22

33
interface User {
44
id: string;

0 commit comments

Comments
 (0)