diff --git a/examples/getstarted/config/middlewares.js b/examples/getstarted/config/middlewares.js index 1fd1a394fa..10cf998e29 100644 --- a/examples/getstarted/config/middlewares.js +++ b/examples/getstarted/config/middlewares.js @@ -17,4 +17,10 @@ module.exports = [ }, 'strapi::favicon', 'strapi::public', + { + name: 'global::test-middleware', + config: { + foo: 'bar', + }, + }, ]; diff --git a/packages/core/database/lib/utils/content-types.js b/packages/core/database/lib/utils/content-types.js index 907191b9f6..9a4665da84 100644 --- a/packages/core/database/lib/utils/content-types.js +++ b/packages/core/database/lib/utils/content-types.js @@ -3,7 +3,6 @@ const transformAttribute = attribute => { switch (attribute.type) { case 'media': { - // TODO: handle a filter on field return { type: 'relation', relation: attribute.multiple === true ? 'morphMany' : 'morphOne', diff --git a/packages/core/strapi/bin/strapi.js b/packages/core/strapi/bin/strapi.js index 7a8d72d075..7ba32b39d6 100755 --- a/packages/core/strapi/bin/strapi.js +++ b/packages/core/strapi/bin/strapi.js @@ -183,4 +183,29 @@ program .option('-p, --password ', 'New password for the user') .action(getLocalScript('admin-reset')); +program + .command('routes:list') + .description('List all the application routes') + .action(getLocalScript('routes/list')); + +program + .command('middlewares:list') + .description('List all the application middlewares') + .action(getLocalScript('middlewares/list')); + +program + .command('policies:list') + .description('List all the application policies') + .action(getLocalScript('policies/list')); + +program + .command('content-types:list') + .description('List all the application content-types') + .action(getLocalScript('content-types/list')); + +program + .command('hooks:list') + .description('List all the application hooks') + .action(getLocalScript('hooks/list')); + program.parseAsync(process.argv); diff --git a/packages/core/strapi/lib/Strapi.js b/packages/core/strapi/lib/Strapi.js index 819c0546e6..333a43d17d 100644 --- a/packages/core/strapi/lib/Strapi.js +++ b/packages/core/strapi/lib/Strapi.js @@ -82,44 +82,60 @@ class Strapi { return ee({ dir: this.dirs.root, logger: this.log }); } + get services() { + return this.container.get('services').getAll(); + } + service(uid) { return this.container.get('services').get(uid); } + get controllers() { + return this.container.get('controllers').getAll(); + } + controller(uid) { return this.container.get('controllers').get(uid); } + get contentTypes() { + return this.container.get('content-types').getAll(); + } + contentType(name) { return this.container.get('content-types').get(name); } - get contentTypes() { - return this.container.get('content-types').getAll(); + get policies() { + return this.container.get('policies').getAll(); } policy(name) { return this.container.get('policies').get(name); } - middleware(name) { - return this.container.get('middlewares').get(name); + get middlewares() { + return this.container.get('middlewares').getAll(); } - plugin(name) { - return this.container.get('plugins').get(name); + middleware(name) { + return this.container.get('middlewares').get(name); } get plugins() { return this.container.get('plugins').getAll(); } - hook(name) { - return this.container.get('hooks').get(name); + plugin(name) { + return this.container.get('plugins').get(name); } get hooks() { - return this.container.get('hooks'); + return this.container.get('hooks').getAll(); + } + + hook(name) { + return this.container.get('hooks').get(name); } // api(name) { @@ -283,8 +299,8 @@ class Strapi { } registerInternalHooks() { - this.hooks.set('strapi::content-types.beforeSync', createAsyncParallelHook()); - this.hooks.set('strapi::content-types.afterSync', createAsyncParallelHook()); + this.container.get('hooks').set('strapi::content-types.beforeSync', createAsyncParallelHook()); + this.container.get('hooks').set('strapi::content-types.afterSync', createAsyncParallelHook()); this.hook('strapi::content-types.beforeSync').register(draftAndPublishSync.disable); this.hook('strapi::content-types.afterSync').register(draftAndPublishSync.enable); diff --git a/packages/core/strapi/lib/commands/content-types/list.js b/packages/core/strapi/lib/commands/content-types/list.js new file mode 100644 index 0000000000..91ddd99805 --- /dev/null +++ b/packages/core/strapi/lib/commands/content-types/list.js @@ -0,0 +1,24 @@ +'use strict'; + +const CLITable = require('cli-table3'); +const chalk = require('chalk'); + +const strapi = require('../../index'); + +module.exports = async function() { + const app = await strapi().load(); + + const list = app.contentTypes; + + const infoTable = new CLITable({ + head: [chalk.blue('Name')], + }); + + Object.keys(list).forEach(name => { + infoTable.push([name]); + }); + + console.log(infoTable.toString()); + + await app.destroy(); +}; diff --git a/packages/core/strapi/lib/commands/hooks/list.js b/packages/core/strapi/lib/commands/hooks/list.js new file mode 100644 index 0000000000..bbb9f6886d --- /dev/null +++ b/packages/core/strapi/lib/commands/hooks/list.js @@ -0,0 +1,24 @@ +'use strict'; + +const CLITable = require('cli-table3'); +const chalk = require('chalk'); + +const strapi = require('../../index'); + +module.exports = async function() { + const app = await strapi().load(); + + const list = app.hooks; + + const infoTable = new CLITable({ + head: [chalk.blue('Name')], + }); + + Object.keys(list).forEach(name => { + infoTable.push([name]); + }); + + console.log(infoTable.toString()); + + await app.destroy(); +}; diff --git a/packages/core/strapi/lib/commands/middlewares/list.js b/packages/core/strapi/lib/commands/middlewares/list.js new file mode 100644 index 0000000000..ec01fe5922 --- /dev/null +++ b/packages/core/strapi/lib/commands/middlewares/list.js @@ -0,0 +1,24 @@ +'use strict'; + +const CLITable = require('cli-table3'); +const chalk = require('chalk'); + +const strapi = require('../../index'); + +module.exports = async function() { + const app = await strapi().load(); + + const list = app.middlewares; + + const infoTable = new CLITable({ + head: [chalk.blue('Name')], + }); + + Object.keys(list).forEach(name => { + infoTable.push([name]); + }); + + console.log(infoTable.toString()); + + await app.destroy(); +}; diff --git a/packages/core/strapi/lib/commands/policies/list.js b/packages/core/strapi/lib/commands/policies/list.js new file mode 100644 index 0000000000..93197cc794 --- /dev/null +++ b/packages/core/strapi/lib/commands/policies/list.js @@ -0,0 +1,24 @@ +'use strict'; + +const CLITable = require('cli-table3'); +const chalk = require('chalk'); + +const strapi = require('../../index'); + +module.exports = async function() { + const app = await strapi().load(); + + const list = app.policies; + + const infoTable = new CLITable({ + head: [chalk.blue('Name')], + }); + + Object.keys(list).forEach(name => { + infoTable.push([name]); + }); + + console.log(infoTable.toString()); + + await app.destroy(); +}; diff --git a/packages/core/strapi/lib/commands/routes/list.js b/packages/core/strapi/lib/commands/routes/list.js new file mode 100644 index 0000000000..2daf6324f5 --- /dev/null +++ b/packages/core/strapi/lib/commands/routes/list.js @@ -0,0 +1,28 @@ +'use strict'; + +const CLITable = require('cli-table3'); +const chalk = require('chalk'); +const { toUpper } = require('lodash/fp'); + +const strapi = require('../../index'); + +module.exports = async function() { + const app = await strapi().load(); + + const list = app.server.listRoutes(); + + const infoTable = new CLITable({ + head: [chalk.blue('Method'), chalk.blue('Path')], + colWidths: [20, 80], + }); + + list + .filter(route => route.methods.length) + .forEach(route => { + infoTable.push([route.methods.map(toUpper).join('|'), route.path]); + }); + + console.log(infoTable.toString()); + + await app.destroy(); +}; diff --git a/packages/core/strapi/lib/services/server/index.js b/packages/core/strapi/lib/services/server/index.js index e412fe79ac..0cac670c61 100644 --- a/packages/core/strapi/lib/services/server/index.js +++ b/packages/core/strapi/lib/services/server/index.js @@ -83,6 +83,10 @@ const createServer = strapi => { }, mount() { + if (state.mounted) { + return this; + } + state.mounted = true; Object.values(apis).forEach(api => api.mount(router)); @@ -103,11 +107,13 @@ const createServer = strapi => { return this; }, - listen(...args) { - if (!state.mounted) { - this.mount(); - } + listRoutes() { + this.mount(); + return router.stack.map(route => route); + }, + listen(...args) { + this.mount(); return httpServer.listen(...args); },