Add listing commands

This commit is contained in:
Alexandre Bodin 2021-09-29 09:34:37 +02:00
parent cfc2a7f650
commit d6a448d44a
10 changed files with 192 additions and 16 deletions

View File

@ -17,4 +17,10 @@ module.exports = [
}, },
'strapi::favicon', 'strapi::favicon',
'strapi::public', 'strapi::public',
{
name: 'global::test-middleware',
config: {
foo: 'bar',
},
},
]; ];

View File

@ -3,7 +3,6 @@
const transformAttribute = attribute => { const transformAttribute = attribute => {
switch (attribute.type) { switch (attribute.type) {
case 'media': { case 'media': {
// TODO: handle a filter on field
return { return {
type: 'relation', type: 'relation',
relation: attribute.multiple === true ? 'morphMany' : 'morphOne', relation: attribute.multiple === true ? 'morphMany' : 'morphOne',

View File

@ -183,4 +183,29 @@ program
.option('-p, --password <password>', 'New password for the user') .option('-p, --password <password>', 'New password for the user')
.action(getLocalScript('admin-reset')); .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); program.parseAsync(process.argv);

View File

@ -82,44 +82,60 @@ class Strapi {
return ee({ dir: this.dirs.root, logger: this.log }); return ee({ dir: this.dirs.root, logger: this.log });
} }
get services() {
return this.container.get('services').getAll();
}
service(uid) { service(uid) {
return this.container.get('services').get(uid); return this.container.get('services').get(uid);
} }
get controllers() {
return this.container.get('controllers').getAll();
}
controller(uid) { controller(uid) {
return this.container.get('controllers').get(uid); return this.container.get('controllers').get(uid);
} }
get contentTypes() {
return this.container.get('content-types').getAll();
}
contentType(name) { contentType(name) {
return this.container.get('content-types').get(name); return this.container.get('content-types').get(name);
} }
get contentTypes() { get policies() {
return this.container.get('content-types').getAll(); return this.container.get('policies').getAll();
} }
policy(name) { policy(name) {
return this.container.get('policies').get(name); return this.container.get('policies').get(name);
} }
middleware(name) { get middlewares() {
return this.container.get('middlewares').get(name); return this.container.get('middlewares').getAll();
} }
plugin(name) { middleware(name) {
return this.container.get('plugins').get(name); return this.container.get('middlewares').get(name);
} }
get plugins() { get plugins() {
return this.container.get('plugins').getAll(); return this.container.get('plugins').getAll();
} }
hook(name) { plugin(name) {
return this.container.get('hooks').get(name); return this.container.get('plugins').get(name);
} }
get hooks() { get hooks() {
return this.container.get('hooks'); return this.container.get('hooks').getAll();
}
hook(name) {
return this.container.get('hooks').get(name);
} }
// api(name) { // api(name) {
@ -283,8 +299,8 @@ class Strapi {
} }
registerInternalHooks() { registerInternalHooks() {
this.hooks.set('strapi::content-types.beforeSync', createAsyncParallelHook()); this.container.get('hooks').set('strapi::content-types.beforeSync', createAsyncParallelHook());
this.hooks.set('strapi::content-types.afterSync', 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.beforeSync').register(draftAndPublishSync.disable);
this.hook('strapi::content-types.afterSync').register(draftAndPublishSync.enable); this.hook('strapi::content-types.afterSync').register(draftAndPublishSync.enable);

View File

@ -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();
};

View File

@ -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();
};

View File

@ -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();
};

View File

@ -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();
};

View File

@ -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();
};

View File

@ -83,6 +83,10 @@ const createServer = strapi => {
}, },
mount() { mount() {
if (state.mounted) {
return this;
}
state.mounted = true; state.mounted = true;
Object.values(apis).forEach(api => api.mount(router)); Object.values(apis).forEach(api => api.mount(router));
@ -103,11 +107,13 @@ const createServer = strapi => {
return this; return this;
}, },
listen(...args) { listRoutes() {
if (!state.mounted) {
this.mount(); this.mount();
} return router.stack.map(route => route);
},
listen(...args) {
this.mount();
return httpServer.listen(...args); return httpServer.listen(...args);
}, },