Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added generator for middleware #261

Merged
merged 4 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh here's the culprit. We should probably replace that with a regexp /^(model|middleware)$/.test(type).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind that's the name haha. I got excited.

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*/) {

}
`;
};