Skip to content

Commit

Permalink
Feat: Added generator for middleware (#261)
Browse files Browse the repository at this point in the history
* Feat: Added generator for middleware

* feat: added destroy command for middleware
  • Loading branch information
adampash authored Aug 4, 2016
1 parent d9d0081 commit 94abf14
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/packages/cli/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export async function create(name, database) {
fs.mkdirAsync(`${project}/app/models`),
fs.mkdirAsync(`${project}/app/serializers`),
fs.mkdirAsync(`${project}/app/controllers`),
fs.mkdirAsync(`${project}/app/middleware`),
fs.mkdirAsync(`${project}/config/environments`),
fs.mkdirAsync(`${project}/db/migrate`)
]);
Expand Down
4 changes: 4 additions & 0 deletions src/packages/cli/commands/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export async function destroyType(type, name) {
name = pluralize(name);
path = `app/${pluralize(type)}/${name}.js`;
break;

case 'middleware':
path = `app/${type}/${name}.js`;
break;
}

if (await exists(`${CWD}/${path}`)) {
Expand Down
10 changes: 9 additions & 1 deletion src/packages/cli/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import serializerTemplate from '../templates/serializer';
import controllerTemplate from '../templates/controller';
import emptyMigrationTemplate from '../templates/empty-migration';
import modelMigrationTemplate from '../templates/model-migration';
import middlewareTemplate from '../templates/middleware';

import indent from '../utils/indent';

Expand Down Expand Up @@ -49,13 +50,18 @@ export async function generateType(type, name, cwd, attrs = []) {
case 'controller':
data = controllerTemplate(name, attrs);
break;

case 'middleware':
data = middlewareTemplate(name);
break;
}

if (type === 'model') {
name = singularize(name);
}

if (type !== 'model' && type !== 'migration' && name !== 'application') {
if (type !== 'model' && type !== 'migration' && type !== 'middleware' &&
name !== 'application') {
name = pluralize(name);
}

Expand All @@ -67,6 +73,8 @@ export async function generateType(type, name, cwd, attrs = []) {
const timestamp = generateTimestamp();

path = `db/migrate/${timestamp}-create-${pluralize(name)}.js`;
} else if (type === 'middleware') {
path = `app/${type}/${name}.js`;
} else {
path = `app/${pluralize(type)}/${name}.js`;
}
Expand Down
18 changes: 18 additions & 0 deletions src/packages/cli/templates/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @flow
import { camelize } from 'inflection';

import underscore from '../../../utils/underscore';
import template from '../../template';

/**
* @private
*/
export default (name: string): string => {
name = camelize(underscore(name), true);

return template`
export default function ${name}(/*request, response*/) {
}
`;
};

0 comments on commit 94abf14

Please sign in to comment.