diff --git a/src/packages/cli/commands/create.js b/src/packages/cli/commands/create.js index a5d67b6d..13f411fa 100644 --- a/src/packages/cli/commands/create.js +++ b/src/packages/cli/commands/create.js @@ -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`) ]); diff --git a/src/packages/cli/commands/destroy.js b/src/packages/cli/commands/destroy.js index 063bb397..d45ca9bc 100644 --- a/src/packages/cli/commands/destroy.js +++ b/src/packages/cli/commands/destroy.js @@ -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}`)) { diff --git a/src/packages/cli/commands/generate.js b/src/packages/cli/commands/generate.js index 9a5039a2..5e1d148b 100644 --- a/src/packages/cli/commands/generate.js +++ b/src/packages/cli/commands/generate.js @@ -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'; @@ -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); } @@ -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`; } diff --git a/src/packages/cli/templates/middleware.js b/src/packages/cli/templates/middleware.js new file mode 100644 index 00000000..43c79803 --- /dev/null +++ b/src/packages/cli/templates/middleware.js @@ -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*/) { + + } + `; +};