()
| 76 | protected stubPath: string = 'make/middleware/main.stub' |
| 77 | |
| 78 | async run() { |
| 79 | const stackChoices = ['server', 'router', 'named'] |
| 80 | |
| 81 | /** |
| 82 | * Prompt to select the stack under which to register |
| 83 | * the middleware |
| 84 | */ |
| 85 | if (!this.stack) { |
| 86 | this.stack = await this.prompt.choice( |
| 87 | 'Under which stack you want to register the middleware?', |
| 88 | stackChoices |
| 89 | ) |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Error out when mentioned stack is invalid |
| 94 | */ |
| 95 | if (!stackChoices.includes(this.stack)) { |
| 96 | this.exitCode = 1 |
| 97 | this.logger.error( |
| 98 | `Invalid middleware stack "${this.stack}". Select from "${stackChoices.join(', ')}"` |
| 99 | ) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Create middleware |
| 105 | */ |
| 106 | const codemods = await this.createCodemods() |
| 107 | codemods.overwriteExisting = this.force === true |
| 108 | const { destination } = await codemods.makeUsingStub( |
| 109 | stubsRoot, |
| 110 | this.stubPath, |
| 111 | { |
| 112 | flags: this.parsed.flags, |
| 113 | entity: this.app.generators.createEntity(this.name), |
| 114 | }, |
| 115 | { |
| 116 | contentsFromFile: this.contentsFrom, |
| 117 | } |
| 118 | ) |
| 119 | |
| 120 | /** |
| 121 | * Creative relative path for the middleware file from |
| 122 | * the "./app/middleware" directory |
| 123 | */ |
| 124 | const middlewareRelativePath = stringHelpers.toUnixSlash( |
| 125 | relative(this.app.middlewarePath(), destination).replace(extname(destination), '') |
| 126 | ) |
| 127 | |
| 128 | /** |
| 129 | * Take the middleware relative path, remove `_middleware` prefix from it |
| 130 | * and convert everything to camelcase |
| 131 | */ |
| 132 | const name = string.camelCase(basename(middlewareRelativePath).replace(/_middleware$/, '')) |
| 133 | |
| 134 | /** |
| 135 | * Register middleware |
nothing calls this directly
no test coverage detected